Raspberry Pi system clone and/or resize

From WickyWiki
Revision as of 11:25, 4 September 2025 by Wilbert (talk | contribs) (→‎Prepare the new sdcard)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Raspberry Pi system clone and/or resize

Note

At risky points in this text sdX and sdX1 is used, in other places sda and sda1. You will need to replace this with the correct device.

Resize and/or clone Raspberry Pi system sdcard

This method will use the Raspberry Pi running system to clone the files onto a new sdcard. Instead you can also follow these steps and use your Linux PC and clone the files from the sdcard partitions onto an ext4 filesystem.

We will need:

  • Raspberry Pi
  • USB sdcard adapter
  • A new sdcard >=4Gb

Steps:

  1. Connect new sdcard to the Raspberry Pi using the sdcard adapter
  2. Create the boot (FAT) and system (ext4) partition on the new sdcard of the appropriate size
  3. Rsync-copy the files to the new sdcard using the statements provided below
  4. Correct the drive references in the files if needed
  5. You can now boot the Raspberry Pi from the new sdcard

Note:

What partitions do we have?

If Raspbian was installed using de NOOBS installer, it will have a recovery (mmcblk0p1) and settings (mmcblk0p5) partition. We will not need these partitions. We will only need 'boot' and 'root'.

lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0     179:0    0  7.6G  0 disk 
+-mmcblk0p1 179:1    0  1.6G  0 part 			(1: recovery)
+-mmcblk0p2 179:2    0    1K  0 part 			(2: extended 5+6+7)
+-mmcblk0p5 179:5    0   32M  0 part /media/pi/SETTINGS	(5: settings)
+-mmcblk0p6 179:6    0   66M  0 part /boot		(6: boot) 
+-mmcblk0p7 179:7    0    6G  0 part /			(7: root)

If you have connected the new sdcard you will also see device 'sda' (for example sda). This is the device that will be COMPLETELY ERASED. Make sure this is the correct device:

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1  3.8G  0 disk 
+-sda1        8:1    1  3.8G  0 part

Prepare the new sdcard

Note that these actions will cause the data on the device to be COMPLETELY ERASED.

sudo fdisk /dev/sdX

Use the following commands and follow the instructions on the screen, accept defaults when not mentioned here:

'p' - print partitions
'd' - delete partition (enter the number)
'n' - new partition
   sda1 size: +512M (boot partition size)
   sda2 size: +8G (root)
   sda3 size: (data, default is the remaining space)
   
't' - change partition type
   sda1 Id: b (W95 FAT32)
   sda2 Id: 83 (Linux, default)
   sda3 Id: 7 (exFAT)
   
'w' - write and quit

Format:

sudo mkdosfs -F 32 -I /dev/sdX1
sudo mkfs.ext4 /dev/sdX2
sudo mkfs.exfat /dev/sdX3 -L Data

Note: you might need to install the exFat driver:

sudo apt install exfat-fuse exfatprogs

Show the results:

sudo fdisk -l /dev/sda
Disk /dev/sda: 14.9 GiB, 15931539456 bytes, 31116288 sectors
Disk model: STORAGE DEVICE
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb3aef0a8

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1         8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/sda2       532480 31116287 30583808 14.6G 83 Linux
/dev/sda3     31116288      ...      ...   ...  7 HPFS/NTFS/exFAT

Rsync to the new sdcard

Note: if you have an existing copy rsync will only update modified files, this would speedup the process considerably.

Mount the new sdcard partitions:

sudo mkdir "/media/clone-boot"
sudo mkdir "/media/clone-root"
sudo mount "/dev/sda1" "/media/clone-boot/"
sudo mount "/dev/sda2" "/media/clone-root/"

An ISO image file can be mounted like this:

sudo mount -o loop "/media/backup/image.iso" "/media/clone-root/"

Rsync boot partition, mounted as /boot

Rsync copy:

sudo rsync --info=progress2 --force -rlWDEHXAgoptx --delete --exclude '.gvfs' --exclude 'lost\+found/*' "/boot/" "/media/clone-boot/"
ls -l "/media/clone-boot"

Rsync system-root partition, mounted as /

Rsync of the online system-root can take up to 30 minutes, syncing an existing copy should be faster. The actual duration greatly depends on the quality of your sdcard:

