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
  • 99
  • 3
  • @Milliways I have edited the question adding the steps I do. Also the problem is even with a base image (tried yesterday). If it isn't completed of what you are asking you have to be more specific because I didn't understand. Thanks – Ripper346 Aug 08 '22 at 09:03
  • I should have asked for fdisk details (seniors moment). It you have copied a partial image (as your dd suggests) it is incomplete and unworkable - missing vitaI FS data stored at the end if the partition . I have never used pishrink but understand that it works on a full image. – Milliways Aug 08 '22 at 09:46
  • I actually use a imaging backup tool which creates minimal or arbitrary sized images but there are many ways of manipulating images. – Milliways Aug 08 '22 at 09:49
  • Ok, but the problem isn't the creation of the image, the image works fine, I am able to flash and boot it with other cards/pis without any problem. The issue I asked for is to autoexpand the file system. Or can you do it with your backup tool? So, what is its name? I am open to change the method as long as it works, but I need a very noob proof method without linux since I won't do the load myself. – Ripper346 Aug 08 '22 at 11:00
  • I continue to not understand what you mean for fdisk details, they are the standard partitions on raspberry os, boot is in FAT32, rootfs is in ext4 and I shrink rootfs to be smaller for dd and for compatibility with other sd cards – Ripper346 Aug 08 '22 at 11:01
  • sudo fdisk -l /dev/mmcblk0 will show details of the original image. You need to use appropriate device if mounted elsewhere. The tool I use use (which is not my code) is in https://raspberrypi.stackexchange.com/a/103991/8697. ddis a poor backup strategy - see https://raspberrypi.stackexchange.com/a/5492/8697 for reasons – Milliways Aug 08 '22 at 11:24
  • Your script doesn't work for new images, the original yes. On a raspberry pi zero is slow as hell to do it. I don't understand even why it made an image of 9.4gb when I used less than 3.5gb, I used sudo /mnt/usb/image-backup --initial /mnt/usb/pi.img,,4500. Even if it allocated 4.5gb more there are almost 2gb more than it should. However, looking at the script, the script does only a backup, if I made it from a 16gb card and I load it to a 32gb (can't try right now), the partition will be of 16gb, which doesn't answer my question. – Ripper346 Aug 08 '22 at 13:34

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
  • 99
  • 3