0

I am using Raspberry OS for a batch of zeros, I wanted to prepare the OS one time and then flash the prepared image for the others. After flashing the OS I installed some useful things for later, I inserted the SD in the computer, I resized to the minimum size the rootfs partition with gparted and I created an img file. To re-expand the partition I readded into /boot/cmdline.txt the init=/usr/lib/raspi-config/init_resize.sh, when I boot the raspberry but nothing happens, the partition is always little and the string in cmdline is removed like it should after the operation. How can I auto-expand the partition at the boot? I am using latest Raspberry OS, April '22 release. All I found is a bunch of answers really old that I don't think they fit anymore and to add that line as I described.

To create the custom image I am doing sudo dd if=/dev/sdb of=pi.img bs=1M count=4500 and then use a script I found pishrink. However the method doesn't work even if I flash the base image, I start the raspberry, I resize back the image, I add the line and plug in it again.

Since it was asked, this is the SD card before shrinking:

Disk /dev/sdb: 14,84 GiB, 15931539456 bytes, 31116288 sectors
Disk model: Transcend       
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x1a66848a

Device Boot Start End Sectors Size Id Type /dev/sdb1 8192 532479 524288 256M c W95 FAT32 (LBA) /dev/sdb2 532480 31115263 30582784 14,6G 83 Linux

Ripper346
  • 109
  • 4

1 Answers1

0

I came up with a solution myself. Does it work with any OS? Not sure, I only tested it on Raspberry OS build 2022-04-04 with a Zero w. All the lines are necessary? Again, not sure, after a lot of tries I came up myself with the fdisk command and resize2fs, but for example the mounting of proc, sys and run I copied them from the OS script init_resize.sh, maybe it is only necessary proc that I saw it was fundamental to do something with resize2fs, if anyone wants to clean the code is welcome.

This code require to append in /boot/cmdline.txt init=/home/pi/resize.sh and create the script with this code in /home/pi/resize.sh.

What it does? First we remove from cmdline.txt the string to launch as we want it executed only one time. Then we delete and recreate the rootfs partition in the partition table at max of the capacity of the sd card with fdisk, align the partition with the partition table with resize2fs, remove the script and reboot.

#!/bin/sh

mount /dev/mmcblk0p1 /boot sed -i 's| init=/home/pi/resize.sh||' /boot/cmdline.txt

mount /dev/mmcblk0p1 /boot -o remount,ro sync

INFO=fdisk -l /dev/mmcblk0 END1=$((echo $INFO | sed -nE 's|^.+/dev/mmcblk0p1 [0-9]+ +([0-9]+).*$|\1|p' + 1)) SECTORS=$((echo $INFO | sed -nE 's|^.*Disk /dev/mmcblk0.+ ([0-9]+) sectors.*$|\1|p' - 1))

fdisk /dev/mmcblk0 <<EOF d 2 n p 2 $END1 $SECTORS w EOF

sync

mount -t proc proc /proc mount -t sysfs sys /sys mount -t tmpfs tmp /run mount /dev/mmcblk0p1 /boot -o remount,ro mount /dev/mmcblk0p2 / -o remount,rw

resize2fs -fp /dev/mmcblk0p2 echo done

rm /home/pi/resize.sh sleep 5

reboot -f

Ripper346
  • 109
  • 4