Skip to content

Instantly share code, notes, and snippets.

@matijs

matijs/README.md

Last active Nov 14, 2020
Embed
What would you like to do?
Resizing a partition and filesystem on a headless Raspberry Pi running Arch Linux using a USB drive

Resizing a partition and filesystem on a headless Raspberry Pi running Arch Linux using a USB drive

disclaimer: this worked for me, your mileage may vary. Your Pi, your responsibility :)

After putting Arch Linux on a 16GB SD card using these instructions, I ended up with about 14GB of free space.

Arch Linux uses one primary partition (/dev/mmcblk0p1) and an extended partition (/dev/mmcblk0p2) containing one logical partition (/dev/mmcblk0p5). The primary partition is the boot partition and the logical partition is the root partition. Rather than adding another primary partition I just wanted to resize the root partition and filesystem.

According to this bugreport parted no longer handles resizing of partitions and gparted needs a graphical environment to run. So I had to come up with something else to resize my partitions.

Booting a Raspberry Pi off a USB thumb drive

Start by putting Arch Linux on a USB thumb drive using the same instructions as above. I did this on a Mac but you could just as well use the Pi itself.

# obviously replace `rdisk3` below with the appropriate (raw) device
$ sudo dd bs=1m if=/path/to/raspberry_pi.img of=/dev/rdisk3

If you haven't already, plug the USB thumb drive into your Pi and find out the name of the device if you haven't already. Mine was /dev/sda. Now edit /boot/cmdline.txt and replace the partition Arch Linux will use as its root partition (/dev/mmcblk0p5) with the one on the USB thumb drive you just created (/dev/sda5).

$ sudo --edit /boot/cmdline.txt

Reboot:

$ sudo shutdown --reboot now

Your Raspberry Pi will still boot from the SD card but because it uses the root partition on the USB thumb drive, the partition on the SD card stays unmounted which allows us to resize it, like so (# comments inline):

$ fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.24.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/mmcblk0: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x417ee54b

Device         Boot     Start       End  Blocks  Id System
/dev/mmcblk0p1           2048    186367   92160   c W95 FAT32 (LBA)
/dev/mmcblk0p2         186368   3667967 1740800   5 Extended
/dev/mmcblk0p5         188416   3667967 1739776  83 Linux

# delete the extended (and therefore the logical) partition

Command (m for help): d
Partition number (1,2,5, default 5): 2

Partition 2 has been deleted.

# create a new extended partition and use up all the space on the SD card

Command (m for help): n

Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (2-4, default 2): 
First sector (186368-31116287, default 186368): 
Last sector, +sectors or +size{K,M,G,T,P} (186368-31116287, default 31116287): 

Created a new partition 2 of type 'Extended' and of size 14.8 GiB.

# create a new logical partition, also using up all the available space in the extended partition

Command (m for help): n

Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l

Adding logical partition 5
First sector (188416-31116287, default 188416): 
Last sector, +sectors or +size{K,M,G,T,P} (188416-31116287, default 31116287): 

Created a new partition 5 of type 'Linux' and of size 14.8 GiB.

# and finally write the partition table back to the disk

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

The partition has now been resized, next resize the filesystem:

$ sudo resize2fs /dev/sda5
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mmcblk0p5 to 15200752 (1k) blocks.
The filesystem on /dev/mmcblk0p5 is now 15200752 blocks long.

Finally, change /boot/cmdline.txt back so the Raspberry Pi uses the newly resized partition on the SD card as the root partition. Voilà.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment