RPi-Distro / pi-gen

Tool used to create the official Raspberry Pi OS images
BSD 3-Clause "New" or "Revised" License
2.57k stars 1.61k forks source link

Image export from stage1 fails - No space left on device #784

Open DanyloKD opened 1 month ago

DanyloKD commented 1 month ago

Problem description

I'm trying to create a truly minimal system, so after reading documentation I decided to skip stages 2-5. Unfortunately, build process terminates with error during export-image step with the following error: E: Failed to fetch http://raspbian.raspberrypi.com/raspbian/dists/bookworm/main/binary-armhf/Packages Write error - write (28: No space left on device)

After closer investigation of code I've found that too small image file (IMG_SIZE) is allocated in export-image/prerun.sh. It appears that step export-image/02-set-sources installs additional packages into mounted image and fails. https://github.com/RPi-Distro/pi-gen/blob/dca93d24c7dfb943d84601d2bc4a089f9b66b87f/export-image/02-set-sources/01-run.sh#L7

I was able to fix build by increasing coefficient used for calculation of ROOT_MARGIN in export-image/prerun.sh: https://github.com/RPi-Distro/pi-gen/blob/dca93d24c7dfb943d84601d2bc4a089f9b66b87f/export-image/prerun.sh#L21

Steps to reproduce

Please, use following commands to reproduce the issue.

git clone https://github.com/RPI-Distro/pi-gen.git

cd pi-gen
echo "IMG_NAME='custom'" > config

touch ./stage2/SKIP ./stage3/SKIP ./stage4/SKIP ./stage5/SKIP
touch ./stage2/SKIP_IMAGES ./stage3/SKIP_IMAGES ./stage4/SKIP_IMAGES ./stage5/SKIP_IMAGES
touch ./stage1/EXPORT_IMAGE

sudo ./build.sh

Proposed Solution

Export stage shouldn't install new packages according to this comment: https://github.com/RPi-Distro/pi-gen/issues/620#issuecomment-1178852847. But I'm not sure if it will be easy to remove install command, because some people may already rely on this "side effect" to build their own images. Also, in my opinion, increase of 0.2 coefficient will rather hide than than solve a problem.

I think it would be great to defer creation of image file to the later stage after all packages are installed. In other words, I propose to add support for a postrun.sh script and allocate image after all changes to the file system are done.

The only challenge I foresee is export-image/04-set-partuuid/00-run.sh which requires image blob being allocated and partitioned: https://github.com/RPi-Distro/pi-gen/blob/dca93d24c7dfb943d84601d2bc4a089f9b66b87f/export-image/04-set-partuuid/00-run.sh#L5C1-L5C6 But I'm happy to dedicate some time to search of alternative implementation.

Please, let me know what are your thoughts. I will be happy to prepare a PR once we reach an agreement on solution.

XECDesign commented 1 month ago

I plan to alter the behaviour to create a sparse image of a pre-defined size to start with (in stage 0), install everything there and then shrink it down to minimum size + a bit of padding.

You're right, the coefficient is just an ugly workaround and we shouldn't be installing packages in the export stage either.

Sorry, it will take me a while to get around to implementing these changes or testing a PR, so I don't think it's wroth sending one right now. I'd feel guilty leaving it to hang for months. There are some other major changes that I need to make to pi-gen, so I'll do these at the same time.

Many thanks