是否可以将现有的和配置的Raspbian安装复制到较小的SD卡上?
当我第一次安装Raspbian时,我手头只有32 GB的卡,显然比所需的要大。
是否可以将现有的和配置的Raspbian安装复制到较小的SD卡上?
当我第一次安装Raspbian时,我手头只有32 GB的卡,显然比所需的要大。
Answers:
在这个答案中,我演示了逐步执行的操作,以使人们理解解决方案背后的逻辑并能够针对其他问题应用步骤。
但首先,应该指出的是,将文件系统从SD卡迁移到较小的(但足以容纳数据的)SD卡是一个普遍的问题(不是raspi特有的问题)。
带有微型SD卡读卡器和运行Linux(我更喜欢Ubuntu)的笔记本电脑。
PIBOX : Raspberry Pi which is used
SD_CARD_A : 8GB micro SD card which is used on PIBOX and on which Raspbian-lite (the OS) is installed
SD_CARD_B : 2GB micro SD card which will be used on PIBOX and on which Raspbian-lite (the OS) will be installed
当PIBOX运行时,我们列出了分区(此处未显示不必要的系统分区)。
root@pibox:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/root ext4 7.3G 1.1G 5.9G 16% /
/dev/mmcblk0p1 vfat 63M 21M 43M 33% /boot
SD_CARD_A上有2个分区,分别为/
和/boot
。甚至没有总共使用2GB。
关闭并停止PIBOX之后,我们从PIBOX板上取出SD_CARD_A并将其放入笔记本电脑的读卡器中。
SD_CARD_A的分区将作为/dev/sdc1
和自动安装到我们的系统/dev/sdc2
。
root@mylaptop:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb2 ext4 22G 13G 7.9G 63% /
/dev/sdb1 vfat 197M 2.6M 195M 2% /boot/efi
/dev/sda8 ext4 66G 11G 52G 17% /home
/dev/sdc1 vfat 63M 21M 43M 33% /media/some_user_name/boot
/dev/sdc2 ext4 7.3G 1.1G 5.9G 16% /media/some_user_name/some_uuid_serial
我们从系统中卸载这些分区以成功对其进行操作。
root@mylaptop:~# umount /dev/sdc1
root@mylaptop:~# umount /dev/sdc2
我们将详细显示SD_CARD_A的设备信息,以供下一步确认。
root@mylaptop:~# fdisk -l /dev/sdc
Disk /dev/sdc: 7969 MB, 7969177600 bytes
246 heads, 62 sectors/track, 1020 cylinders, total 15564800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2019f6d8
Device Boot Start End Blocks Id System
/dev/sdc1 8192 137215 64512 c W95 FAT32 (LBA)
/dev/sdc2 137216 15564799 7713792 83 Linux
在上方可以看到SD_CARD_A的容量为8GB。
我们将SD_CARD_A克隆到pibox.img文件中。
root@mylaptop:~# dd bs=4MB if=/dev/sdc of=pibox.img
1992+1 records in
1992+1 records out
7969177600 bytes (8.0 GB) copied, 416.582 s, 19.1 MB/s
检查复制字节的大小,它等于我们通过fdisk -l /dev/sdc
命令获得的值。
Linux有一个称为回送的模块,它使我们能够将文件作为块设备进行处理。
我们加载回送模块。
root@mylaptop:~# modprobe loop
我们找到了未使用的回送设备路径。
root@mylaptop:~# losetup -f /dev/loop0
现在,我们为pibox.img文件创建一个回送设备。
root@mylaptop:~# losetup /dev/loop0 pibox.img
我们触发有关分区更改的内核。
root@mylaptop:~# partprobe /dev/loop0
我们确认先前的操作是否成功。
root@mylaptop:~# losetup /dev/loop0
/dev/loop0: [0806]:69 (/root/pibox.img)
我们将详细显示回送设备信息,以将其与SD_CARD_A进行比较。
root@mylaptop:~# fdisk -l /dev/loop0
Disk /dev/loop0: 7969 MB, 7969177600 bytes
255 heads, 63 sectors/track, 968 cylinders, total 15564800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2019f6d8
Device Boot Start End Blocks Id System
/dev/loop0p1 8192 137215 64512 c W95 FAT32 (LBA)
/dev/loop0p2 137216 15564799 7713792 83 Linux
在上面可以看到环回设备的大小(= 7969177600字节)和分区与SD_CARD_A相同。
从现在开始,我们将专注于分区/dev/loop0p2
。我们将其命名为THE_PARTITION。
块大小为512字节(如打印在以Units =扇区.....开头的行上)
THE_PARTITION从块137216开始,到块15564799结束,这意味着它的大小为15427584 blocks
(= 15564799-137216 + 1)。
因此,以字节为单位的THE_PARTITION的大小为7898923008 bytes
(= 512 * 15427584)。
为了将THE_PARTITION放入SD_CARD_B中,我们希望它具有或的新大小 (= 512 * 3710940)。3710940 blocks
1900001280 bytes
因此,新的结束程序段号3848155
由start block number
(= 137216)+ size in blocks
(= 3710940)-计算1
。
有2个操作不可相互混淆。
3710940 blocks
。3848155
。在缩小文件系统之前,应使用标记为干净e2fsck
。
root@mylaptop:~# e2fsck -f /dev/loop0p2
e2fsck 1.42.9 (4-Feb-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/loop0p2: 41175/475776 files (0.2% non-contiguous), 309183/1928448 blocks
我们使用缩小文件系统resize2fs
。
root@mylaptop:~# resize2fs /dev/loop0p2 3710940s
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/loop0p2 to 463867 (4k) blocks.
The filesystem on /dev/loop0p2 is now 463867 blocks long.
我们将了解THE_PARTITION号码parted
。
root@mylaptop:~# parted /dev/loop0
GNU Parted 2.3
Using /dev/loop0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Loopback device (loop)
Disk /dev/loop0: 7969MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 4194kB 70.3MB 66.1MB primary fat16 lba
2 70.3MB 7969MB 7899MB primary ext4
(parted) quit
我们使用缩小THE_PARTITION parted
。
root@mylaptop:~# parted /dev/loop0 unit s resizepart 2 3848155
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? Yes
环回设备已完成。我们分离它。
root@mylaptop:~# losetup -d /dev/loop0
我们验证新的分区表。
root@mylaptop:~# fdisk -l pibox.img
Disk pibox.img: 7969 MB, 7969177600 bytes
255 heads, 63 sectors/track, 968 cylinders, total 15564800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2019f6d8
Device Boot Start End Blocks Id System
pibox.img1 8192 137215 64512 c W95 FAT32 (LBA)
pibox.img2 137216 3848155 1855470 83 Linux
在输出中,可以清楚地看到THE_PARTITION的结束块数减少了from 15564799 to 3848155
。
我们使用的最后一个块是3848155
。块编号从0开始。因此,我们总共有3848155 + 1个块,并且pibox.img文件的新大小应为1970255872 bytes
(=(3848155 + 1)* 512)。
我们截断pibox.img文件。
root@mylaptop:~# truncate --size=1970255872 pibox.img
我们验证pibox.img文件的新大小。
root@mylaptop:~# ls -l pibox.img
-rw-r--r-- 1 root root 1970255872 Oct 13 21:53 pibox.img
我们将SD_CARD_B放入笔记本电脑的读卡器中。SD_CARD_B的分区将作为/dev/sdc1
和自动安装到我们的系统/dev/sdc2
。
root@mylaptop:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb2 ext4 22G 13G 7.9G 63% /
/dev/sdb1 vfat 197M 2.6M 195M 2% /boot/efi
/dev/sda8 ext4 66G 11G 52G 17% /home
/dev/sdc1 vfat 63M 21M 43M 33% /media/some_user_name/boot
/dev/sdc2 ext4 1.8G 1.6G 59M 97% /media/some_user_name/some_uuid_serial
在上方可以看到SD_CARD_B的容量为2GB。
我们从系统中卸载了这些分区,以成功在SD_CARD_B上运行。
root@mylaptop:~# umount /dev/sdc1
root@mylaptop:~# umount /dev/sdc2
我们将pibox.img文件克隆到SD_CARD_B中。
root@mylaptop:~# dd bs=4MB if=pibox.img of=/dev/sdc
492+1 records in
492+1 records out
1970255872 bytes (2.0 GB) copied, 646.967 s, 3.0 MB/s
检查复制字节的大小,它等于我们通过ls -l pibox.img
命令获得的值。
从笔记本电脑中取出SD_CARD_B放入PIBOX板后,我们启动系统并登录到PIBOX控制台。
我们列出了分区(此处未显示一些其他不必要的系统分区)。
root@pibox:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/root ext4 1.8G 1.1G 601M 64% /
/dev/mmcblk0p1 vfat 63M 21M 43M 33% /boot
losetup
甚至-o loop=whatever
。根据我刚刚使用的其他文章mount -o offset=123 /imagefilepath /mntpoint
,回送的使用是隐式的。我认为现在在Linux上通常是这样-尝试看看。然后,您可以减少到仅说分区是通过虚拟“回送设备”安装的。
使用parted(分区编辑器)之类的东西将主分区缩小到较小的尺寸,然后使用Clonezilla之类的工具从现在较小的分区复制到新卡上。但是,您可能必须在另一台计算机上执行此操作。
dd if=/dev/sdx of=/path/to/image bs=1M
从这个线程:raspberrypi.stackexchange.com/questions/311/...
使用已提及的方法之一创建卡的映像- 如何备份Raspberry Pi?
使用位于http://sirlagz.net/2013/03/10/script-automatic-rpi-image-downsizer/的脚本来缩小图像尺寸
在新的较小卡上还原缩小的图像
script.sh
使用chmod
并执行该文件。
我一直在rsync
一段时间内一直在将文件系统从一个磁盘复制到另一个磁盘,而不会出现打no的情况。使用rsync的好处是它正在复制文件系统的内容,而不是对设备进行块级复制。因此,只要目标驱动器有足够的空间来容纳数据,它实际上就不会在乎目标驱动器和源驱动器的大小。
所以这是我的做法:
rsync -avx oldFilesystem newFilesystem
的文件系统复制/覆盖新卡上的文件系统。rpi-update
以确保固件一致且最新。之后,您的新卡上应该安装了功能完善的Raspbian系统。
我创建了一个Shell脚本来备份和还原SD卡上的所有数据。它首先删除一些数据(对应于我的项目),然后将分区缩小到最小大小,以便映像与SD卡上的数据一样大。另外,脚本会创建图像的* .zip文件。将创建的映像还原到另一个SD卡后,分区将被放大到最大大小。该脚本使用其他答案中提到的命令。因为这是我这种大小的拳头外壳脚本,所以花了我几个小时才能创建它,而且它并不是完美的喷气机。特别是我不知道如何处理resize2fs和fdisk的返回值,因此用户必须键入我需要的值。有什么解决办法吗?我希望这个脚本可以帮助其他人。随时进行编辑和改进。
"Usage:
<skriptname> -b <path> create backup of SC Card (dev/mmcblk0) to file <path>/JJJJ-MM-DD_HHMM.img
<skriptname> -r <path>/FILENAME.img restore an exitsting image (<path>/FILENAME.img) to the SD Card (dev/mmcblk0)
<skriptname> -r <path>/FILENAME.zip unzip and restore an exitsting image (<path>/FILENAME.zip) to the SD Card (dev/mmcblk0)
<skriptname> -h show this hlep
这里是:
#!/bin/bash
# check if the user is root
if (( $EUID != 0 )); then
echo "This script requires root privileges please run as root"
exit
fi
while getopts ":b:r:h" opt; do
case $opt in
b)
mode="backup"
OUTPATH=$OPTARG
;;
r)
mode="restore"
DIRFILENAME=$OPTARG
;;
h)
mode="help"
;;
\?)
echo "Invalid option: -$OPTARG. Use -h for help" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument. Use -h for help" >&2
exit 1
;;
esac
done
# no option
if [ $OPTIND == 1 ]
then
echo "$(basename "$0") needs an option! Use -h for help"
exit 1
fi
myMount(){
# create mountpoint if not existing
if [ ! -d /tmp/sd2/ ] ; then
mkdir /tmp/sd2
fi
# mount partition
mount -v -t ext4 /dev/mmcblk0p2 /tmp/sd2
err=$?
if [ $err != 0 ]; then
echo "mount failed error: $err"
exit 1
fi
}
myUmount(){
cd /home/ # otherwise umount will fail
# fuser -vm /tmp/sd2/
# umount partition
umount -v /tmp/sd2
err=$?
if [ $err != 0 ]; then
echo "umount failed error: $err"
exit 1
fi
}
myEnlarge(){
echo "enlarge partition..."
# enlarge partition is not posible with fdisk -> delete and recreate it
(
echo d # delete partition
echo 2 # patition number
echo n # add a new partition
echo p # primary partition
echo 2 # partition number
echo # first sector (accept default: varies)
echo # last sector (accept default: varies)
echo w # write changes
) | fdisk /dev/mmcblk0
echo "\n check filesystem... "
e2fsck -f -v -C 0 /dev/mmcblk0p2
# enlarge filesystem to maxsize
resize2fs -p /dev/mmcblk0p2
}
case "$mode" in
"help")
echo "Usage:
$(basename "$0") -b <path> create backup of SC Card (dev/mmcblk0) to file <path>/JJJJ-MM-DD_HHMM.img
$(basename "$0") -r <path>/FILENAME.img restore an exitsting image (<path>/FILENAME.img) to the SD Card (dev/mmcblk0)
$(basename "$0") -r <path>/FILENAME.zip unzip and restore an exitsting image (<path>/FILENAME.zip) to the SD Card (dev/mmcblk0)
$(basename "$0") -h show this hlep
--------------------------------
Adrian Zeitler, Germany 2017"
;;
"backup") ####################################### backup #######################################
echo "an image of the SD Card (/dev/mmcblk0) whitch is as smal as possible will be created to $OUTPATH."
# ------------------ delete some data --------------------
echo "Do you want to delete tempfiles? [y/n]"
read delfiles
if [ "$delfiles" = "y" ]
then
echo "Delete tempfiles..."
myMount
# remove some data
cd /tmp/sd2/home/alarm/
rm -v -f hagelbeere.db
rm -v -f HAILcam.log
rm -v -f HAILcam.log.1
rm -v -f test.jpg
myUmount
elif [ "$delfiles" = "n" ]
then
echo "I don't delete anything."
else
echo "Sorry, I didn't understand."
exit 1
fi
# --------------------------------------------------------------
# shrink partition 2 to minimum size
echo "check file system... "
e2fsck -f -v -C 0 /dev/mmcblk0p2
err=$?
if [ $err != 0 ]; then
echo "file system check failed, error: $err"
exit 1
fi
echo "shrink filesystem of partition 2 to minimum size..."
resize2fs -p -M /dev/mmcblk0p2
err=$?
if [ $err != 0 ]; then
echo "resize2fs failed, error: $err"
exit 1
fi
# --> Das Dateisystem auf /dev/mmcblk0p2 ist nun 692365 Blöcke groß.
echo "Please tell me the new filesystem size displayed above:"
read size
# from resize2fs blocksize, fdisk wants sector: sector = block * 8
size=$(( $size*8 ))
# shrink partition is not posible with fdisk -> delete and recreate it
(
echo d # delete partition
echo 2 # patition number
echo n # add a new partition
echo p # primary partition
echo 2 # partition number
echo # first sector (accept default: varies)
echo +$size # last sector
echo w # write changes
) | fdisk /dev/mmcblk0
err=$?
if [ $err != 0 ]; then
echo "fdisk failed, error: $err"
exit 1
fi
# --------------------------------------------------------------
# fill unused space with zeros
echo "Do you want to fill unused space with zeros? [y/n]"
read fillzeros
if [ "$fillzeros" = "y" ]
then
echo "Copy zeros. This will end up with an error. But this is ok."
myMount
dd if=/dev/zero | pv | dd of=/tmp/sd2/nullen.datei conv=noerror,notrunc,sync bs=10240
# exits with error -> this is normal
# dlelete zeros
rm -v -f /tmp/sd2/nullen.datei
sync
myUmount
elif [ "$fillzeros" = "n" ]
then
echo "I don't delete anything."
else
echo "Sorry, I didn't understand."
exit 1
fi
# --------------------------------------------------------------
# find out end of partition
fdisk -l /dev/mmcblk0
echo "Please tell me the end of mmcblk0p2 displayed above."
read count
DATE=$(date +"%Y-%m-%d_%H%M")
IMGFILENAME=$DATE.img
echo "Do you want to create image with filename $OUTPATH$IMGFILENAME? [y/n]"
read answer
if [ "$answer" = "y" ]
then
echo "Do you want to create a *.zip file of the created image? [y/n]"
read zip
echo "Do you want to enlarge partition 2 to maxsize after image creation? [y/n]"
read enlarge
echo "create image..."
cd $OUTPATH
# create image with dd, stop at and of partition
# count=N copy only N input blocks
# bs=BYTES read and write up to BYTES bytes at a time = block size
# pv show status
dd if=/dev/mmcblk0 | pv -s $(( $count*512 )) | dd of=$IMGFILENAME bs=512 count=$count
err=$?
if [ $err != 0 ]; then
echo "dd failed error: $err"
exit 1
fi
# --------------------------------------------------------------
# create zip file
# or like this:
# sudo dd if=/dev/sdX | pv |gzip > /pfad/zur/datei.img.gz
if [ "$zip" = "y" ]
then
echo "create zip file..."
zip $DATE.zip $IMGFILENAME
fi
# --------------------------------------------------------------
fi
# --------------------------------------------------------------
# enlarge partition 2
if [ "$enlarge" = "y" ]
then
myEnlarge
fi
;; #end case mode backup
"restore") ####################################### restore #######################################
#chek if image exists
if [[ -s "$DIRFILENAME" ]]
then
# check if file is an image or zip file
if [[ $DIRFILENAME =~ \.img$ ]]
then
IMGFILENAME=$(basename "$DIRFILENAME")
elif [[ $DIRFILENAME =~ \.zip$ ]]
then
ZIPFILENAME=$(basename "$DIRFILENAME")
else
echo "Not the right file format. I accept *.img and *.zip"
exit 1
fi
else
echo "Image file does not exist."
exit 1
fi
echo "the file $DIRFILENAME will be restored to the SD Card /dev/mmcblk0"
#change to the path of the imagefile
SOURCEPATH=$(dirname "$DIRFILENAME")
cd $SOURCEPATH
if [ "$ZIPFILENAME" != "" ]
then
echo "unzip file"
# change file extention form zip zu img
l=$(( ${#ZIPFILENAME}-3 ))
IMGFILENAME="${ZIPFILENAME:0:l}img"
unzip $ZIPFILENAME
fi
echo "Do you realy want to restore $SOURCEPATH/$IMGFILENAME to the SD card /dev/mmcblk0?
Warning: all data on the device /dev/mmcblk0 will be lost! [y/n]"
read answer
if [ "$answer" = "y" ]
then
echo "Do you want to enlarge partition 2 to maxsize after restoring? [y/n]"
read enlarge
echo "restore image..."
filesize=$(wc -c <"$IMGFILENAME")
echo "Filesize = $filesize Byte"
dd if=$IMGFILENAME | pv -s $filesize | dd of=/dev/mmcblk0 bs=512
err=$?
if [ $err != 0 ]; then
echo "dd failed error: $err"
exit 1
fi
fi
# --------------------------------------------------------------
# enlarge partition 2
if [ "$enlarge" = "y" ]
then
myEnlarge
fi
;; #end case mode restore
esac
我发现最简单的解决方案是使用上面概述的dd命令备份较大的原始卡,然后使用piwriter等将图像还原到较小的存储卡。dd可能也可以工作...不确定。PiWriter由于空间不足而返回了错误,但是由于映像中没有包含超出较小卡大小的任何实际数据,因此它只是截断了空扇区。我不确定这意味着什么...分区可能需要检查或修复,但是当我将其放入Pi中时,我可以验证它是否有效。
我使用旧版本的win32diskimager-RELEASE-0.1-r15-win32
来读取图像,它甚至从8GB SD卡创建4GB的图像,然后使用最新版本的win32diskimager写入该图像。
我使用旧版本,因为旧版本会跳过所有错误。