Introduced option -plug_test

This commit is contained in:
Thomas Schmitt 2019-11-30 15:18:47 +01:00
parent 81272c80ca
commit b8f77670d9

View File

@ -56,6 +56,9 @@ print_usage() {
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"
@ -64,6 +67,12 @@ print_usage() {
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."
@ -99,6 +108,7 @@ print_usage() {
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"
echo " $0 -with_sudo -image_file debian-live-10.0.0-amd64-xfce.iso -DO_WRITE -plug_test"
echo
}
@ -123,6 +133,7 @@ reset_job() {
image_file=
do_write=
dummy_run=
do_plug_test=
# Status
sudo_cmd=
@ -159,6 +170,9 @@ arg_interpreter() {
list_all=y
with_vendor_model=y
show_reasons=y
elif test "$i" = "-plug_test"
then
do_plug_test=y
elif test "$i" = "-max_size"
then
next_is="max_size"
@ -211,6 +225,77 @@ arg_interpreter() {
fi
}
## Trying to find the desired device by watching plug-in effects
plug_in_watcher() {
found_devices=
echo
echo "Caused by option -plug_test: Attempt to find the desired device"
echo "by watching it appear after being plugged in."
echo
echo "Step 1:"
echo "Please make sure that the desired target device is plugged _out_ now."
echo "Press the Enter key when ready."
read dummy
old_device_list=' '$($lsblk_cmd -d -n -o NAME | grep -v '^sr[0-9]' | grep -v '^fd[0-9]' | tr '\n\r' ' ')' '
# <<< 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
# old_device_list=' '$($lsblk_cmd -d -n -o NAME | grep -v '^sr[0-9]' | grep -v '^fd[0-9]' | tr '\n\r' ' ' | sed -e 's/sd[e-f]//')' '
echo "Found and noted as _not_ desired: $old_device_list"
echo
echo "Step 2:"
echo "Please plug in the desired target device and then press the Enter key."
read dummy
echo "Waiting up to 10 seconds for a new device to be listed ..."
end_time="$(expr $(date +'%s') + 10)"
while test $(date +'%s') -le "$end_time"
do
new_device_list=' '$($lsblk_cmd -d -n -o NAME | grep -v '^sr[0-9]' | grep -v '^fd[0-9]' | tr '\n\r' ' ')' '
if test "$old_device_list" = "$new_device_list"
then
sleep 1
else
for i in $new_device_list
do
if echo "$old_device_list" | fgrep -v ' '"$i"' ' >/dev/null
then
found_devices="$found_devices $i"
fi
# Break the waiting loop
end_time=0
done
fi
done
if test -z "$found_devices"
then
echo "SORRY: No new candidate device was found." >&2
return 8
fi
num=$(echo $found_devices | wc -w)
if test "$num" -gt 1
then
echo "SORRY: More than one new candidate device appeared: $found_devices"
return 9
fi
echo "Found and noted as desired device: $found_devices"
if test -n "$devs"
then
echo "(-plug_test is overriding device list given by arguments: $devs )"
fi
if test -n "$list_all"
then
echo "(-plug_test is overriding -list_all)"
list_all=
fi
devs_named=y
with_vendor_model=y
show_reasons=y
devs=$(echo -n $found_devices)
echo
return 0
}
## Evaluation of available devices and suitability
list_devices() {
if test -n "$list_all"
@ -468,6 +553,17 @@ write_image() {
reset_job
arg_interpreter "$@"
if test -n "$do_plug_test"
then
plug_in_watcher
ret=$?
if test "$ret" -ne 0
then
exit $ret
fi
fi
list_devices
if test -n "$list_all"
then
@ -496,6 +592,7 @@ then
devs="$candidates"
list_devices >&2
echo "HINT: Unplug the unwanted devices from the machine,"
echo " or work with option -plug_test," >&2
echo " or add the desired name out of {$(echo $candidates | sed -e 's/ /,/g')} as additional argument." >&2
exit 3
else