ResizeRaspberryPiImage

From Qt Wiki
Jump to navigation Jump to search

How to increase RaspberryPi OS initial image size ?

If you download raspberry-pi-os image you might want to increase the initial size to able to add or modify the content of the image.

  • Let's now download some image:
wget https://downloads.raspberrypi.com/raspios_arm64/images/raspios_arm64-2023-12-06/2023-12-05-raspios-bookworm-arm64.img.xz
  • Let's extract the image:
unxz --keep 2023-12-05-raspios-bookworm-arm64.img.xz
  • Lets add at least 2G space to the image:
dd if=/dev/zero bs=1M count=2048 >> 2023-12-05-raspios-bookworm-arm64.img
  • Make loop device for the image:
losetup -fP  2023-12-05-raspios-bookworm-arm64.img
losetup -a
/dev/loop0: []: (~/my/downloaded/image/2023-12-05-raspios-bookworm-arm64.img)
  • Check the partition layout and note the start sector of the root partition (here /dev/loop0p2):
fdisk -u -l /dev/loop0

Disk /dev/loop1: 7.44 GiB, 7985954816 bytes, 15597568 sectors
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: 0x870ea24b

Device       Boot   Start      End  Sectors  Size Id Type
/dev/loop0p1         8192  1056767  1048576  512M  c W95 FAT32 (LBA)
/dev/loop0p2      1056768 11403263 10346496  4.9G 83 Linux
  • Recreate second partition to use extra space:
fdisk /dev/loop0

# Type d to proceed to delete a partition.
# Type 2 to select the 2nd partition and press enter.
# Type n to add the partition
# Type p to select primary partition
# Type 2 to select partition number
# Type 1056768 as first sector
# Type Enter to select default last sector
# Type N to keep the signature
# Type w to save partition table and exit
  • Finally resize partition:
e2fsck -f /dev/loop0p2
resize2fs /dev/loop0p2
  • Detach loop device:
losetup -d /dev/loop0