Skip to content

Commit 158490c

Browse files
authored
Merge pull request #16 from matthijskooijman/keep-fs-labels
Keep filesystem labels
2 parents fb2c8cf + df6d278 commit 158490c

File tree

1 file changed

+88
-57
lines changed

1 file changed

+88
-57
lines changed

rpi-clone

Lines changed: 88 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -565,26 +565,80 @@ print_options()
565565
printf "%-23s:\n" "-----------------------"
566566
}
567567

568-
ext_label()
568+
dst_part_label()
569569
{
570570
pnum=$1
571571
fs_type=$2
572-
flag=$3
573-
label_arg=""
572+
label=""
574573

575574
if [ "$ext_label" != "" ] && [[ "$fs_type" == *"ext"* ]]
576575
then
577576
rep="${ext_label: -1}"
578577
if [ "$rep" == "#" ]
579578
then
580-
label_arg=${ext_label:: -1}
581-
label_arg="$flag $label_arg$pnum"
579+
label="${ext_label:: -1}"
582580
elif ((pnum == root_part_num))
583581
then
584-
label_arg="$flag $ext_label"
582+
label="$ext_label"
585583
fi
586584
fi
587-
printf -v "${4}" "%s" "$label_arg"
585+
586+
if [ -z "$label" -a -n "${src_label[$pnum]}" ]
587+
then
588+
label="${src_label[$pnum]}"
589+
fi
590+
printf -v "${3}" "%s" "$label"
591+
}
592+
593+
mkfs_label()
594+
{
595+
pnum=$1
596+
fs_type=$2
597+
598+
label_flag=""
599+
case "$fs_type" in
600+
# This list is probably overcomplete, but might simplify
601+
# future additions.
602+
vfat|msdos|exfat|fat16|fat32)
603+
label_flag=-n
604+
;;
605+
ext2|ext3|ext4|ntfs|xfs)
606+
label_flag=-L
607+
;;
608+
hfs|hfsplus)
609+
label_flag=-v
610+
;;
611+
reiserfs)
612+
label_flag=-l
613+
;;
614+
esac
615+
616+
617+
label_arg=""
618+
dst_part_label "$pnum" "$fs_type" label
619+
if [ -n "$label" -a -n "$label_flag" ]
620+
then
621+
label_arg="$label_flag $label"
622+
fi
623+
printf -v "${3}" "%s" "$label_arg"
624+
}
625+
626+
change_label()
627+
{
628+
pnum=$1
629+
fs_type=$2
630+
dev=$3
631+
632+
dst_part_label "$pnum" "$fs_type" label
633+
if [ "$label" != "" ] && [[ "$fs_type" == *"ext"* ]]
634+
then
635+
echo " e2label $dev $label"
636+
e2label $dev $label
637+
elif [ "$label" != "" ] && [[ "$fs_type" == *"fat"* ]]
638+
then
639+
echo " fatlabel $dev $label"
640+
fatlabel $dev $label
641+
fi
588642
}
589643

590644
get_src_disk()
@@ -655,8 +709,11 @@ fi
655709
# src_root_dev, if on device other than booted, is not in src_partition_table
656710
# and src_fdisk_table, but is in src_df_table and src_mount_table
657711
#
658-
src_partition_table=$(parted -m "/dev/$src_disk" unit s print | tr -d ';')
712+
src_partition_table=$(parted --script -m "/dev/$src_disk" unit s print | tr -d ';')
659713
src_fdisk_table=$(fdisk -l /dev/$src_disk | grep "^/dev/")
714+
# Parted seems to force a partition rescan, which hides fs labels from
715+
# lsblk output. Wait for kernel/udev to be done processing.
716+
udevadm settle
660717

661718
tmp=$(df | grep -e "^/dev/$src_disk" -e "^/dev/root" -e "$src_root_dev" \
662719
| tr -s " ")
@@ -758,13 +815,10 @@ do
758815
src_name[p]="${src_mounted_dir[p]}"
759816
fi
760817

761-
if [[ "$part_type" == *"ext"* ]]
818+
label=`lsblk --raw --output label --noheadings "${src_device[p]}"`
819+
if [ "$label" != "" ]
762820
then
763-
label=`e2label ${src_device[p]} 2> /dev/null`
764-
if [ "$label" != "" ]
765-
then
766-
src_label[p]="$label"
767-
fi
821+
src_label[p]="$label"
768822
fi
769823
done
770824

@@ -1115,7 +1169,11 @@ then
11151169
exit 1
11161170
fi
11171171

1118-
dst_partition_table=$(parted -m "/dev/$dst_disk" unit s print | tr -d ';')
1172+
dst_partition_table=$(parted --script -m "/dev/$dst_disk" unit s print | tr -d ';')
1173+
# Parted seems to force a partition rescan, which hides fs labels from
1174+
# lsblk output. Wait for kernel/udev to be done processing.
1175+
udevadm settle
1176+
11191177
n_dst_parts=$(echo "$dst_partition_table" | tail -n 1 | cut -d ":" -f 1)
11201178
if [ "$n_dst_parts" == "/dev/$dst_disk" ]
11211179
then
@@ -1168,16 +1226,15 @@ do
11681226
if [[ "$part_type" == *"linux-swap"* ]]
11691227
then
11701228
dst_fs_type[p]="swap"
1171-
elif [[ "$part_type" == *"ext"* ]]
1229+
elif ((p == ext_num))
11721230
then
1173-
label=`e2label ${dst_device[p]} 2> /dev/null`
1231+
dst_fs_type[p]="EXT"
1232+
else
1233+
label=`lsblk --raw --output label --noheadings "${dst_device[p]}"`
11741234
if [ "$label" != "" ]
11751235
then
11761236
dst_label[p]="$label"
11771237
fi
1178-
elif ((p == ext_num))
1179-
then
1180-
dst_fs_type[p]="EXT"
11811238
fi
11821239
done
11831240

