Mount SD to allow installation plugins

Hello!

USE WITH YOUR OWN RISK!

Wrote following script:

#!/bin/bash

echo "Use at your own risk. There is no guarantee that this will work correctly."
read -p "Continue? y/n " -n 1 -r
echo    # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

echo "Updating packages..."
opkg update

echo "Installing necessary packages..."
opkg install block-mount kmod-usd-storage kmod-fs-ext4 e2fsprogs

echo "Checking for USB/SD card..."
DEVICE=$(ls /dev/sd* || ls /dev/mmcblk0*)
echo "Found device: $DEVICE"

echo "Formatting the USB/SD card..."
mkfs.ext4 $DEVICE << EOF
Y
EOF

echo "Mounting the USB/SD card..."
mount -t ext4 $DEVICE /mnt

echo "Creating a temporary root directory..."
mkdir /tmp/root

echo "Binding mount the root filesystem..."
mount -o bind / /tmp/root

echo "Copying the root filesystem to the USB/SD card..."
cp /tmp/root/* /mnt -a

echo "Unmounting the temporary root directory and the USB/SD card..."
umount /tmp/root
umount /mnt

echo "Setting up auto-mount..."
block detect > /etc/config/fstab
uci set fstab.@mount[0].target='/overlay'
uci set fstab.@mount[0].enabled='1'
uci commit fstab

echo "Rebooting the router..."
reboot

This script will automate this official instructions:

No error handling at all, which could lead to major issues - especially when formatting something.
I would add a big warning at the beginning of the script :sweat_smile:

1 Like

Added! :blush:


#!/bin/bash

# Function to handle errors
handle_error() {
    echo "An error occurred. Exiting script."
    exit 1
}

# Trap errors
trap 'handle_error' ERR

echo "Use at your own risk. There is no guarantee that this will work correctly."
read -p "Continue? y/n " -n 1 -r
echo    # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

echo "Updating packages..."
opkg update || { echo "Failed to update packages."; exit 1; }

echo "Installing necessary packages..."
opkg install block-mount kmod-usd-storage kmod-fs-ext4 e2fsprogs || { echo "Failed to install packages."; exit 1; }

echo "Checking for USB/SD card..."
DEVICE=$(ls /dev/sd* || ls /dev/mmcblk0*) || { echo "Failed to find USB/SD card."; exit 1; }
echo "Found device: $DEVICE"

echo "Formatting the USB/SD card..."
mkfs.ext4 $DEVICE << EOF
Y
EOF

echo "Mounting the USB/SD card..."
mount -t ext4 $DEVICE /mnt || { echo "Failed to mount USB/SD card."; exit 1; }

echo "Creating a temporary root directory..."
mkdir /tmp/root || { echo "Failed to create temporary root directory."; exit 1; }

echo "Binding mount the root filesystem..."
mount -o bind / /tmp/root || { echo "Failed to bind mount the root filesystem."; exit 1; }

echo "Copying the root filesystem to the USB/SD card..."
cp /tmp/root/* /mnt -a || { echo "Failed to copy root filesystem."; exit 1; }

echo "Unmounting the temporary root directory and the USB/SD card..."
umount /tmp/root || { echo "Failed to unmount temporary root directory."; exit 1; }
umount /mnt || { echo "Failed to unmount USB/SD card."; exit 1; }

echo "Setting up auto-mount..."
block detect > /etc/config/fstab || { echo "Failed to set up auto-mount."; exit 1; }
uci set fstab.@mount[0].target='/overlay' || { echo "Failed to set target for auto-mount."; exit 1; }
uci set fstab.@mount[0].enabled='1' || { echo "Failed to enable auto-mount."; exit 1; }
uci commit fstab || { echo "Failed to commit auto-mount settings."; exit 1; }

echo "Rebooting the router..."
reboot || { echo "Failed to reboot the router."; exit 1; }

With error handling from Claude :slight_smile:

1 Like