Home
Collections
Build Your Own
Catalog
How To Videos
Quick Reference
View Order Pad

About Us
Contact Us
FAQ
Linux News
Blog


 
 
Acceptance Mark
Amazon Pay





 

The LinuxCollections.com Blog

 

LinuxCollections.com's Blog

Raspberry Pi: Reducing file system to fit on smaller SD Card
written December 22, 2021 by TimeTraveler
Category: BlogEntry Tags: Debian; Raspberry Pi; Raspbian; resize2fs    #41
 
For various projects, we have done some things on a Raspberry Pi. There are now multiple "flavors" of OSes for the RPi, but this example uses raspbian, a customized version of Debian for the Pi.

These are just loose notes on resizing an SD card to a smaller size for a Raspberry Pi. If moving from a smaller card to a larger card, you just need to manage the partitions - you may still want to use resize2fs, but you don't have to worry about size, like you do if shrinking the file system. There are various options, and your best bet is to review Raspberry Pi Forums, as you may find something more relevant to what your actual situation is. However, there were several commands that are interesting, and documenting the general approach and indicating how to use these commands may be useful for future reference.

As a quick reference, if only enlarging, there are just a few key steps.

  1. Use fdisk to enlarge the partition. Review the exact layout to ensure the correct starting sector/location. Because you need to delete the appropriate partition, then re-create it, so it must start at the same spot as the one you just deleted. Note until you write, you can still reverse back out of fdisk.
  2. Then run e2fsck
  3. Then run resize2fs

So this was a RPi 3 with a 8GB SD card, with no extra 8GB SD card anywhere to be found, but several 2GB cards were available. The files in use for the root filesystem was 1.6GB, so just enough room to make it all work.

Note these examples are working with a standalone PC, not working with the RPi itself - again, if that is all you have, the RPi Forums are a great resource.

Here is an approach that can work if you were using the exact same SD card (i.e. cloning the existing system):

NOTE: All commands done as root (superuser or user with sufficient privileges)

Create the image from the SD Card:

dd if=/dev/8gbsd of=/home/user/pi_images/8gbsd_image.img
-or could be something like-
dd if=/dev/sdb of=/home/user/pi_images/8gbsd_image.img

Restore an image to disk:

dd if=/home/user/pi_images/8gbsd_image.img of=/dev/8gbsd
-or-
dd if=/home/user/pi_images/8gbsd_image.img of=/dev/sdb

Now if you try and use a 2GB card, e.g.

dd if=/home/user/pi_images/8gbsd_image.img of=/dev/2gbsd
-or-
dd if=/home/user/pi_images/8gbsd_image.img of=/dev/sdb

You will find there will be some warnings during the dd and that the 2GB SD card won't boot (kernel panic or file system issue)

So what to do?

You need some common / readily available utilities - GPartEd (GNOME Partition Editor), fdisk, e2fsck, resize2fs, e4defrag

If not installed/available, these are debian packages for reference:

e2fsprogs (e4defrag resize2fs e2fsck), fdisk package (fdisk), gparted package (gparted)

You need a scratch disk or USB flash drive or other media to do your resize work (theoretically you could use the 8GB card, but this was a working system, and there was no intention of modifying the original working system on the 8GB SD Card - so image it, and then put the working 8GB SD card aside)

For this example, a 16GB USB flash drive was used.

Partition as Linux (fdisk), and then format ext4 (mkfs.ext4)

Make sure it is not mounted, and then duplicate ext4 partition from 8GB card (both SD card and USB inserted, e.g. /dev/sdb and /dev/sdc)

dd if=/dev/sdb2 of=/dev/sdc1 (grab partition 2 from SD card and drop onto the USB drive)

Test USB drive

mount /dev/sdc1 /mnt
ls /mnt

You should see duplicated root file system on USB drive

Now prep the drive - you can delete any files you know aren't needed, then defragment the drive to optimize the layout of the file system, e.g.

/sbin/e4defrag /dev/sdc1

Then do file system check / prep for resize

/sbin/e2fsck -f /dev/sdc1

Now unmount the partition for resize step

umount /dev/sdc1

Finally, use the resize2fs utility to resize the file system

You can use this to see details on minimum size needed:

/sbin/resize2fs -P /dev/sdc1

The -M option minimizes the file system at optimum size:

/sbin/resize2fs -M /dev/sdc1

If this isn't small enough, you need to make sure you can still fit the files onto the 2GB card. If the usage is 1.6 GB used, you can try. If it is 2.6GB, there is no way the 2GB card will work. You can go back to the Test step, mount the USB, and try and delete files, then repeat the steps.

You can force the size with the -f option - note that if the existing files are too large to fit in your specified size, you can run into corrupted files, or it simply won't work.

