From f8c120c39af34b083fd9f64647aece7baa62aa0a Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 14 Jul 2020 15:49:14 +0200 Subject: [PATCH 1/5] Do not hardcode /mnt/clone in rpi-clone-setup Instead, pass the mount location through an environment variable. --- rpi-clone | 6 +++--- rpi-clone-setup | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/rpi-clone b/rpi-clone index 22e53a4..e947d50 100755 --- a/rpi-clone +++ b/rpi-clone @@ -84,9 +84,9 @@ then apt-get install -y --no-install-recommends $need_packages fi -clone=/mnt/clone -clone_src=/mnt/clone-src -clone_log=/var/log/$PGM.log +export clone=/mnt/clone +export clone_src=/mnt/clone-src +export clone_log=/var/log/$PGM.log HOSTNAME=`hostname` diff --git a/rpi-clone-setup b/rpi-clone-setup index 694779b..2614dc5 100755 --- a/rpi-clone-setup +++ b/rpi-clone-setup @@ -4,11 +4,13 @@ # eg: sudo rpi-clone-setup bozo # # This script is automatically run by rpi-clone (when it is given -s options) -# to setup an alternate hostname. A cloned file system mounted on /mnt/clone -# is expected unless testing with the -t option. +# to setup an alternate hostname. It expects $clone to be set in the +# environment containing the path to a mounted cloned file system unless +# testing with the -t option. # # Or, this script can be run by hand at the end of a clone when rpi-clone -# pauses with the cloned file systems still mounted on /mnt/clone. +# pauses with the cloned file systems still mounted (make sure to set +# the $clone env variable properly, then). # # Or, run this script by hand with -t to process files in test directories # under /tmp/clone-test. Run -t and look at the files to see if the files @@ -27,7 +29,7 @@ file_list="etc/hostname etc/hosts" -clone=/mnt/clone +# $clone is set by caller clone_test=/tmp/clone-test PGM=`basename $0` @@ -81,9 +83,15 @@ fi echo -e "\t$newhost\t- target hostname" -if ((!testing)) && [ ! -d /mnt/clone/etc ] +if ((!testing)) && [ -z "$clone" ] then - echo "A destination clone file system is not mounted on /mnt/clone" + echo "No clone mountpoint was passed in the \$clone variable" + echo "Aborting!" + exit 0 +fi +if ((!testing)) && [ ! -d "$clone/etc" ] +then + echo "A destination clone file system is not mounted on $clone" echo "Aborting!" exit 0 fi From 8b3210edaad1b89051a3ade7b010e39c9135f73b Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 14 Jul 2020 15:56:19 +0200 Subject: [PATCH 2/5] Create mount directories earlier The `$clone` directory is already used when figuring out disk space for unmounted partitions, so this could fail on the first run. --- rpi-clone | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/rpi-clone b/rpi-clone index e947d50..18f4a8b 100755 --- a/rpi-clone +++ b/rpi-clone @@ -997,6 +997,15 @@ then exit 0 fi +if [ ! -d $clone ] +then + mkdir $clone +fi +if [ ! -d $clone_src ] +then + mkdir $clone_src +fi + # dst_mount_flag enumerations: live=1 temp=2 @@ -1252,16 +1261,6 @@ unmount_or_abort "$mounted_dev" \ mounted_dev=$(findmnt /mnt -o source -n) unmount_or_abort "$mounted_dev" "$mounted_dev is currently mounted on /mnt." - -if [ ! -d $clone ] -then - mkdir $clone -fi -if [ ! -d $clone_src ] -then - mkdir $clone_src -fi - # Do not include a dhpys swapfile in rsync. It regenerates at boot. # if [ -f /etc/dphys-swapfile ] From 43884d96620ac3b48e25e1f0a8832ba25c0e46e0 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 14 Jul 2020 15:49:49 +0200 Subject: [PATCH 3/5] Use a random temp directory for mounting This allows using the script multiple times in parallel on different disks. To prevent all these directories littering the filesystem, an exit handler is registered to remove them again when the script exits (also unmounting any mounted filesystems just in case). --- rpi-clone | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/rpi-clone b/rpi-clone index 18f4a8b..fbdf380 100755 --- a/rpi-clone +++ b/rpi-clone @@ -84,8 +84,6 @@ then apt-get install -y --no-install-recommends $need_packages fi -export clone=/mnt/clone -export clone_src=/mnt/clone-src export clone_log=/var/log/$PGM.log HOSTNAME=`hostname` @@ -997,15 +995,25 @@ then exit 0 fi -if [ ! -d $clone ] -then - mkdir $clone -fi -if [ ! -d $clone_src ] +export clone=$(mktemp --tmpdir --directory rpi-clone-dst.XXXXXX) +export clone_src=$(mktemp --tmpdir --directory rpi-clone-src.XXXXXX) + +if ! [ -n "$clone" -a -d "$clone" -a -n "$clone_src" -a -d "$clone_src" ] then - mkdir $clone_src + echo "Failed to create temporary mount directories" + echo "Aborting!" + exit 1 fi +# Make sure we cleanup on exit, regardless of how we exit +cleanup() { + umount "$clone" 2> /dev/null + umount "$clone_src" 2> /dev/null + rmdir "$clone" + rmdir "$clone_src" +} +trap cleanup EXIT + # dst_mount_flag enumerations: live=1 temp=2 From 0ecf02ae4b1375c0974b2bae7b86bf124f60d4cd Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 8 Apr 2024 10:18:27 +0200 Subject: [PATCH 4/5] fixup! Use a random temp directory for mounting --- rpi-clone | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rpi-clone b/rpi-clone index fbdf380..d14b03d 100755 --- a/rpi-clone +++ b/rpi-clone @@ -998,7 +998,7 @@ fi export clone=$(mktemp --tmpdir --directory rpi-clone-dst.XXXXXX) export clone_src=$(mktemp --tmpdir --directory rpi-clone-src.XXXXXX) -if ! [ -n "$clone" -a -d "$clone" -a -n "$clone_src" -a -d "$clone_src" ] +if [[ -z "$clone" || ! -d "$clone" || -z "$clone_src" || ! -d "$clone_src" ]] then echo "Failed to create temporary mount directories" echo "Aborting!" @@ -1007,8 +1007,9 @@ fi # Make sure we cleanup on exit, regardless of how we exit cleanup() { - umount "$clone" 2> /dev/null - umount "$clone_src" 2> /dev/null + echo "Cleaning up mounts, this might produce errors if they were already unmounted" >> "$clone_log" + umount "$clone" &>> "$clone_log" + umount "$clone_src" &>> "$clone_log" rmdir "$clone" rmdir "$clone_src" } From 2bf258a8b905994da39ce37a9e22dcbf8d9793a0 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 8 Apr 2024 16:48:16 +0200 Subject: [PATCH 5/5] fixup! Use a random temp directory for mounting --- rpi-clone | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpi-clone b/rpi-clone index d14b03d..4442105 100755 --- a/rpi-clone +++ b/rpi-clone @@ -1010,8 +1010,8 @@ cleanup() { echo "Cleaning up mounts, this might produce errors if they were already unmounted" >> "$clone_log" umount "$clone" &>> "$clone_log" umount "$clone_src" &>> "$clone_log" - rmdir "$clone" - rmdir "$clone_src" + rmdir "$clone" &>> "$clone_log" + rmdir "$clone_src" &>> "$clone_log" } trap cleanup EXIT