Introduction
Copying the contents of an SD card to a smaller one can be a challenging task, but it's sometimes necessary. For example, I needed to do this to reuse old SD cards to run a Raspberry Pi, which was originally using a larger SD card with pre-built servers. This guide will take you through the steps required to safely and efficiently copy data from a larger SD card to a smaller one.
Prerequisites
Before you begin, make sure you have the following:
- An SD card reader connected to your computer
- Two SD cards: one with the original data and one smaller card
- Basic knowledge of command line operations
- A Linux system or a Linux live USB to run the necessary commands (I used a Raspberry Pi)
Step-by-Step Instructions
Step 1: Check Disk Usage
The first step is to check the current disk usage on the original SD card. This ensures that the data will fit on the smaller SD card after resizing.
-
Mount the SD card to a directory, such as
/mnt
:sudo mount /dev/sda1 /mnt
-
Check the disk usage to verify how much space is being used:
df -h
-
Unmount the SD card before proceeding:
sudo umount /dev/sda1
Step 2: Resize the Filesystem
Next, resize the filesystem on the SD card to ensure it fits within the new, smaller partition size.
-
Run a filesystem check to ensure there are no errors:
sudo e2fsck -f /dev/sda1
-
Resize the filesystem to the desired size, making sure it is smaller than the capacity of the new SD card. For example, to resize to 4GB:
sudo resize2fs /dev/sda1 4G
Step 3: Resize the Partition
After resizing the filesystem, adjust the partition size to match.
-
Start the parted utility to manage partitions:
sudo parted /dev/sda
-
Display the current partition layout:
(parted) print
-
Resize the partition to a size slightly larger than the filesystem. Replace
PARTITION_NUMBER
with the appropriate partition number andEND
with the desired size, such as 4GB or 10%. Negative values count from the end of the disk. For example,-1s
specifies exactly the last sector:(parted) resizepart PARTITION_NUMBER END
-
Once the resizing is complete, exit the parted utility with
quit
command.(parted) quit
Step 4: Create the Image File
Backup the SD card using the dd
command:
dd if=/dev/sda of=path/to/backup.img bs=4M
Step 5: Shrink the Image
The image created in the previous step is the same size as the original SD card, so it needs to be shrunk to restore it to a smaller SD card.
-
Download and prepare the
pishrink.sh
script:wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x pishrink.sh
-
Shrink the image:
sudo ./pishrink.sh -v backup.img shrinked-backup.img
Step 6: Restore the Image to the Smaller SD Card
Restore the shrunken image to the smaller SD card using the dd
command:
dd if=path/to/shrinked-backup.img of=/dev/sda bs=4M
However, if your Pi doesn't start up using this method, I recommend using balenaEtcher, which worked for me when dd
did not.
Conclusion
By following these steps, you can successfully transfer the contents of your larger SD card to a smaller one. This process involves careful resizing of both the filesystem and the partition, ensuring that all your data fits neatly onto the new card.
Comments