/sbin/resize2fs -f /dev/sdc1 1820M

So now you've got a reduced ext4 partition that can fit on the 2GB card, but the 2GB card needs to be structured so it will boot on the RPi.

Start with gparted, and document what partitions are on the 8 GB card, note start point, size, and number of sectors per partition.

Now drop the 8GB image onto the 2GB - this will ensure the beginning part of the drive will match the layout of the boot partition.

dd if=/home/user/pi_images/8gbsd_image.img of=/dev/sdb

You can use gparted or fdisk, but you want to reduce the second partition so it will fit on the 2GB SD card. With fdisk, you'd delete partition 2, make a new one, and then select/verify the correct start sector and fit the maximum size for the new second partition. When asked about ext4 signature, you do not want to delete.

Again, you can use gparted to view & compare the 8GB vs. the 2GB - the beginning should be the same, and the second ext4 partition should fit on the card.

Now, you can put the reduced root partition from your USB drive:

dd if=/dev/sdc1 of=/dev/sdb2 (grab the USB drive and drop onto partition 2 from SD card)

Now, if the sizes are all good, and no errors or warnings, you can remove the USB drive, and try and mount partition 2/ext4 root file system:

mount /dev/sdb2 /mnt
ls /mnt

If all looks good, unmount the drive:

umount /dev/sdb2

Now put the SD card into your Raspberry Pi and Boot Away!

Here are a few other notes regarding layout on SD card (for reference):

Clone MBR + BootSector to a new drive:

dd if=/dev/sdb of=/dev/sdc bs=512 count=2

To restore the MBR from a backup image you want to copy only the first 446 bytes:

dd if=mbrbackup.img of=/dev/sdc bs=446 count=1

Clone only MBR from source disk to a new drive:

dd if=/dev/sdb of=/dev/sdc bs=446 count=1

View Current
View News & Commentary
View Support Items
View All

Blog Information

Happy New Year 2024!
January 2, 2024
Time Traveler
Category: BlogEntry
Tags: USB; Notes; Price; Amazon Pay; What's happening;
 

First Steps - How To Boot from USB
October 30, 2023
Time Traveler
Category: BlogEntry
Tags: USB; Booting; Intro; How To;
 

A few notes and happenings…
September 12, 2023
Time Traveler
Category: BlogEntry
Tags: USB; Notes; What's happening;
 

Debian 12 Bookworm now available!
June 16, 2023
Time Traveler
Category: BlogEntry
Tags: New; Debian; Bookworm; Debian 12;
 

Updates & Info
May 11, 2023
Time Traveler
Category: BlogEntry
Tags: New; Debian; Ubuntu; Kubuntu; OpenMandriva
 

Random Notes, What's Happening…
February 8, 2023
Time Traveler
Category: BlogEntry
Tags: New; UEFI; Debian
 

Debian Complete Collection USB now has hardware .deb packages
January 5, 2023
TimeTraveler
Category: BlogEntry
Tags: Debian; Debian Complete Collection USB; Drivers; Hardware
 

USB Promo / Ubuntu 22.10 updates and notes
October 27, 2022
TimeTraveler
Category: BlogEntry
Tags: Ubuntu; Ubuntu Studio; USB Promo
 

Partitions, setup and configuration of the Debian Complete Collection USB
September 28, 2022
TimeTraveler
Category: BlogEntry
Tags: Debian; USB Partitions; GRUB;
 

Some notes on Debian and Firmware
April 7, 2022
TimeTraveler
Category: BlogEntry
Tags: Debian; Ubuntu; Firmware; non-free;
 

Read only USB (physical write protect switch)
January 12, 2022
TimeTraveler
Category: BlogEntry
Tags: USB; Physical write protect switch; Read Only
 

Raspberry Pi: Reducing file system to fit on smaller SD Card
December 22, 2021
TimeTraveler
Category: BlogEntry
Tags: Debian; Raspberry Pi; Raspbian; resize2fs
 

Some notes on Live Linux distros
December 10, 2021
TimeTraveler
Category: SupportNote
Tags: Debian; Fedora; Ubuntu; Most Popular; USB Collections
 

All USB Collections updated!
November 10, 2021
TimeTraveler
Category: BlogEntry
Tags: Debian; Fedora; Ubuntu; Most Popular; USB Collections
 

LinuxCollections.com media production has been green for years
September 20, 2021
TimeTraveler
Category: BlogEntry
Tags: Media Production
 

Debian 11.0.0 Release
August 26, 2021
TimeTraveler
Category: BlogEntry
Tags: Debian; Debian 11; Release;
 

Debian 10.10.0 USB Developer Collection
July 8, 2021
Time Traveler
Category: BlogEntry
Tags: New; Updates; Debian;
 

