Skip to content

Commit 3441dac

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 #100.
1 parent 74eb6a7 commit 3441dac

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
@@ -546,26 +546,80 @@ print_options()
546546
printf "%-23s:\n" "-----------------------"
547547
}
548548

549-
ext_label()
549+
dst_part_label()
550550
{
551551
pnum=$1
552552
fs_type=$2
553-
flag=$3
554-
label_arg=""
553+
label=""
555554

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

571625
get_src_disk()
@@ -1501,9 +1555,9 @@ Use -U for unattended even if initializing.
15011555

15021556
if [ "${src_mounted_dir[p]}" == "/boot" ] && ((p == 1))
15031557
then
1504-
ext_label $p "$fs_type" "-L" label
1505-
printf " => mkfs -t $mkfs_type $label $dst_dev ..."
1506-
yes | mkfs -t $mkfs_type $label $dst_dev &>> /tmp/$PGM-output
1558+
mkfs_label $p "$fs_type" label_opt
1559+
printf " => mkfs -t $fs_type $label_opt $dst_dev ..."
1560+
yes | mkfs -t $mkfs_type $label_opt $dst_dev &>> /tmp/$PGM-output
15071561
echo ""
15081562
else
15091563
if [ "$fs_type" == "swap" ]
@@ -1514,9 +1568,9 @@ Use -U for unattended even if initializing.
15141568
then
15151569
if [ "${src_mounted_dir[p]}" != "" ] || ((p == n_image_parts))
15161570
then
1517-
ext_label $p $fs_type "-L" label
1518-
printf " => mkfs -t $mkfs_type $label $dst_dev ..."
1519-
yes | mkfs -t $mkfs_type $label $dst_dev &>> /tmp/$PGM-output
1571+
mkfs_label "$p" "$fs_type" label_opt
1572+
printf " => mkfs -t $mkfs_type $label_opt $dst_dev ..."
1573+
yes | mkfs -t $mkfs_type $label_opt $dst_dev &>> /tmp/$PGM-output
15201574
echo ""
15211575
if ((p == n_image_parts))
15221576
then
@@ -1536,12 +1590,7 @@ Use -U for unattended even if initializing.
15361590
else
15371591
echo ""
15381592
fi
1539-
ext_label $p $fs_type "" label
1540-
if [ "$label" != "" ]
1541-
then
1542-
echo " e2label $dst_dev $label"
1543-
e2label $dst_dev $label
1544-
fi
1593+
change_label "$p" "$fs_type" "$dst_dev"
15451594
fi
15461595
fi
15471596
fi
@@ -1663,13 +1712,7 @@ do
16631712
sync_msg_done=1
16641713
dst_dev=/dev/${dst_part_base}${p}
16651714
fs_type=${src_fs_type[$p]}
1666-
ext_label $p $fs_type "" label
1667-
if [ "$label" != "" ]
1668-
then
1669-
qecho " e2label $dst_dev $label"
1670-
e2label $dst_dev $label
1671-
fi
1672-
1715+
change_label "$p" "$fs_type" "$dst_dev"
16731716
mount_partition ${src_device[p]} $clone_src ""
16741717
mount_partition $dst_dev $clone "$clone_src"
16751718
unmount_list="$clone_src $clone"
@@ -1681,12 +1724,7 @@ done
16811724
qprintf "Syncing mounted partitions:\n"
16821725

16831726
fs_type=${src_fs_type[$root_part_num]}
1684-
ext_label $root_part_num $fs_type "" label
1685-
if [ "$label" != "" ]
1686-
then
1687-
qecho " e2label $dst_root_dev $label"
1688-
e2label $dst_root_dev $label
1689-
fi
1727+
change_label "$root_part_num" "$fs_type" "$dst_root_dev"
16901728

16911729
mount_partition $dst_root_dev $clone ""
16921730
unmount_list="$clone"
@@ -1709,12 +1747,7 @@ do
17091747

17101748
dst_dev=/dev/${dst_part_base}${p}
17111749
fs_type=${src_fs_type[$p]}
1712-
ext_label $p $fs_type "" label
1713-
if [ "$label" != "" ]
1714-
then
1715-
qecho " e2label $dst_dev $label"
1716-
e2label $dst_dev $label
1717-
fi
1750+
change_label "$p" "$fs_type" "$dst_dev"
17181751

17191752
mount_partition "$dst_dev" "$dst_dir" "$unmount_list"
17201753
rsync_file_system "${src_mounted_dir[p]}/" "${dst_dir}" ""

0 commit comments

Comments
 (0)