Skip to content

Commit 16f99d9

Browse files
Preserve existing FS labels where possible
Instead of only setting FS labels on ext partitions when specified with the --label-partitions option and leave all other partitions unlabeled, this tries to copy the source filesystem labels to the destination where possible. Setting labels requires filesystem-specific commands or mkfs options, so not all filesystems are supported. For changing labels on existing partitions, only ext and fat partitions are supported. For mkfs a few more are supported, though these are probably not used in practice. This also refactors some of the code, introducing a `mkfs_label()` and `change_label()` function to prevent having to duplicate the filesystem-type checking code. This fixes billw2#100.
1 parent ee05c6e commit 16f99d9

File tree

1 file changed

+71
-38
lines changed

1 file changed

+71
-38
lines changed

rpi-clone

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -545,26 +545,80 @@ print_options()
545545
printf "%-23s:\n" "-----------------------"
546546
}
547547

548-
ext_label()
548+
dst_part_label()
549549
{
550550
pnum=$1
551551
fs_type=$2
552-
flag=$3
553-
label_arg=""
552+
label=""
554553

555554
if [ "$ext_label" != "" ] && [[ "$fs_type" == *"ext"* ]]
556555
then
557556
rep="${ext_label: -1}"
558557
if [ "$rep" == "#" ]
559558
then
560-
label_arg=${ext_label:: -1}
561-
label_arg="$flag $label_arg$pnum"
559+
label="${ext_label:: -1}"
562560
elif ((pnum == root_part_num))
563561
then
564-
label_arg="$flag $ext_label"
562+
label="$ext_label"
565563
fi
566564
fi
567-
printf -v "${4}" "%s" "$label_arg"
565+
566+
if [ -z "$label" -a -n "${src_label[$pnum]}" ]
567+
then
568+
label="${src_label[$pnum]}"
569+
fi
570+
printf -v "${3}" "%s" "$label"
571+
}
572+
573+
mkfs_label()
574+
{
575+
pnum=$1
576+
fs_type=$2
577+
578+
label_flag=""
579+
case "$fs_type" in
580+
# This list is probably overcomplete, but might simplify
581+
# future additions.
582+
vfat|msdos|exfat|fat16|fat32)
583+
label_flag=-n
584+
;;
585+
ext2|ext3|ext4|ntfs|xfs)
586+
label_flag=-L
587+
;;
588+
hfs|hfsplus)
589+
label_flag=-v
590+
;;
591+
reiserfs)
592+
label_flag=-l
593+
;;
594+
esac
595+
596+
597+
label_arg=""
598+
dst_part_label "$pnum" "$fs_type" label
599+
if [ -n "$label" -a -n "$label_flag" ]
600+
then
601+
label_arg="$label_flag $label"
602+
fi
603+
printf -v "${3}" "%s" "$label_arg"
604+
}
605+
606+
change_label()
607+
{
608+
pnum=$1
609+
fs_type=$2
610+
dev=$3
611+
612+
dst_part_label "$pnum" "$fs_type" label
613+
if [ "$label" != "" ] && [[ "$fs_type" == *"ext"* ]]
614+
then
615+
echo " e2label $dev $label"
616+
e2label $dev $label
617+
elif [ "$label" != "" ] && [[ "$fs_type" == *"fat"* ]]
618+
then
619+
echo " fatlabel $dev $label"
620+
fatlabel $dev $label
621+
fi
568622
}
569623

570624
get_src_disk()
@@ -1505,9 +1559,9 @@ Use -U for unattended even if initializing.
15051559

15061560
if [ "${src_mounted_dir[p]}" == "/boot" ] && ((p == 1))
15071561
then
1508-
ext_label $p "$fs_type" "-L" label
1509-
printf " => mkfs -t $mkfs_type $label $dst_dev ..."
1510-
yes | mkfs -t $mkfs_type $label $dst_dev &>> /tmp/$PGM-output
1562+
mkfs_label $p "$fs_type" label_opt
1563+
printf " => mkfs -t $fs_type $label_opt $dst_dev ..."
1564+
yes | mkfs -t $mkfs_type $label_opt $dst_dev &>> /tmp/$PGM-output
15111565
echo ""
15121566
else
15131567
if [ "$fs_type" == "swap" ]
@@ -1518,9 +1572,9 @@ Use -U for unattended even if initializing.
15181572
then
15191573
if [ "${src_mounted_dir[p]}" != "" ] || ((p == n_image_parts))
15201574
then
1521-
ext_label $p $fs_type "-L" label
1522-
printf " => mkfs -t $mkfs_type $label $dst_dev ..."
1523-
yes | mkfs -t $mkfs_type $label $dst_dev &>> /tmp/$PGM-output
1575+
mkfs_label "$p" "$fs_type" label_opt
1576+
printf " => mkfs -t $mkfs_type $label_opt $dst_dev ..."
1577+
yes | mkfs -t $mkfs_type $label_opt $dst_dev &>> /tmp/$PGM-output
15241578
echo ""
15251579
if ((p == n_image_parts))
15261580
then
@@ -1540,12 +1594,7 @@ Use -U for unattended even if initializing.
15401594
else
15411595
echo ""
15421596
fi
1543-
ext_label $p $fs_type "" label
1544-
if [ "$label" != "" ]
1545-
then
1546-
echo " e2label $dst_dev $label"
1547-
e2label $dst_dev $label
1548-
fi
1597+
change_label "$p" "$fs_type" "$dst_dev"
15491598
fi
15501599
fi
15511600
fi
@@ -1667,13 +1716,7 @@ do
16671716
sync_msg_done=1
16681717
dst_dev=/dev/${dst_part_base}${p}
16691718
fs_type=${src_fs_type[$p]}
1670-
ext_label $p $fs_type "" label
1671-
if [ "$label" != "" ]
1672-
then
1673-
qecho " e2label $dst_dev $label"
1674-
e2label $dst_dev $label
1675-
fi
1676-
1719+
change_label "$p" "$fs_type" "$dst_dev"
16771720
mount_partition ${src_device[p]} $clone_src ""
16781721
mount_partition $dst_dev $clone "$clone_src"
16791722
unmount_list="$clone_src $clone"
@@ -1685,12 +1728,7 @@ done
16851728
qprintf "Syncing mounted partitions:\n"
16861729

16871730
fs_type=${src_fs_type[$root_part_num]}
1688-
ext_label $root_part_num $fs_type "" label
1689-
if [ "$label" != "" ]
1690-
then
1691-
qecho " e2label $dst_root_dev $label"
1692-
e2label $dst_root_dev $label
1693-
fi
1731+
change_label "$root_part_num" "$fs_type" "$dst_root_dev"
16941732

16951733
mount_partition $dst_root_dev $clone ""
16961734
unmount_list="$clone"
@@ -1713,12 +1751,7 @@ do
17131751

17141752
dst_dev=/dev/${dst_part_base}${p}
17151753
fs_type=${src_fs_type[$p]}
1716-
ext_label $p $fs_type "" label
1717-
if [ "$label" != "" ]
1718-
then
1719-
qecho " e2label $dst_dev $label"
1720-
e2label $dst_dev $label
1721-
fi
1754+
change_label "$p" "$fs_type" "$dst_dev"
17221755

17231756
mount_partition "$dst_dev" "$dst_dir" "$unmount_list"
17241757
rsync_file_system "${src_mounted_dir[p]}/" "${dst_dir}" ""

0 commit comments

Comments
 (0)