agama/live/live-root/usr/bin/checkmedia-service

102 lines
2.6 KiB
Bash
Executable file

#! /bin/bash
# get list of all system consoles from /proc/consoles
get_consoles() {
if [ -f /proc/consoles ]; then
while read -r console _rest; do
if [ -c "/dev/$console" ]; then
echo "$console"
fi
done < /proc/consoles | sort -u
fi
}
readarray -t consoles < <(get_consoles)
# print the specified message on all consoles
broadcast() {
local msg="$1"
# trivial implementation for a single console
if [ ${#consoles[@]} -le 1 ]; then
echo -e "$msg"
else
for c in "${consoles[@]}"; do
echo -e "$msg" > "/dev/$c"
done
fi
}
# wait for a key press on any console
wait_any_key() {
local msg="$1"
local tmpdir
local fifo
local -a pids
# trivial implementation for a single console
if [ ${#consoles[@]} -le 1 ]; then
read -r -n1 -s -p "$msg"
echo
return
fi
# use a fifo for synchronizing the processes
tmpdir=$(mktemp -d)
fifo="$tmpdir/fifo"
mkfifo "$fifo"
# spawn a read process for each console
for c in "${consoles[@]}"; do
echo -n "$msg" > "/dev/$c"
(
read -r -n1 -s < "/dev/$c"
echo "done" > "$fifo" 2> /dev/null
) &
pids+=("$!")
done
# block until one of the background jobs receives a key press and writes to the fifo
read -r _ < "$fifo"
# cleanup, kill the other waiting processes
for pid in "${pids[@]}"; do
kill "$pid" 2> /dev/null
done
rm -rf "$tmpdir"
# finish the printed line
for c in "${consoles[@]}"; do
echo > "/dev/$c"
done
}
# get the partition where the live ISO is mounted, the real name is set by the
# config.sh script which gets the live partition label name from KIWI
# Note: the LIVE_MEDIUM_LABEL placeholder is replaced by the real label in config.sh script
label="@@LIVE_MEDIUM_LABEL@@"
disk="/dev/disk/by-label/$label"
if [ -b "$disk" ]; then
# when booting from disk/USB flash checkmedia expects the device name, not the partition
# get the parent device if there is any
parent=$(lsblk --noheadings --output PKNAME "$disk")
[ -b "/dev/$parent" ] && disk="/dev/$parent"
fi
if [ ! -b "$disk" ]; then
broadcast "\e[31mPartition \"$label\" not found, skipping media check\e[0m"
wait_any_key "Press any key to continue... "
exit 0
fi
broadcast "Checking data integrity of device $disk ($label))..."
if checkmedia -v "$disk"; then
broadcast "\e[32mMedium check succeeded\e[0m"
wait_any_key "Press any key to continue... "
else
broadcast "\e[31mERROR: Medium check failed!\e[0m\nThe installation medium is broken, it should not be used for installation."
wait_any_key "Press any key to halt the system... "
halt
fi