[Raspberry PI 4B]how to clone a 32 GB SD card to a 16GB SD?

profesor發表於2024-04-01

So you need to do this from a command line because you don't have a gui? To list your partitions, use

Code: Select all

lsblk

or

Code: Select all

sudo fdisk -l

This will show you how many partitions.

One way of shrinking a clone is to copy the sd card to an image file and then shrink the file. You can do that by swapping the source and target from the command on this page:
https://www.raspberrypi.org/documentati ... s/linux.md
to get

Code: Select all

dd bs=4M if=/dev/sdX of=~/"$(date -I)"-raspbian.img conv=fsync

"$(date -I)" is a command-line substitution that puts the date (yyyy-mm-dd) in the image name.

Please note that block size set to 4M will work most of the time. If not, try 1M, although this will take considerably longer.
Also note that if you are not logged in as root you will need to prefix this with sudo.

One you have the image, you can shrink it. Adafruit has a script for doing this:
https://learn.adafruit.com/resizing-ras ... ing-images
You can use the script or look at the lines of the script to see the commands they used.

Or you can look at this page for a detailed guide:
https://raspberrypi.stackexchange.com/q ... er-sd-card

來源:

https://forums.raspberrypi.com/viewtopic.php?p=1532481&sid=21c55f0e4782a3b89cbed53a04dc1670#p1532481

重要的是這個shrink scripts:

#!/bin/env bash

IMG="$1"

if [[ -e $IMG ]]; then
  P_START=$( fdisk -lu $IMG | grep Linux | awk '{print $2}' ) # Start of 2nd partition in 512 byte sectors
  P_SIZE=$(( $( fdisk -lu $IMG | grep Linux | awk '{print $4}' ) * 512 )) # Partition size in bytes
  losetup /dev/loop2 $IMG -o $(($P_START * 512)) --sizelimit $P_SIZE
  fsck -f /dev/loop2
  resize2fs -M /dev/loop2 # Make the filesystem as small as possible
  fsck -f /dev/loop2
  P_NEWSIZE=$( dumpe2fs /dev/loop2 2>/dev/null | grep '^Block count:' | awk '{print $3}' ) # In 4k blocks
  P_NEWEND=$(( $P_START + ($P_NEWSIZE * 8) - 1 )) # in 512 byte sectors
  losetup -d /dev/loop2
  echo -e "p\nd\n2\nn\np\n2\n$P_START\n$P_NEWEND\np\nw\n" | fdisk $IMG
  I_SIZE=$((($P_NEWEND + 1) * 512)) # New image size in bytes
  truncate -s $I_SIZE $IMG
else
  echo "Usage: $0 filename"
fi

來源:https://learn.adafruit.com/resizing-raspberry-pi-boot-partition/bonus-shrinking-images

相關文章