-1

I installed raspbian on an sd card and then booted into a pi zero to do my required configuration and setup.

I then made an .img off of this sd card which I'm planning to use in a manufacturing process.

In order to reduce burn time I made it about 3 GB using dd's count option.

Now when I burn the image to a new SD card I want to extend rootfs to the full card length, so I'm running the following commands:

# echo -e "d\n2\nw\n" | fdisk /dev/sdc
# parted -s /dev/sdc unit B mkpart primary 50331648 100%

Where 50331648 was the start of rootfs before deleting it, gotten from parted -m /dev/sdc unit B print.

Then I run

resize2fs -p /dev/sdc

And I get

resize2fs 1.46.2 (28-Feb-2021)
resize2fs: Bad magic number in super-block while trying to open /dev/sdc
Couldn't find valid filesystem superblock.

All my searches led to primarily answers about centOS or non applicable scenarios. The only relevant question I found was that one, which also has no answers..

How can I do this?

php_nub_qq
  • 225
  • 1
  • 9
  • "I made it about 3 GB using dd's count option" - in other words you corrupted your image by deleting essential FS data "Bad magic number in super-block"! – Milliways Jul 08 '22 at 11:29
  • @Milliways I assumed it was safe to snip the end off as the filesystem was about 1.7G in size.. – php_nub_qq Jul 08 '22 at 11:50
  • You have deleted the FS data stored at the end of the partition. There are many ways of making a reduced image but if you are planning to produce your own you should do it properly to make a custom image. – Milliways Jul 08 '22 at 12:01
  • @Milliways well it still appears to run, never knew there is essential data at the end of the partition. Is it possible to restore this lost data, as there is quite a lot of work put into this supposedly now corrupt image? – php_nub_qq Jul 08 '22 at 12:03
  • If you made an iSD Card it should still work - it is just the `dd` that only copied partial data. – Milliways Jul 08 '22 at 12:06
  • @Milliways I'm slightly confused, what do you mean partial data? – php_nub_qq Jul 08 '22 at 12:07
  • @Milliways I just spent a good couple hours to figure out that this is not true. Even on a new install of the now called Raspberry OS (bullseye), running e2fsck still reports broken magic number. I don't know what that's all about but apparently it's not my fault. – php_nub_qq Jul 08 '22 at 19:15

2 Answers2

1

Have you already tried expanding rootfs via

sudo raspi-config

then first update and at last go for Expand Filesystem ?

This is the usual and simplest way of doing so.

Edit: I would suggest https://github.com/Drewsif/PiShrink for shrinking and also resizing if the pi should never be booted to prepare it.

bitdruid
  • 21
  • 2
  • This means I would have to boot the pi and do it on every unit. That is a production step I'm trying to skip by doing what I'm trying to do. – php_nub_qq Jul 08 '22 at 11:24
  • so you just want to run commands? try raspi-config --expand-rootfs // a i got it. you dont want to boot it up... okay. so i'd check out the raspi-config script and transfer the process – bitdruid Jul 08 '22 at 11:26
  • I'm not burning the cards on a raspberry pi, obviously. I could add this in a start-up script in the image but what I found is that `raspi-config --expand-rootfs` not always works and sometimes corrupts the sd card, I can't risk having broken units. – php_nub_qq Jul 08 '22 at 11:27
  • 1
    ok as mentioned maybe check out the raspi-config script (how this specifically is working) and rewrite it in your own script? also you may check out https://github.com/Drewsif/PiShrink for shrinking your pi-images – bitdruid Jul 08 '22 at 11:31
  • I examined `/usr/bin/raspi-config` and it seems to be doing just about what I'm doing. The script `PiShrink` also uses `/usr/bin/env raspi-config --expand-rootfs` in it's expanding function. – php_nub_qq Jul 08 '22 at 11:45
  • Oh well here we go again, another xy problem. I just burned a fresh sd card and without expanding just running `e2fsck` on it reports broken superblock... *Facepalm* – php_nub_qq Jul 08 '22 at 11:49
  • I think you are close to your solution ! Keep on :) – bitdruid Jul 08 '22 at 12:08
  • I think it's just that I can't cut off the image like I thought I could. I'd have to look into how PiShrink is doing it and generate a proper image, then I believe the commands from my question would work. – php_nub_qq Jul 08 '22 at 12:10
  • It actually turned out to be a very stupid mistake... I put it in an answer for anyone who might find it in the future. – php_nub_qq Jul 08 '22 at 19:26
0

Well, as silly as it might be, thanks to this answer I found out that I'm supposed to use resize2fs on the partition, not the device...

So the commands I had to run to resize the partition successfully were as follows:

parted /dev/sdc resizepart 2 100%
e2fsck -f /dev/sdc2
resize2fs /dev/sdc2
php_nub_qq
  • 225
  • 1
  • 9