Skip to content

Commit d10b873

Browse files
committed
Handle unmounting busy destination partitions
1 parent c65e13f commit d10b873

File tree

1 file changed

+44
-21
lines changed

1 file changed

+44
-21
lines changed

rpi-clone

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,54 @@ then
111111
exit 0
112112
fi
113113

114+
unmount_or_abort()
115+
{
116+
echo -n "Do you want to unmount $1? (yes/no): "
117+
read resp
118+
if [ "$resp" = "y" ] || [ "$resp" = "yes" ]
119+
then
120+
if ! umount $1
121+
then
122+
echo "Sorry, $PGM could not unmount $1."
123+
echo -e "Aborting!\n"
124+
exit 0
125+
fi
126+
else
127+
echo -e "Aborting!\n"
128+
exit 0
129+
fi
130+
}
131+
132+
DST_ROOT_PARTITION=/dev/${DST_DISK}2
133+
DST_BOOT_PARTITION=/dev/${DST_DISK}1
134+
135+
# Check that none of the destination partitions are busy (mounted).
136+
#
137+
DST_ROOT_CURMOUNT=`fgrep "$DST_ROOT_PARTITION " /etc/mtab | cut -f 2 -d ' ' `
138+
DST_BOOT_CURMOUNT=`fgrep "$DST_BOOT_PARTITION " /etc/mtab | cut -f 2 -d ' ' `
139+
140+
if [ "$DST_ROOT_CURMOUNT" != "" ] || [ "$DST_BOOT_CURMOUNT" != "" ]
141+
then
142+
echo "A destination partition is busy (mounted). Mount status:"
143+
echo " $DST_ROOT_PARTITION: $DST_ROOT_CURMOUNT"
144+
echo " $DST_BOOT_PARTITION: $DST_BOOT_CURMOUNT"
145+
if [ "$DST_BOOT_CURMOUNT" != "" ]
146+
then
147+
unmount_or_abort $DST_BOOT_CURMOUNT
148+
fi
149+
if [ "$DST_ROOT_CURMOUNT" != "" ]
150+
then
151+
unmount_or_abort $DST_ROOT_CURMOUNT
152+
fi
153+
fi
154+
114155

115156
TEST_MOUNTED=`fgrep " $CLONE " /etc/mtab | cut -f 1 -d ' ' `
116157
if [ "$TEST_MOUNTED" != "" ]
117158
then
118159
echo "This script uses $CLONE for mounting filesystems, but"
119160
echo "$CLONE is already mounted with $TEST_MOUNTED."
120-
echo -e "Unmount $CLONE before running this script.\n"
121-
exit 0
161+
unmount_or_abort $CLONE
122162
fi
123163

124164
if [ ! -d $CLONE ]
@@ -129,8 +169,8 @@ then
129169
mkdir $CLONE
130170
else
131171
echo "$MNT_MOUNT is currently mounted on /mnt."
132-
echo -e "Unmount /mnt before running $PGM.\n"
133-
exit 0
172+
unmount_or_abort /mnt
173+
mkdir $CLONE
134174
fi
135175
fi
136176

@@ -161,23 +201,6 @@ EOF
161201

162202
# =========== Disk Setup and Checks ===========
163203
#
164-
DST_ROOT_PARTITION=/dev/${DST_DISK}2
165-
DST_BOOT_PARTITION=/dev/${DST_DISK}1
166-
167-
# Check that none of the destination partitions are busy (mounted).
168-
#
169-
DST_ROOT_CURMOUNT=`fgrep "$DST_ROOT_PARTITION " /etc/mtab | cut -f 2 -d ' ' `
170-
DST_BOOT_CURMOUNT=`fgrep "$DST_BOOT_PARTITION " /etc/mtab | cut -f 2 -d ' ' `
171-
172-
if [ "$DST_ROOT_CURMOUNT" != "" ] || [ "$DST_BOOT_CURMOUNT" != "" ]
173-
then
174-
echo "A destination partition is busy (mounted). Mount status:"
175-
echo " $DST_ROOT_PARTITION: $DST_ROOT_CURMOUNT"
176-
echo " $DST_BOOT_PARTITION: $DST_BOOT_CURMOUNT"
177-
echo -e "Aborting!\n"
178-
exit 0
179-
fi
180-
181204
# Check that destination partitions are the right type.
182205
#
183206
DST_BOOT_PARTITION_TYPE=`parted /dev/$DST_DISK -ms p \

0 commit comments

Comments
 (0)