865 lines
26 KiB
Bash
Executable File
865 lines
26 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# Copyright (c) 2019
|
|
# Nio Wiklund alias sudodus <nio dot wiklund at gmail dot com>
|
|
# Thomas Schmitt <scdbackup@gmx.net>
|
|
# 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.
|
|
|
|
|
|
## 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 " -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 " -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 " -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 "$2" = "device_name"
|
|
then
|
|
if echo "$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
|
|
fi
|
|
elif test "$2" = "image_file"
|
|
then
|
|
if echo "$1" | grep '[$`[*?<>|&!{\]' >/dev/null
|
|
then
|
|
echo "SORRY: Given image file name contains unexpected character. Not ok: "'[$`[*?<>|&!{\]' >&2
|
|
return 15
|
|
fi
|
|
else
|
|
if echo "$1" | grep -v '^[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_do_plug_test=
|
|
|
|
# Status
|
|
xdt_sudo_cmd=
|
|
xdt_have_su_power=
|
|
|
|
return 0
|
|
}
|
|
|
|
## Predict superuser power. Possibly enable sudo with lsblk -o FSTYPE and dd.
|
|
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
|
|
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 "$xdt_i" = "-list_all"
|
|
then
|
|
xdt_list_all=y
|
|
xdt_with_vendor_model=y
|
|
xdt_show_reasons=y
|
|
elif test "$xdt_i" = "-list_long"
|
|
then
|
|
xdt_do_list_long=y
|
|
elif test "$xdt_i" = "-plug_test"
|
|
then
|
|
xdt_do_plug_test=y
|
|
elif test "$xdt_i" = "-max_size"
|
|
then
|
|
xdt_next_is="max_size"
|
|
elif test "$xdt_i" = "-min_size"
|
|
then
|
|
xdt_next_is="min_size"
|
|
elif test "$xdt_i" = "-with_vendor_model"
|
|
then
|
|
xdt_with_vendor_model=y
|
|
elif test "$xdt_i" = "-look_for_iso"
|
|
then
|
|
xdt_look_for_iso=y
|
|
elif test "$xdt_i" = "-with_sudo"
|
|
then
|
|
xdt_with_sudo=y
|
|
elif test "$xdt_i" = "-image_file"
|
|
then
|
|
xdt_next_is="image_file"
|
|
elif test "$xdt_i" = "-dummy"
|
|
then
|
|
xdt_dummy_run=y
|
|
elif test "$xdt_i" = "-dummy_force"
|
|
then
|
|
xdt_dummy_run=y
|
|
xdt_do_write=y
|
|
xdt_dummy_force=y
|
|
elif test "$xdt_i" = "-DO_WRITE"
|
|
then
|
|
xdt_do_write=y
|
|
elif test "$xdt_i" = "-help"
|
|
then
|
|
xdt_print_usage
|
|
return 0
|
|
elif echo "$xdt_i" | grep -v '^-' >/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 "$?"
|
|
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" -o "$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" -a -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}" = "" \
|
|
-a -z "$xdt_have_su_power"
|
|
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" -o -n "$xdt_min_size"
|
|
then
|
|
xdt_size=$("$xdt_lsblk_cmd" -n -b -o SIZE /dev/"$xdt_name" \
|
|
| head -1 | 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" -a -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" -a -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)
|
|
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 "$2"
|
|
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
|
|
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 || return "$?"
|
|
|
|
if test -z "$xdt_dd_cmd"
|
|
then
|
|
echo "No executable program dd found in: $xdt_sudo_x_dir_list" >&2
|
|
return 6
|
|
fi
|
|
if test -n "$xdt_dummy_run"
|
|
then
|
|
echo " $xdt_sudo_cmd $xdt_dd_cmd if='${1}' bs=1M of=/dev/'${2}' ; sync"
|
|
else
|
|
echo "Performing:"
|
|
echo " $xdt_sudo_cmd $xdt_dd_cmd if='${1}' bs=1M of=/dev/'${2}' ; sync"
|
|
$xdt_sudo_cmd "$xdt_dd_cmd" if="${1}" bs=1M of=/dev/"${2}"
|
|
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
|
|
|
|
# >>> ??? Erase possible GPT backup table at end of device ?
|
|
|
|
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" -a "$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" -a -n "$xdt_dummy_run" \
|
|
-a "$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 "$xdt_dummy" = "yes" -o "$xdt_dummy" = "'yes'" \
|
|
-o "$xdt_dummy" = '"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_do_plug_test"
|
|
then
|
|
xdt_plug_in_watcher || return "$?"
|
|
fi
|
|
|
|
xdt_list_devices || return "$?"
|
|
|
|
if test -z "$xdt_list_all" -a -n "$xdt_do_write"
|
|
then
|
|
xdt_decide_writing || return "$?"
|
|
fi
|
|
|
|
if test -n "$xdt_devs"
|
|
then
|
|
return $xdt_not_advised
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
if test -z "$no_xorriso_dd_target_run"
|
|
then
|
|
xdt_main "$@" || exit "$?"
|
|
fi
|
|
|