2019-11-29 11:13:42 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2021-01-30 14:27:55 +00:00
|
|
|
# Copyright (c) 2019 - 2021
|
2019-11-29 11:13:42 +00:00
|
|
|
# Nio Wiklund alias sudodus <nio dot wiklund at gmail dot com>
|
|
|
|
# Thomas Schmitt <scdbackup@gmx.net>
|
|
|
|
# Provided under GPL version 2 or later.
|
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
# 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.
|
|
|
|
|
2020-01-06 21:02:19 +00:00
|
|
|
xdt_print_version() {
|
2021-01-30 20:16:25 +00:00
|
|
|
echo "xorriso-dd-target , version 1.5.5 , 2021.01.30.200107"
|
2020-01-06 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
|
|
|
|
## 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
|
|
|
|
}
|
2019-11-29 11:13:42 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
## 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
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "This program is entirely specialized on Linux kernel device names." >&2
|
|
|
|
echo "Found to be on: '$(uname -s)'" >&2
|
|
|
|
return 2
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
|
|
|
|
# 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"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_sudo_x_dir_list="/usr/bin /bin /usr/sbin /sbin"
|
|
|
|
else
|
|
|
|
xdt_sudo_x_dir_list="/usr/sbin /sbin /usr/bin /bin"
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
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"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "No executable program lsblk found in: $xdt_sudo_x_dir_list" >&2
|
|
|
|
return 5
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
2019-11-29 11:13:42 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_print_usage() {
|
2019-11-29 11:13:42 +00:00
|
|
|
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"
|
2019-12-09 20:40:58 +00:00
|
|
|
echo "not begin by '/dev/'. E.g. 'sdc' is valid, '/dev/sdc' is not valid."
|
2019-11-30 09:18:25 +00:00
|
|
|
echo "If device names are given, then they get listed with advice shown."
|
2019-11-29 11:13:42 +00:00
|
|
|
echo "If one of the given device names gets not advised, the exit value is 1."
|
|
|
|
echo
|
2019-11-30 14:18:47 +00:00
|
|
|
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
|
2019-11-29 11:13:42 +00:00
|
|
|
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."
|
2019-11-30 12:06:16 +00:00
|
|
|
echo "Option -dummy prevents this kind of real action and rather shows the"
|
|
|
|
echo "unmount and write commands on stdout."
|
2019-11-29 11:13:42 +00:00
|
|
|
echo
|
|
|
|
echo "Options:"
|
2019-11-30 14:18:47 +00:00
|
|
|
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."
|
2019-11-29 11:13:42 +00:00
|
|
|
echo " -list_all Print list of all found devices with advice, vendor"
|
2019-12-02 09:56:15 +00:00
|
|
|
echo " and model. One per line. Ignore any device names."
|
2019-11-29 11:13:42 +00:00
|
|
|
echo " Ignore -DO_WRITE."
|
2019-12-07 20:28:13 +00:00
|
|
|
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."
|
2019-11-29 11:13:42 +00:00
|
|
|
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."
|
2019-12-15 18:47:38 +00:00
|
|
|
echo " -with_sudo Run '$xdt_lsblk_cmd -o FSTYPE' by sudo."
|
2019-11-29 11:13:42 +00:00
|
|
|
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."
|
2020-02-12 15:06:49 +00:00
|
|
|
echo " -trust_lsblk_udev Suppress the reason no_fs_while_not_su- if lsblk"
|
2020-02-12 20:03:10 +00:00
|
|
|
echo " is linked with libudev.so. In this case it is"
|
2020-02-12 15:06:49 +00:00
|
|
|
echo " likely that lsblk can retrieve FSTYPE even if"
|
|
|
|
echo " run by a non-priviledged user."
|
2019-11-29 11:13:42 +00:00
|
|
|
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."
|
2020-01-06 20:44:11 +00:00
|
|
|
echo " -no_pacifier Do not use dd options to print progress messages"
|
|
|
|
echo " and to perform synchronized output."
|
2019-11-30 12:06:16 +00:00
|
|
|
echo " -dummy Report the -DO_WRITE actions but do not perform"
|
|
|
|
echo " them."
|
2019-11-30 17:35:31 +00:00
|
|
|
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."
|
2020-01-06 21:02:19 +00:00
|
|
|
echo " -version Print version text and then end the program."
|
2019-11-29 11:13:42 +00:00
|
|
|
echo " -help Print this text to stdout and then end the program."
|
|
|
|
echo "Examples:"
|
|
|
|
echo " $0 -with_sudo -list_all"
|
|
|
|
echo " $0 sdc"
|
2019-12-09 20:40:58 +00:00
|
|
|
echo " $0 -with_sudo -image_file debian-live-10.0.0-amd64-xfce.iso -DO_WRITE -dummy"
|
2019-11-30 14:18:47 +00:00
|
|
|
echo " $0 -with_sudo -image_file debian-live-10.0.0-amd64-xfce.iso -DO_WRITE -plug_test"
|
2019-11-29 11:13:42 +00:00
|
|
|
echo
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-11-29 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Roughly convert human readable sizes and plain numbers to 1 / million
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_round_down_div_million() {
|
2019-12-05 19:11:19 +00:00
|
|
|
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/'
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-11-29 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 14:35:41 +00:00
|
|
|
## Check for harmless name or number in program argument
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_check_parameter() {
|
2019-12-21 10:07:39 +00:00
|
|
|
if test "X$2" = "Xdevice_name"
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
2019-12-21 10:07:39 +00:00
|
|
|
if echo "X$1" | grep '[^A-Za-z0-9_/-]' >/dev/null
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
|
|
|
echo "SORRY: Given device name contains unexpected character. Ok: [A-za-z0-9_/-]" >&2
|
|
|
|
return 12
|
2019-12-21 10:07:39 +00:00
|
|
|
elif echo "X$1" | grep '^X-' >/dev/null
|
|
|
|
then
|
|
|
|
echo "SORRY: Given device name begins by '-' character." >&2
|
|
|
|
return 15
|
2019-12-15 18:47:38 +00:00
|
|
|
fi
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$2" = "Ximage_file"
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
2019-12-21 10:07:39 +00:00
|
|
|
if echo "X$1" | grep '[$`[*?<>|&!{\]' >/dev/null
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
|
|
|
echo "SORRY: Given image file name contains unexpected character. Not ok: "'[$`[*?<>|&!{\]' >&2
|
|
|
|
return 15
|
2019-12-21 10:07:39 +00:00
|
|
|
elif echo "X$1" | grep '^X[-(]' >/dev/null
|
|
|
|
then
|
|
|
|
echo "SORRY: Given image file name begins by problematic character. Not ok: "'[-(]' >&2
|
|
|
|
return 15
|
2019-12-15 18:47:38 +00:00
|
|
|
fi
|
|
|
|
else
|
2019-12-21 10:07:39 +00:00
|
|
|
if echo "X$1" | grep -v '^X[0-9][0-9]*[0-9MGTmgt]$' >/dev/null
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
|
|
|
echo "SORRY: Number for $2 too short or bad character. Ok: [0-9][0-9MGTmgt]" >&2
|
|
|
|
return 14
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
return 0
|
2019-12-07 14:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 11:13:42 +00:00
|
|
|
### Assessing arguments and setting up the job
|
|
|
|
|
|
|
|
# Settings
|
2019-12-15 18:47:38 +00:00
|
|
|
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=
|
2020-01-06 20:44:11 +00:00
|
|
|
xdt_no_pacifier=
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_do_plug_test=
|
2020-02-12 15:06:49 +00:00
|
|
|
xdt_trust_lsblk_udev=
|
2019-11-29 11:13:42 +00:00
|
|
|
|
|
|
|
# Status
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_sudo_cmd=
|
|
|
|
xdt_have_su_power=
|
2020-01-06 21:02:19 +00:00
|
|
|
xdt_end_after_setup=
|
2020-01-07 15:47:41 +00:00
|
|
|
xdt_dev_end=
|
|
|
|
xdt_dd_bs="bs=1M"
|
|
|
|
xdt_dd_silent=
|
2019-12-15 18:47:38 +00:00
|
|
|
|
|
|
|
return 0
|
2019-11-29 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
## Predict superuser power. Possibly enable sudo with lsblk -o FSTYPE and dd.
|
2020-02-12 15:06:49 +00:00
|
|
|
## Also predict whether lsblk -o FSTYPE will bring results as non-root.
|
2019-12-15 18:47:38 +00:00
|
|
|
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
|
2020-02-12 15:06:49 +00:00
|
|
|
|
|
|
|
# lsblk linked with libudev.so usually can obtain filesystem info
|
|
|
|
# without superuser powers.
|
|
|
|
if test -n "$xdt_trust_lsblk_udev"
|
|
|
|
then
|
2020-02-12 20:03:10 +00:00
|
|
|
if ldd "$xdt_lsblk_cmd" | grep '\blibudev\.so' >/dev/null
|
2020-02-12 15:06:49 +00:00
|
|
|
then
|
|
|
|
xdt_lsblk_fs_wo_su=y
|
|
|
|
fi
|
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## Sets xdt_variables according to its arguments
|
|
|
|
xdt_arg_interpreter() {
|
|
|
|
xdt_next_is=
|
|
|
|
for xdt_i in "$@"
|
2019-11-29 11:13:42 +00:00
|
|
|
do
|
|
|
|
# The next_is option parameter readers get programmed by the -options
|
2019-12-15 18:47:38 +00:00
|
|
|
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"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "FAILURE: Cannot obtain size of -image_file '$xdt_i'" >&2
|
|
|
|
return 13
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_min_size="$(expr $xdt_min_size + 1)"
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_next_is=
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-list_all"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_list_all=y
|
|
|
|
xdt_with_vendor_model=y
|
|
|
|
xdt_show_reasons=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-list_long"
|
2019-12-07 08:58:55 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_do_list_long=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-plug_test"
|
2019-11-30 14:18:47 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_do_plug_test=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-max_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_next_is="max_size"
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-min_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_next_is="min_size"
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-with_vendor_model"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_with_vendor_model=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-look_for_iso"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_look_for_iso=y
|
2020-02-12 15:06:49 +00:00
|
|
|
elif test "X$xdt_i" = "X-trust_lsblk_udev"
|
|
|
|
then
|
|
|
|
xdt_trust_lsblk_udev=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-with_sudo"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_with_sudo=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-image_file"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_next_is="image_file"
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-dummy"
|
2019-11-30 12:06:16 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_dummy_run=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-dummy_force"
|
2019-11-30 17:35:31 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_dummy_run=y
|
|
|
|
xdt_do_write=y
|
|
|
|
xdt_dummy_force=y
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-DO_WRITE"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_do_write=y
|
2020-01-06 20:44:11 +00:00
|
|
|
elif test "X$xdt_i" = "X-no_pacifier"
|
|
|
|
then
|
|
|
|
xdt_no_pacifier=y
|
2020-01-06 21:02:19 +00:00
|
|
|
elif test "X$xdt_i" = "X-version"
|
|
|
|
then
|
|
|
|
xdt_print_version
|
|
|
|
xdt_end_after_setup=y
|
|
|
|
return 0
|
2019-12-21 10:07:39 +00:00
|
|
|
elif test "X$xdt_i" = "X-help"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_print_usage
|
2020-01-06 21:02:19 +00:00
|
|
|
xdt_end_after_setup=y
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-12-21 10:07:39 +00:00
|
|
|
elif echo "X$xdt_i" | grep -v '^X-' >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_check_parameter "$xdt_i" device_name || return "$?"
|
|
|
|
xdt_devs_named=y
|
|
|
|
xdt_devs="$xdt_devs $xdt_i"
|
|
|
|
xdt_show_reasons=y
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "$0 : Unknown option: '$xdt_i'" >&2
|
2019-11-29 11:13:42 +00:00
|
|
|
echo >&2
|
2019-12-07 14:35:41 +00:00
|
|
|
echo "For a help text run: $0 -help" >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
return 16
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
done
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_predict_su_power || return "$?"
|
2020-01-06 20:44:11 +00:00
|
|
|
# Prepare for using dd pacifier if desired and available
|
2020-01-07 15:47:41 +00:00
|
|
|
# Prepare for using silent dd with GPT backup erasure if available
|
2020-01-06 20:44:11 +00:00
|
|
|
xdt_dd_bs="bs=1M"
|
2020-01-07 15:47:41 +00:00
|
|
|
xdt_dd_silent=
|
2020-01-06 20:44:11 +00:00
|
|
|
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
|
2020-01-07 15:47:41 +00:00
|
|
|
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
|
2020-01-06 20:44:11 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-11-29 11:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 08:25:50 +00:00
|
|
|
## Obtain a blank separated list of top-level names which do not look like
|
|
|
|
## CD, floppy, RAM dev, or loop device.
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_collect_devices() {
|
|
|
|
"$xdt_lsblk_cmd" -d -n -o NAME \
|
2019-12-06 08:25:50 +00:00
|
|
|
| grep -v '^sr[0-9]' \
|
|
|
|
| grep -v '^fd[0-9]' \
|
|
|
|
| grep -v '^zram[0-9]' \
|
|
|
|
| grep -v '^loop[0-9]' \
|
|
|
|
| tr '\n\r' ' '
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-12-06 08:25:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 08:58:55 +00:00
|
|
|
## Let lsblk print extra info for the given devices
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_list_long() {
|
|
|
|
if test -z "$xdt_do_list_long"
|
2019-12-07 08:58:55 +00:00
|
|
|
then
|
|
|
|
return 0
|
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
$xdt_sudo_cmd "$xdt_lsblk_cmd" -o NAME,SIZE,FSTYPE,TRAN,LABEL \
|
|
|
|
/dev/"$1"
|
2019-12-07 08:58:55 +00:00
|
|
|
echo
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-12-07 08:58:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 14:18:47 +00:00
|
|
|
## Trying to find the desired device by watching plug-in effects
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_plug_in_watcher() {
|
2019-12-10 15:45:32 +00:00
|
|
|
# How long to wait for a first device to appear, how long to wait for more
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_wait_span_0=10
|
|
|
|
xdt_wait_span_1=5
|
2019-12-10 15:45:32 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_found_devices=
|
2019-12-02 09:56:15 +00:00
|
|
|
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
|
2019-12-02 18:56:08 +00:00
|
|
|
echo "If it is currently plugged in, make sure to unmount all its fileystems" >&2
|
|
|
|
echo "and then unplug it." >&2
|
2019-12-02 09:56:15 +00:00
|
|
|
echo "Press the Enter key when ready." >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
read xdt_dummy
|
|
|
|
xdt_old_device_list=' '$(xdt_collect_devices)' '
|
2019-11-30 14:18:47 +00:00
|
|
|
|
|
|
|
# <<< 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
|
2019-12-15 18:47:38 +00:00
|
|
|
# xdt_old_device_list=' '$(echo -n $xdt_old_device_list | sed -e 's/sdc//')' '
|
2019-11-30 14:18:47 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "Found and noted as _not_ desired: $xdt_old_device_list" >&2
|
2019-12-02 09:56:15 +00:00
|
|
|
echo >&2
|
|
|
|
echo "Step 2:" >&2
|
|
|
|
echo "Please plug in the desired target device and then press the Enter key." >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
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"
|
2019-11-30 14:18:47 +00:00
|
|
|
do
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_new_device_list=' '$(xdt_collect_devices)' '
|
|
|
|
for xdt_i in $xdt_new_device_list
|
2019-12-10 15:45:32 +00:00
|
|
|
do
|
2019-12-15 18:47:38 +00:00
|
|
|
if echo "$xdt_old_device_list $xdt_found_devices " \
|
|
|
|
| grep -F -v ' '"$xdt_i"' ' >/dev/null
|
2019-12-10 15:45:32 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
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
|
2019-12-10 15:45:32 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
sleep 1
|
|
|
|
echo -n '.' >&2
|
2019-11-30 14:18:47 +00:00
|
|
|
done
|
2019-12-02 09:56:15 +00:00
|
|
|
echo >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -z "$xdt_found_devices"
|
2019-11-30 14:18:47 +00:00
|
|
|
then
|
|
|
|
echo "SORRY: No new candidate device was found." >&2
|
|
|
|
return 8
|
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_num=$(echo $xdt_found_devices | wc -w)
|
|
|
|
if test "$xdt_num" -gt 1
|
2019-11-30 14:18:47 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "SORRY: More than one new candidate device appeared: $xdt_found_devices" >&2
|
2019-11-30 14:18:47 +00:00
|
|
|
return 9
|
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "Found and noted as desired device: $xdt_found_devices" >&2
|
|
|
|
if test -n "$xdt_devs"
|
2019-11-30 14:18:47 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "(-plug_test is overriding device list given by arguments: $xdt_devs )" >&2
|
2019-11-30 14:18:47 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_list_all"
|
2019-11-30 14:18:47 +00:00
|
|
|
then
|
2019-12-02 09:56:15 +00:00
|
|
|
echo "(-plug_test is overriding -list_all)" >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_list_all=
|
2019-11-30 14:18:47 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_devs_named=y
|
|
|
|
xdt_with_vendor_model=y
|
|
|
|
xdt_show_reasons=y
|
|
|
|
xdt_devs=$(echo -n $xdt_found_devices)
|
2019-12-02 09:56:15 +00:00
|
|
|
echo >&2
|
2019-12-08 20:52:41 +00:00
|
|
|
|
2019-11-30 14:18:47 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-11-29 11:13:42 +00:00
|
|
|
## Evaluation of available devices and suitability
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_list_devices() {
|
|
|
|
if test -n "$xdt_list_all"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_devs=
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -z "$xdt_devs"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-05 17:15:55 +00:00
|
|
|
# Obtain list of top-level names which do not look like CD, floppy, RAM dev
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_devs=$(xdt_collect_devices)
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
|
|
|
|
xdt_not_advised=0
|
|
|
|
for xdt_name in $xdt_devs
|
2019-11-29 11:13:42 +00:00
|
|
|
do
|
|
|
|
# Collect reasons
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=
|
|
|
|
xdt_reasons=
|
|
|
|
xdt_good_trans=
|
|
|
|
xdt_good_fs=
|
|
|
|
xdt_bad_trans=
|
|
|
|
xdt_bad_fs=
|
2019-11-29 11:13:42 +00:00
|
|
|
|
|
|
|
# Unwanted device name patterns
|
2019-12-15 18:47:38 +00:00
|
|
|
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)
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}looks_like_disk_partition- "
|
|
|
|
elif echo "$xdt_name" | grep '^sr[0-9]' >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}looks_like_cd_drive- "
|
|
|
|
elif echo "$xdt_name" | grep '^fd[0-9]' >/dev/null
|
2019-12-05 17:15:55 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}looks_like_floppy- "
|
|
|
|
elif echo "$xdt_name" | grep '^loop[0-9]' >/dev/null
|
2019-12-06 12:02:43 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}looks_like_loopdev- "
|
|
|
|
elif echo "$xdt_name" | grep '^zram[0-9]' >/dev/null
|
2019-12-05 17:15:55 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}looks_like_ramdev- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# >>> recognize the device from which Debian Live booted
|
|
|
|
|
|
|
|
# Connection type. Normally by lsblk TRAN, but in case of mmcblk artificial.
|
2019-12-15 18:47:38 +00:00
|
|
|
if echo "$xdt_name" | grep '^mmcblk[0-9]' >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_transports="mmcblk"
|
|
|
|
elif echo "$xdt_name" | grep -F "/" >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_transports=not_an_expected_name
|
|
|
|
xdt_reasons="${xdt_reasons}name_with_slash- "
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_transports=$("$xdt_lsblk_cmd" -n -o TRAN /dev/"$xdt_name")
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
for xdt_trans in $xdt_transports
|
2019-11-29 11:13:42 +00:00
|
|
|
do
|
2019-12-22 16:02:02 +00:00
|
|
|
if test "$xdt_trans" = "usb" || test "$xdt_trans" = "mmcblk"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_good_trans="${xdt_trans}+"
|
|
|
|
elif test -n "$xdt_trans"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_bad_trans="$xdt_trans"
|
|
|
|
xdt_yucky=y
|
|
|
|
if test "$xdt_transports" = "not_an_expected_name"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_dummy=dummy
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
if echo "$xdt_reasons" | grep -F -v "not_usb" >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_reasons="${xdt_reasons}not_usb- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -z "$xdt_good_trans" && test -z "$xdt_bad_trans"
|
2019-11-30 09:18:25 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}no_bus_info- "
|
|
|
|
elif test -z "$xdt_bad_trans"
|
2019-11-30 09:18:25 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_reasons="${xdt_reasons}$xdt_good_trans "
|
2019-11-30 09:18:25 +00:00
|
|
|
fi
|
2019-11-29 11:13:42 +00:00
|
|
|
|
|
|
|
# Wanted or unwanted filesystem types
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_fstypes=$($xdt_sudo_cmd "$xdt_lsblk_cmd" -n -o FSTYPE \
|
|
|
|
/dev/"$xdt_name")
|
2019-11-29 11:13:42 +00:00
|
|
|
if test "$?" -gt 0
|
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_fstypes="lsblk_fstype_error"
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
# Get overview of filesystems
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_has_iso=
|
|
|
|
xdt_has_vfat=
|
|
|
|
xdt_has_other=
|
|
|
|
for xdt_fstype in $xdt_fstypes
|
2019-11-29 11:13:42 +00:00
|
|
|
do
|
2019-12-15 18:47:38 +00:00
|
|
|
if test "$xdt_fstype" = "iso9660"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_has_iso=y
|
|
|
|
if echo "$xdt_good_fs" | grep -F -v "has_$xdt_fstype" >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_good_fs="${xdt_good_fs}has_${xdt_fstype}+ "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
elif test "$xdt_fstype" = "vfat"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_has_vfat=y
|
|
|
|
if echo "$xdt_good_fs" | grep -F -v "has_$xdt_fstype" >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_good_fs="${xdt_good_fs}has_${xdt_fstype}+ "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
elif test -n "$xdt_fstype"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_has_other=y
|
|
|
|
if echo "$xdt_bad_fs" | grep -F -v "has_$xdt_fstype" >/dev/null
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_bad_fs="${xdt_bad_fs}has_${xdt_fstype}- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# Decide whether the found filesystems look dispensible enough
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_reasons="${xdt_reasons}${xdt_good_fs}${xdt_bad_fs}"
|
|
|
|
if test "${xdt_bad_fs}${xdt_good_fs}" = "" \
|
2020-02-12 15:06:49 +00:00
|
|
|
&& test -z "$xdt_have_su_power" && test -z "$xdt_lsblk_fs_wo_su"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}no_fs_while_not_su- "
|
|
|
|
elif test -n "$xdt_look_for_iso"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_has_iso"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_reasons="${xdt_reasons}look_for_iso++ "
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}no_iso9660- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
elif test -n "$xdt_has_other"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Optional tests for size
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -n "$xdt_max_size" || test -n "$xdt_min_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2020-01-07 15:47:41 +00:00
|
|
|
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)
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -z "$xdt_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}lsblk_no_size- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -n "$xdt_max_size" && test -n "$xdt_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
if test "$xdt_size" -gt "$xdt_max_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}size_too_large- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -n "$xdt_min_size" && test -n "$xdt_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
if test "$xdt_size" -lt "$xdt_min_size"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_yucky=y
|
|
|
|
xdt_reasons="${xdt_reasons}size_too_small- "
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Now decide overall and report
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_descr=
|
|
|
|
if test -n "$xdt_with_vendor_model"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_descr=": "$("$xdt_lsblk_cmd" -n -o VENDOR,MODEL \
|
|
|
|
/dev/"$xdt_name" | tr '\n\r' ' ' | tr -s ' ')
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_yucky"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_show_reasons"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "$xdt_name : NO : $xdt_reasons$xdt_descr"
|
|
|
|
xdt_list_long "$xdt_name"
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_not_advised=1
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_show_reasons"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "$xdt_name : YES : $xdt_reasons$xdt_descr"
|
|
|
|
xdt_list_long "$xdt_name"
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "$xdt_name"
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:13:00 +00:00
|
|
|
## Puts list of mounted (sub-)devices of $1 into $mounted_devs
|
2019-12-15 18:47:38 +00:00
|
|
|
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
|
2019-11-30 09:18:25 +00:00
|
|
|
do
|
2019-12-02 09:56:15 +00:00
|
|
|
# Show the found mount lines and add their device paths to list
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_mount_line=$(mount | grep '^'"$xdt_i"' ')
|
|
|
|
if test -n "$xdt_mount_line"
|
2019-11-30 09:18:25 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo " $xdt_mount_line"
|
|
|
|
xdt_mounted_devs="$xdt_mounted_devs $xdt_i"
|
2019-11-30 09:18:25 +00:00
|
|
|
fi
|
|
|
|
done
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
## Unmount xdt_mounted_devs (maybe filled by xdt_list_mounted_of)
|
2019-12-16 12:11:55 +00:00
|
|
|
## $1 : base device name
|
2019-12-15 18:47:38 +00:00
|
|
|
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
|
2019-12-16 12:11:55 +00:00
|
|
|
xdt_list_mounted_of "$1"
|
2019-12-15 18:47:38 +00:00
|
|
|
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
|
2019-12-02 16:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## Does the work of unmounting and dd-ing
|
2019-12-16 12:11:55 +00:00
|
|
|
## $1 : image file path
|
|
|
|
## $2 : base device name
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_write_image() {
|
2019-12-02 16:13:00 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -z "$xdt_umount_cmd"
|
2019-12-02 16:13:00 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "No executable program umount found in: $xdt_sudo_x_dir_list" >&2
|
2019-12-02 16:13:00 +00:00
|
|
|
return 6
|
|
|
|
fi
|
|
|
|
echo "Looking for mount points of $2:"
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_mounted_devs=
|
|
|
|
xdt_list_mounted_of "$2"
|
2019-12-02 16:13:00 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_dummy_force"
|
2019-12-02 09:56:15 +00:00
|
|
|
then
|
|
|
|
echo "AGAINST THE ADVICE BY THIS PROGRAM, a daring user could do:"
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_dummy_run=y
|
|
|
|
elif test -n "$xdt_dummy_run"
|
2019-12-02 09:56:15 +00:00
|
|
|
then
|
|
|
|
echo "Would do if not -dummy:"
|
|
|
|
fi
|
2019-11-29 11:13:42 +00:00
|
|
|
|
2019-12-16 12:11:55 +00:00
|
|
|
xdt_unmount "$2" || return "$?"
|
2019-11-30 12:06:16 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -z "$xdt_dd_cmd"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "No executable program dd found in: $xdt_sudo_x_dir_list" >&2
|
2019-11-29 11:13:42 +00:00
|
|
|
return 6
|
|
|
|
fi
|
2020-01-07 15:47:41 +00:00
|
|
|
|
|
|
|
# Erase possible GPT backup table at end of device
|
|
|
|
# if expr can properly divide device size by 512
|
2020-01-08 20:50:21 +00:00
|
|
|
xdt_dev_bytes=$("$xdt_lsblk_cmd" -n -b -o SIZE /dev/"${2}" | head -1)
|
2020-01-07 15:47:41 +00:00
|
|
|
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"
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_dummy_run"
|
2019-11-30 12:06:16 +00:00
|
|
|
then
|
2020-01-07 15:47:41 +00:00
|
|
|
if test -n "$xdt_dev_end"
|
|
|
|
then
|
|
|
|
echo " $xdt_gpt_erase_line"
|
|
|
|
fi
|
|
|
|
echo " $xdt_copy_line"
|
2019-11-30 12:06:16 +00:00
|
|
|
else
|
2019-12-03 09:34:19 +00:00
|
|
|
echo "Performing:"
|
2020-01-07 15:47:41 +00:00
|
|
|
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"
|
2020-01-06 20:44:11 +00:00
|
|
|
$xdt_sudo_cmd "$xdt_dd_cmd" if="${1}" of=/dev/"${2}" $xdt_dd_bs
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_ret="$?"
|
2019-12-12 22:26:04 +00:00
|
|
|
sync
|
2019-12-15 18:47:38 +00:00
|
|
|
if test "$xdt_ret" -ne 0
|
2019-12-12 22:26:04 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "FAILURE: $xdt_sudo_cmd $xdt_dd_cmd was not successful" >&2
|
2019-12-12 22:26:04 +00:00
|
|
|
return 18
|
|
|
|
fi
|
2019-11-30 12:06:16 +00:00
|
|
|
fi
|
2019-11-29 11:13:42 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_dummy_force"
|
2019-11-30 17:35:31 +00:00
|
|
|
then
|
|
|
|
echo "BE SMART. BE CAUTIOUS. BEWARE."
|
|
|
|
fi
|
2019-11-29 11:13:42 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
## 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)
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -n "$xdt_dummy_force" && test "$xdt_num_devs" -ne 1
|
2019-11-30 17:35:31 +00:00
|
|
|
then
|
2019-12-02 09:56:15 +00:00
|
|
|
echo "SORRY: Refusing -dummy_force with not exactly one device given." >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
return 10
|
2019-11-30 17:35:31 +00:00
|
|
|
fi
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -n "$xdt_dummy_force" && test -n "$xdt_dummy_run" \
|
|
|
|
&& test "$xdt_num_cand" -ne 1
|
2019-11-30 17:35:31 +00:00
|
|
|
then
|
|
|
|
# -dummy_force in a situation where the program would normally refuse
|
2019-11-30 18:49:54 +00:00
|
|
|
echo
|
2019-11-30 17:35:31 +00:00
|
|
|
echo "Overriding any advice because of -dummy_force"
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_candidates="$xdt_devs"
|
|
|
|
xdt_num_cand=1
|
|
|
|
elif test -n "$xdt_dummy_force"
|
2019-11-30 17:35:31 +00:00
|
|
|
then
|
|
|
|
# Downgrade -dummy_force to -dummy in order to avoid the ugly warning
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_dummy_force=
|
|
|
|
xdt_dummy_run=y
|
2019-11-30 17:35:31 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
if test "$xdt_num_cand" -eq 1
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_image_file"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_do_plug_test"
|
2019-11-30 16:26:18 +00:00
|
|
|
then
|
2019-12-03 09:34:19 +00:00
|
|
|
echo >&2
|
2019-12-02 09:56:15 +00:00
|
|
|
echo "Step 3:" >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_dummy_run"
|
2019-11-30 16:26:18 +00:00
|
|
|
then
|
2019-12-02 18:56:08 +00:00
|
|
|
echo "This would be the last chance to abort. Enter the word 'yes' to see -dummy report." >&2
|
2019-11-30 16:26:18 +00:00
|
|
|
else
|
2019-12-02 18:56:08 +00:00
|
|
|
echo "Last chance to abort. Enter the word 'yes' to start REAL WRITING." >&2
|
2019-11-30 16:26:18 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
read xdt_dummy
|
2019-12-22 16:02:02 +00:00
|
|
|
if test "X$xdt_dummy" = "Xyes" || test "X$xdt_dummy" = "X'yes'" \
|
|
|
|
|| test "X$xdt_dummy" = 'X"yes"'
|
2019-12-02 18:56:08 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_dummy=dummy
|
2019-12-02 18:56:08 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "WRITE RUN PREVENTED by user input '$xdt_dummy'." >&2
|
|
|
|
return 17
|
2019-12-02 18:56:08 +00:00
|
|
|
fi
|
2019-11-30 16:26:18 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_write_image "$xdt_image_file" $xdt_candidates || return "$?"
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_candidates=$(echo $xdt_candidates)
|
|
|
|
if test -n "$xdt_dummy_run"
|
2019-12-06 08:25:50 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "Would simulate writing to /dev/$xdt_candidates if an -image_file were given."
|
2019-12-06 08:25:50 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
echo "Would write to /dev/$xdt_candidates if an -image_file were given."
|
2019-12-06 08:25:50 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
return 0
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
elif test "$xdt_num_cand" -gt 1
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
|
|
|
echo "WILL NOT WRITE ! More than one candidate found for target device:" >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_show_reasons=y
|
|
|
|
xdt_with_vendor_model=y
|
|
|
|
xdt_devs="$xdt_candidates"
|
|
|
|
xdt_list_devices >&2
|
2019-12-02 09:56:15 +00:00
|
|
|
echo "HINT: Unplug the unwanted devices from the machine," >&2
|
2019-11-30 14:18:47 +00:00
|
|
|
echo " or work with option -plug_test," >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
echo " or add the desired name out of {$(echo $xdt_candidates | sed -e 's/ /,/g')} as additional argument." >&2
|
|
|
|
return 3
|
2019-11-29 11:13:42 +00:00
|
|
|
else
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -n "$xdt_devs_named"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
|
|
|
echo "NO CANDIDATE FOR TARGET DEVICE AMONG THE GIVEN NAMES !" >&2
|
|
|
|
else
|
|
|
|
echo "NO CANDIDATE FOR TARGET DEVICE FOUND !" >&2
|
|
|
|
fi
|
2019-12-02 09:56:15 +00:00
|
|
|
echo "Overall available devices:" >&2
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_list_all=y
|
|
|
|
xdt_list_long=
|
|
|
|
xdt_show_reasons=y
|
|
|
|
xdt_with_vendor_model=y
|
|
|
|
xdt_list_devices >&2
|
|
|
|
return 4
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
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 "$?"
|
2020-01-06 21:02:19 +00:00
|
|
|
if test -n "$xdt_end_after_setup"
|
|
|
|
then
|
|
|
|
return 0
|
|
|
|
fi
|
2019-12-15 18:47:38 +00:00
|
|
|
|
|
|
|
if test -n "$xdt_do_plug_test"
|
|
|
|
then
|
|
|
|
xdt_plug_in_watcher || return "$?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
xdt_list_devices || return "$?"
|
|
|
|
|
2019-12-22 16:02:02 +00:00
|
|
|
if test -z "$xdt_list_all" && test -n "$xdt_do_write"
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
|
|
|
xdt_decide_writing || return "$?"
|
|
|
|
fi
|
|
|
|
|
2020-01-01 08:06:01 +00:00
|
|
|
if test -n "$xdt_devs_named"
|
2019-12-15 18:47:38 +00:00
|
|
|
then
|
|
|
|
return $xdt_not_advised
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
2019-11-29 11:13:42 +00:00
|
|
|
|
2019-12-15 18:47:38 +00:00
|
|
|
if test -z "$no_xorriso_dd_target_run"
|
2019-11-29 11:13:42 +00:00
|
|
|
then
|
2019-12-15 18:47:38 +00:00
|
|
|
xdt_main "$@" || exit "$?"
|
2019-11-29 11:13:42 +00:00
|
|
|
fi
|
|
|
|
|