HowTo: Resize your Raspberry Pi SDCard

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.

  1. Schedule a change window with your kids/partner/whoever and shutdown your Raspberry Pi.

  2. Remove the SDCard and insert the into your Mac or Linux computer. (Sorry Windows users, I know nothing about CLI for M$.)

  3. 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.

  1. 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.

  1. 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.

  1. Insert into your Raspberry Pi, it should boot the same as before if all was successful with the image.

  2. 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)
  1. 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.

  1. 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.

  1. Press "d" to delete a partition, follow the prompts to delete the Linux partition (2 in this example).

  2. 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.

  3. Press "w" to write the partition table and reboot the Raspberry Pi for the changes to take effect.

  4. 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
  1. 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.
  1. 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.

You can't actually resize an SD card.

Also, I fear there is a more hassle-free way of expanding your filesystem and partition to make use of the entire available space on the SD card when using RaspberryPi OS.
I'd recommend to use

sudo raspi-config

Advanced Options | Expand Filesystem.

EDIT: It doesn't require 15 manual steps, and it's also faster, just requring a reboot. :wink:

Above ones do the exact same thing, write the block device disk3 to an image file pihole.bin.
To list block devices with their partitions:

pi@ph5:~ $ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0     179:0    0 14.9G  0 disk
├─mmcblk0p1 179:1    0  256M  0 part /boot
└─mmcblk0p2 179:2    0 14.6G  0 part /

Add a sync for safety before you pull SD cards from the system:

pi@ph5:~ $ man sync
[..]
DESCRIPTION
       Synchronize cached writes to persistent storage

Alternatively:

pi@ph5:~ $ findmnt /
TARGET SOURCE         FSTYPE OPTIONS
/      /dev/mmcblk0p2 ext4   rw,noatime

Alternatively:

pi@ph5:~ $ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  1.6G   13G  12% /

Things are a bit confusing.
You should distinct more clearly from which host those commands are run, is the rpi01 host the one getting its filesystem expanded ?
Like for example how are you even able to boot if you deleted the EXT4 partition that contains the root filesystem with the df and resize2fs commands ?
Why do you even delete the old partition to create a new empty bigger one ?
You should use parted to resize a partition that contains data before resizing the filesystem with resize2fs:

pi@ph5:~ $ sudo parted -l /dev/mmcblk0
Model: SD SE16G (sd/mmc)
Disk /dev/mmcblk0: 15.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  273MB   268MB   primary  fat32        lba
 2      273MB   15.9GB  15.7GB  primary  ext4

pi@ph5:~ $ parted -h
Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE.  If no COMMAND(s) are given, run in
interactive mode.

OPTIONs:
  -h, --help                      displays this help message
  -l, --list                      lists partition layout on all block devices
  -m, --machine                   displays machine parseable output
  -s, --script                    never prompts for user intervention
  -v, --version                   displays the version
  -a, --align=[none|cyl|min|opt]  alignment for new partitions

COMMANDs:
  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

Report bugs to bug-parted@gnu.org
1 Like

I'm using Ubuntu, and this is cloning the SDCard then moving to a larger SDCard, then expand the file system. There is no raspi-config option for the RPI Ubuntu distro that I'm aware of.

You might have missed this part.. "Insert your new (better quality and larger)" SDCard. That's the reason I called this process a resize. Larger SDCard and primary partition.

Thanks @deHakkelaar, I will clean up my writing and add your recommendations. I'm not a technical writer and English isn't my first language so it might not be as clear as I would have liked.

1 Like

Well I'm new to all this so excuse my ignorance but this sort of thread frightens me enough to put me off trying anything like this for life!

Couldn't you have just used the built-in SD Card Copier to achieve the same thing or have I missed something fundamental here?

My Raspberry Pi 4 Model B 4GB was recently purchased with a Panasonic 16GB NOOBS microSD Card which very quickly went to 75% full so I bought a SanDisk 32GB Extreme microSD Card for its additional speed and extra capacity.

I would have gone even larger to 64GB but had read posts on possible issues using cards larger than 32GB with regard to FAT32/filesystem/whatever and needed a simple solution due to my inexperience with all things Pi.

I plugged it into a USB->microSD card adaptor and put that into one of the USB ports on the Pi.

Using RDP as it is a headless system I selected Menu/Accessories/SD Card Copier, chose the Internal SD Card (/dev/mmcblk0) as source, the Generic SD/MMC (/dev/sda) as destination, hit start to clone the card with its 5 partitions and job done!

Shut the Pi down swapped the original 16GB for the 32GB and rebooted.

Boom...doubled the capacity (now only 25% full) and got a noticeably quicker machine in the process!

What have I missed here as this seems far too simple a solution to achieve the same thing?

-=Glyn=-

Maybe your approach also works just fine with deleting the partition and creating a larger one on the same spot.
But its just so counter intuitive :wink:
And me being used to using parted or gparted to resize or move partitions around.

Most of us dont have a desktop for obvious reasons.
Plus using bash allows to automate more easily when scripting.

Forgive me for assuming that you are using Raspberry Pi OS.
Your post has quite a few explicit references to Raspberry Pi, but you never mentioned Ubuntu until now.

It would certainly help to know which Ubuntu distro and version you are using, e.g. Ubuntu MATE indeed comes with raspi-config since version 16.04 (though I think they are planning to replace it in future versions).

I am aware of what you are trying to achieve, but you simply can't resize an SD card.
It's capacity is defined by hardware. You can only adjust your partition and file system sizes accordingly, and you are trying to expand those to make full use of your new sd card's capacity.
A resize goes both way, grow or shrink, so your current title may also attract those users who want to shrink their partitions.

I think you could find a title for you topic that better reflects what you are trying to help with, e.g. "Moving to a larger SD card" or similar. (As a side note, this is more a generic issue, not specifcially related to Pi-hole.)

That said: In absence of raspi-config, I would actually suggest a slightly different approach once you've copied your image to your computer.

After you've finished your step 3, you could then use pishrink to actually shrink that image.
This has two immediate benefits: It will drastically reduce the image size on your computer's disk, and this in turn will speed up writing that image file to your new sd card. (I do this routinely upon creating a backup image of Pi-hole when updating, for both the old and the freshly upgraded new version).

Another benefit is that such a pishrinked image will by default automatically expand the root partition and filesystem to make use of the entire available space on first boot.

1 Like