diff options
Diffstat (limited to 'rpi2-gen-image.sh')
-rwxr-xr-x | rpi2-gen-image.sh | 69 |
1 files changed, 67 insertions, 2 deletions
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 <<EOF > $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} <<EOF2 || true +p +d +\$PART_NUM +n +p +\$PART_NUM +\$PART_START +\$PART_LAST +p +w +EOF2 + # Reload the partition table, resize root filesystem then remove resizing code from this file + partprobe && + resize2fs /dev/\${ROOT_PART} && + logger -t "rc.expandroot" "Root partition successfuly resized." && + sed -i '/.*rc.expandroot/d' /etc/rc.local + rm -f /etc/rc.expandroot +EOF + chmod +x $R/etc/rc.expandroot + sed -i '/exit 0/d' $R/etc/rc.local + echo /etc/rc.expandroot >> $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 |