From 33668aa2768a52add75b575263ec5dc5cec7d145 2016-03-06 08:25:53 From: Filip Pytloun Date: 2016-03-06 08:25:53 Subject: [PATCH] Option to expand partition and rootfs on first boot Initial version from vknecht --- diff --git a/README.md b/README.md index 998c534..9afe18b 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,9 @@ Set default system locale. This setting can also be changed inside the running O ##### `TIMEZONE`="Europe/Berlin" Set default system timezone. All available timezones can be found in the `/usr/share/zoneinfo/` directory. This setting can also be changed inside the running OS using the `dpkg-reconfigure tzdata` command. +##### `EXPANDROOT`=true +Expand the root partition and filesystem automatically on first boot. + #### Keyboard settings: These options are used to configure keyboard layout in `/etc/default/keyboard` for console and Xorg. These settings can also be changed inside the running OS using the `dpkg-reconfigure keyboard-configuration` command. ##### `XKBMODEL`="" diff --git a/rpi2-gen-image.sh b/rpi2-gen-image.sh index 4a2ba7d..391c870 100755 --- a/rpi2-gen-image.sh +++ b/rpi2-gen-image.sh @@ -55,6 +55,7 @@ XKBMODEL=${XKBMODEL:=""} XKBLAYOUT=${XKBLAYOUT:=""} XKBVARIANT=${XKBVARIANT:=""} XKBOPTIONS=${XKBOPTIONS:=""} +EXPANDROOT=${EXPANDROOT:=true} # Network settings ENABLE_DHCP=${ENABLE_DHCP:=true} @@ -158,6 +159,11 @@ else APT_INCLUDES="${APT_INCLUDES},locales,keyboard-configuration,console-setup" fi +# Add parted package, required to get partprobe utility +if [ "$EXPANDROOT" = true ] ; then + APT_INCLUDES="${APT_INCLUDES},parted" +fi + # Add dbus package, recommended if using systemd if [ "$ENABLE_DBUS" = true ] ; then APT_INCLUDES="${APT_INCLUDES},dbus" @@ -602,14 +608,73 @@ ssh-keygen -q -t ed25519 -N "" -f /etc/ssh/ssh_host_ed25519_key sync systemctl restart sshd -sed -i 's/.*rc.firstboot.*/exit 0/g' /etc/rc.local +sed -i '/.*rc.firstboot/d' /etc/rc.local rm -f /etc/rc.firstboot EOM chmod +x $R/etc/rc.firstboot - sed -i 's,exit 0,/etc/rc.firstboot,g' $R/etc/rc.local + sed -i '/exit 0/d' $R/etc/rc.local + echo /etc/rc.firstboot >> $R/etc/rc.local rm -f $R/etc/ssh/ssh_host_* fi +if [ "$EXPANDROOT" = true ] ; then + cat < $R/etc/rc.expandroot +#!/bin/sh + +ROOT_PART=\$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') +PART_NUM=\$(echo \${ROOT_PART} | grep -o '[1-9][0-9]*$') +case "\${ROOT_PART}" in + mmcblk0*) ROOT_DEV=mmcblk0 ;; + sda*) ROOT_DEV=sda ;; +esac +if [ "\$PART_NUM" = "\$ROOT_PART" ]; then + logger -t "rc.expandroot" "\$ROOT_PART is not an SD card. Don't know how to expand" + return 0 +fi +# NOTE: the NOOBS partition layout confuses parted. For now, let's only +# agree to work with a sufficiently simple partition layout +if [ "\$PART_NUM" -gt 2 ]; then + logger -t "rc.expandroot" "Your partition layout is not currently supported by this tool." + return 0 +fi +LAST_PART_NUM=\$(parted /dev/\${ROOT_DEV} -ms unit s p | tail -n 1 | cut -f 1 -d:) +if [ \$LAST_PART_NUM -ne \$PART_NUM ]; then + logger -t "rc.expandroot" "\$ROOT_PART is not the last partition. Don't know how to expand" + return 0 +fi +# Get the starting offset of the root partition +PART_START=\$(parted /dev/\${ROOT_DEV} -ms unit s p | grep "^\${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g') +[ "\$PART_START" ] || return 1 +# Get the possible last sector for the root partition +PART_LAST=\$(fdisk -l /dev/\${ROOT_DEV} | grep '^Disk.*sectors' | awk '{ print \$7 - 1 }') +[ "\$PART_LAST" ] || return 1 +# Return value will likely be error for fdisk as it fails to reload the +# partition table because the root fs is mounted +### Since rc.local is run with "sh -e", let's add "|| true" to prevent premature exit +fdisk /dev/\${ROOT_DEV} <> $R/etc/rc.local +fi + # Disable rsyslog if [ "$ENABLE_RSYSLOG" = false ]; then sed -i 's|[#]*ForwardToSyslog=yes|ForwardToSyslog=no|g' $R/etc/systemd/journald.conf