xiongyihui / notes

Notes
https://xiongyihui.github.io/notes/
3 stars 0 forks source link

How to copy pi image and resize image file #21

Open xiongyihui opened 5 years ago

xiongyihui commented 5 years ago

Suppose we made a Raspberry Pi image in a SD card and want to copy the image to hundreds of SD cards. How to do it fast?

First, let's have a look at the contents of the SD card.

image

There are two solutions in my mind:

Solution A

  1. Shrink the rootfs partition of the SD card. We can use GParted or Gnome Disks to resize the rootfs partition and get some free space at the end of the SD card.

    image

  2. Use dd to copy the front part (with the boot partition and the rootfs partition, without the free space at the end) to a image file. For example, copy 6G data to pi.img image file.

    sudo dd if=/dev/mmcblk0 of=pi.img bs=1G count=6 status=progress
  3. Use dd or Gnome Disks (open the image file with Gnome Disks) to write the image file to other SD cards.

  4. Extend the rootfs partition.

Solution B

If the writing speed of the SD card is very slow, shrink the rootfs of the SD card can be very slow. Then we can copy the whole contents to a image file and resize the image file. (Another case is that we already get a image file, skip step 1).

  1. Use dd or Gnome Disks to copy the contents of the SD card to a image file.

    sudo dd if=/dev/mmcblk0 of=pi.img bs=8M status=progress
  2. Setup the image file as a loop device.

    sudo losetup --find --partscan pi.img
    lsblk                               # find the loop device, the rootfs partition will be like loop0p2
    sudo e2fsck -f /dev/loop0p2
    sudo resize2fs -M xxx.img     # or use Gnome Disks or GParted to resize the rootfs partition
  3. Use dd to copy the front part of the image file (without the free space at the end) to other SD cards. For examples:

    sudo dd if=pi.img of=/dev/mmcblk0 bs=1G count=6 status=progress
xiongyihui commented 5 years ago
sudo losetup -d /dev/loop0