Skip to content

Commit 1597f6b

Browse files
committed
Convert fstab device names to PARTUUID option. README changes.
Added --convert-fstab-to-partuuid option that converts a device name using fstab and cmdline.txt to use PARTUUID as is standard in recent Raspbian distributions. Just a helper option for users that want to convert. Added back in clone to smaller 4GB card example.
1 parent 1784931 commit 1597f6b

File tree

2 files changed

+123
-9
lines changed

2 files changed

+123
-9
lines changed

README.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ grub can be configured for this to be an officially supported way of
1919
using rpi-clone.
2020

2121
#### Clone by initialization
22-
Source disk mounted partition file system types are compared to
22+
Source disk mounted partition types are compared to
2323
corresponding destination disk partitions.
2424
If the types are not compatible, then the clone is an
2525
initialization. First, the destination partition structure is
@@ -111,6 +111,7 @@ usage: rpi-clone sdN {-v|--verbose} {-f|--force-initialize}
111111
{-s|--setup} {-e|--edit-fstab sdX }
112112
{-m|--mountdir dir } {-l|--leave-sd-usb-boot}
113113
{-a|--all-sync} {-F|--Force-sync} {-x} {-V|--version}
114+
{--convert-fstab-to-partuuid}
114115
115116
-v - verbose rsync, list all files as they are copied.
116117
-f - force initialize the destination disk by imaging the booted disk.
@@ -140,6 +141,25 @@ usage: rpi-clone sdN {-v|--verbose} {-f|--force-initialize}
140141
-s option that has different meaning.
141142

142143
## rpi-clone Example Runs
144+
#### An aside note
145+
You will see in one example below that the clone command will need
146+
to be run differently depending on if device names or PARTUUID is used
147+
in /etc/fstab. If device names are used you will have to add a "-e sdX"
148+
where sdX will be the "expected" disk name a USB disk is assigned during
149+
boot. Usually this works, but if you have multiple disk devices plugged
150+
into USB ports it may not work. While this may not be an issue for you now,
151+
recent Raspbian releases now use PARTUUID as standard and in the long
152+
run you may at some point want to convert.
153+
So as a convenience, if you want to convert to using
154+
PARTUUID, rpi-clone can do that for you, run:
155+
```
156+
$ sudo rpi-clone --convert-fstab-to-partuuid
157+
```
158+
You only need to ever do this once. Subsequent rpi-clone runs will propagate
159+
PARTUUID usage to disks that you clone to. This also converts cmdline.txt.
160+
But get some clone backups before doing this because this changes
161+
your booted disk.
162+
143163
#### 1) First clone to a new SD card in USB card reader
144164
In this example a new SD card in a USB card reader has been plugged in
145165
that I want to clone to. It shows up as sdb because I have another USB
@@ -313,7 +333,44 @@ likely has the SD card /boot partition mounted, the SD card is in use
313333
and using rpi-clone for a clone back to the SD card slot will not work.
314334

315335

316-
#### 6) Creating a Pi3 bootable USB hard drive with extra partitions
336+
#### 6) Clone to smaller 4GB SD card
337+
I happen to have an old 4GB SD card and here's a try to clone to it:
338+
```
339+
root@rpi2: ~$ rpi-clone sda
340+
341+
Booted disk: mmcblk0 15.8GB Destination disk: sda 4.0GB
342+
---------------------------------------------------------------------------
343+
Part Size FS Label Part Size FS Label
344+
1 /boot 58.4MB fat16 -- 1 58.4MB fat16 --
345+
2 root 15.8GB ext4 SD-RPI-16N 2 3.9GB ext4 --
346+
---------------------------------------------------------------------------
347+
== SYNC mmcblk0 file systems to sda ==
348+
/boot (22.5MB used) : SYNC to sda1 (58.4MB size)
349+
/ (5.9GB used) : SYNC to sda2 (3.9GB size)
350+
---------------------------------------------------------------------------
351+
Run setup script : no
352+
Verbose mode : no
353+
-----------------------:
354+
** FATAL ** : Partition 2: source used > destination space.
355+
-----------------------:
356+
357+
Aborting!
358+
Use -F to override used > space fail.
359+
360+
```
361+
So even if rpi-clone thinks that the sync won't work because of lack of
362+
space, there is a -F option which will allow the clone to proceed
363+
anyway. The interesting thing about this case is that while this might
364+
seem a bad idea, the sync will actually come close to succeeding. That's
365+
because the root used space includes a 1.8GB file system based
366+
swap file (/var/swap) that will be excluded from the sync. If this
367+
clone is forced with -F, the card may boot, but there could be some missing
368+
files if the rsync runs out of space and fails to complete and some things
369+
would not work.
370+
This is just a FYI.
371+
372+
373+
#### 7) Creating a Pi3 bootable USB hard drive with extra partitions
317374
I wanted to have a Pi3 hard drive USB boot with extra data partitions
318375
and I want to be able to clone back to 2 partition SD cards for use
319376
in other SD card booted Pis. So when I initially clone to the hard drive
@@ -370,10 +427,10 @@ and powered back on into a hard drive boot. I had previously boot
370427
enabled the Pi3.
371428

372429
## Cloning from a USB booted Pi with extra partitions
373-
Now I have booted the USB hard drive I cloned to in example 6 and will
430+
Now I have booted the USB hard drive I cloned to in example 7 and will
374431
try a few clones.
375432

