Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions rpi-clone
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ 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_log=/var/log/$PGM.log

HOSTNAME=`hostname`

Expand Down Expand Up @@ -997,6 +995,25 @@ then
exit 0
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" ]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negated tests are hard to read. I suggest to use the following test instead

if  [[ -z "$clone" ||  ! -d "$clone" || -z "$clone_src" || ! -d "$clone_src" ]]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agreed, also using [[ as you suggest makes it even more readable by allowing || instead of -o.

then
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to redirect all output into the log clone_log

umount "$clone" &>> $clone_log

Same for rmdir.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good plan, I've also added a message to the log beforehand, to notify users that any umount errors might not necessarily be problematic.

umount "$clone_src" 2> /dev/null
rmdir "$clone"
rmdir "$clone_src"
}
trap cleanup EXIT

# dst_mount_flag enumerations:
live=1
temp=2
Expand Down Expand Up @@ -1252,16 +1269,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 ]
Expand Down
20 changes: 14 additions & 6 deletions rpi-clone-setup
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
Expand Down Expand Up @@ -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
Expand Down