Skip to content

Commit 31b2c6b

Browse files
Refactor cmdline/fstab updating into functions
This mostly moves code into functions, but also makes minor changes to how the code is called to prepare for more reuse of this code. Moving these functions up in the file with the other functions is left for the next commit to make review easier. Diff best viewed with --ignore-all-space.
1 parent 34dda4f commit 31b2c6b

File tree

1 file changed

+74
-63
lines changed

1 file changed

+74
-63
lines changed

rpi-clone

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,77 +1786,88 @@ do
17861786
done
17871787
qecho ""
17881788

1789-
# Fix PARTUUID or device name references in cmdline.txt and fstab
1790-
#
1791-
fstab=${clone}/etc/fstab
1792-
cmdline_txt=/boot/cmdline.txt
1793-
cmdline_boot=/boot/cmdline.boot
1794-
if [ ! -f "${clone}${cmdline_txt}" ]; then
1795-
# Ubuntu
1796-
cmdline_txt=/boot/firmware/cmdline.txt
1797-
cmdline_boot=/boot/firmware/cmdline.boot
1798-
fi
1799-
if [ ! -f "${clone}${cmdline_txt}" ]; then
1800-
cmdline_txt=/boot/armbianEnv.txt
1801-
cmdline_boot=/boot/armbianEnv.boot
1802-
fi
1789+
# Fix PARTUUID/UUID or device name references in cmdline.txt
1790+
fixup_boot_partition()
1791+
{
1792+
src_mount=$1
1793+
dst_mount=$2
1794+
1795+
# Just try all flavors
1796+
# Paths are below the /boot directory/mountpoint
1797+
fixup_cmdline_txt "${src_mount}" "${dst_mount}" /cmdline.txt /cmdline.boot
1798+
fixup_cmdline_txt "${src_mount}" "${dst_mount}" /armbianEnv.txt /armbianEnv.boot
1799+
fixup_cmdline_txt "${src_mount}" "${dst_mount}" /firmware/cmdline.txt /firmware/cmdline.boot # Ubuntu
1800+
}
18031801