376-
#### 7) USB disk routine clone to 16GB SD card
433+
#### 8) USB disk routine clone to 16GB SD card
377434
For this case I haven't mounted any of the extra partitions and
378435
the Pi has only the /boot partition mounted. The kernel has seen my hard
379436
drive as sdb but I'm using PARTUUID in fstab so there's no problem.
@@ -402,7 +459,7 @@ Verbose mode : no
402459
Ok to proceed with the clone? (yes/no):
403460
```
404461

405-
#### 8) USB disk with mounted partition 5 clone to 16GB SD card
462+
#### 9) USB disk with mounted partition 5 clone to 16GB SD card
406463
Now I try the clone with one of my extra partitions mounted:
407464
```
408465
pi@rpi0: ~$ sudo rpi-clone sda
@@ -426,7 +483,7 @@ rpi-clone sees the mounted partition 5 and wants to clone it but finds
426483
there's not enough space on the destination disk and won't let me.
427484
A bigger disk is needed to clone all the way through partition 5.
428485

429-
#### 9) USB disk with mounted partition 5 clone to 16GB SD card try 2
486+
#### 10) USB disk with mounted partition 5 clone to 16GB SD card try 2
430487
I've got things I'm working on and don't want to unmount the partition
431488
to make the clone work, so I use the -m option to tell rpi-clone to
432489
only clone root and /boot and exclude any other directory mounts not given

rpi-clone

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
version=2.0.2
3+
version=2.0.4
44

55
PGM=`basename $0`
66
setup_command="$PGM-setup"
@@ -70,6 +70,7 @@ usage: $PGM sdN {-v|--verbose} {-f|--force-initialize}
7070
{-s|--setup} {-e|--edit-fstab sdX }
7171
{-m|--mountdir dir } {-l|--leave-sd-usb-boot}
7272
{-a|--all-sync} {-F|--Force-sync} {-x} {-V|--version}
73+
{--convert-fstab-to-partuuid}
7374
7475
-v - verbose rsync, list all files as they are copied.
7576
-f - force initialize the destination disk by imaging the booted disk.
@@ -113,7 +114,6 @@ usage: $PGM sdN {-v|--verbose} {-f|--force-initialize}
113114
Download:
114115
git clone https://github.com/billw2/rpi-clone
115116
"
116-
117117
exit 1
118118
}
119119

@@ -614,6 +614,7 @@ Unattended=0
614614
quiet=0
615615
custom_sync=0
616616
leave_sd_usb_boot=0
617+
convert_to_partuuid=0
617618

618619
while [ "$1" ]
619620
do
@@ -688,6 +689,9 @@ do
688689
-F|--Force-sync)
689690
force_sync=1
690691
;;
692+
--convert-fstab-to-partuuid)
693+
convert_to_partuuid=1
694+
;;
691695
-V|--version)
692696
echo $PGM Version: $version
693697
exit 0
@@ -734,6 +738,57 @@ then
734738
exit 1
735739
fi
736740

741+
if ((convert_to_partuuid))
742+
then
743+
unattended=0
744+
Unattended=0
745+
746+
fstab=/etc/fstab
747+
fstab_tmp=/tmp/fstab
748+
fstab_save=${fstab}.${PGM}-save
749+
confirm "This will change your $fstab, are you sure?" "abort"
750+
751+
cp $fstab $fstab_tmp
752+
printf "\nConverting $fstab from device names to PARTUUID\n"
753+
count=0
754+
for ((p = 1; p <= n_src_parts; p++))
755+
do
756+
if grep -q "^/dev/${src_partition[p]}" $fstab_tmp
757+
then
758+
partuuid=$(lsblk -n -o PARTUUID /dev/${src_partition[p]})
759+
sed -i "s/\/dev\/${src_partition[p]}/$partuuid/" $fstab_tmp
760+
printf " Editing $fstab, changing /dev/${src_partition[p]} to $partuuid\n"
761+
((++count))
762+
fi
763+
done
764+
if ((count))
765+
then
766+
cp $fstab $fstab_save
767+
cp $fstab_tmp $fstab
768+
printf "Your original fstab is backed up to $fstab_save\n"
769+
770+
cmdline_txt=/boot/cmdline.txt
771+
cmdline_save=$cmdline_txt.${PGM}-save
772+
if [ -f $cmdline_txt ] && grep -q "$src_root_dev" $cmdline_txt
773+
then
774+
root_part=${src_partition[root_part_num]}
775+
partuuid=$(lsblk -n -o PARTUUID $src_root_dev)
776+
if [ "$partuuid" != "" ]
777+
then
778+
cp $cmdline_txt $cmdline_save
779+
sed -i "s/\/dev\/$root_part/PARTUUID=$partuuid/" $cmdline_txt
780+
printf " Editing $cmdline_txt, changing root=$src_root_dev to root=PARTUUID=$partuuid\n"
781+
printf "Your original cmdline.txt is backed up to $cmdline_save\n"
782+
fi
783+
fi
784+
else
785+
printf "Could not find any $src_disk partition names in $fstab, nothing changed.\n"
786+
fi
787+
rm $fstab_tmp
788+
echo ""
789+
exit 0
790+
fi
791+
737792
# dst_mount_flag enumerations:
738793
live=1
739794
temp=2
@@ -1031,10 +1086,12 @@ The destination disk is too small.
10311086
"Destination last partition resize to $space_readable"
10321087
printf "%-22s : %s\n" "" \
10331088
" is too small to hold source used $used_readable."
1034-
if [ "$n_src_parts" == "$root_part_num" ]
1089+
if [ "$n_src_parts" == "$root_part_num" ] && ((!force_sync))
10351090
then
10361091
printf "%-22s : %s\n" "" \
10371092
" This is the root partition, so aborting!"
1093+
printf "%-22s : %s\n" "" \
1094+
" Use -F to override."
10381095
abort=1
10391096
elif ((!force_sync))
10401097
then

0 commit comments

Comments
 (0)