sudo rsync --info=progress2 --force -rlWDEHXAgoptx --delete --exclude '.gvfs' --exclude 'lost\+found/*' --exclude '/dev/*' --exclude '/proc/*' --exclude '/run/*' --exclude '/sys/*' --exclude '/tmp/*' --exclude '/media/*' --exclude '/media/*' --exclude '/boot/*' --exclude '/media/pi/SETTINGS/*' --exclude '/var/swap' --exclude '/etc/pihole/pihole-FTL.db' "/" "/media/clone-root/"

Shutdown the database and sync datafiles separately:

sudo service mariadb stop
sudo rsync --info=progress2 --force -rlWDEHXAgoptx --delete "/var/lib/mysql/" "/media/clone-root/var/lib/mysql/"
sudo service mariadb start
ls -l "/media/clone-root"

Cleanup

sudo umount "/media/clone-boot/"
sudo rmdir "/media/clone-boot"
sudo umount "/media/clone-root/"
sudo rmdir "/media/clone-root/"

Rsync image to partition basics

src="/media/src/"
dest="/media/dest/"
sudo mkdir "$src"
sudo mkdir "$dest"

# boot

lsblk
#BE CAREFUL !! - CHECK THE DESTINATION:
sudo mkdosfs -F 32 -I "/dev/sdX1"
ls -l /media/backup/backupimg/
sudo mount -r -o loop "/media/backup/backupimg/..._boot.img" "$src"
sudo mount "/dev/sdX1" "$dest"
ls -l "$src"
ls -l "$dest"
#BE CAREFUL !! - CHECK THE DESTINATION:
sudo rsync --info=progress2 --force -rlWDEHXAgoptx --delete --exclude '.gvfs' --exclude 'lost\+found/*' "$src" "$dest"
ls -l "$dest"
#Check drive references
sudo nano $dest/cmdline.txt
sudo umount "$src"
sudo umount "$dest"

# root

lsblk
#BE CAREFUL !! - CHECK THE DESTINATION:
sudo mkfs.ext4 "/dev/sdX2"
ls -l /media/backup/backupimg/
sudo mount -r -o loop "/media/backup/backupimg/..._root.img" "$src"
sudo mount "/dev/sdX2" "$dest"
ls -l "$src"
ls -l "$dest"
#BE CAREFUL !! - CHECK THE DESTINATION:
sudo rsync --info=progress2 --force -rlWDEHXAgoptx --delete --exclude '.gvfs' --exclude 'lost\+found/*' --exclude '/dev/*' --exclude '/proc/*' --exclude '/run/*' --exclude '/sys/*' --exclude '/tmp/*' --exclude '/media/*' --exclude '/media/*' --exclude '/boot/*' --exclude '/media/pi/SETTINGS/*' --exclude '/var/swap' --exclude '/etc/pihole/pihole-FTL.db' "$src" "$dest"
ls -l "$dest"
#Check drive references
sudo nano $dest/etc/fstab
sudo umount "$src"
sudo umount "$dest"

sudo rmdir "$src"
sudo rmdir "$dest"

Fix device references

If needed, fix references in cmdline.txt:

You can also use the device ID:

blkid /dev/sda2

/dev/sda2: UUID="ed1abdff-c5d7-45a1-95fd-46029cc2a255" TYPE="ext4" PARTUUID="c017ea97-02"

In cmdline.txt use:

root=/dev/mmcblk0p2

sudo nano /media/clone-boot/cmdline.txt

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Modify fstab to only mount 'boot' and 'root' for now. Others can be added later:

  • boot: /dev/mmcblk0p1
  • root: /dev/mmcblk0p2
nano /media/clone-root/etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1

Other info

Create or restore sdcard file images

Insert the sdcard adapter with the sdcard and make sure you are using the right device name (sdc in this case)

sudo fdisk -l
sudo lsblk

Complete image of all partitions:

sudo dd if=/dev/sda of=~/sdcard.img

Select a certain partition (we will need boot and system):

sudo dd if=/dev/sda1 of=~/sdcard1.img

Restore, BE CAREFUL:

  • All data on the target device WILL BE LOST
  • Make sure this is the device (in this example 'sdX') by checking the size, the name and/or the contents
dd if=~/sdcard.img of=/dev/sdX