forked from AndyTaylorTweet/Pi-Star_Binaries_sbin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpi-clone
executable file
·1835 lines (1640 loc) · 42.4 KB
/
rpi-clone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# rpi-clone is Copyright (c) 2018-2019 Bill Wilson
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the conditions of the BSD LICENSE file at
# the rpi-clone github source repository:
# https://github.com/billw2/rpi-clone
# This updated code is located in a fork of Bill Wilson's git repository
# at https://github.com/geerlingguy/rpi-clone
version=2.0.23
# setup trusted paths for dependancies (like rsync, grub, fdisk, etc)
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# auto run grub-install if grub detected
grub_auto=1
PGM=`basename $0`
setup_command="$PGM-setup"
rsync_options="--force -rltWDEHXAgoptx"
if [ `id -u` != 0 ]
then
echo -e "$PGM needs to be run as root.\n"
exit 1
fi
rpios=0
rpios_recent=0
if [ -f /etc/os-release ]; then
if [[ -e /etc/rpi-issue ]]; then
rpios=1
fi
pretty="$(cat /etc/os-release | grep PRETTY)"
if ((rpios)) && [[ ! "$pretty" =~ wheezy|jessie|stretch ]]; then
rpios_recent=1
fi
fi
confirm()
{
if ((unattended || (initialize && Unattended) ))
then
return 0
fi
printf "\n%s (yes/no): " "$1"
read resp
if [ "$resp" = "y" ] || [ "$resp" = "yes" ]
then
return 0
fi
if [ "$2" == "abort" ]
then
echo -e "Aborting!\n"
exit 0
fi
return 1
}
# sfdisk is in fdisk package
commands="rsync parted fdisk findmnt column fsck.vfat"
packages="rsync parted util-linux mount bsdmainutils dosfstools"
need_packages=""
idx=1
for cmd in $commands
do
if ! command -v $cmd > /dev/null
then
pkg=$(echo "$packages" | cut -d " " -f $idx)
printf "%-30s %s\n" "Command not found: $cmd" "Package required: $pkg"
need_packages="$need_packages $pkg"
fi
((++idx))
done
if [ "$need_packages" != "" ]
then
confirm "Do you want to apt-get install the packages?" "abort"
apt-get install -y --no-install-recommends $need_packages
fi
clone=/mnt/clone
clone_src=/mnt/clone-src
clone_log=/var/log/$PGM.log
HOSTNAME=`hostname`
usage()
{
echo $"
usage: $PGM sdN {-v|--verbose} {-f|--force-initialize} {-f2}
{-p|--p1-size size} {-u|--unattended} {-U|--Unattended} {-q|--quiet}
{-s|--setup host} {-e|--edit-fstab sdX } {-m|--mountdir dir }
{-L|--label-partitions label} {-l|--leave-sd-usb-boot}
{-a|--all-sync} {-F|--Force-sync} {-x} {-V|--version}
{--convert-fstab-to-partuuid}
{--exclude=PATTERN} {--exclude-from=FILE}
-v - verbose rsync, list all files as they are copied.
-f - force initialize the destination disk by imaging the booted disk
partition structure. File systems are then synced or imaged.
-f2 - force initialize only the first 2 partitions to the destination.
So a multi partition USB boot can initialize clone back to
a 2 partition SD card.
-p size - resize destination partition 1 to 'size' bytes. For two partition
initialize (when first clone to blank disk or using -f2 or -f).
Use 'sizeM' for MiB size units. eg -p 256M equals -p 268435456
-u - unattended clone if not initializing. No confirmations asked,
but abort if disk needs initializing or on error.
-U - unattended even if initializing. No confirmations asked,
but abort only on errors.
-q - quiet mode, no output unless errors or initializing. Implies -u.
-s host - add 'host' to args passed to script rpi-clone-setup and run it
after cloning but before unmounting partitions. For setting
clone disk hostname, but args can be what the script expects.
You can give multiple '-s arg' options.
-e sdX - edit destination fstab to change booted device names to new
device 'sdX'. This is Only for fstabs that use device names.
Used for setting up a USB bootable disk.
-m dir - Add dir to a custom list of mounted directories to sync. Then
the custom list will be synced instead of the default of all
mounted directories. The root directory is always synced.
Not for when initializing.
-L lbl - label for ext type partitions. If 'lbl' ends with '#', replace
'#' with a partition number and label all ext partitions.
Otherwise, apply label to root partition only.
-l - leave SD card to USB boot alone when cloning to SD card mmcblk0
from a USB boot. This preserves a SD card to USB boot setup
by leaving the SD card cmdline.txt using the USB root. When
cloning to USB from SD card this option sets up the SD card
cmdline.txt to boot to the USB disk.
-a - Sync all partitions if types compatible, not just mounted ones.
-F - force file system sync or image for some errors. eg:
If source used > destination space error, do the sync anyway.
If a source partition mount error, skip it and do other syncs.
-x - use set -x for very verbose bash shell script debugging
-V - print rpi-clone version.
Clone a booted file system to a destination disk which is bootable.
The destination disk is a SD card (USB card reader) or USB disk 'sdN' plugged
into a USB port. The 'sdN' name should be a full disk name like sda and not
a partition name like sda1. $PGM works on a Raspberry Pi and can work on
other systems. For a destination disk that shows up as sda, run:
$ sudo rpi-clone sda
Clones can be from a booted SD card or USB disk. For a description, example
clone runs and example usage of above options, see the README.md at:
https://github.com/geerlingguy/rpi-clone
A line logging a $PGM run is written to $clone_log.
Download:
git clone https://github.com/geerlingguy/rpi-clone
"
exit 1
}
readable_MiB()
{
val=$1
if [ "$val" == "" ]
then
result=" ??"
else
blk_size=$2
val=$((val / 1024 * blk_size))
if ((val < 1024 * 1024))
then
result=$(echo $val \
| awk '{ byte =$1 /1024; printf "%.1f%s", byte, "M" }')
elif ((val < 1024 * 1024 * 1024))
then
result=$(echo $val \
| awk '{ byte =$1 /1024/1024; printf "%.1f%s", byte, "G" }')
else
result=$(echo $val \
| awk '{ byte =$1 /1024/1024/1024; printf "%.1f%s", byte, "T" }')
fi
fi
printf -v "${3}" "%s" "$result"
}
readable_MB()
{
val=$1
if [ "$val" == "" ]
then
result=" ??"
else
blk_size=$2
val=$((val / 1000 * blk_size))
if ((val < 1000 * 1000))
then
result=$(echo $val \
| awk '{ byte =$1 /1000; printf "%.1f%s", byte, "MB" }')
elif ((val < 1000 * 1000 * 1000))
then
result=$(echo $val \
| awk '{ byte =$1 /1000/1000; printf "%.1f%s", byte, "GB" }')
else
result=$(echo $val \
| awk '{ byte =$1 /1000/1000/1000; printf "%.1f%s", byte, "TB" }')
fi
fi
printf -v "${3}" "%s" "$result"
}
qecho()
{
if ((!quiet))
then
echo "$@"
fi
}
qprintf()
{
if ((!quiet))
then
printf "$@"
fi
}
unmount_or_abort()
{
if [ "$1" == "" ]
then
return
fi
qprintf "\n $2\n The clone cannot proceed unless it is unmounted."
if confirm "Do you want to unmount $1?" "abort"
then
if ! umount $1
then
echo "$PGM could not unmount $1."
echo -e "Aborting!\n"
exit 0
fi
fi
}
unmount_list()
{
if [ "$1" == "" ]
then
return
fi
for dir in $1
do
qecho " unmounting $dir"
if ! umount $dir
then
qecho " Failed to unmount: $dir"
fi
done
}
mount_partition()
{
qecho " Mounting $1 on $2"
if ! mount $1 $2
then
echo " Mount failure of $1 on $2."
if [ "$3" != "" ]
then
unmount_list $3
fi
echo "Aborting!"
exit 1
fi
}
rsync_file_system()
{
src_dir="$1"
dst_dir="$2"
qprintf " => rsync $1 $2 $3 ..."
if [ "$3" == "with-root-excludes" ]
then
rsync $rsync_options --delete \
$exclude_useropt \
$exclude_swapfile \
--exclude '.gvfs' \
--exclude '/dev/*' \
--exclude '/mnt/clone/*' \
--exclude '/proc/*' \
--exclude '/run/*' \
--exclude '/sys/*' \
--exclude '/tmp/*' \
--exclude 'lost\+found/*' \
$src_dir \
$dst_dir
else
rsync $rsync_options --delete \
$exclude_useropt \
--exclude '.gvfs' \
--exclude 'lost\+found/*' \
$src_dir \
$dst_dir
fi
qecho ""
}
print_partitions()
{
if ((quiet)) && ((!initialize))
then
return
fi
n_parts=$(( (n_src_parts >= n_dst_parts) ? n_src_parts : n_dst_parts ))
readable_MB $src_disk_size "512" src_size_readable
readable_MB $dst_disk_size "512" dst_size_readable
printf "\n%-43s%s" "Booted disk: $src_disk $src_size_readable" \
"Destination disk: $dst_disk $dst_size_readable"
echo $"
---------------------------------------------------------------------------"
out=$'Part, Size,FS,Label ,Part, Size,FS,Label\n'
for ((p = 1; p <= n_parts; p++))
do
if ((p <= n_src_parts && src_exists[p]))
then
readable_MiB ${src_size_sectors[p]} "512" tmp
printf -v sectors_readable "%7s" $tmp
pname="$p ${src_name[p]}"
out=${out}$"$pname,$sectors_readable,${src_fs_type[p]},${src_label[p]},"
else
out=${out}$" , , , ,"
fi
if ((p <= n_dst_parts && dst_exists[p]))
then
readable_MiB ${dst_size_sectors[p]} "512" tmp
printf -v sectors_readable "%7s" $tmp
out=${out}$"$p,$sectors_readable,${dst_fs_type[p]},${dst_label[p]},"
else
out=${out}$" , , , ,"
fi
out=${out}$'\n'
done
echo $"$out" | column -t -s ','
if ((alt_root_part_num > 0))
then
echo $"
** Assuming destination root partition for the clone is $dst_part_base$root_part_num
The root FS mount is not from booted $src_disk. It is ${src_root_dev#/dev/}"
fi
echo $"---------------------------------------------------------------------------"
}
print_sync_actions()
{
if ((quiet))
then
return
fi
for ((p = 1; p <= n_src_parts; p++))
do
if ((!src_exists[p]))
then
continue
fi
if ((p == root_part_num && alt_root_part_num > 0))
then
part=${src_root_dev#/dev/}
flow="$part to $dst_part_base$p"
else
flow="to $dst_part_base$p"
fi
if ((src_sync_part[p]))
then
if [ "${src_mounted_dir[p]}" != "" ]
then
src_label="${src_mounted_dir[p]}"
action_label="SYNC"
else
src_label="/dev/${src_partition[p]}"
action_label="MOUNT SYNC"
fi
readable_MiB ${src_used_sectors[p]} "512" used
readable_MiB ${dst_size_sectors[p]} "512" size
printf "%-22s%-14s : %s %s\n" \
"$src_label" "(${used} used)" "$action_label" \
"$flow (${size} size)"
fi
done
}
print_image_actions()
{
for ((p = 1; p <= n_src_parts; p++))
do
if ((!src_exists[p]))
then
continue
fi
pname="$p ${src_name[p]}"
fs_type=${src_fs_type[$p]}
if ((p == root_part_num && alt_root_part_num > 0))
then
part=${src_root_dev#/dev/}
flow="$part to $dst_part_base$p"
else
flow="to $dst_part_base$p"
fi
action=""
if ((p <= n_image_parts))
then
if ((p == 1))
then
if ((p1_size_new > 0))
then
action="RESIZE MKFS SYNC $flow"
else
action="MKFS SYNC $flow"
fi
elif [ "$fs_type" == "swap" ]
then
action="MKSWAP"
elif ((p != ext_part_num))
then
if [ "${src_mounted_dir[p]}" != "" ] || ((p == n_src_parts))
then
if ((p < n_src_parts || last_part_space || force_sync))
then
action="MKFS SYNC $flow"
else
action="MKFS **NO SYNC**"
fi
else
action="IMAGE $flow"
fi
fi
fi
if ((p == n_image_parts))
then
readable_MiB ${src_used_sectors[n_image_parts]} "512" used
printf "%-22s%-14s : RESIZE %s\n" \
"$pname" "(${used} used)" "$action"
elif ((src_used_sectors[$p] > 0 && p < n_image_parts))
then
readable_MiB ${src_used_sectors[p]} "512" used
printf "%-22s%-14s : $action\n" "$pname" "(${used} used)"
elif [ "$action" != "" ]
then
printf "%-36s : $action\n" "$pname"
fi
done
}
print_options()
{
if ((quiet))
then
return
fi
echo $"---------------------------------------------------------------------------"
if ((force_sync))
then
printf "%-22s : %s\n" "-F" \
"forcing clone to skip some errors."
fi
if ((force_2_parts))
then
printf "%-22s : %s\n" "-f2" \
"force initialize to first two partitions only."
fi
if ((p1_size_new > 0))
then
printf "%-3s%-19s : %s %s %s\n" "-p " "$p1_size_arg" \
"resize /boot to" "$p1_size_new" "blocks of 512 Bytes."
fi
if [ "$edit_fstab_name" != "" ]
then
printf "%-22s : %s\n" "-e clone fstab edit" \
"edit $src_part_base device entries to $edit_fstab_name."
fi
if [ "$ext_label" != "" ]
then
rep="${ext_label: -1}"
if [ "$rep" == "#" ]
then
msg="all ext partition types"
else
msg="root partition only"
fi
printf "%-22s : %s\n" "-L $ext_label" \
"volume label for $msg."
fi
if ((leave_sd_usb_boot))
then
if ((SD_slot_dst))
then
msg="leave SD card cmdline.txt bootable to USB."
elif ((SD_slot_boot))
then
msg="install boot to USB cmdline.txt on SD card."
else
msg="-l ignored. Src or dst is not a SD card slot."
fi
printf "%-22s : %s\n" "-l SD to USB boot mode" "$msg"
fi
if [ "$setup_args" != "" ]
then
printf "%-22s : %s\n" "Run setup script" "$setup_command $setup_args"
else
printf "%-22s : no.\n" "Run setup script"
fi
if ((have_grub))
then
printf "%-22s : %s\n" "Run grub" \
"grub-install --root-directory=$clone /dev/$dst_disk"
fi
printf "%-22s : %s.\n" "Verbose mode" "$verbose"
printf "%-23s:\n" "-----------------------"
}
ext_label()
{
pnum=$1
fs_type=$2
flag=$3
label_arg=""
if [ "$ext_label" != "" ] && [[ "$fs_type" == *"ext"* ]]
then
rep="${ext_label: -1}"
if [ "$rep" == "#" ]
then
label_arg=${ext_label:: -1}
label_arg="$flag $label_arg$pnum"
elif ((pnum == root_part_num))
then
label_arg="$flag $ext_label"
fi
fi
printf -v "${4}" "%s" "$label_arg"
}
get_src_disk()
{
partition=${1#/dev/}
disk=${partition:: -1}
num="${partition: -1}"
if [[ $disk == *"mmcblk"* || $disk == *"nvme"* ]]
then
SD_slot_boot=1
disk=${disk:0:7}
src_part_base=${disk}p
fi
printf -v "${2}" "%s" "$disk"
printf -v "${3}" "%s" "$num"
}
# ==== source (booted) disk info and default mount list
#
src_boot_dev=`findmnt /boot -o source -n | grep "/dev/"`
src_root_dev=`findmnt / -o source -n | grep "/dev/"`
SD_slot_boot=0
SD_slot_dst=0
src_part_base=""
boot_part_num=0
alt_root_part_num=0
if [ "$src_boot_dev" == "" ]
then
get_src_disk "$src_root_dev" "src_disk" "unused"
else
get_src_disk "$src_boot_dev" "src_disk" "boot_part_num"
fi
get_src_disk "$src_root_dev" "src_root_disk" "root_part_num"
if [ "$src_disk" == "" ]
then
echo "Cannot find booted device."
exit 1
fi
if [ "$src_part_base" == "" ]
then
src_part_base=$src_disk
fi
if [ "$src_disk" != "$src_root_disk" ]
then
if ((SD_slot_boot))
then
# Handle SD card boots with root on different USB disk device.
# But will assume SD card has a root partition just above its root.
#
alt_root_part_num="$root_part_num"
root_part_num=$((boot_part_num + 1))
else
echo $"
Boot and root are on different disks and it's not a SD card boot.
Don't know how to partition the destination disk!"
exit 1
fi
fi
# src_root_dev, if on device other than booted, is not in src_partition_table
# and src_fdisk_table, but is in src_df_table and src_mount_table
#
src_partition_table=$(parted -m "/dev/$src_disk" unit s print | tr -d ';')
src_fdisk_table=$(fdisk -l /dev/$src_disk | grep "^/dev/")
tmp=$(df | grep -e "^/dev/$src_disk" -e "^/dev/root" -e "$src_root_dev" \
| tr -s " ")
dev=${src_root_dev#/dev/}
src_df_table=$(echo "$tmp" | sed "s/root/$dev/")
n_src_parts=$(echo "$src_partition_table" | tail -n 1 | cut -d ":" -f 1)
src_disk_size=$(echo "$src_partition_table" \
| grep "^/dev/$src_disk" | cut -d ":" -f 2 | tr -d 's')
line=$(fdisk -l /dev/$src_disk | grep "Disk identifier:")
src_disk_ID=${line#*x}
src_mount_table=$(findmnt -o source,target -n -l \
| grep -e "^/dev/$src_disk" -e "^$src_root_dev" | tr -s " ")
n_mounts=$(echo "$src_mount_table" | wc -l)
if ((alt_root_part_num > 0 && n_src_parts < 2))
then
echo $"
Booted disk has only one partition and the root is from another device.
Don't know how to partition the destination disk!
"
exit 1
fi
line=$(echo "$src_fdisk_table" | grep "Extended")
if [ "$line" != "" ]
then
dev=$(echo "$line" | cut -d " " -f 1)
ext_part_num="${dev: -1}"
else
ext_part_num=0
fi
for ((p = 1; p <= n_src_parts; p++))
do
line=$(echo "$src_partition_table" | grep -e "^${p}:")
if [ "$line" == "" ]
then
src_exists[p]=0
continue
fi
src_exists[p]=1
if ((p == root_part_num))
then
src_partition[p]=${src_root_dev#/dev/}
src_device[p]=$src_root_dev
else
src_partition[p]="${src_part_base}${p}"
src_device[p]="/dev/${src_partition[p]}"
fi
# parted sectors are 512 bytes
src_start_sector[p]=$(echo "$line" | cut -d ":" -f 2 | tr -d 's')
src_size_sectors[p]=$(echo "$line" | cut -d ":" -f 4 | tr -d 's')
part_type=$(echo "$line" | cut -d ":" -f 5)
src_mounted_dir[p]=$(echo "$src_mount_table" \
| grep -m 1 -e "^${src_device[p]}" | cut -d " " -f 2)
if [ "${src_mounted_dir[p]}" != "" ]
then
src_sync_part[p]=1
else
src_sync_part[p]=0
fi
src_name[p]=""
if [ "$part_type" != "" ]
then
src_fs_type[p]="$part_type"
else
src_fs_type[p]="--"
fi
src_label[p]="--"
if [ "${src_mounted_dir[p]}" == "/" ]
then
src_name[p]="root"
#
# If root on device other than booted SD card, root_part_num assumed to be
# booted /boot part_num + 1 and alt_root_part_num is from root device.
#
elif ((p == root_part_num)) && ((alt_root_part_num > 0))
then
src_name[p]="root**"
elif ((p == ext_part_num))
then
src_fs_type[p]="EXT"
elif [[ "$part_type" == *"linux-swap"* ]]
then
src_fs_type[p]="swap"
elif [ "${src_mounted_dir[p]}" != "" ]
then
src_name[p]="${src_mounted_dir[p]}"
fi
if [[ "$part_type" == *"ext"* ]]
then
label=`e2label ${src_device[p]} 2> /dev/null`
if [ "$label" != "" ]
then
src_label[p]="$label"
fi
fi
done
# command line
#
setup_args=""
edit_fstab_name=""
ext_label=""
verbose="no"
force_initialize=0
force_2_parts=0
force_sync=0
all_sync=0
usage_error=0
unattended=0
Unattended=0
quiet=0
custom_sync=0
leave_sd_usb_boot=0
convert_to_partuuid=0
p1_size_new=0
while [ "$1" ]
do
case "$1" in
-v|--verbose)
verbose="yes"
rsync_options=${rsync_options}v
;;
-u|--unattended)
unattended=1
;;
-U|--Unattended-init)
unattended=1
Unattended=1
;;
-q|--quiet)
unattended=1
quiet=1
rsync_options=${rsync_options}q
;;
--exclude=*|--exclude-from=*)
exclude_useropt="${exclude_useropt} $1"
;;
-s|--setup)
shift
if ! command -v $setup_command > /dev/null
then
echo "Cannot find script $setup_command for setup arg \"$1\"."
usage_error=1
fi
if [ "$setup_args" == "" ]
then
setup_args="$1"
else
setup_args="$setup_args $1"
fi
;;
-e|--edit-fstab)
shift
edit_fstab_name=$1
;;
-f|--force-initialize)
force_initialize=1
;;
-f2)
force_initialize=1
force_2_parts=1
;;
-p|--p1-size)
shift
p1_size_arg=$1
p1_size_new=$1
if [[ $p1_size_arg =~ ^[0-9MG]+$ ]]
then
if [[ $p1_size_new == *"M" ]]
then
size=$(echo $p1_size_new | cut -d M -f 1)
p1_size_new=$(($size * 1024 * 1024 / 512))
elif [[ $p1_size_new == *"G" ]]
then
size=$(echo $p1_size_new | cut -d G -f 1)
p1_size_new=$(($size * 1024 * 1024 * 1024 / 512))
fi
if [[ $p1_size_new =~ ^[0-9]+$ ]]
then
if ((!force_sync && p1_size_new < 200 * 1024))
then
echo "Setting /boot partition size less than 100 MB seems wrong so will not try."
echo " Use -F before -p to override."
exit 1
fi
else
echo "Confused by -p $p1_size_arg."
exit 1
fi
else
echo "Invalid character in -p size. Use digits + M or G like: -p 256M"
exit 1
fi
;;
-x)
set -x
;;
-a|--all-sync)
all_sync=1
;;
-m|--mountdir)
shift
mount_ok=0
for ((p = 1; p <= n_src_parts; p++))
do
if ((!src_exists[p]))
then
continue
fi
if ((!custom_sync)) && ((p != root_part_num))
then
src_sync_part[p]=0
fi
if [ "${src_mounted_dir[p]}" == "$1" ]
then
src_sync_part[p]=1
mount_ok=1
fi
done
if ((!mount_ok))
then
echo "Asking to clone directory \"$1\", but it is not mounted."
usage_error=1
fi
custom_sync=1
;;
-L|--label_partitions)
shift
ext_label=$1
;;
-l|--leave-sd-usb-boot)
leave_sd_usb_boot=1
;;
-F|--Force-sync)
force_sync=1
;;
--convert-fstab-to-partuuid)
convert_to_partuuid=1
;;
-V|--version)
echo $PGM Version: $version
exit 0
;;
-h|--help)
usage
;;
*)
if [ "$dst_disk" != "" ]
then
echo "Bad arg: $1"
echo "Run $PGM with -h or no args for usage."
exit 1
fi
dst_disk=$1
dir=`expr substr $dst_disk 1 5`
if [ "$dir" == "/dev/" ]
then
dst_disk=${dst_disk#/dev/}
fi
;;
esac
shift
done
if ((custom_sync)) && ((all_sync))
then
echo "-m and -a options at the same time conflict."
exit 1
fi
if ((custom_sync)) && ((force_initialize))
then
echo "-m and -f options at the same time conflict."
exit 1
fi
if [[ "$verbose" == "yes" ]] && ((quiet))
then
echo "-q and -v options at the same time conflict."
exit 1
fi
if ((usage_error))
then
echo ""
exit 1
fi
# Starting with bookworm /boot/firmware/cmdline.txt is used. All previous RaspbianOS releases use /boot/cmdline.txt
cmdlinedir=$(mount | awk '$3 ~ "^/boot(/firmware)?$" && $5 == "vfat" { print substr($3, 2); exit }' )
if [[ -z "$cmdlinedir" ]]; then
echo "Unable to locate boot device"
exit 1
fi
if ((convert_to_partuuid))
then
unattended=0
Unattended=0
fstab=/etc/fstab
fstab_tmp=/tmp/fstab
fstab_save=${fstab}.${PGM}-save
confirm "This will change your $fstab, are you sure?" "abort"
cp $fstab $fstab_tmp
printf "\nConverting $fstab from device names to PARTUUID\n"
count=0
for ((p = 1; p <= n_src_parts; p++))
do
if ((!src_exists[p]))
then
continue
fi
if grep -q "^/dev/${src_partition[p]}" $fstab_tmp
then
partuuid=$(lsblk -n -o PARTUUID /dev/${src_partition[p]})
sed -i "s/\/dev\/${src_partition[p]}/PARTUUID=$partuuid/" $fstab_tmp
printf " Editing $fstab, changing /dev/${src_partition[p]} to $partuuid\n"
((++count))
fi
done
if ((count))
then
cp $fstab $fstab_save
cp $fstab_tmp $fstab
printf "Your original fstab is backed up to $fstab_save\n"
cmdline_txt=/${cmdlinedir}/cmdline.txt
cmdline_save=$cmdline_txt.${PGM}-save
if [ -f $cmdline_txt ] && grep -q "$src_root_dev" $cmdline_txt
then
root_part=${src_partition[root_part_num]}
partuuid=$(lsblk -n -o PARTUUID $src_root_dev)
if [ "$partuuid" != "" ]
then
cp $cmdline_txt $cmdline_save
sed -i "s/\/dev\/$root_part/PARTUUID=$partuuid/" $cmdline_txt
printf " Editing $cmdline_txt, changing root=$src_root_dev to root=PARTUUID=$partuuid\n"
printf "Your original cmdline.txt is backed up to $cmdline_save\n"
fi
fi
else
printf "Could not find any $src_disk partition names in $fstab, nothing changed.\n"
fi
rm $fstab_tmp