How I resized the file system of my Ubuntu Server 15.10.3 image to utilize the entire microSD card space on my Raspberry Pi 3
After setting up a LEMP web server on Raspberry Pi 3 with an Ubuntu Server 15.10.3 image to host my new WordPress website, I decided to create some swap space to complement the 1GB ram on my Raspberry Pi 3 in running more services.
When I tried to create a 4GB swap file, the fallocate command complained that there was no space left on my Raspberry Pi 3:
sudo fallocate -l 4G /swapfile fallocate: fallocate failed: No space left on device
To ensure that I wouldn't crash my Raspbery Pi 3, I quickly remove /swapfile
.
To understand why there was no space to create a 4GB swap file on a Raspberry Pi 3 with a 32GB microSD card, I ran:
df -h
and got the following output:
Filesystem Size Used Avail Use% Mounted on /dev/root 3.5G 1.5G 2.0G 43% / devtmpfs 459M 0 459M 0% /dev tmpfs 463M 0 463M 0% /dev/shm tmpfs 463M 6.2M 457M 2% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 463M 0 463M 0% /sys/fs/cgroup /dev/mmcblk0p1 64M 20M 45M 32% /boot
This told me that my Ubuntu Server 15.10.3 image did not utilize the entire microSD card space from the SanDisk Ultra 32GB microSDHC card on my Raspberry Pi 3 motherboard.
Using GNU parted to resize the file system of my Ubuntu Server 15.10.3 image to utilize the entire microSD card space
It wouldn't make economical sense when my Raspberry Pi 3 was not using the whole of my 32GB microSD card. Since my Ubuntu Server 15.10.3 image had GNU parted installed, I turned to GNU parted to resize the file system of my Ubuntu Server 15.10.3 image to utilize the entire microSD card space:
sudo parted
Executing the command presented me the following environment, which allowed me to interact with GNU parted.
GNU Parted 3.2 Using /dev/mmcblk0 Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END resizepart NUMBER END resize partition NUMBER rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted
Viewing the partition table on my microSD card
I first checked the entries inside the partition table on my microSD card:
(parted) print
Through the 'print' command, I was presented with the following output:
Model: SD SL32G (sd/mmc) Disk /dev/mmcblk0: 31.1GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 68.2MB 67.1MB primary fat16 boot, lba 2 68.2MB 3932MB 3864MB primary ext4
This output told me that I was only utilizing 3864MB of my microSD card for my Ubuntu Server 15.10.3.
Resizing the ext4 partition
Since the ext4 partition contains the Ubuntu Server 15.10.3 image, resizing that partition will allow my Ubuntu Server 15.10.3 to utilize the entire microSD card disk space.
To do so, I supplied the following input:
(parted) resizepart 2 End? [3932MB]? 31.1GB
I first supplied the command 'resizepart 2' to tell GNU parted that I wished to resize the ext4 partition. It then replied me with 'End? [3932MB]?' which I followed up with the string '31.1GB' to indicate that I want it to use the rest of the unallocated space on my microSD card.
Verifying that my Ubuntu Server 15.10.3 was able to utilize the rest of the unallocated space on my microSD card
To verify that the ext4 partition had successfully resized, I ran the 'print' command again:
(parted) print
which gave me the following output:
Model: SD SL32G (sd/mmc) Disk /dev/mmcblk0: 31.1GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 68.2MB 67.1MB primary fat16 boot, lba 2 68.2MB 31.1GB 31.0GB primary ext4
Once I saw that the partition had taken more space, I exited the interaction screen with the parted program by typing 'quit'.
Getting the Ubuntu Server 15.10.3 file system to recognize the partition resize
Once I am out of the parted interaction screen, I ran the following to see if my root directory contains more space:
df -h
To my dismay, I still saw the same output as before:
Filesystem Size Used Avail Use% Mounted on /dev/root 3.5G 1.5G 2.0G 43% / devtmpfs 459M 0 459M 0% /dev tmpfs 463M 0 463M 0% /dev/shm tmpfs 463M 6.2M 457M 2% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 463M 0 463M 0% /sys/fs/cgroup /dev/mmcblk0p1 64M 20M 45M 32% /boot
This told me that GNU parted had not resized the file system to take the additional partition space that had been created.
To get the file system to take the additional partition space that had been created, I ran the following:
sudo resize2fs /dev/mmcblk0p2
After the command had completed, I ran
df -h
and got the following output:
Filesystem Size Used Avail Use% Mounted on /dev/root 29G 1.5G 27G 6% / devtmpfs 459M 0 459M 0% /dev tmpfs 463M 0 463M 0% /dev/shm tmpfs 463M 6.2M 457M 2% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 463M 0 463M 0% /sys/fs/cgroup /dev/mmcblk0p1 64M 20M 45M 32% /boot
With that, I was able to continue with the creation of the swapfile to complement the 1GB ram on my Raspberry Pi 3.