rootfsinstall: rpi: filesystem resize at first boot and boot partition mount in /boot

This commit is contained in:
Silvan Calarco 2020-10-03 19:55:03 +02:00
parent e8834c3080
commit 77694b96b1
3 changed files with 187 additions and 31 deletions

View File

@ -18,6 +18,7 @@ mambadir := ${datadir}/openmamba
polkitdir := ${datadir}/polkit-1/actions
dbussystemdir := ${sysconfdir}/dbus-1/system.d
localesdir := ${datadir}/locale
libexecdir := /usr/libexec
DESTDIR :=
INSTALL := /usr/bin/install
@ -52,12 +53,14 @@ install-dirs:
@$(INSTALL_DIR) $(DESTDIR)$(mambadir)/usbinstall/img
@$(INSTALL_DIR) $(DESTDIR)$(polkitdir)
@$(INSTALL_DIR) $(DESTDIR)$(dbussystemdir)
@$(INSTALL_DIR) $(DESTDIR)$(libexecdir)
install: install-dirs install-locales
$(INSTALL_PROGRAM) src/usbinstall.py $(DESTDIR)$(mambadir)/usbinstall/usbinstall.py
$(INSTALL_SCRIPT) src/usbinstall.sh $(DESTDIR)$(sbindir)/usbinstall
$(INSTALL_SCRIPT) src/rootfsinstall.sh $(DESTDIR)$(sbindir)/rootfsinstall
$(INSTALL_SCRIPT) src/mmcinstall.sh $(DESTDIR)$(sbindir)/mmcinstall
$(INSTALL_SCRIPT) src/init_resize.sh $(DESTDIR)$(libexecdir)/init_resize
$(INSTALL_DATA) src/img/*.png $(DESTDIR)$(mambadir)/usbinstall/img/
$(INSTALL_DATA) src/org.openmamba.usbinstall.policy $(DESTDIR)$(polkitdir)/org.openmamba.usbinstall.policy
$(INSTALL_DATA) src/org.openmamba.usbinstall.conf $(DESTDIR)$(dbussystemdir)/org.openmamba.usbinstall.conf

164
src/init_resize.sh Executable file
View File

@ -0,0 +1,164 @@
#!/bin/sh
#
# openmamba - automatically grow root partition at first boot
# Based on raspi-config init_resize.sh script
#
reboot_pi () {
umount /boot
mount / -o remount,ro
sync
echo b > /proc/sysrq-trigger
sleep 5
exit 0
}
check_commands () {
for COMMAND in grep cut sed parted fdisk findmnt resize2fs; do
if ! command -v $COMMAND > /dev/null; then
FAIL_REASON="$COMMAND not found"
return 1
fi
done
return 0
}
get_variables () {
ROOT_PART_DEV=$(findmnt / -o source -n)
ROOT_PART_NAME=$(echo "$ROOT_PART_DEV" | cut -d "/" -f 3)
ROOT_DEV_NAME=$(echo /sys/block/*/"${ROOT_PART_NAME}" | cut -d "/" -f 4)
ROOT_DEV="/dev/${ROOT_DEV_NAME}"
ROOT_PART_NUM=$(cat "/sys/block/${ROOT_DEV_NAME}/${ROOT_PART_NAME}/partition")
BOOT_PART_DEV=$(findmnt /boot -o source -n)
BOOT_PART_NAME=$(echo "$BOOT_PART_DEV" | cut -d "/" -f 3)
BOOT_DEV_NAME=$(echo /sys/block/*/"${BOOT_PART_NAME}" | cut -d "/" -f 4)
BOOT_PART_NUM=$(cat "/sys/block/${BOOT_DEV_NAME}/${BOOT_PART_NAME}/partition")
OLD_DISKID=$(fdisk -l "$ROOT_DEV" | sed -n 's/Disk identifier: 0x\([^ ]*\)/\1/p')
ROOT_DEV_SIZE=$(cat "/sys/block/${ROOT_DEV_NAME}/size")
TARGET_END=$((ROOT_DEV_SIZE - 1))
PARTITION_TABLE=$(parted -m "$ROOT_DEV" unit s print | tr -d 's')
LAST_PART_NUM=$(echo "$PARTITION_TABLE" | tail -n 1 | cut -d ":" -f 1)
ROOT_PART_LINE=$(echo "$PARTITION_TABLE" | grep -e "^${ROOT_PART_NUM}:")
ROOT_PART_START=$(echo "$ROOT_PART_LINE" | cut -d ":" -f 2)
ROOT_PART_END=$(echo "$ROOT_PART_LINE" | cut -d ":" -f 3)
}
fix_partuuid() {
mount -o remount,rw "$ROOT_PART_DEV"
mount -o remount,rw "$BOOT_PART_DEV"
DISKID="$(tr -dc 'a-f0-9' < /dev/hwrng | dd bs=1 count=8 2>/dev/null)"
fdisk "$ROOT_DEV" > /dev/null <<EOF
x
i
0x$DISKID
r
w
EOF
if [ "$?" -eq 0 ]; then
sed -i "s/${OLD_DISKID}/${DISKID}/g" /etc/fstab
sed -i "s/${OLD_DISKID}/${DISKID}/" /boot/cmdline.txt
sync
fi
mount -o remount,ro "$ROOT_PART_DEV"
mount -o remount,ro "$BOOT_PART_DEV"
}
check_variables () {
if [ "$BOOT_DEV_NAME" != "$ROOT_DEV_NAME" ]; then
FAIL_REASON="Boot and root partitions are on different devices"
return 1
fi
if [ "$ROOT_PART_NUM" -ne "$LAST_PART_NUM" ]; then
FAIL_REASON="Root partition should be last partition"
return 1
fi
if [ "$ROOT_PART_END" -gt "$TARGET_END" ]; then
FAIL_REASON="Root partition runs past the end of device"
return 1
fi
if [ ! -b "$ROOT_DEV" ] || [ ! -b "$ROOT_PART_DEV" ] || [ ! -b "$BOOT_PART_DEV" ] ; then
FAIL_REASON="Could not determine partitions"
return 1
fi
}
check_kernel () {
MAJOR="$(uname -r | cut -f1 -d.)"
MINOR="$(uname -r | cut -f2 -d.)"
if [ "$MAJOR" -eq "4" ] && [ "$MINOR" -lt "9" ]; then
return 0
fi
if [ "$MAJOR" -lt "4" ]; then
return 0
fi
NEW_KERNEL=1
}
main () {
get_variables
if ! check_variables; then
return 1
fi
check_kernel
if [ "$ROOT_PART_END" -eq "$TARGET_END" ]; then
reboot_pi
fi
if ! parted -m "$ROOT_DEV" u s resizepart "$ROOT_PART_NUM" "$TARGET_END"; then
FAIL_REASON="Root partition resize failed"
return 1
fi
if ! resize2fs "$ROOT_DEV"; then
FAIL_REASON="Root filesystem resize failed"
return 1
fi
fix_partuuid
return 0
}
mount -t proc proc /proc
mount -t sysfs sys /sys
mount -t tmpfs tmp /run
mkdir -p /run/systemd
mount /boot
mount / -o remount,ro
sed -i 's| init=/usr/share/openmamba/bin/init_resize\.sh||' /boot/cmdline.txt
sed -i 's| sdhci\.debug_quirks2=4||' /boot/cmdline.txt
if ! grep -q splash /boot/cmdline.txt; then
sed -i "s/ quiet//g" /boot/cmdline.txt
fi
mount /boot -o remount,ro
sync
echo 1 > /proc/sys/kernel/sysrq
if ! check_commands; then
reboot_pi
fi
if main; then
echo "Resized root filesystem. Rebooting in 5 seconds..."
else
echo "Could not expand filesystem.\n${FAIL_REASON}"
fi
sleep 5
reboot_pi

View File

@ -195,6 +195,7 @@ case $TARGETDEVICE in
chipone_ts grub-efi rtl8723bs_bt rtl8723bs axpd acpi_tables_patch $INSTALLPACKAGES"
REMOVEPACKAGES=""
FSTAB_ROOT_DEVICE="/dev/sda2"
FSTAB_BOOT_DEVICE="/dev/mmcblk0p1"
;;
bpi) ARCH=arm
BOOTFSTYPE=fat32
@ -205,32 +206,22 @@ case $TARGETDEVICE in
INSTALLPACKAGES="kernel-sunxi kernel-sunxi-modules u-boot-sunxi-Bananapi $INSTALLPACKAGES"
REMOVEPACKAGES="kernel-mamba-arm"
FSTAB_ROOT_DEVICE="/dev/mmcblk0p2"
FSTAB_BOOT_DEVICE="/dev/mmcblk0p1"
;;
rpi) ARCH=arm
BOOTFSTYPE=fat32
BOOTFSSIZE=100M
BOOTFSSTART=0
[ "$USEKERNEL" ] || USEKERNEL="zImage-*-rpi"
[ "$USEKERNEL2" ] || USEKERNEL2="zImage-*-rpi-v7"
[ "$USEKERNEL3" ] || USEKERNEL3="zImage-*-rpi-v7l"
KERNELDEST="@BOOT@/kernel.img"
KERNELDEST2="@BOOT@/kernel7.img"
KERNELDEST3="@BOOT@/kernel7l.img"
INSTALLPACKAGES="kernel-rpi kernel-rpi-modules raspberrypi-firmware \
kernel-rpi-v7 kernel-rpi-v7-modules \
kernel-rpi-v7l kernel-rpi-v7l-modules \
raspberrypi-utils raspi-config $INSTALLPACKAGES"
raspberrypi-utils xf86-video-fbturbo $INSTALLPACKAGES"
REMOVEPACKAGES="kernel-mamba-arm"
RPI_FIRMWARE_FILES="bootcode.bin \
fixup_x.dat start_x.elf \
fixup4x.dat start4x.elf \
bcm2708-rpi-b-plus.dtb bcm2708-rpi-b.dtb bcm2708-rpi-cm.dtb \
bcm2708-rpi-zero-w.dtb bcm2708-rpi-zero.dtb \
bcm2709-rpi-2-b.dtb bcm2710-rpi-2-b.dtb \
bcm2710-rpi-3-b-plus.dtb bcm2710-rpi-3-b.dtb \
bcm2710-rpi-cm3.dtb bcm2711-rpi-4-b.dtb \
cmdline.txt config.txt"
FSTAB_ROOT_DEVICE="/dev/mmcblk0p2"
FSTAB_BOOT_DEVICE="/dev/mmcblk0p1"
;;
cubox) ARCH=arm
BOOTFSTYPE=ext3
@ -354,7 +345,6 @@ if [ "${GROWIMAGE}" ]; then
resize2fs ${PARTITION2}
fi
[ "$BOOTFSTYPE" ] && BOOTMP=`mktemp -d -t rootfsinstall.XXXXXXXX`
ROOTMP=`mktemp -d -t rootfsinstall.XXXXXXXX`
[ -d $ROOTMP ] || {
@ -369,6 +359,8 @@ mount $PARTITION2 $ROOTMP || {
trap "[ "$ROOTMP" -a -e $ROOTMP ] && umount $ROOTMP/dev/pts $ROOTMP/dev $ROOTMP/proc $ROOTMP/sys $ROOTMP/run $ROOTMP && rmdir $ROOTMP;[ "$BOOTMP" -a -e $BOOTMP ] && umount $BOOTMP && rmdir $BOOTMP" 0 HUP INT QUIT ABRT KILL TERM
if [ "$BOOTFSTYPE" ]; then
BOOTMP=$ROOTMP/boot
mkdir $BOOTMP
mount $PARTITION1 $BOOTMP || {
echo "ERROR: unable to mount partition $PARTITION1; aborting."
exit 1
@ -554,22 +546,12 @@ _EOF
dd if=$ROOTMP/boot/Bananapi/u-boot-sunxi-with-spl.bin of=${DRIVE} bs=1024 seek=8
:
elif [ "$TARGETDEVICE" = "rpi" ]; then
# fetch and install firmware files if missing
for f in $RPI_FIRMWARE_FILES; do
if [ -e $ROOTMP/boot/$f ]; then
cp $ROOTMP/boot/$f $BOOTMP/$f
elif [ "$f" != "cmdline.txt" -a "$f" != "config.txt" ]; then
echo "ERROR: firmware file $f for rpi is missing in /boot directory; aborting."
exit 1
fi
done
if [ -e $ROOTMP/boot/overlays ]; then
cp -a $ROOTMP/boot/overlays $BOOTMP/
fi
# IMAGE=`ls $ROOTMP/boot/$USEKERNEL | head -n 1`
cp $ROOTMP/boot/$USEKERNEL $KERNELDEST
cp $ROOTMP/boot/$USEKERNEL2 $KERNELDEST2
cp $ROOTMP/boot/$USEKERNEL3 $KERNELDEST3
[ "$USEKERNEL" -a -e $KERNELDEST ] || cp $ROOTMP/boot/$USEKERNEL $KERNELDEST
[ "$USEKERNEL2" -a -e $KERNELDEST2 ] || cp $ROOTMP/boot/$USEKERNEL2 $KERNELDEST2
[ "$USEKERNEL3" -a -e $KERNELDEST3 ] || cp $ROOTMP/boot/$USEKERNEL3 $KERNELDEST3
[ -e /usr/libexec/init_resize ] && cp /usr/libexec/init_resize ${ROOTMP}/usr/libexec/
[ -e ${ROOTMP}/usr/libexec/init_resize ] && CMDLINE_ADD=" init=/usr/libexec/init_resize"
# cmdline.txt : parameters passed to the kernel on boot
[ ! -e $ROOTMP/boot/cmdline.txt ] && \
@ -579,7 +561,8 @@ root=/dev/mmcblk0p2 \
rootfstype=$SYSTEMFSTYPE \
quiet \
rootwait \
splash" > $BOOTMP/cmdline.txt
splash\
$CMDLINE_ADD" > $BOOTMP/cmdline.txt
# Create the kernel configuration file
[ ! -e $ROOTMP/boot/config.txt ] && \
@ -640,6 +623,12 @@ $FSTAB_ROOT_DEVICE / auto defaults 0 0
_EOF
fi
if [ "$FSTAB_BOOT_DEVICE" ]; then
cat >> $ROOTMP/etc/fstab << _EOF
$FSTAB_BOOT_DEVICE / auto defaults 0 2
_EOF
fi
if [ "$USEKERNEL" ]; then
if [ -r $ROOTMP/boot/$USEKERNEL ]; then
cp $ROOTMP/boot/$USEKERNEL $KERNELDEST || {