From dc9ced8ed8098883a76608dd31f63762e0ad0473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Tue, 10 Jul 2018 13:11:05 +0200 Subject: [PATCH 1/9] Flexible resizerootfs Catches issue #15 --- raspi3.yaml | 1 + rpi3-resizerootfs | 158 ++-------------------------------------------- 2 files changed, 7 insertions(+), 152 deletions(-) diff --git a/raspi3.yaml b/raspi3.yaml index 42bfb20..29837de 100644 --- a/raspi3.yaml +++ b/raspi3.yaml @@ -66,6 +66,7 @@ steps: - apt: install packages: - ssh + - parted - dosfstools # Contains /lib/firmware/brcm/brcmfmac43430-sdio.bin (required for WiFi). - firmware-brcm80211 diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index af5508d..d7b461d 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -1,155 +1,9 @@ -#!/usr/bin/env perl -# vim:ts=4:sw=4:et -# -# © 2017 Michael Stapelberg -# -# Extends the root partition and root file system to cover the entire remainder -# of the device. Requires the root file system to be a block device matching -# /.+p[0-9]$/, e.g. /dev/mmcblk0p2. -# -# Requires only perl-base (present on all Debian installations) +#!/bin/sh -use strict; -use Fcntl qw(:DEFAULT :seek); +roottmp=$(lsblk -l -o MAJ:MIN,MOUNTPOINT | grep '/$') +rootpart=/dev/block/${roottmp% /} +rootdev=${rootpart%:*}:0 -################################################################################ -# Find the root device by looking at what is mounted on / -################################################################################ +parted -s $rootdev resizepart 2 100% -sub slurp { - my ($fn) = @_; - open(my $fh, '<', $fn) - or die qq|open($fn): $!|; - local $/; - <$fh>; -} - -my ($rootpart) = grep { $_ ne '' } map { /([^ ]+) \/ / && $1 } split("\n", slurp()); -my $rootdev; -my $pno; -if ($rootpart =~ /^(.+)p([0-9])$/) { - $rootdev = $1; - $pno = int($2); -} else { - die qq|root partition "$rootpart" unexpectedly does not end in p[0-9]|; -} - -################################################################################ -# Get the size of the root block device in bytes -################################################################################ - -sub SYS_ioctl { 29; } # aarch64-specific -sub BLKGETSIZE64 { 2148012658; } - -sysopen(my $root, $rootdev, O_RDWR) - or die qq|sysopen($rootdev): $!|; - -my $devsizep = pack("Q", ()); -$! = 0; -syscall(SYS_ioctl(), fileno($root), BLKGETSIZE64(), $devsizep) >= 0 - or die qq|ioctl(BLKGETSIZE64): $!|; -my ($devsize) = unpack("Q", $devsizep); - -################################################################################ -# Read the partition table entry -################################################################################ - -sub boot_code { 446; } -sub partition_table_entry { 16; } -sub partition_table_start_offset { 8; } -sub partition_table_length_offset { 12; } -sub partition_count { 4; } -sub partition_signature_len { 2; } -sub tablelength { partition_table_entry() * partition_count() + partition_signature_len(); } - -# The contents of $original_partition_table must be updated whenever the -# partition layout on the SD card image changes. -# This layout matches -# https://people.debian.org/~stapelberg/raspberrypi3/2017-10-08/2017-10-08-raspberry-pi-3-buster-PREVIEW.img.bz2 -my $original_partition_table = pack('H4' x (tablelength()/2), - "0020", "2100", "0c3e", "1826", "0008", "0000", "0058", "0900", - "003e", "1926", "833a", "2e8c", "0060", "0900", "0000", "1900", - "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000", - "0000", "0000", "0000", "0000", "0000", "0000", "0000", "0000", - "55aa"); - -sub check_orig_partition_table { - my ($offset, $len, $bytes) = @_; - sysseek($root, $offset, SEEK_SET) - or die qq|sysseek($offset): $!|; - my $buf; - sysread($root, $buf, $len) - or die qq|sysread(): $!|; - if ($buf ne $bytes) { - print "Partition table already modified\n"; - exit 0; - } -} - -sub read_uint32_le { - my ($offset) = @_; - sysseek($root, $offset, SEEK_SET) - or die qq|sysseek($offset): $!|; - my $buf; - sysread($root, $buf, 4) - or die qq|sysread(): $!|; - my ($val) = unpack("V", $buf); - return $val; -} - -# Ensure partition table has not been modified by the user. -check_orig_partition_table(boot_code(), tablelength(), $original_partition_table); - -my $entry_offset = boot_code() + (partition_table_entry() * ($pno - 1)); -my $start = 512 * read_uint32_le($entry_offset + partition_table_start_offset()); -my $oldlength = 512 * read_uint32_le($entry_offset + partition_table_length_offset()); -my $newlength = ($devsize - $start); -if ($oldlength == $newlength) { - print "Partition $rootpart already at maximum size $newlength\n"; - exit 0; -} - -################################################################################ -# Change the partition length -################################################################################ - -sub write_uint32_le { - my ($offset, $val) = @_; - sysseek($root, $offset, SEEK_SET) - or die qq|sysseek($offset): $!|; - my $buf = pack("V", $val); - syswrite($root, $buf, 4) - or die qq|syswrite: $!|; -} - -print "Resizing partition $rootpart from $oldlength to $newlength bytes\n"; -write_uint32_le($entry_offset + partition_table_length_offset(), $newlength / 512); - -################################################################################ -# Tell linux about the new partition size using the BLKPG ioctl (BLKRRPART -# cannot be used when the device is mounted, even read-only). -################################################################################ - -sub BLKPG { 0x1269; } -sub BLKPG_RESIZE_PARTITION { 3; } - -my $part = pack("q q i Z64 Z64", $start, $newlength, $pno, "devname", "volname"); -my $partb = "\x00" x length($part); -my $op = BLKPG_RESIZE_PARTITION(); -my $ioctl_arg = pack("i i i x4 p", $op, 0, length($part), $part); -$! = 0; -syscall(SYS_ioctl(), fileno($root), BLKPG(), $ioctl_arg) >= 0 - or die "ioctl(BLKPG): $!"; - -################################################################################ -# Run resize2fs to enlarge the file system -################################################################################ - -# resize2fs requires the file system to be writeable. -my @remountcmd = ('mount', '-o', 'remount,rw', $rootpart); -system(@remountcmd) == 0 - or die qq|system(| . join(" ", @remountcmd) . qq|): $!|; - -my @resizecmd = ('resize2fs', "${rootpart}"); -exec { $resizecmd[0] } @resizecmd - or die qq|exec(| . join(" ", @resizecmd) . qq|): $!|; +resize2fs $rootpart From 6e0b7a4a96a17efc576717384287bd2718c4976f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Tue, 10 Jul 2018 22:42:06 +0200 Subject: [PATCH 2/9] Fix parted invocation --- rpi3-resizerootfs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index d7b461d..e62f699 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -4,6 +4,6 @@ roottmp=$(lsblk -l -o MAJ:MIN,MOUNTPOINT | grep '/$') rootpart=/dev/block/${roottmp% /} rootdev=${rootpart%:*}:0 -parted -s $rootdev resizepart 2 100% +echo 'y\n-1s\n' | parted $rootdev resizepart 2 resize2fs $rootpart From 9adfac696473dabcad0c9b3ae02098c019a01a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Wed, 11 Jul 2018 06:49:42 +0200 Subject: [PATCH 3/9] Fix parted invocation again --- rpi3-resizerootfs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index e62f699..bdba954 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -4,6 +4,6 @@ roottmp=$(lsblk -l -o MAJ:MIN,MOUNTPOINT | grep '/$') rootpart=/dev/block/${roottmp% /} rootdev=${rootpart%:*}:0 -echo 'y\n-1s\n' | parted $rootdev resizepart 2 +printf "y\n-1s\n" | parted $rootdev resizepart 2 resize2fs $rootpart From d4874ba3dfc9b3bd6ab8e10300a31079ca42e89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Sat, 14 Jul 2018 21:57:03 +0200 Subject: [PATCH 4/9] Screw parted, use sfdisk --- rpi3-resizerootfs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index bdba954..32c861c 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -4,6 +4,10 @@ roottmp=$(lsblk -l -o MAJ:MIN,MOUNTPOINT | grep '/$') rootpart=/dev/block/${roottmp% /} rootdev=${rootpart%:*}:0 -printf "y\n-1s\n" | parted $rootdev resizepart 2 +sfdisk -f $rootdev -N 2 < Date: Sun, 15 Jul 2018 00:03:02 +0200 Subject: [PATCH 5/9] Fix resizerootfs --- rpi3-resizerootfs | 14 ++++++++++++-- rpi3-resizerootfs.service | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index 32c861c..37a0b40 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -4,10 +4,20 @@ roottmp=$(lsblk -l -o MAJ:MIN,MOUNTPOINT | grep '/$') rootpart=/dev/block/${roottmp% /} rootdev=${rootpart%:*}:0 -sfdisk -f $rootdev -N 2 < Date: Sun, 15 Jul 2018 11:12:56 +0200 Subject: [PATCH 6/9] Disable resizerootfs service timeout --- rpi3-resizerootfs.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpi3-resizerootfs.service b/rpi3-resizerootfs.service index 131955e..f94d888 100644 --- a/rpi3-resizerootfs.service +++ b/rpi3-resizerootfs.service @@ -5,7 +5,7 @@ DefaultDependencies=no [Service] Type=oneshot -TimeoutSec=600 +TimeoutSec=infinity ExecStart=/usr/sbin/rpi3-resizerootfs ExecStart=/bin/systemctl --no-reload disable %n From 47cc06fe3a70f43fa59c0dd7e99e307d7377d621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Sun, 15 Jul 2018 13:03:32 +0200 Subject: [PATCH 7/9] Move resizerootfs.service further up the chain --- rpi3-resizerootfs.service | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpi3-resizerootfs.service b/rpi3-resizerootfs.service index f94d888..39c9d0c 100644 --- a/rpi3-resizerootfs.service +++ b/rpi3-resizerootfs.service @@ -1,6 +1,6 @@ [Unit] Description=resize root file system -Before=systemd-remount-fs.service +Before=systemd-fsck-root.service DefaultDependencies=no [Service] @@ -10,4 +10,4 @@ ExecStart=/usr/sbin/rpi3-resizerootfs ExecStart=/bin/systemctl --no-reload disable %n [Install] -RequiredBy=systemd-remount-fs.service +RequiredBy=systemd-fsck-root.service From e196f6caec6243d07a16f29136c317937166a099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Mon, 16 Jul 2018 17:12:52 +0200 Subject: [PATCH 8/9] Move rpi3-resizerootfs.service further up the chain --- rpi3-resizerootfs.service | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpi3-resizerootfs.service b/rpi3-resizerootfs.service index 39c9d0c..02207e4 100644 --- a/rpi3-resizerootfs.service +++ b/rpi3-resizerootfs.service @@ -1,6 +1,6 @@ [Unit] Description=resize root file system -Before=systemd-fsck-root.service +Before=local-fs-pre.target DefaultDependencies=no [Service] @@ -10,4 +10,4 @@ ExecStart=/usr/sbin/rpi3-resizerootfs ExecStart=/bin/systemctl --no-reload disable %n [Install] -RequiredBy=systemd-fsck-root.service +RequiredBy=local-fs-pre.target From e918d6d0b6b0b0cdf3ea8d9d9e9305e1a4eaa6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schl=C3=BCter?= Date: Tue, 17 Jul 2018 08:32:30 +0200 Subject: [PATCH 9/9] Fix resizerootfs string manipulation Works with several usb devices plugged in on first boot. --- rpi3-resizerootfs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rpi3-resizerootfs b/rpi3-resizerootfs index 37a0b40..217c20c 100755 --- a/rpi3-resizerootfs +++ b/rpi3-resizerootfs @@ -1,8 +1,9 @@ #!/bin/sh -roottmp=$(lsblk -l -o MAJ:MIN,MOUNTPOINT | grep '/$') -rootpart=/dev/block/${roottmp% /} -rootdev=${rootpart%:*}:0 +roottmp=$(lsblk -l -o NAME,MOUNTPOINT | grep '/$') +rootpart=/dev/${roottmp%% */} +rootdev=${rootpart%2} +rootdev=${rootdev%p} flock $rootdev sfdisk -f $rootdev -N 2 <