Process refinement

* Migration stages are now piped through to a dialog progressbox for monitoring
* Migration stages are now proxied through a separate function to monitor for breaking errors
* Each migration stage now ends with a 3-second sleep to provide time to review the last lines written to the progressbox
* Limited the wget spider retry amount to prevent an indefinite wait during a bad connectivity check
* Nulled stderr on select calls during formatting and data-copy to hide non-critical extraneous errors
* Added a finalization step to inform the user of an impending reboot
pull/46/head
jrwimmer 2021-12-20 19:42:19 -08:00
parent d02f467a0c
commit 3e108f1cba
No known key found for this signature in database
GPG Key ID: A62B2B67E6233F10
1 changed files with 82 additions and 40 deletions

View File

@ -44,22 +44,27 @@ function check_network {
# First, extract a server URL from distfeeds.conf # First, extract a server URL from distfeeds.conf
opkg_baseurl=$(sed -E -n "s/^[^#]*(http:\/\/[^\/]*)\/.*/\1/p;q" /etc/opkg/distfeeds.conf) &> /dev/null opkg_baseurl=$(sed -E -n "s/^[^#]*(http:\/\/[^\/]*)\/.*/\1/p;q" /etc/opkg/distfeeds.conf) &> /dev/null
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
/usr/bin/dialog --title "Extroot" --msgbox "\nCould not extract an opkg repo from distfeeds.conf\n\nIf this is unexpected, you may need to upgrade or restore the firmware." 9 72 echo "\nCould not extract an opkg repo from distfeeds.conf\n\nIf this is unexpected, you may need to upgrade or restore the firmware." > $1
exit 1 return 1
fi fi
# ...then, use wget to determine if the server can be reached # ...then, use wget to determine if the server can be reached
wget -q --spider "$opkg_baseurl" &> /dev/null wget -q --tries 3 --spider "$opkg_baseurl" &> /dev/null
if [[ $? -ne 0 ]]; then wget_returncode=$?
/usr/bin/dialog --title "Extroot" --msgbox "\nThe LAN Turtle could not connect to $output\nPlease connect the LAN Turtle to the Internet and try again." 9 72 if [[ $wget_returncode -ne 0 ]]; then
exit 1 echo "\nLAN Turtle could not connect to $opkg_baseurl\nVerify the device's internet connection and try again (wget error $wget_returncode)" > $1
return 1
fi fi
} }
function install_dependencies { function install_dependencies {
/bin/opkg update | /usr/bin/dialog --progressbox "Updating opkg" 14 72 echo "Updating opkg"
/bin/opkg update
/bin/opkg install $REQUIRED_PACKAGES | /usr/bin/dialog --progressbox "Checking/installing dependencies" 14 72 echo "Checking/installing dependencies"
/bin/opkg install $REQUIRED_PACKAGES
sleep 3
} }
function remount_rootfsdata { function remount_rootfsdata {
@ -74,87 +79,124 @@ function remount_rootfsdata {
uci set fstab.rwm.device="${rootfsdata_dev}" uci set fstab.rwm.device="${rootfsdata_dev}"
uci set fstab.rwm.target="/rwm" uci set fstab.rwm.target="/rwm"
uci commit fstab uci commit fstab
sleep 3
} }
function format_sdcard { function format_sdcard {
# Identify our storage device echo "Checking SD card presence"
sdcard_device=$(ls /dev/sd* 2> /dev/null | head -n1)
# Check SD card presence
[[ "$(ls /dev/sd* 2> /dev/null | wc -l)" == "0" ]] && { [[ "$(ls /dev/sd* 2> /dev/null | wc -l)" == "0" ]] && {
echo "No SD card inserted or device not supported" echo "\nError: No SD card is inserted or the card is not supported\n\nAdditional Details:\nCould not find a device with prefix 'sd'" > $1
exit 1 return 1
} }
# Unmount storage as applicable sdcard_device=$(ls /dev/sd* 2> /dev/null | head -n1)
echo "New overlay device identified as: $sdcard_device"
echo "Unmounting any active external storage"
block umount &> /dev/null block umount &> /dev/null
# Partition SD card for storage and swap echo "Partitioning SD card"
echo -e "o\nn\np\n2\n\n+1024M\nn\np\n1\n\np\n\nw\n" | fdisk $sdcard_device echo -e "o\nn\np\n2\n\n+1024M\nn\np\n1\n\np\n\nw\n" | fdisk $sdcard_device 2>/dev/null
echo y | mkfs.ext4 ${sdcard_device}1 mkfs.ext4 -F ${sdcard_device}1
mkswap ${sdcard_device}2 mkswap ${sdcard_device}2
# Restart the storage bus echo "Restarting the storage bus"
echo "1-0:1.0" > /sys/bus/usb/drivers/hub/unbind echo "1-0:1.0" > /sys/bus/usb/drivers/hub/unbind 2>/dev/null
echo "1-0:1.0" > /sys/bus/usb/drivers/hub/bind echo "1-0:1.0" > /sys/bus/usb/drivers/hub/bind 2>/dev/null
sleep 3 sleep 3
# Commit/activate swap and remove existing SD card mounts echo "Commit/activate swap and remove existing SD card mounts"
block detect > /etc/config/fstab block detect > /etc/config/fstab
uci -q delete fstab.@mount[0] uci -q delete fstab.@mount[0]
uci commit fstab uci commit fstab
block mount &> /dev/null block mount &> /dev/null
sleep 3
} }
function make_extroot { function make_extroot {
# Identify our storage partition
sdcard_device=$(ls /dev/sd* 2> /dev/null | head -n1)1 sdcard_device=$(ls /dev/sd* 2> /dev/null | head -n1)1
echo "New overlay partition identified as: $sdcard_device"
# Identify the partition's UUID
eval $(block info ${sdcard_device} | grep -o -e "UUID=\S*") eval $(block info ${sdcard_device} | grep -o -e "UUID=\S*")
echo "New overlay partition has a UUID of: $UUID"
# Delete and recreate the overlay fstab entry echo "Recreating overlay fstab entry"
uci -q delete fstab.overlay uci -q delete fstab.overlay
uci set fstab.overlay="mount" uci set fstab.overlay="mount"
uci set fstab.overlay.uuid="${UUID}" uci set fstab.overlay.uuid="${UUID}"
uci set fstab.overlay.target="/overlay" uci set fstab.overlay.target="/overlay"
uci commit fstab uci commit fstab
sleep 3
} }
function migrate_overlay { function migrate_overlay {
# Identify our storage partition
sdcard_device=$(ls /dev/sd* 2> /dev/null | head -n1)1 sdcard_device=$(ls /dev/sd* 2> /dev/null | head -n1)1
echo "New overlay partition identified as: $sdcard_device"
# Create a temporary mount point directory echo "Creating temporary mount point directory at: /tmp/cproot"
mkdir -p /tmp/cproot mkdir -p /tmp/cproot
# Bind the current overlay to the mount point echo "Binding active overlay to: /tmp/cproot"
mount --bind /overlay /tmp/cproot mount --bind /overlay /tmp/cproot
# Mount the new overlay storage echo "Mounting new overlay parition at: /mnt"
mount ${sdcard_device} /mount mount ${sdcard_device} /mnt
# Copy over the data echo "Copying data; please wait..."
tar -C /tmp/cproot -cvf - . | tar -C /mnt -xf - tar -C /tmp/cproot -cvf - . 2>/dev/null | tar -C /mnt -xf - 2>/dev/null
echo "Copy process complete"
# Unmount the new overlay and temporary mount point echo "Unmounting new overlay and removing bind"
umount /tmp/cproot /mnt umount /tmp/cproot /mnt
sleep 3
}
function finalize {
/usr/bin/dialog --title "Extroot: Info" --msgbox "::: NOTICE :::\n\nOperation complete. To finalize changes, the system will now reboot." 14 72
reboot
}
function perform_step {
# Create a tmpfile in which we can store error data
active_tmpfile=$(mktemp)
# Default error data to 0
echo 0 > $active_tmpfile
# Perform the requested step
eval $1 | /usr/bin/dialog --progressbox "$2" 14 72
# Retrieve the last error written to the tmpfile
last_return_code=$(cat $active_tmpfile)
# Delete the tmpfile
rm $active_tmpfile
# Determine if an error occurred
if [[ "$last_return_code" != 0 ]]; then
# Notify the user of the problem
/usr/bin/dialog --title "Extroot: Error" --msgbox "$last_return_code" 9 72
exit 1
fi
} }
function perform_migration { function perform_migration {
check_network
install_dependencies perform_step "check_network" "Checking network connectivity"
remount_rootfsdata perform_step "install_dependencies" "Installing requried dependencies"
format_sdcard perform_step "remount_rootfsdata" "Creating backup mount for current overlay"
make_extroot perform_step "format_sdcard" "Formatting SD card"
migrate_overlay perform_step "make_extroot" "Configuring new overlay"
reboot perform_step "migrate_overlay" "Migrating overlay data"
finalize
} }
function start { function start {