Reorganization and scripts fixing
This commit is contained in:
@ -2,11 +2,24 @@
|
||||
|
||||
trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
|
||||
|
||||
MNTPATH="/tmp/boot-gpt-mnt"
|
||||
MNTPATH="/tmp/mbr2gpt-mnt"
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
echo "Usage: $0 <device>"
|
||||
echo ""
|
||||
exit 1
|
||||
}
|
||||
|
||||
mntpart()
|
||||
{
|
||||
MNTED=TRUE
|
||||
if [ ! -d "${MNTPATH}/" ]; then
|
||||
mkdir "${MNTPATH}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
@ -17,6 +30,7 @@ mntpart()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to mount $2 partition"
|
||||
fi
|
||||
MNTED=TRUE
|
||||
}
|
||||
|
||||
umntpart()
|
||||
@ -25,22 +39,8 @@ umntpart()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to unmount partition"
|
||||
fi
|
||||
rm -r "${MNTPATH}/"
|
||||
MNTED=FALSE
|
||||
}
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
echo "Usage: $0 /dev/sdX"
|
||||
echo ""
|
||||
exit 1
|
||||
rm -r "${MNTPATH}/"
|
||||
}
|
||||
|
||||
MNTED=FALSE
|
||||
@ -79,9 +79,13 @@ DEVICE="$1"
|
||||
if [ "${DEVICE}" = "" ]; then
|
||||
errexit "No device specified"
|
||||
fi
|
||||
if [[ ! "${DEVICE}" =~ ^/dev/sd[a-z]$ || ! -b "${DEVICE}" ]]; then
|
||||
if [[ (! "${DEVICE}" =~ ^/dev/sd[a-z]$ && ! "${DEVICE}" =~ ^/dev/nvme0n1$ && ! "${DEVICE}" =~ ^/dev/mmcblk0$) || ! -b "${DEVICE}" ]]; then
|
||||
errexit "Invalid DEVICE: ${DEVICE}"
|
||||
fi
|
||||
DEVICE_P="${DEVICE}"
|
||||
if [[ "${DEVICE_P}" = "/dev/nvme0n1" || "${DEVICE}" = "/dev/mmcblk0" ]];then
|
||||
DEVICE_P+='p'
|
||||
fi
|
||||
if [ $(mount | grep -c "^${DEVICE}") -ne 0 ]; then
|
||||
errexit "${DEVICE} is in use (mounted)"
|
||||
fi
|
||||
@ -110,7 +114,7 @@ if [ "${PTTYPE}" = "MBR" ]; then
|
||||
done
|
||||
sgdisk -z "${DEVICE}" &> /dev/null
|
||||
fi
|
||||
INFO="$(gdisk -l ${DEVICE})"
|
||||
INFO="$(gdisk -l ${DEVICE} 2> /dev/null)"
|
||||
LAST=$(sed -n 's|^.*last usable sector is \(\S\+\).*|\1|p' <<< "${INFO}")
|
||||
START=$(sed -n 's|^\s\+2\s\+\(\S\+\).*|\1|p' <<< "${INFO}")
|
||||
END=$(sed -n 's|^\s\+2\s\+\S\+\s\+\(\S\+\).*|\1|p' <<< "${INFO}")
|
||||
@ -174,10 +178,10 @@ if [[ "${PTTYPE}" = "MBR" || "${SHRINK}" = "TRUE" || "${EXPAND}" = "TRUE" || "${
|
||||
done
|
||||
if [ "${SHRINK}" = "TRUE" ]; then
|
||||
echo ""
|
||||
resize2fs -f -M "${DEVICE}2"
|
||||
resize2fs -f -M "${DEVICE_P}2"
|
||||
fi
|
||||
if [[ "${SHRINK}" = "TRUE" || "${EXPAND}" = "TRUE" ]]; then
|
||||
gdisk "${DEVICE}" <<EOF > /dev/null
|
||||
gdisk "${DEVICE}" <<EOF &> /dev/null
|
||||
d
|
||||
2
|
||||
n
|
||||
@ -189,9 +193,9 @@ w
|
||||
y
|
||||
EOF
|
||||
echo ""
|
||||
resize2fs -f "${DEVICE}2"
|
||||
resize2fs -f "${DEVICE_P}2"
|
||||
fi
|
||||
gdisk "${DEVICE}" <<EOF > /dev/null
|
||||
gdisk "${DEVICE}" <<EOF &> /dev/null
|
||||
r
|
||||
h
|
||||
1
|
||||
@ -202,22 +206,32 @@ n
|
||||
w
|
||||
y
|
||||
EOF
|
||||
PARTUUID_1="$(blkid ${DEVICE}1 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
PARTUUID_2="$(blkid ${DEVICE}2 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
mntpart "${DEVICE}1" "BOOT"
|
||||
sed -i '/^[[:space:]]*#/!s|\(^.*rootwait\).*$|\1|' "${MNTPATH}/cmdline.txt"
|
||||
PARTUUID_1="$(blkid ${DEVICE_P}1 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
PARTUUID_2="$(blkid ${DEVICE_P}2 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
mntpart "${DEVICE_P}1" "BOOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PARTUUID_2}\2|" "${MNTPATH}/cmdline.txt"
|
||||
sed -i '/^[[:space:]]*#/!s| init=/usr/lib/raspi-config/init_resize\.sh||' "${MNTPATH}/cmdline.txt"
|
||||
umntpart
|
||||
mntpart "${DEVICE}2" "ROOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\s\+.*\)$|PARTUUID=${PARTUUID_1}\1|" "${MNTPATH}/etc/fstab"
|
||||
mntpart "${DEVICE_P}2" "ROOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\S*\s\+vfat\s\+.*\)$|PARTUUID=${PARTUUID_1}\1|" "${MNTPATH}/etc/fstab"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/\s\+.*\)$|PARTUUID=${PARTUUID_2}\1|" "${MNTPATH}/etc/fstab"
|
||||
sed -i '/resize-root-fs/d' "${MNTPATH}/etc/rc.local" &> /dev/null
|
||||
rm "${MNTPATH}/etc/resize-root-fs" &> /dev/null
|
||||
if [ -f "${MNTPATH}/usr/lib/raspberrypi-sys-mods/firstboot" ]; then
|
||||
cp "${MNTPATH}/usr/lib/raspberrypi-sys-mods/firstboot" "${MNTPATH}/usr/lib/raspberrypi-sys-mods/first-boot"
|
||||
sed -i 's|firstboot|first-boot|g' "${MNTPATH}/usr/lib/raspberrypi-sys-mods/first-boot"
|
||||
sed -i 's|^\(\s*whiptail --infobox \"Resizing root filesystem.*\)$| return 0\n\n\1|' "${MNTPATH}/usr/lib/raspberrypi-sys-mods/first-boot" &> /dev/null
|
||||
umntpart
|
||||
mntpart "${DEVICE_P}1" "BOOT"
|
||||
sed -i '/^[[:space:]]*#/!s| init=/usr/lib/raspberrypi-sys-mods/firstboot| init=/usr/lib/raspberrypi-sys-mods/first-boot|' "${MNTPATH}/cmdline.txt"
|
||||
fi
|
||||
umntpart
|
||||
if [ "${SDBOOT}" = "TRUE" ]; then
|
||||
mntpart "/dev/mmcblk0p1" "BOOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PARTUUID_2}\2|" "${MNTPATH}/cmdline.txt"
|
||||
umntpart
|
||||
mntpart "${DEVICE}2" "ROOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\s\+.*\)$|/dev/mmcblk0p1\1|" "${MNTPATH}/etc/fstab"
|
||||
mntpart "${DEVICE_P}2" "ROOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\S*\s\+vfat\s\+.*\)$|/dev/mmcblk0p1\1|" "${MNTPATH}/etc/fstab"
|
||||
umntpart
|
||||
fi
|
||||
if [[ "${SHRINK}" = "FALSE" && "${EXPAND}" = "FALSE" ]]; then
|
@ -4,9 +4,22 @@ trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
|
||||
|
||||
MNTPATH="/tmp/sdc-boot-mnt"
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
echo "Usage: $0 [ /dev/sdX2 | /dev/mmcblk0p2 | hhhhhhhh-02 | hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh ]"
|
||||
echo ""
|
||||
exit 1
|
||||
}
|
||||
|
||||
mntpart()
|
||||
{
|
||||
MNTED=TRUE
|
||||
if [ ! -d "${MNTPATH}/" ]; then
|
||||
mkdir "${MNTPATH}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
@ -17,6 +30,7 @@ mntpart()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to mount $2 partition"
|
||||
fi
|
||||
MNTED=TRUE
|
||||
}
|
||||
|
||||
umntpart()
|
||||
@ -25,22 +39,8 @@ umntpart()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to unmount partition"
|
||||
fi
|
||||
rm -r "${MNTPATH}/"
|
||||
MNTED=FALSE
|
||||
}
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
echo "Usage: $0 [ /dev/sdX2 | /dev/mmcblk0p2 | hhhhhhhh-02 | hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh ]"
|
||||
echo ""
|
||||
exit 1
|
||||
rm -r "${MNTPATH}/"
|
||||
}
|
||||
|
||||
MNTED=FALSE
|
||||
@ -100,7 +100,7 @@ else
|
||||
umntpart
|
||||
if [ "${DEVICE}" != "/dev/mmcblk0p2" ]; then
|
||||
mntpart "${DEVICE}" "device ROOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\s\+.*\)$|/dev/mmcblk0p1\1|" "${MNTPATH}/etc/fstab"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\S*\s\+vfat\s\+.*\)$|/dev/mmcblk0p1\1|" "${MNTPATH}/etc/fstab"
|
||||
umntpart
|
||||
fi
|
||||
fi
|
@ -4,9 +4,22 @@ trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
|
||||
|
||||
MNTPATH="/tmp/set-partuuid-mnt"
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
echo "Usage: $0 device [ hhhhhhhh-02 | hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh | random ]"
|
||||
echo ""
|
||||
exit 1
|
||||
}
|
||||
|
||||
mntpart()
|
||||
{
|
||||
MNTED=TRUE
|
||||
if [ ! -d "${MNTPATH}/" ]; then
|
||||
mkdir "${MNTPATH}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
@ -17,6 +30,7 @@ mntpart()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to mount $2 partition"
|
||||
fi
|
||||
MNTED=TRUE
|
||||
}
|
||||
|
||||
umntpart()
|
||||
@ -25,22 +39,8 @@ umntpart()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to unmount partition"
|
||||
fi
|
||||
rm -r "${MNTPATH}/"
|
||||
MNTED=FALSE
|
||||
}
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
echo "Usage: $0 device [ hhhhhhhh-02 | hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh | random ]"
|
||||
echo ""
|
||||
exit 1
|
||||
rm -r "${MNTPATH}/"
|
||||
}
|
||||
|
||||
MNTED=FALSE
|
||||
@ -149,7 +149,7 @@ EOF
|
||||
PARTUUID_1="$(blkid "${DEVICE_P}1" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
PARTUUID_2="$(blkid "${DEVICE_P}2" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
mntpart "${DEVICE_P}2" "ROOT"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(PARTUUID=\)${ORIGUUID_1}\(\s\+/boot\s\+.*\)$|\1${PARTUUID_1}\2|" "${MNTPATH}/etc/fstab"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(PARTUUID=\)${ORIGUUID_1}\(\s\+/boot\S*\s\+vfat\s\+.*\)$|\1${PARTUUID_1}\2|" "${MNTPATH}/etc/fstab"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(PARTUUID=\)${ORIGUUID_2}\(\s\+/\s\+.*\)$|\1${PARTUUID_2}\2|" "${MNTPATH}/etc/fstab"
|
||||
umntpart
|
||||
mntpart "${DEVICE_P}1" "BOOT"
|
@ -4,9 +4,21 @@ trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
|
||||
|
||||
MNTPATH="/tmp/usb-boot-mnt"
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${BOOTMNT}/" &> /dev/null
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
exit 1
|
||||
}
|
||||
|
||||
mntdev()
|
||||
{
|
||||
MNTED=TRUE
|
||||
if [ ! -d "${MNTPATH}/" ]; then
|
||||
mkdir "${MNTPATH}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
@ -17,13 +29,14 @@ mntdev()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to mount ROOT partition"
|
||||
fi
|
||||
if [ ! -d "${MNTPATH}/boot/" ]; then
|
||||
mkdir -p "${MNTPATH}/boot/"
|
||||
MNTED=TRUE
|
||||
if [ ! -d "${BOOTMNT}/" ]; then
|
||||
mkdir -p "${BOOTMNT}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to make BOOT partition mount point"
|
||||
fi
|
||||
fi
|
||||
mount "${1}1" "${MNTPATH}/boot/"
|
||||
mount "${1}1" "${BOOTMNT}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to mount BOOT partition"
|
||||
fi
|
||||
@ -31,7 +44,7 @@ mntdev()
|
||||
|
||||
umntdev()
|
||||
{
|
||||
umount "${MNTPATH}/boot/"
|
||||
umount "${BOOTMNT}/"
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to unmount BOOT partition"
|
||||
fi
|
||||
@ -39,21 +52,8 @@ umntdev()
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to unmount ROOT partition"
|
||||
fi
|
||||
rm -r "${MNTPATH}/"
|
||||
MNTED=FALSE
|
||||
}
|
||||
|
||||
errexit()
|
||||
{
|
||||
echo ""
|
||||
echo "$1"
|
||||
echo ""
|
||||
if [ "${MNTED}" = "TRUE" ]; then
|
||||
umount "${MNTPATH}/boot/" &> /dev/null
|
||||
umount "${MNTPATH}/" &> /dev/null
|
||||
rm -rf "${MNTPATH}/" &> /dev/null
|
||||
fi
|
||||
exit 1
|
||||
rm -r "${MNTPATH}/"
|
||||
}
|
||||
|
||||
MNTED=FALSE
|
||||
@ -92,9 +92,15 @@ rsync --version &> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "rsync not installed (run: apt-get install rsync)"
|
||||
fi
|
||||
BOOTMNT="${MNTPATH}$(sed -n 's|^\S\+\s\+\(/boot\S*\)\s\+.*$|\1|p' /etc/fstab)"
|
||||
if [ "${BOOTMNT}" = "${MNTPATH}/boot/firmware" ]; then
|
||||
BOOTSIZE=512
|
||||
else
|
||||
BOOTSIZE=256
|
||||
fi
|
||||
ROOT_PART="$(mount | sed -n 's|^\(/dev/.*\) on / .*|\1|p')"
|
||||
ROOT_DEV="$(sed 's/[0-9]\+$//' <<< "${ROOT_PART}")"
|
||||
if [ "${ROOT_DEV}" = "/dev/mmcblk0p" ]; then
|
||||
if [[ "${ROOT_DEV}" = "/dev/mmcblk0p" || "${ROOT_DEV}" = "/dev/nvme0n1p" ]]; then
|
||||
ROOT_DEV="${ROOT_DEV:0:(${#ROOT_DEV} - 1)}"
|
||||
fi
|
||||
ROOT_TYPE="$(blkid "${ROOT_PART}" | sed -n 's|^.*TYPE="\(\S\+\)".*|\1|p')"
|
||||
@ -103,17 +109,27 @@ if [ -b /dev/mmcblk0 ]; then
|
||||
else
|
||||
USESDC=FALSE
|
||||
fi
|
||||
if [[ $(cat /proc/cpuinfo | grep -c "^Revision\s\+:\s\+[abcd]0311[1245]$") -ne 0 || $(cat /proc/cpuinfo | grep -c "^Revision\s\+:\s\+c03130$") -ne 0 ]]; then
|
||||
RPI_4=TRUE
|
||||
if [ -f "/sys/firmware/devicetree/base/system/linux,revision" ]; then
|
||||
BDINFO="$(od -v -An -t x1 /sys/firmware/devicetree/base/system/linux,revision | tr -d ' \n')"
|
||||
elif grep -q Revision /proc/cpuinfo; then
|
||||
BDINFO="$(sed -n '/^Revision/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo)"
|
||||
elif command -v vcgencmd > /dev/null; then
|
||||
BDINFO="$(vcgencmd otp_dump | grep '30:' | sed 's/.*://')"
|
||||
else
|
||||
RPI_4=FALSE
|
||||
errexit "Raspberry Pi board info not found"
|
||||
fi
|
||||
BDCHIP=$(((0x${BDINFO} >> 12) & 15))
|
||||
if [[ ${BDCHIP} = 3 || ${BDCHIP} = 4 ]]; then
|
||||
RPI_45=TRUE
|
||||
else
|
||||
RPI_45=FALSE
|
||||
fi
|
||||
if [ "$(vcgencmd otp_dump | grep 17:)" = "17:3020000a" ]; then
|
||||
RPI_3=TRUE
|
||||
else
|
||||
RPI_3=FALSE
|
||||
fi
|
||||
if [[ "${USESDC}" = "TRUE" && "${RPI_4}" = "TRUE" ]]; then
|
||||
if [[ "${USESDC}" = "TRUE" && "${RPI_45}" = "TRUE" ]]; then
|
||||
whiptail --backtitle "USB Boot" --title "USB Boot Method" --yesno --defaultno "\nUse SD card to boot the USB device?" 12 40
|
||||
YESNO=$?
|
||||
if [ ${YESNO} -eq 255 ]; then
|
||||
@ -129,10 +145,10 @@ elif [[ "${USESDC}" = "TRUE" && "${RPI_3}" = "TRUE" ]]; then
|
||||
elif [ ${YESNO} -ne 0 ]; then
|
||||
USESDC=FALSE
|
||||
fi
|
||||
elif [[ "${USESDC}" = "FALSE" && "${RPI_4}" = "FALSE" && "${RPI_3}" = "FALSE" ]]; then
|
||||
errexit "Not a Raspberry Pi 4, Raspberry Pi 3B+, or Raspberry Pi 3B with OTP set, and no SD card present or used"
|
||||
elif [[ "${USESDC}" = "FALSE" && "${RPI_45}" = "FALSE" && "${RPI_3}" = "FALSE" ]]; then
|
||||
errexit "Not a Raspberry Pi 5, Raspberry Pi 4, Raspberry Pi 3B+, or Raspberry Pi 3B with OTP set, and no SD card present or used"
|
||||
fi
|
||||
USBDEVS=($(ls -l /dev/sd? 2> /dev/null | sed -n 's|^.*\(/dev/.*\)|\1|p'))
|
||||
USBDEVS=($(ls -l /dev/sd? /dev/nvme0n1 2> /dev/null | sed -n 's|^.*\(/dev/.*\)|\1|p'))
|
||||
if [ ${#USBDEVS[@]} -eq 0 ]; then
|
||||
errexit "No available USB mass storage devices found"
|
||||
fi
|
||||
@ -144,8 +160,12 @@ USB_DEST="$(whiptail --backtitle "USB Boot" --title "USB Mass Storage Devices" -
|
||||
if [[ $? -ne 0 || "${USB_DEST}" = "" ]]; then
|
||||
errexit "Aborted"
|
||||
fi
|
||||
USB_BOOT="${USB_DEST}1"
|
||||
USB_ROOT="${USB_DEST}2"
|
||||
USB_DEST_P=${USB_DEST}
|
||||
if [ "${USB_DEST_P}" = "/dev/nvme0n1" ]; then
|
||||
USB_DEST_P+='p'
|
||||
fi
|
||||
USB_BOOT="${USB_DEST_P}1"
|
||||
USB_ROOT="${USB_DEST_P}2"
|
||||
if [ "${ROOT_DEV}" != "${USB_DEST}" ]; then
|
||||
whiptail --backtitle "USB Boot" --title "Replicate BOOT/ROOT Contents" --yesno "\nReplicate BOOT/ROOT contents from ${ROOT_DEV} to ${USB_DEST}?" 12 64
|
||||
YESNO=$?
|
||||
@ -168,7 +188,7 @@ if [ "${ROOT_DEV}" != "${USB_DEST}" ]; then
|
||||
if [[ $? -ne 0 || "${PTTYPE}" = "" ]]; then
|
||||
errexit "Aborted"
|
||||
fi
|
||||
whiptail --backtitle "USB Boot" --title "WARNING" --yesno "\nWARNING\n\nAll existing data on USB device ${USB_DEST} will be destroyed!\n\nDo you wish to continue?" 14 64
|
||||
whiptail --backtitle "USB Boot" --title "WARNING" --yesno "\nWARNING\n\nAll existing data on USB device ${USB_DEST} will be destroyed!\n\nDo you wish to continue?" 14 68
|
||||
YESNO=$?
|
||||
if [ ${YESNO} -ne 0 ]; then
|
||||
errexit "Aborted"
|
||||
@ -182,18 +202,18 @@ if [ "${ROOT_DEV}" != "${USB_DEST}" ]; then
|
||||
partprobe
|
||||
echo "label: dos" | sfdisk "${USB_DEST}" > /dev/null
|
||||
sfdisk "${USB_DEST}" <<EOF &> /dev/null
|
||||
,256MiB,c
|
||||
,${BOOTSIZE}MiB,c
|
||||
,+,83
|
||||
EOF
|
||||
else
|
||||
sgdisk -Z "${USB_DEST}" &> /dev/null
|
||||
sgdisk -n 1:0:+256M "${USB_DEST}" &> /dev/null
|
||||
sgdisk -n 1:0:+${BOOTSIZE}M "${USB_DEST}" &> /dev/null
|
||||
sgdisk -t 1:0700 "${USB_DEST}" > /dev/null
|
||||
sgdisk -n 2:0:0 "${USB_DEST}" &> /dev/null
|
||||
sgdisk -t 2:8300 "${USB_DEST}" > /dev/null
|
||||
fi
|
||||
partprobe
|
||||
mkfs.vfat -F 32 "${USB_BOOT}" > /dev/null
|
||||
mkfs.vfat -F 32 -n boot -s 4 "${USB_BOOT}" &> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to create BOOT filesystem"
|
||||
fi
|
||||
@ -204,12 +224,12 @@ EOF
|
||||
if [ "${ROOT_TYPE}" = "f2fs" ]; then
|
||||
mkfs.f2fs -f "${USB_ROOT}" > /dev/null
|
||||
else
|
||||
mkfs.ext4 -F -q -b 4096 "${USB_ROOT}" > /dev/null
|
||||
mkfs.ext4 -b 4096 -F -L rootfs -q "${USB_ROOT}" > /dev/null
|
||||
fi
|
||||
if [ $? -ne 0 ]; then
|
||||
errexit "Unable to create ROOT filesystem"
|
||||
fi
|
||||
mntdev "${USB_DEST}"
|
||||
mntdev "${USB_DEST_P}"
|
||||
rsync -aDH --partial --numeric-ids --delete --force --exclude "${MNTPATH}" --exclude '/dev' --exclude '/lost+found' --exclude '/media' --exclude '/mnt' \
|
||||
--exclude '/proc' --exclude '/run' --exclude '/sys' --exclude '/tmp' --exclude '/etc/udev/rules.d/70-persistent-net.rules' \
|
||||
--exclude '/var/lib/asterisk/astdb.sqlite3-journal' "${OPTIONS[@]}" / "${MNTPATH}/"
|
||||
@ -228,7 +248,7 @@ EOF
|
||||
fi
|
||||
if [ "${USESDC}" = "TRUE" ]; then
|
||||
if [ -b /dev/mmcblk0 ]; then
|
||||
while [ "$(blkid /dev/mmcblk0p2 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')" = "$(blkid ${USB_DEST}2 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')" ]
|
||||
while [ "$(blkid /dev/mmcblk0p2 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')" = "$(blkid ${USB_DEST_P}2 | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')" ]
|
||||
do
|
||||
echo ""
|
||||
echo "WARNING : SD card (/dev/mmcblk0) and USB device (${USB_DEST}) have the same PARTUUID : SD card will boot instead of USB device"
|
||||
@ -283,97 +303,28 @@ PARTUUID_2="$(blkid "${USB_ROOT}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
if [ "${USESDC}" = "TRUE" ]; then
|
||||
if [ -b /dev/mmcblk0 ]; then
|
||||
mntdev "/dev/mmcblk0p"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PARTUUID_2}\2|" "${MNTPATH}/boot/cmdline.txt"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PARTUUID_2}\2|" "${BOOTMNT}/cmdline.txt"
|
||||
umntdev
|
||||
else
|
||||
errexit "No SD card present"
|
||||
fi
|
||||
fi
|
||||
mntdev "${USB_DEST}"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PARTUUID_2}\2|" "${MNTPATH}/boot/cmdline.txt"
|
||||
mntdev "${USB_DEST_P}"
|
||||
sed -i "/^[[:space:]]*#/!s|^\(.*root=\)\S\+\(\s\+.*\)$|\1PARTUUID=${PARTUUID_2}\2|" "${BOOTMNT}/cmdline.txt"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/\s\+.*\)$|PARTUUID=${PARTUUID_2}\1|" "${MNTPATH}/etc/fstab"
|
||||
if [ "${USESDC}" = "TRUE" ]; then
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\s\+.*\)$|/dev/mmcblk0p1\1|" "${MNTPATH}/etc/fstab"
|
||||
if [ $(grep -v '^[[:space:]]*#' "${MNTPATH}/etc/rc.local" | grep -c 'usb-boot-init') -eq 0 ]; then
|
||||
cat <<\EOF1 > "${MNTPATH}/etc/usb-boot-init"
|
||||
#!/bin/bash
|
||||
|
||||
MNTBOOT="/tmp/usb-boot-init-mnt"
|
||||
|
||||
REBOOT=FALSE
|
||||
ROOT_PART="$(mount | sed -n 's|^\(/dev/.*\) on / .*|\1|p')"
|
||||
ROOT_DEV="$(sed 's/[0-9]\+$//' <<< "${ROOT_PART}")"
|
||||
if [ "${ROOT_DEV}" = "/dev/mmcblk0p" ]; then
|
||||
ROOT_DEV="${ROOT_DEV:0:(${#ROOT_DEV} - 1)}"
|
||||
fi
|
||||
mkdir -p "${MNTBOOT}/"
|
||||
mount "${ROOT_DEV}1" "${MNTBOOT}/"
|
||||
if [ "$(find "${MNTBOOT}/" -type f -iregex "${MNTBOOT}/ssh\(\.txt\)?" -print -delete)" != "" ]; then
|
||||
update-rc.d ssh enable && invoke-rc.d ssh start
|
||||
fi
|
||||
if [ -f "${MNTBOOT}/wpa_supplicant.conf" ]; then
|
||||
mv "${MNTBOOT}/wpa_supplicant.conf" /etc/wpa_supplicant/wpa_supplicant.conf
|
||||
REBOOT=TRUE
|
||||
fi
|
||||
if [[ $(grep -v '^[[:space:]]*#' "${MNTBOOT}/cmdline.txt" | grep -c 'init=/usr/lib/raspi-config/init_resize.sh') -ne 0 ]]; then
|
||||
sed -i '/^[[:space:]]*#/!s| init=/usr/lib/raspi-config/init_resize\.sh||' "${MNTBOOT}/cmdline.txt"
|
||||
START2=$(sfdisk -d "${ROOT_DEV}" | sed -n "s|^${ROOT_PART}\s\+:.*start=\s*\([0-9]\+\).*$|\1|p")
|
||||
PTTYPE="$(blkid "${ROOT_DEV}" | sed -n 's|^.*PTTYPE="\(\S\+\)".*|\1|p')"
|
||||
if [ "${PTTYPE}" = "dos" ]; then
|
||||
sfdisk --delete "${ROOT_DEV}" 2 > /dev/null
|
||||
echo "${START2},+" | sfdisk --no-reread --no-tell-kernel -N2 "${ROOT_DEV}" &> /dev/null
|
||||
else
|
||||
PARTUUID="$(blkid "${ROOT_PART}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
sgdisk -d 2 "${ROOT_DEV}" > /dev/null
|
||||
sgdisk -n 2:${START2}:0 "${ROOT_DEV}" > /dev/null
|
||||
sgdisk -u 2:${PARTUUID} "${ROOT_DEV}" > /dev/null
|
||||
fi
|
||||
cat <<\EOF2 > /etc/init.d/resize_root_fs
|
||||
#!/bin/sh
|
||||
### BEGIN INIT INFO
|
||||
# Provides: resize_root_fs
|
||||
# Required-Start:
|
||||
# Required-Stop:
|
||||
# Default-Start: 3
|
||||
# Default-Stop:
|
||||
# Short-Description: Resize root filesystem
|
||||
# Description:
|
||||
### END INIT INFO
|
||||
|
||||
case "$1" in
|
||||
start)
|
||||
resize2fs "$(mount | sed -n 's|^\(/dev/.*\) on / .*|\1|p')"
|
||||
update-rc.d resize_root_fs remove
|
||||
rm /etc/init.d/resize_root_fs
|
||||
;;
|
||||
*)
|
||||
exit 3
|
||||
;;
|
||||
esac
|
||||
EOF2
|
||||
chmod +x /etc/init.d/resize_root_fs
|
||||
update-rc.d resize_root_fs defaults
|
||||
REBOOT=TRUE
|
||||
fi
|
||||
umount "${MNTBOOT}/"
|
||||
rm -r "${MNTBOOT}/"
|
||||
sed -i '/usb-boot-init/d' /etc/rc.local
|
||||
rm /etc/usb-boot-init
|
||||
if [ "${REBOOT}" = "TRUE" ]; then
|
||||
shutdown --no-wall -r now
|
||||
fi
|
||||
EOF1
|
||||
chmod +x "${MNTPATH}/etc/usb-boot-init"
|
||||
sed -i 's|^exit 0$|/etc/usb-boot-init\nexit 0|' "${MNTPATH}/etc/rc.local"
|
||||
fi
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\S*\s\+vfat\s\+.*\)$|/dev/mmcblk0p1\1|" "${MNTPATH}/etc/fstab"
|
||||
else
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\s\+.*\)$|PARTUUID=${PARTUUID_1}\1|" "${MNTPATH}/etc/fstab"
|
||||
sed -i "/^[[:space:]]*#/!s|^\S\+\(\s\+/boot\S*\s\+vfat\s\+.*\)$|PARTUUID=${PARTUUID_1}\1|" "${MNTPATH}/etc/fstab"
|
||||
fi
|
||||
umntdev
|
||||
DEV_LIST=()
|
||||
if [ -b /dev/mmcblk0 ]; then
|
||||
DEV_LIST+=/dev/mmcblk0p2
|
||||
fi
|
||||
if [ -b /dev/nvme0n1 ]; then
|
||||
DEV_LIST+=/dev/nvme0n1p2
|
||||
fi
|
||||
DEV_LIST+=($(ls -l /dev/sd?2 2> /dev/null | sed -n 's|^.*\(/dev/.*\)|\1|p'))
|
||||
if [ ${#DEV_LIST[@]} -gt 1 ]; then
|
||||
for i in ${!DEV_LIST[@]}; do
|
||||
@ -381,7 +332,7 @@ if [ ${#DEV_LIST[@]} -gt 1 ]; then
|
||||
j=$((i + 1))
|
||||
while [ ${j} -lt ${#DEV_LIST[@]} ]; do
|
||||
if [ "$(blkid "${DEV_LIST[i]}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')" = "$(blkid "${DEV_LIST[j]}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')" ]; then
|
||||
if [[ "${DEV_LIST[i]}" != "/dev/mmcblk0p2" || "${DEV_LIST[j]}" != "${USB_DEST}2" ]]; then
|
||||
if [[ "${DEV_LIST[i]}" != "/dev/mmcblk0p2" || "${DEV_LIST[j]}" != "${USB_DEST_P}2" ]]; then
|
||||
echo ""
|
||||
echo "WARNING : ${DEV_LIST[i]} and ${DEV_LIST[j]} have the same PARTUUID : $(blkid "${DEV_LIST[i]}" | sed -n 's|^.*PARTUUID="\(\S\+\)".*|\1|p')"
|
||||
fi
|
@ -1,16 +1,14 @@
|
||||
Running Raspbian on USB Devices : Made Easy
|
||||
Running Raspberry Pi OS on USB Devices : Made Easy
|
||||
|
||||
A recurring topic of discussion is how to configure and reliably run Raspbian on a USB flash drive, USB hard drive, or USB SSD instead of an SD card.
|
||||
A recurring topic of discussion is how to configure and reliably run Raspberry Pi OS on a USB flash drive, USB hard drive, or USB SSD instead of an SD card.
|
||||
|
||||
A Raspberry Pi 3B+ has a native USB boot mode (this mode has to be manually enabled by setting an OTP bit on a Raspberry Pi 3B). This native USB boot mode has serious compatibility issues. A bootcode.bin file is available for older Raspberry Pi models. Unfortunately, both of these approaches have serious limitations and once working, can easily be broken by simply plugging in an additional USB storage device.
|
||||
A Raspberry Pi 4 or Raspberry Pi 5 has a native USB boot mode that is reliable and should be used.
|
||||
|
||||
The easiest and most reliable way to run Raspbian on a USB device with any Raspberry Pi is to leave an SD card containing Raspbian in place, but use it only for starting Raspbian that is residing on a USB device. While setting up such a configuration is not rocket science, it can be confusing to a newcomer or someone unfamiliar with Linux internals. In an effort to simplify the task, I've created the attached script named 'usb-boot' to automate the process.
|
||||
A Raspberry Pi 3B+ has a native USB boot mode (this mode has to be manually enabled by setting an OTP bit on a Raspberry Pi 3B). This native USB boot mode has serious compatibility issues. A bootcode.bin file is available for older Raspberry Pi models. Unfortunately, these have serious limitations and once working, can easily become broken. The easiest and most reliable way to run Raspberry Pi OS on a USB device with any Raspberry Pi prior to the model 4 or 5 is to leave an SD card containing Raspberry Pi OS in place, but use it only for starting the Raspberry Pi OS that is residing on a USB device.
|
||||
|
||||
If usb-boot is running on a Raspberry Pi 4, usb-boot first prompts: 'Use SD card to boot the USB device?'
|
||||
If usb-boot is running on a Raspberry Pi 4 or Raspberry Pi 5, usb-boot first prompts: 'Use SD card to boot the USB device?'
|
||||
|
||||
If 'No' is selected, the SD card will not be altered and the direct USB boot capability of the Raspberry Pi 4 will be used.
|
||||
|
||||
[NOTE: The direct USB boot capability of the Raspberry Pi 4 requires an updated bootloader and firmware: https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2711_bootloader_config.md]
|
||||
If 'No' is selected, the SD card will not be altered and the direct USB boot capability of the Raspberry Pi 4 or Raspberry Pi 5 will be used.
|
||||
|
||||
If usb-boot is running on a Raspberry Pi 3B+ or a Raspberry Pi 3B with its OTP bit set, usb-boot first prompts: 'Use SD card to boot the USB device (recommended)?'
|
||||
|
||||
@ -24,9 +22,9 @@ usb-boot will then prompt: 'Replicate BOOT/ROOT contents from /dev/mmcblk0 to /d
|
||||
|
||||
/dev/mmcblk0 is the SD card and /dev/sdX is the USB device.
|
||||
|
||||
Select 'No' if the USB device already has Raspbian on it and you wish to use it (nothing will be copied).
|
||||
Select 'No' if the USB device already has Raspberry Pi OS on it and you wish to use it (nothing will be copied).
|
||||
|
||||
Select 'Yes' if you want to copy the Raspbian on your SD card to the USB device (everything will be copied).
|
||||
Select 'Yes' if you want to copy the Raspberry Pi OS on your SD card to the USB device (everything will be copied).
|
||||
|
||||
If you select 'Yes', usb-boot will then prompt: 'Select the partition table type to use (MBR = 2TB Maximum)'
|
||||
|
||||
@ -36,7 +34,7 @@ If you select 'Yes', the copy will begin. The time required for this process wi
|
||||
|
||||
usb-boot will then complete the configuration process and warn you of any potential conflicts it detects.
|
||||
|
||||
When usb-boot has finished, you should be able to reboot and be running Raspbian on the USB device (first power off and remove the SD card if not using the SD card to boot the USB device).
|
||||
When usb-boot has finished, you should be able to reboot and be running Raspberry Pi OS on the USB device (first power off and remove the SD card if not using the SD card to boot the USB device).
|
||||
|
||||
=====
|
||||
|
||||
@ -59,10 +57,10 @@ If no device is specified, the currently selected boot device will be displayed.
|
||||
GPT partition tables are necessary for devices whose size is over 2TB.
|
||||
|
||||
mbr2gpt converts an MBR partition table on a USB device to a GPT partition table, as well as optionally expanding the ROOT partition and enabling booting via an SD card.
|
||||
|
||||
mbr2gpt converts any size USB device, including SD cards placed in a USB adapter.
|
||||
|
||||
!!! DO NOT PROCEED UNLESS YOU HAVE A RELIABLE BACKUP OF THE DEVICE BEING CONVERTED !!!
|
||||
!!! INITIAL TESTING SHOULD BE PERFORMED ON A USB DEVICE CONTAINING EXPENDABLE DATA !!!
|
||||
|
||||
Usage syntax is:
|
||||
|
||||
|
Reference in New Issue
Block a user