Notes on WiFi Drivers
May 12, 2021
Time Traveler
Category:
Tags: WiFi; Wireless; Drivers; Firmware;
 

Cloned Drives and UUIDs
April 28, 2021
Time Traveler
Category:
Tags: UUID; lsblk; fstab;
 

Debian Buster (10) sources.list example
March 2, 2021
Time Traveler
Category: SupportNote
Tags: Debian; Buster; sources.list; apt; apt-get
 

Debian 10.8.0 USB Complete Collection
February 15, 2021
Time Traveler
Category: BlogEntry
Tags: New; Updates; Debian;
 

Random Notes, What's New 2021
January 22, 2021
Time Traveler
Category: BlogEntry
Tags: New; Hardware;
 

New! The By Request page
December 10, 2020
Time Traveler
Category: BlogEntry
Tags: New; Requests;
 

How to create a separate bootable USB for specific Live version from Debian USB Complete Collection
September 2, 2020
Time Traveler
Category: SupportNote
Tags: Debian; USB Complete Collection; Install; Create Live USB;
 

Debian catalog backfilled a bit
August 19, 2020
Time Traveler
Category: BlogEntry
Tags: Debian; Hamm; Slink; Potato; Woody;
 

Understanding Debian main, contrib, non-free in sources.list
August 17, 2020
Time Traveler
Category: BlogEntry
Tags: Debian; sources.list; apt-get; apt; /etc/apt;
 

Most Popular USB Collection now available!
July 23, 2020
Time Traveler
Category: BlogEntry
Tags: USB Collections; Fedora; Linux Lite; LinuxMint; Manjaro; MX Linux; openSuSE; Ubuntu; Kubuntu
 

How do I learn about all the programs available in the Debian USB Complete Collection (or the Debian all discs option)?
June 16, 2020
Time Traveler
Category: SupportNote
Tags: Debian Packages, Debian USB Complete Collection, mountusb, ISO, Synaptic, apt
 

How to access ALL Debian packages from LinuxCollections.com's Debian Complete Collection on USB
June 4, 2020
Time Traveler
Category: SupportNote
Tags: mountusb, ISO, Debian Packages, Debian USB Complete Collection
 

Older Linux Distributions
May 26, 2020
Time Traveler
Category: SupportNote
Tags: Old distributions, BIOS, UEFI, hd vs. sd
 

Updates - new LinuxCollections.com Disc case, Facebook Links
May 6, 2020
Time Traveler
Category: BlogEntry
Tags: LinuxCollections.com Disc Case, Facebook, Advertising
 

Ubuntu 20.04 LTS now available in all flavors on USB!
April 20, 2020
Time Traveler
Category: BlogEntry
Tags: Ubuntu; Kubuntu; Lubuntu; Xubuntu; Ubuntu Studio; Ubuntu Budgie; Ubuntu MATE; USB Complete Collection;
 

Understanding Linux on a USB Flash drive
April 20, 2020
Time Traveler
Category: SupportNote
Tags: USB Flash Drive; USB Complete Collections
 

What NOT to do with a Bootable USB Drive or Collection
March 31, 2020
Time Traveler
Category: SupportNote
Tags: USB Flash Drive; USB Complete Collections
 

Lost or forgot Admin password (root user password)
March 25, 2020
Time Traveler
Category: SupportNote
Tags: LinuxCollections.com Blog
 

What to expect from a Linux Disc
March 24, 2020
Time Traveler
Category: BlogEntry
Tags: LinuxCollections.com Blog
 

Issues with new Debian 10 install - no Desktop / login prompt only
March 14, 2020
Time Traveler
Category: SupportNote
Tags: LinuxCollections.com Blog
 

Solving Problems
January 28, 2020
Time Traveler
Category: SupportNote
Tags: LinuxCollections.com Blog
 

US Postage rates go up
January 28, 2020
Time Traveler
Category: BlogEntry
Tags: LinuxCollections.com Blog
 

Need a different operating system
January 25, 2020
Time Traveler
Category: BlogEntry
Tags: LinuxCollections.com Blog
 

Support Stories - Disc Data
January 15, 2020
Time Traveler
Category: SupportNote
Tags: LinuxCollections.com Blog
 

The Facebook Link Conundrum
January 10, 2020
Time Traveler
Category: BlogEntry
Tags: LinuxCollections.com Blog
 

Welcome to the LinuxCollections.com Blog!
January 6, 2020
Time Traveler
Category: BlogEntry
Tags: LinuxCollections.com Blog
 

 
 

LinuxCollections.com Logo
  • LinuxCollections.com News, Technical information, and other interesting items.
  • LinuxCollections.com is your premier source for Linux distros on CD/DVD/USB.

Send LinuxCollections.com a note via e-mail...