ResizeRaspberryPiImage

From Qt Wiki
Revision as of 14:28, 17 December 2023 by Michal Klocek (talk | contribs) (add detach step)
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/loop0: 5.2 GiB, 5628755968 bytes, 10993664 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: 0xcd48578f

Device       Boot Start     End Sectors  Size Id Type
/dev/loop0p1       8192   96042   87851 42.9M  c W95 FAT32 (LBA)
/dev/loop0p2      98304 6799359 6701056  3.2G 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 98304 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