@@ -1534,9 +1591,9 @@ Use -U for unattended even if initializing.
15341591

15351592
if [ "${src_mounted_dir[p]}" == "/boot" ] && ((p == 1))
15361593
then
1537-
ext_label $p "$fs_type" "-L" label
1538-
printf " => mkfs -t $mkfs_type $label $dst_dev ..."
1539-
yes | mkfs -t $mkfs_type $label $dst_dev &>> /tmp/$PGM-output
1594+
mkfs_label $p "$fs_type" label_opt
1595+
printf " => mkfs -t $fs_type $label_opt $dst_dev ..."
1596+
yes | mkfs -t $mkfs_type $label_opt $dst_dev &>> /tmp/$PGM-output
15401597
echo ""
15411598
else
15421599
if [ "$fs_type" == "swap" ]
@@ -1547,9 +1604,9 @@ Use -U for unattended even if initializing.
15471604
then
15481605
if [ "${src_mounted_dir[p]}" != "" ] || ((p == n_image_parts))
15491606
then
1550-
ext_label $p $fs_type "-L" label
1551-
printf " => mkfs -t $mkfs_type $label $dst_dev ..."
1552-
yes | mkfs -t $mkfs_type $label $dst_dev &>> /tmp/$PGM-output
1607+
mkfs_label "$p" "$fs_type" label_opt
1608+
printf " => mkfs -t $mkfs_type $label_opt $dst_dev ..."
1609+
yes | mkfs -t $mkfs_type $label_opt $dst_dev &>> /tmp/$PGM-output
15531610
echo ""
15541611
if ((p == n_image_parts))
15551612
then
@@ -1569,12 +1626,7 @@ Use -U for unattended even if initializing.
15691626
else
15701627
echo ""
15711628
fi
1572-
ext_label $p $fs_type "" label
1573-
if [ "$label" != "" ]
1574-
then
1575-
echo " e2label $dst_dev $label"
1576-
e2label $dst_dev $label
1577-
fi
1629+
change_label "$p" "$fs_type" "$dst_dev"
15781630
fi
15791631
fi
15801632
fi
@@ -1696,13 +1748,7 @@ do
16961748
sync_msg_done=1
16971749
dst_dev=/dev/${dst_part_base}${p}
16981750
fs_type=${src_fs_type[$p]}
1699-
ext_label $p $fs_type "" label
1700-
if [ "$label" != "" ]
1701-
then
1702-
qecho " e2label $dst_dev $label"
1703-
e2label $dst_dev $label
1704-
fi
1705-
1751+
change_label "$p" "$fs_type" "$dst_dev"
17061752
mount_partition ${src_device[p]} $clone_src ""
17071753
mount_partition $dst_dev $clone "$clone_src"
17081754
unmount_list="$clone_src $clone"
@@ -1714,12 +1760,7 @@ done
17141760
qprintf "Syncing mounted partitions:\n"
17151761

17161762
fs_type=${src_fs_type[$root_part_num]}
1717-
ext_label $root_part_num $fs_type "" label
1718-
if [ "$label" != "" ]
1719-
then
1720-
qecho " e2label $dst_root_dev $label"
1721-
e2label $dst_root_dev $label
1722-
fi
1763+
change_label "$root_part_num" "$fs_type" "$dst_root_dev"
17231764

17241765
mount_partition $dst_root_dev $clone ""
17251766
unmount_list="$clone"
@@ -1742,12 +1783,7 @@ do
17421783

17431784
dst_dev=/dev/${dst_part_base}${p}
17441785
fs_type=${src_fs_type[$p]}
1745-
ext_label $p $fs_type "" label
1746-
if [ "$label" != "" ]
1747-
then
1748-
qecho " e2label $dst_dev $label"
1749-
e2label $dst_dev $label
1750-
fi
1786+
change_label "$p" "$fs_type" "$dst_dev"
17511787

17521788
mount_partition "$dst_dev" "$dst_dir" "$unmount_list"
17531789
rsync_file_system "${src_mounted_dir[p]}/" "${dst_dir}" ""
@@ -1798,12 +1834,7 @@ fi
17981834

17991835
rm -f $clone/etc/udev/rules.d/70-persistent-net.rules
18001836

1801-
dst_root_vol_name=`e2label $dst_root_dev`
1802-
1803-
if [ "$dst_root_vol_name" = "" ]
1804-
then
1805-
dst_root_vol_name="no label"
1806-
fi
1837+
dst_root_vol_name=${dst_label[$root_part_num]}
18071838

18081839
if ((have_grub))
18091840
then

0 commit comments

Comments
 (0)