1804-
if [ -f "${clone}${cmdline_txt}" ]
1805-
then
1806-
if ((leave_sd_usb_boot && SD_slot_dst))
1802+
fixup_cmdline_txt()
1803+
{
1804+
src_mount=$1
1805+
dst_mount=$2
1806+
cmdline_txt=$3
1807+
cmdline_boot=$4
1808+
1809+
if [ -f "${dst_mount}${cmdline_txt}" ]
18071810
then
1808-
qecho "Leaving SD to USB boot alone."
1809-
cp "${clone}${cmdline_txt}" "${clone}${cmdline_boot}"
1810-
cmdline_txt=${cmdline_boot}
1811+
if ((leave_sd_usb_boot && SD_slot_dst))
1812+
then
1813+
qecho "Leaving SD to USB boot alone."
1814+
cp "${dst_mount}${cmdline_txt}" "${dst_mount}${cmdline_boot}"
1815+
cmdline_txt=${cmdline_boot}
1816+
fi
1817+
if grep -q "$src_disk_ID" "${dst_mount}${cmdline_txt}"
1818+
then
1819+
qecho "Editing $cmdline_txt PARTUUID to use $dst_disk_ID"
1820+
sed -i "s/${src_disk_ID}/${dst_disk_ID}/" "${dst_mount}${cmdline_txt}"
1821+
elif [ "${src_fsuuid[root_part_num]}" != "" ] && grep -q "${src_fsuuid[root_part_num]}" "${dst_mount}${cmdline_txt}"
1822+
then
1823+
new_fsuuid=${dst_fsuuid[root_part_num]}
1824+
qecho "Editing $cmdline_txt UUID to use $new_fsuuid"
1825+
sed -i "s/${src_fsuuid[root_part_num]}/${new_fsuuid}/" "${dst_mount}${cmdline_txt}"
1826+
elif [ "$edit_fstab_name" != "" ] && grep -q "${src_part_base}" "${dst_mount}${cmdline_txt}"
1827+
then
1828+
qecho "Editing $cmdline_txt references from $src_part_base to $edit_fstab_name"
1829+
sed -i "s/${src_part_base}/$edit_fstab_name/" "${dst_mount}${cmdline_txt}"
1830+
fi
1831+
if ((leave_sd_usb_boot && SD_slot_boot))
1832+
then
1833+
qecho "Copying USB cmdline.txt to SD card to set up USB boot."
1834+
# Note that this leaves out $clone to modify the original SD
1835+
# card to boot from the clone instead
1836+
cp "${src_mount}${cmdline_txt}" "${src_mount}${cmdline_boot}"
1837+
cp "${dst_mount}${cmdline_txt}" "${src_mount}${cmdline_txt}"
1838+
fi
18111839
fi
1812-
if grep -q "$src_disk_ID" "${clone}${cmdline_txt}"
1813-
then
1814-
qecho "Editing $cmdline_txt PARTUUID to use $dst_disk_ID"
1815-
sed -i "s/${src_disk_ID}/${dst_disk_ID}/" "${clone}${cmdline_txt}"
1816-
elif [ "${src_fsuuid[root_part_num]}" != "" ] && grep -q "${src_fsuuid[root_part_num]}" "${clone}${cmdline_txt}"
1840+
}
1841+
1842+
fixup_device_references_in_file() {
1843+
file=$1
1844+
if grep -q $src_disk_ID $file
18171845
then
1818-
new_fsuuid=${dst_fsuuid[root_part_num]}
1819-
qecho "Editing $cmdline_txt UUID to use $new_fsuuid"
1820-
sed -i "s/${src_fsuuid[root_part_num]}/${new_fsuuid}/" "${clone}${cmdline_txt}"
1821-
elif [ "$edit_fstab_name" != "" ] && grep -q "${src_part_base}" "${clone}${cmdline_txt}"
1846+
qecho "Editing $file PARTUUID to use $dst_disk_ID"
1847+
sed -i "s/${src_disk_ID}/${dst_disk_ID}/g" "$file"
1848+
elif grep -q ^UUID= $file
18221849
then
1823-
qecho "Editing $cmdline_txt references from $src_part_base to $edit_fstab_name"
1824-
sed -i "s/${src_part_base}/$edit_fstab_name/" "${clone}${cmdline_txt}"
1825-
fi
1826-
if ((leave_sd_usb_boot && SD_slot_boot))
1850+
for ((p = 1; p <= n_src_parts; p++))
1851+
do
1852+
old_fsuuid=${src_fsuuid[p]}
1853+
new_fsuuid=${dst_fsuuid[p]}
1854+
if [ "$old_fsuuid" == "" -o "$new_fsuuid" == "" ] || ! grep -q "$old_fsuuid" "$file";
1855+
then
1856+
continue
1857+
fi
1858+
1859+
qecho "Editing $file partition $p UUID to use $new_fsuuid"
1860+
sed -i "s/$old_fsuuid/${new_fsuuid}/" "$file"
1861+
done
1862+
elif [ "$edit_fstab_name" != "" ] && grep -q ${src_part_base} $file
18271863
then
1828-
qecho "Copying USB cmdline.txt to SD card to set up USB boot."
1829-
# Note that this leaves out $clone to modify the original SD
1830-
# card to boot from the clone instead
1831-
cp "${cmdline_txt}" "${cmdline_boot}"
1832-
cp "${clone}${cmdline_txt}" "${cmdline_txt}"
1864+
qecho "Editing $file references from $src_part_base to $edit_fstab_name"
1865+
sed -i "s/${src_part_base}/${edit_fstab_name}/" "$file"
18331866
fi
1834-
fi
1835-
1836-
if grep -q $src_disk_ID $fstab
1837-
then
1838-
qecho "Editing $fstab PARTUUID to use $dst_disk_ID"
1839-
sed -i "s/${src_disk_ID}/${dst_disk_ID}/g" "$fstab"
1840-
elif grep -q ^UUID= $fstab
1841-
then
1842-
for ((p = 1; p <= n_src_parts; p++))
1843-
do
1844-
old_fsuuid=${src_fsuuid[p]}
1845-
new_fsuuid=${dst_fsuuid[p]}
1846-
if [ "$old_fsuuid" == "" -o "$new_fsuuid" == "" ] || ! grep -q "$old_fsuuid" "$fstab";
1847-
then
1848-
continue
1849-
fi
1850-
1851-
qecho "Editing $fstab partition $p UUID to use $new_fsuuid"
1852-
sed -i "s/$old_fsuuid/${new_fsuuid}/" "$fstab"
1853-
done
1854-
elif [ "$edit_fstab_name" != "" ] && grep -q ${src_part_base} $fstab
1855-
then
1856-
qecho "Editing $fstab references from $src_part_base to $edit_fstab_name"
1857-
sed -i "s/${src_part_base}/${edit_fstab_name}/" "$fstab"
1858-
fi
1867+
}
18591868

1869+
fixup_boot_partition /boot "${clone}/boot"
1870+
fixup_device_references_in_file "${clone}/etc/fstab"
18601871

18611872
rm -f $clone/etc/udev/rules.d/70-persistent-net.rules
18621873

0 commit comments

Comments
 (0)