-
Notifications
You must be signed in to change notification settings - Fork 0
/
xorriso-dd-target
961 lines (908 loc) · 28.9 KB
/
xorriso-dd-target
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
#!/bin/sh
# Copyright (c) 2019 - 2020
# Nio Wiklund alias sudodus <nio dot wiklund at gmail dot com>
# Thomas Schmitt <[email protected]>
# Provided under GPL version 2 or later.
# All names of variables and functions begin by "xdt_" in order to facilitate
# the re-use of this code by inclusion or forking and expansion.
# Before using most of the functions it is necessary to run
# xdt_init ; xdt_reset_job
# The only function which may be run earlier is xdt_set_lang_c .
# The function xtd_main gets run by the code at the end of this file
# if $no_xorriso_dd_target_run is empty. It implements what xdt_print_usage
# announces.
xdt_print_version() {
echo "xorriso-dd-target , version 1.5.3 , 2020.02.12.195952"
}
## This obtrusive setting shall make the script safe against exotic locales.
## Supposed to stabilize grep expression interpretation and error messages.
## It is optional but highly advisable.
xdt_set_lang_c() {
export LANG=C
export LC_ALL=C
return 0
}
## This function has to be called before any real work can be done.
xdt_init() {
# Check whether we are on GNU/Linux
if uname -s | grep -v '^Linux' >/dev/null
then
echo "This program is entirely specialized on Linux kernel device names." >&2
echo "Found to be on: '$(uname -s)'" >&2
return 2
fi
# Accept sudo-executable commands only in well known directories.
# (Listed with increasing priority.)
xdt_lsblk_cmd=
xdt_dd_cmd=
xdt_umount_cmd=
if test "$(whoami)" = "root"
then
xdt_sudo_x_dir_list="/usr/bin /bin /usr/sbin /sbin"
else
xdt_sudo_x_dir_list="/usr/sbin /sbin /usr/bin /bin"
fi
for xdt_i in $xdt_sudo_x_dir_list
do
if test -x "$xdt_i"/lsblk
then
xdt_lsblk_cmd="$xdt_i"/lsblk
fi
if test -x "$xdt_i"/dd
then
xdt_dd_cmd="$xdt_i"/dd
fi
if test -x "$xdt_i"/umount
then
xdt_umount_cmd="$xdt_i"/umount
fi
done
if test -z "$xdt_lsblk_cmd"
then
echo "No executable program lsblk found in: $xdt_sudo_x_dir_list" >&2
return 5
fi
return 0
}
xdt_print_usage() {
echo "usage: $0 [options] [device_name [device_name ...]]"
echo
echo "Looks on GNU/Linux for USB and Memory Card devices and evaluates"
echo "whether the found devices are plausible targets for image copying."
echo "If no device names and no -list_all are given, then a plain list of"
echo "advisable device names is printed to stdout. One per line."
echo "Device names must not begin by '-' and must be single words. They must"
echo "not begin by '/dev/'. E.g. 'sdc' is valid, '/dev/sdc' is not valid."
echo "If device names are given, then they get listed with advice shown."
echo "If one of the given device names gets not advised, the exit value is 1."
echo
echo "The option -plug_test can determine the desired target device by"
echo "inquiring the system with unplugged device and then with plugged one."
echo
echo "Only if option -DO_WRITE is given and -list_all is not, and if exactly"
echo "one advisable device is listed, it really gets overwritten by the"
echo "file content of the given -image_file. In this case the exit value"
echo "is zero if writing succeeded, non-zero else."
echo "Option -dummy prevents this kind of real action and rather shows the"
echo "unmount and write commands on stdout."
echo
echo "Options:"
echo " -plug_test Find the target device by asking the user to press"
echo " Enter when the desired target is _not_ plugged in,"
echo " to then plug it in, and to press Enter again."
echo " This overrides device names and option -list_all."
echo " The found device is then shown with advice, vendor,"
echo " and model. Option -DO_WRITE is obeyed if given."
echo " -list_all Print list of all found devices with advice, vendor"
echo " and model. One per line. Ignore any device names."
echo " Ignore -DO_WRITE."
echo " -list_long With each line printed by -list_all or a submitted"
echo " device name, let lsblk print info which led to the"
echo " shown reasons."
echo " -with_vendor_model Print vendor and model with each submitted device"
echo " name."
echo " -max_size n[M|G|T] Set upper byte size limit for advisable devices."
echo " Plain numbers get rounded down to full millions."
echo " Suffix: M = million, G = billion, T = trillion."
echo " Be generous to avoid problems with GB < GiB."
echo " -min_size n[M|G|T] Set lower byte size limit for advisable devices."
echo " After processing like with -max_size, one million"
echo " gets added to the size limit."
echo " -look_for_iso Demand presence of an ISO 9660 filesystem. If so,"
echo " any further filesystem type is acceptable on that"
echo " device. Else only ISO 9660 and VFAT are accepted."
echo " -with_sudo Run '$xdt_lsblk_cmd -o FSTYPE' by sudo."
echo " If no filesystems are detected and the program"
echo " has no superuser power, the device is not advised."
echo " If -DO_WRITE is given, run umount and dd by sudo."
echo " -trust_lsblk_udev Suppress the reason no_fs_while_not_su- if lsblk"
echo " is linked with libudev.so. In this case it is"
echo " likely that lsblk can retrieve FSTYPE even if"
echo " run by a non-priviledged user."
echo " -image_file PATH Set the path of the image file which shall be"
echo " written to a device. Its size will be set as"
echo " -min_size."
echo " -DO_WRITE Write the given -image_file to the one advisable"
echo " device that is found. If more than one such device"
echo " is found, then they get listed but no writing"
echo " happens. In this case, re-run with one of the"
echo " advised device names to get a real write run."
echo " -no_pacifier Do not use dd options to print progress messages"
echo " and to perform synchronized output."
echo " -dummy Report the -DO_WRITE actions but do not perform"
echo " them."
echo " -dummy_force If a single device name is given, do a run of"
echo " -dummy -DO_WRITE even against the advice of"
echo " this program. This probably shows you ways to"
echo " shoot your own foot."
echo " -version Print version text and then end the program."
echo " -help Print this text to stdout and then end the program."
echo "Examples:"
echo " $0 -with_sudo -list_all"
echo " $0 sdc"
echo " $0 -with_sudo -image_file debian-live-10.0.0-amd64-xfce.iso -DO_WRITE -dummy"
echo " $0 -with_sudo -image_file debian-live-10.0.0-amd64-xfce.iso -DO_WRITE -plug_test"
echo
return 0
}
# Roughly convert human readable sizes and plain numbers to 1 / million
xdt_round_down_div_million() {
sed \
-e 's/^[0-9][0-9][0-9][0-9][0-9][0-9]$/0/' \
-e 's/^[0-9][0-9][0-9][0-9][0-9]$/0/' \
-e 's/^[0-9][0-9][0-9][0-9]$/0/' \
-e 's/^[0-9][0-9][0-9]$/0/' \
-e 's/^[0-9][0-9]$/0/' \
-e 's/^[0-9]$/0/' \
-e 's/\.[0-9]*//' \
-e 's/[0-9][0-9][0-9][0-9][0-9][0-9]$//' \
-e 's/[Mm]$//' \
-e 's/[Gg]$/000/' \
-e 's/[Tt]$/000000/'
return 0
}
## Check for harmless name or number in program argument
xdt_check_parameter() {
if test "X$2" = "Xdevice_name"
then
if echo "X$1" | grep '[^A-Za-z0-9_/-]' >/dev/null
then
echo "SORRY: Given device name contains unexpected character. Ok: [A-za-z0-9_/-]" >&2
return 12
elif echo "X$1" | grep '^X-' >/dev/null
then
echo "SORRY: Given device name begins by '-' character." >&2
return 15
fi
elif test "X$2" = "Ximage_file"
then
if echo "X$1" | grep '[$`[*?<>|&!{\]' >/dev/null
then
echo "SORRY: Given image file name contains unexpected character. Not ok: "'[$`[*?<>|&!{\]' >&2
return 15
elif echo "X$1" | grep '^X[-(]' >/dev/null
then
echo "SORRY: Given image file name begins by problematic character. Not ok: "'[-(]' >&2
return 15
fi
else
if echo "X$1" | grep -v '^X[0-9][0-9]*[0-9MGTmgt]$' >/dev/null
then
echo "SORRY: Number for $2 too short or bad character. Ok: [0-9][0-9MGTmgt]" >&2
return 14
fi
fi
return 0
}
### Assessing arguments and setting up the job
# Settings
xdt_reset_job() {
xdt_list_all=
xdt_do_list_long=
xdt_show_reasons=
xdt_look_for_iso=
xdt_devs=
xdt_devs_named=
xdt_max_size=
xdt_with_vendor_model=
xdt_with_sudo=
xdt_image_file=
xdt_do_write=
xdt_dummy_run=
xdt_dummy_force=
xdt_no_pacifier=
xdt_do_plug_test=
xdt_trust_lsblk_udev=
# Status
xdt_sudo_cmd=
xdt_have_su_power=
xdt_end_after_setup=
xdt_dev_end=
xdt_dd_bs="bs=1M"
xdt_dd_silent=
return 0
}
## Predict superuser power. Possibly enable sudo with lsblk -o FSTYPE and dd.
## Also predict whether lsblk -o FSTYPE will bring results as non-root.
xdt_predict_su_power() {
if test "$(whoami)" = "root"
then
xdt_have_su_power=y
elif test -n "$xdt_with_sudo"
then
echo "Testing sudo to possibly get password prompting done now:" >&2
if sudo "$xdt_lsblk_cmd" -h >/dev/null
then
echo "sudo $xdt_lsblk_cmd seems ok." >&2
echo >&2
xdt_sudo_cmd=sudo
xdt_have_su_power=y
else
echo "FAILURE: Cannot execute program $xdt_lsblk_cmd by sudo" >&2
return 11
fi
fi
# lsblk linked with libudev.so usually can obtain filesystem info
# without superuser powers.
if test -n "$xdt_trust_lsblk_udev"
then
if ldd "$xdt_lsblk_cmd" | grep '\blibudev\.so' >/dev/null
then
xdt_lsblk_fs_wo_su=y
fi
fi
return 0
}
## Sets xdt_variables according to its arguments
xdt_arg_interpreter() {
xdt_next_is=
for xdt_i in "$@"
do
# The next_is option parameter readers get programmed by the -options
if test "$xdt_next_is" = "max_size"
then
xdt_check_parameter "$xdt_i" -max_size || return "$?"
xdt_max_size="$(echo "$xdt_i" | xdt_round_down_div_million)"
xdt_next_is=
elif test "$xdt_next_is" = "min_size"
then
xdt_check_parameter "$xdt_i" -min_size || return "$?"
xdt_min_size="$(echo "$xdt_i" | xdt_round_down_div_million)"
xdt_min_size="$(expr $xdt_min_size + 1)"
xdt_next_is=
elif test "$xdt_next_is" = "image_file"
then
xdt_check_parameter "$xdt_i" image_file || return "$?"
xdt_image_file="$xdt_i"
xdt_min_size="$(stat -c '%s' "$xdt_i" \
| xdt_round_down_div_million)"
if test -z "$xdt_min_size"
then
echo "FAILURE: Cannot obtain size of -image_file '$xdt_i'" >&2
return 13
else
xdt_min_size="$(expr $xdt_min_size + 1)"
fi
xdt_next_is=
elif test "X$xdt_i" = "X-list_all"
then
xdt_list_all=y
xdt_with_vendor_model=y
xdt_show_reasons=y
elif test "X$xdt_i" = "X-list_long"
then
xdt_do_list_long=y
elif test "X$xdt_i" = "X-plug_test"
then
xdt_do_plug_test=y
elif test "X$xdt_i" = "X-max_size"
then
xdt_next_is="max_size"
elif test "X$xdt_i" = "X-min_size"
then
xdt_next_is="min_size"
elif test "X$xdt_i" = "X-with_vendor_model"
then
xdt_with_vendor_model=y
elif test "X$xdt_i" = "X-look_for_iso"
then
xdt_look_for_iso=y
elif test "X$xdt_i" = "X-trust_lsblk_udev"
then
xdt_trust_lsblk_udev=y
elif test "X$xdt_i" = "X-with_sudo"
then
xdt_with_sudo=y
elif test "X$xdt_i" = "X-image_file"
then
xdt_next_is="image_file"
elif test "X$xdt_i" = "X-dummy"
then
xdt_dummy_run=y
elif test "X$xdt_i" = "X-dummy_force"
then
xdt_dummy_run=y
xdt_do_write=y
xdt_dummy_force=y
elif test "X$xdt_i" = "X-DO_WRITE"
then
xdt_do_write=y
elif test "X$xdt_i" = "X-no_pacifier"
then
xdt_no_pacifier=y
elif test "X$xdt_i" = "X-version"
then
xdt_print_version
xdt_end_after_setup=y
return 0
elif test "X$xdt_i" = "X-help"
then
xdt_print_usage
xdt_end_after_setup=y
return 0
elif echo "X$xdt_i" | grep -v '^X-' >/dev/null
then
xdt_check_parameter "$xdt_i" device_name || return "$?"
xdt_devs_named=y
xdt_devs="$xdt_devs $xdt_i"
xdt_show_reasons=y
else
echo "$0 : Unknown option: '$xdt_i'" >&2
echo >&2
echo "For a help text run: $0 -help" >&2
return 16
fi
done
xdt_predict_su_power || return "$?"
# Prepare for using dd pacifier if desired and available
# Prepare for using silent dd with GPT backup erasure if available
xdt_dd_bs="bs=1M"
xdt_dd_silent=
if test -n "$xdt_dd_cmd"
then
if test -z "$xdt_no_pacifier" && \
"$xdt_dd_cmd" if=/dev/zero of=/dev/null count=1 \
bs=1M status=progress oflag=dsync 2>/dev/null
then
xdt_dd_bs="bs=1M status=progress oflag=dsync"
fi
if "$xdt_dd_cmd" if=/dev/zero of=/dev/null count=1 bs=512 status=none \
2>/dev/null
then
xdt_dd_silent="status=none"
fi
fi
return 0
}
## Obtain a blank separated list of top-level names which do not look like
## CD, floppy, RAM dev, or loop device.
xdt_collect_devices() {
"$xdt_lsblk_cmd" -d -n -o NAME \
| grep -v '^sr[0-9]' \
| grep -v '^fd[0-9]' \
| grep -v '^zram[0-9]' \
| grep -v '^loop[0-9]' \
| tr '\n\r' ' '
return 0
}
## Let lsblk print extra info for the given devices
xdt_list_long() {
if test -z "$xdt_do_list_long"
then
return 0
fi
$xdt_sudo_cmd "$xdt_lsblk_cmd" -o NAME,SIZE,FSTYPE,TRAN,LABEL \
/dev/"$1"
echo
return 0
}
## Trying to find the desired device by watching plug-in effects
xdt_plug_in_watcher() {
# How long to wait for a first device to appear, how long to wait for more
xdt_wait_span_0=10
xdt_wait_span_1=5
xdt_found_devices=
echo >&2
echo "Caused by option -plug_test: Attempt to find the desired device" >&2
echo "by watching it appear after being plugged in." >&2
echo >&2
echo "Step 1:" >&2
echo "Please make sure that the desired target device is plugged _out_ now." >&2
echo "If it is currently plugged in, make sure to unmount all its fileystems" >&2
echo "and then unplug it." >&2
echo "Press the Enter key when ready." >&2
read xdt_dummy
xdt_old_device_list=' '$(xdt_collect_devices)' '
# <<< Mock-up to save USB socket wear-off by erasing items from old_device_list
# <<< Their presence in new_device_list will let them appear as fresh plugs
# xdt_old_device_list=' '$(echo -n $xdt_old_device_list | sed -e 's/sdc//')' '
echo "Found and noted as _not_ desired: $xdt_old_device_list" >&2
echo >&2
echo "Step 2:" >&2
echo "Please plug in the desired target device and then press the Enter key." >&2
read xdt_dummy
echo -n "Waiting up to $xdt_wait_span_0 seconds for a new device to be listed ..." >&2
xdt_end_time="$(expr $(date +'%s') + "$xdt_wait_span_0")"
while test $(date +'%s') -le "$xdt_end_time"
do
xdt_new_device_list=' '$(xdt_collect_devices)' '
for xdt_i in $xdt_new_device_list
do
if echo "$xdt_old_device_list $xdt_found_devices " \
| grep -F -v ' '"$xdt_i"' ' >/dev/null
then
echo " found: $xdt_i" >&2
xdt_found_devices="$xdt_found_devices $xdt_i"
xdt_end_time=$(expr $(date +'%s') + "$xdt_wait_span_1")
echo -n "Now waiting $xdt_wait_span_1 seconds to let it settle ..." >&2
fi
done
sleep 1
echo -n '.' >&2
done
echo >&2
if test -z "$xdt_found_devices"
then
echo "SORRY: No new candidate device was found." >&2
return 8
fi
xdt_num=$(echo $xdt_found_devices | wc -w)
if test "$xdt_num" -gt 1
then
echo "SORRY: More than one new candidate device appeared: $xdt_found_devices" >&2
return 9
fi
echo "Found and noted as desired device: $xdt_found_devices" >&2
if test -n "$xdt_devs"
then
echo "(-plug_test is overriding device list given by arguments: $xdt_devs )" >&2
fi
if test -n "$xdt_list_all"
then
echo "(-plug_test is overriding -list_all)" >&2
xdt_list_all=
fi
xdt_devs_named=y
xdt_with_vendor_model=y
xdt_show_reasons=y
xdt_devs=$(echo -n $xdt_found_devices)
echo >&2
return 0
}
## Evaluation of available devices and suitability
xdt_list_devices() {
if test -n "$xdt_list_all"
then
xdt_devs=
fi
if test -z "$xdt_devs"
then
# Obtain list of top-level names which do not look like CD, floppy, RAM dev
xdt_devs=$(xdt_collect_devices)
fi
xdt_not_advised=0
for xdt_name in $xdt_devs
do
# Collect reasons
xdt_yucky=
xdt_reasons=
xdt_good_trans=
xdt_good_fs=
xdt_bad_trans=
xdt_bad_fs=
# Unwanted device name patterns
if (echo "$xdt_name" | grep '^sd[a-z][1-9]' >/dev/null) \
|| (echo "$xdt_name" | grep '^mmcblk.*p[0-9]' >/dev/null) \
|| (echo "$xdt_name" | grep '^nvme.*p[0-9]' >/dev/null)
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}looks_like_disk_partition- "
elif echo "$xdt_name" | grep '^sr[0-9]' >/dev/null
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}looks_like_cd_drive- "
elif echo "$xdt_name" | grep '^fd[0-9]' >/dev/null
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}looks_like_floppy- "
elif echo "$xdt_name" | grep '^loop[0-9]' >/dev/null
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}looks_like_loopdev- "
elif echo "$xdt_name" | grep '^zram[0-9]' >/dev/null
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}looks_like_ramdev- "
fi
# >>> recognize the device from which Debian Live booted
# Connection type. Normally by lsblk TRAN, but in case of mmcblk artificial.
if echo "$xdt_name" | grep '^mmcblk[0-9]' >/dev/null
then
xdt_transports="mmcblk"
elif echo "$xdt_name" | grep -F "/" >/dev/null
then
xdt_transports=not_an_expected_name
xdt_reasons="${xdt_reasons}name_with_slash- "
else
xdt_transports=$("$xdt_lsblk_cmd" -n -o TRAN /dev/"$xdt_name")
fi
for xdt_trans in $xdt_transports
do
if test "$xdt_trans" = "usb" || test "$xdt_trans" = "mmcblk"
then
xdt_good_trans="${xdt_trans}+"
elif test -n "$xdt_trans"
then
xdt_bad_trans="$xdt_trans"
xdt_yucky=y
if test "$xdt_transports" = "not_an_expected_name"
then
xdt_dummy=dummy
else
if echo "$xdt_reasons" | grep -F -v "not_usb" >/dev/null
then
xdt_reasons="${xdt_reasons}not_usb- "
fi
fi
fi
done
if test -z "$xdt_good_trans" && test -z "$xdt_bad_trans"
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}no_bus_info- "
elif test -z "$xdt_bad_trans"
then
xdt_reasons="${xdt_reasons}$xdt_good_trans "
fi
# Wanted or unwanted filesystem types
xdt_fstypes=$($xdt_sudo_cmd "$xdt_lsblk_cmd" -n -o FSTYPE \
/dev/"$xdt_name")
if test "$?" -gt 0
then
xdt_fstypes="lsblk_fstype_error"
fi
# Get overview of filesystems
xdt_has_iso=
xdt_has_vfat=
xdt_has_other=
for xdt_fstype in $xdt_fstypes
do
if test "$xdt_fstype" = "iso9660"
then
xdt_has_iso=y
if echo "$xdt_good_fs" | grep -F -v "has_$xdt_fstype" >/dev/null
then
xdt_good_fs="${xdt_good_fs}has_${xdt_fstype}+ "
fi
elif test "$xdt_fstype" = "vfat"
then
xdt_has_vfat=y
if echo "$xdt_good_fs" | grep -F -v "has_$xdt_fstype" >/dev/null
then
xdt_good_fs="${xdt_good_fs}has_${xdt_fstype}+ "
fi
elif test -n "$xdt_fstype"
then
xdt_has_other=y
if echo "$xdt_bad_fs" | grep -F -v "has_$xdt_fstype" >/dev/null
then
xdt_bad_fs="${xdt_bad_fs}has_${xdt_fstype}- "
fi
fi
done
# Decide whether the found filesystems look dispensible enough
xdt_reasons="${xdt_reasons}${xdt_good_fs}${xdt_bad_fs}"
if test "${xdt_bad_fs}${xdt_good_fs}" = "" \
&& test -z "$xdt_have_su_power" && test -z "$xdt_lsblk_fs_wo_su"
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}no_fs_while_not_su- "
elif test -n "$xdt_look_for_iso"
then
if test -n "$xdt_has_iso"
then
xdt_reasons="${xdt_reasons}look_for_iso++ "
else
xdt_yucky=y
xdt_reasons="${xdt_reasons}no_iso9660- "
fi
elif test -n "$xdt_has_other"
then
xdt_yucky=y
fi
# Optional tests for size
if test -n "$xdt_max_size" || test -n "$xdt_min_size"
then
xdt_dev_bytes=$("$xdt_lsblk_cmd" -n -b -o SIZE /dev/"$xdt_name" | head -1)
xdt_size=$(echo "$xdt_dev_bytes" | xdt_round_down_div_million)
if test -z "$xdt_size"
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}lsblk_no_size- "
fi
fi
if test -n "$xdt_max_size" && test -n "$xdt_size"
then
if test "$xdt_size" -gt "$xdt_max_size"
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}size_too_large- "
fi
fi
if test -n "$xdt_min_size" && test -n "$xdt_size"
then
if test "$xdt_size" -lt "$xdt_min_size"
then
xdt_yucky=y
xdt_reasons="${xdt_reasons}size_too_small- "
fi
fi
# Now decide overall and report
xdt_descr=
if test -n "$xdt_with_vendor_model"
then
xdt_descr=": "$("$xdt_lsblk_cmd" -n -o VENDOR,MODEL \
/dev/"$xdt_name" | tr '\n\r' ' ' | tr -s ' ')
fi
if test -n "$xdt_yucky"
then
if test -n "$xdt_show_reasons"
then
echo "$xdt_name : NO : $xdt_reasons$xdt_descr"
xdt_list_long "$xdt_name"
fi
xdt_not_advised=1
else
if test -n "$xdt_show_reasons"
then
echo "$xdt_name : YES : $xdt_reasons$xdt_descr"
xdt_list_long "$xdt_name"
else
echo "$xdt_name"
fi
fi
done
return 0;
}
## Puts list of mounted (sub-)devices of $1 into $mounted_devs
xdt_list_mounted_of() {
xdt_partitions=$("$xdt_lsblk_cmd" -l -n -p -o NAME /dev/"$1" \
| grep -v '^'/dev/"$1"'$' \
| tr '\n\r' ' ')
xdt_mounted_devs=
for xdt_i in /dev/"$1" $xdt_partitions
do
# Show the found mount lines and add their device paths to list
xdt_mount_line=$(mount | grep '^'"$xdt_i"' ')
if test -n "$xdt_mount_line"
then
echo " $xdt_mount_line"
xdt_mounted_devs="$xdt_mounted_devs $xdt_i"
fi
done
return 0
}
## Unmount xdt_mounted_devs (maybe filled by xdt_list_mounted_of)
## $1 : base device name
xdt_unmount() {
if test -z "$xdt_mounted_devs"
then
return 0
fi
for xdt_i in $xdt_mounted_devs
do
if test -n "$xdt_dummy_run"
then
echo " $xdt_sudo_cmd $xdt_umount_cmd $xdt_i"
elif $xdt_sudo_cmd "$xdt_umount_cmd" "$xdt_i"
then
echo "Unmounted: $xdt_i"
else
echo "FAILURE: Non-zero exit value with: $xdt_sudo_cmd $xdt_umount_cmd $xdt_i" >&2
return 7
fi
done
# Check again if any mount points still exist
if test -z "$xdt_dummy_run"
then
xdt_list_mounted_of "$1"
if test -n "$xdt_mounted_devs"
then
echo "FAILURE: $xdt_sudo_cmd $xdt_umount_cmd could not remove all mounts: $xdt_mounted_devs" >&2
return 7
fi
fi
return 0
}
## Does the work of unmounting and dd-ing
## $1 : image file path
## $2 : base device name
xdt_write_image() {
if test -z "$xdt_umount_cmd"
then
echo "No executable program umount found in: $xdt_sudo_x_dir_list" >&2
return 6
fi
echo "Looking for mount points of $2:"
xdt_mounted_devs=
xdt_list_mounted_of "$2"
if test -n "$xdt_dummy_force"
then
echo "AGAINST THE ADVICE BY THIS PROGRAM, a daring user could do:"
xdt_dummy_run=y
elif test -n "$xdt_dummy_run"
then
echo "Would do if not -dummy:"
fi
xdt_unmount "$2" || return "$?"
if test -z "$xdt_dd_cmd"
then
echo "No executable program dd found in: $xdt_sudo_x_dir_list" >&2
return 6
fi
# Erase possible GPT backup table at end of device
# if expr can properly divide device size by 512
xdt_dev_bytes=$("$xdt_lsblk_cmd" -n -b -o SIZE /dev/"${2}" | head -1)
if test -n "$xdt_dev_bytes"
then
xdt_dev_end=$(expr "$xdt_dev_bytes" / 512 2>/dev/null)
if test "$(expr "$xdt_dev_end" '*' 512 2>/dev/null)" = "$xdt_dev_bytes"
then
xdt_dev_end=$(expr "$xdt_dev_end" - 1)
else
xdt_dev_end=
fi
fi
xdt_gpt_erase_line="$xdt_sudo_cmd $xdt_dd_cmd if=/dev/zero of=/dev/'${2}' bs=512 seek='$xdt_dev_end' count=1 $xdt_dd_silent"
xdt_copy_line="$xdt_sudo_cmd $xdt_dd_cmd if='${1}' of=/dev/'${2}' $xdt_dd_bs ; sync"
if test -n "$xdt_dummy_run"
then
if test -n "$xdt_dev_end"
then
echo " $xdt_gpt_erase_line"
fi
echo " $xdt_copy_line"
else
echo "Performing:"
if test -n "$xdt_dev_end"
then
echo " $xdt_gpt_erase_line"
$xdt_sudo_cmd "$xdt_dd_cmd" if=/dev/zero of=/dev/"${2}" bs=512 seek="$xdt_dev_end" count=1 $xdt_dd_silent
fi
echo " $xdt_copy_line"
$xdt_sudo_cmd "$xdt_dd_cmd" if="${1}" of=/dev/"${2}" $xdt_dd_bs
xdt_ret="$?"
sync
if test "$xdt_ret" -ne 0
then
echo "FAILURE: $xdt_sudo_cmd $xdt_dd_cmd was not successful" >&2
return 18
fi
fi
if test -n "$xdt_dummy_force"
then
echo "BE SMART. BE CAUTIOUS. BEWARE."
fi
return 0
}
## Inspects the result of listing, decides whether writing is advisable,
## and starts the write run
xdt_decide_writing()
{
xdt_with_vendor_model=
xdt_show_reasons=
xdt_candidates=$(xdt_list_devices | tr '\n\r' ' ')
xdt_num_cand=$(echo $xdt_candidates | wc -w)
xdt_num_devs=$(echo $xdt_devs| wc -w)
if test -n "$xdt_dummy_force" && test "$xdt_num_devs" -ne 1
then
echo "SORRY: Refusing -dummy_force with not exactly one device given." >&2
return 10
fi
if test -n "$xdt_dummy_force" && test -n "$xdt_dummy_run" \
&& test "$xdt_num_cand" -ne 1
then
# -dummy_force in a situation where the program would normally refuse
echo
echo "Overriding any advice because of -dummy_force"
xdt_candidates="$xdt_devs"
xdt_num_cand=1
elif test -n "$xdt_dummy_force"
then
# Downgrade -dummy_force to -dummy in order to avoid the ugly warning
xdt_dummy_force=
xdt_dummy_run=y
fi
if test "$xdt_num_cand" -eq 1
then
if test -n "$xdt_image_file"
then
if test -n "$xdt_do_plug_test"
then
echo >&2
echo "Step 3:" >&2
if test -n "$xdt_dummy_run"
then
echo "This would be the last chance to abort. Enter the word 'yes' to see -dummy report." >&2
else
echo "Last chance to abort. Enter the word 'yes' to start REAL WRITING." >&2
fi
read xdt_dummy
if test "X$xdt_dummy" = "Xyes" || test "X$xdt_dummy" = "X'yes'" \
|| test "X$xdt_dummy" = 'X"yes"'
then
xdt_dummy=dummy
else
echo "WRITE RUN PREVENTED by user input '$xdt_dummy'." >&2
return 17
fi
fi
xdt_write_image "$xdt_image_file" $xdt_candidates || return "$?"
else
xdt_candidates=$(echo $xdt_candidates)
if test -n "$xdt_dummy_run"
then
echo "Would simulate writing to /dev/$xdt_candidates if an -image_file were given."
else
echo "Would write to /dev/$xdt_candidates if an -image_file were given."
fi
return 0
fi
elif test "$xdt_num_cand" -gt 1
then
echo "WILL NOT WRITE ! More than one candidate found for target device:" >&2
xdt_show_reasons=y
xdt_with_vendor_model=y
xdt_devs="$xdt_candidates"
xdt_list_devices >&2
echo "HINT: Unplug the unwanted devices from the machine," >&2
echo " or work with option -plug_test," >&2
echo " or add the desired name out of {$(echo $xdt_candidates | sed -e 's/ /,/g')} as additional argument." >&2
return 3
else
if test -n "$xdt_devs_named"
then
echo "NO CANDIDATE FOR TARGET DEVICE AMONG THE GIVEN NAMES !" >&2
else
echo "NO CANDIDATE FOR TARGET DEVICE FOUND !" >&2
fi
echo "Overall available devices:" >&2
xdt_list_all=y
xdt_list_long=
xdt_show_reasons=y
xdt_with_vendor_model=y
xdt_list_devices >&2
return 4
fi
return 0
}
## The main function which runs the others in standalone mode
xdt_main()
{
xdt_set_lang_c
xdt_init || return "$?"
xdt_reset_job
xdt_arg_interpreter "$@" || return "$?"
if test -n "$xdt_end_after_setup"
then
return 0
fi
if test -n "$xdt_do_plug_test"
then
xdt_plug_in_watcher || return "$?"
fi
xdt_list_devices || return "$?"
if test -z "$xdt_list_all" && test -n "$xdt_do_write"
then
xdt_decide_writing || return "$?"
fi
if test -n "$xdt_devs_named"
then
return $xdt_not_advised
fi
return 0
}
if test -z "$no_xorriso_dd_target_run"
then
xdt_main "$@" || exit "$?"
fi