A short and sweet guide to resizing your Raspberry Pi SDCard. When I first fired up Pihole it was a simple proof of concept which quickly took over as a critical component in my home (office) network.
Since I was just testing initially I used an old SDCard that has been around for a long time. I wanted this instance of Pihole to become permanent, with one small caveat, convert it to a larger, better quality SDCard.
Steps to resize your SDCard.
-
Schedule a change window with your kids/partner/whoever and shutdown your Raspberry Pi.
-
Remove the SDCard and insert the into your Mac or Linux computer. (Sorry Windows users, I know nothing about CLI for M$.)
-
Run dd command to clone the SDCard to your computer.
sudo dd if=/dev/disk3 of=/Users/entropy/pihole.bin
**Note: This will take some time.**
When it's complete it will output the following (record counts will vary):
15523840+0 records in
15523840+0 records out
7948206080 bytes transferred in 1540.923594 secs (5158079 bytes/sec)
Great, you now have a backed up image of your entire pihole SDCard.
Remove the original SDCard from your computer and set aside.
- Insert your new (better quality and larger) SDCard into your computer.
Run the "mount" command noting the new SDCard disk name, then using "diskutil unmountDisk /dev/disk3" as it must not be mounted to copy the image. Verify your actual mounted disk name, in my case the SDCard mounted as /dev/disk3.
- You don't need to format the new or used SDCard, the dd command will write the image and format according to the input which is our original pihole image.
sudo dd if=/dev/disk3 of=/Users/entropy/pihole.bin
Caution, double check your SDCard mount position, disk3 in this case was my own experience.
This can take a significant amount of time depending on the speed of your computer and size of the new SDCard.
When complete it will report the following:
entropy@mbp15 ~ % sudo dd if=/Users/entropy/pihole.bin of=/dev/disk3
10817065+0 records in
10817064+0 records out
5538336768 bytes transferred in 29715.792117 secs (186377 bytes/sec)
Ensure the disk is not mounted and remove from the computer.
-
Insert into your Raspberry Pi, it should boot the same as before if all was successful with the image.
-
Now that your RPI is up and running again it's time to resize the partition and expand the primary file system.
Using the mount command, locate your SDCard disk name.
entropy@rpi01:~ $ mount
/dev/mmcblk0p2 on / type ext4 (rw,noatime)
- Run fdisk to start the resizing process:
fdisk /dev/mmcblk0
(note the "p2" is removed, we're working on the disk, not a single partition.
- In fdisk, type "p" to print the partition table.
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 532479 524288 256M c W95 FAT32 (LBA)
/dev/mmcblk0p2 532480 15523839 14991360 7.2G 83 Linux
Note the START sector of your Linux partition, in this case, 532480.
-
Press "d" to delete a partition, follow the prompts to delete the Linux partition (2 in this example).
-
Press "n" to add a new partition, follow the prompts and enter the START block per your original, in my example 532480. The system will automatically use an end block to the maximum size. You could size the partition accordingly if you do not want to use the whole SDCard.
-
Press "w" to write the partition table and reboot the Raspberry Pi for the changes to take effect.
-
When you log back in you'll notice the Raspberry Pi still has the same old partition sizes even though we've added a larger SDCard and expanded the actual partition. In the example below, it's still 7.1G, the same from the original 8GB SDCard.
entropy@rpi01:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 7.1G 1.4G 5.4G 21% /
devtmpfs 184M 0 184M 0% /dev
tmpfs 217M 0 217M 0% /dev/shm
tmpfs 217M 3.1M 214M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 217M 0 217M 0% /sys/fs/cgroup
/dev/mmcblk0p1 253M 55M 198M 22% /boot
- Run the following command to expand the primary partition:
entropy@rpi01:~ $ sudo resize2fs /dev/mmcblk0p2
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p2 is now 3723264 (4k) blocks long.
- When resize is complete (should only take a few minutes) verify that your primary partition is now increased. In this example, it's now 14G which with is intended with the 16GB SDCard used.
entropy@rpi01:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 14G 1.6G 12G 12% /
devtmpfs 184M 0 184M 0% /dev
tmpfs 217M 0 217M 0% /dev/shm
tmpfs 217M 3.1M 214M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 217M 0 217M 0% /sys/fs/cgroup
/dev/mmcblk0p1 253M 55M 198M 22% /boot
All done, no other restart of services or reboot required after it's complete. If something goes wrong during the process you still have the original SDCard and the backup image of your RPI so it's a relatively low risk process overall.