Linux 如何在不包含可用空间的情况下创建光盘(sd 卡)的 .IMG 映像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19355036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:04:41  来源:igfitidea点击:

How to create an .IMG image of a disc (sd card) without including free space?

linuximagedd

提问by Necktwi

In Linux, we can do

在 Linux 中,我们可以这样做

dd if=/dev/sdb of=bckup.img

but if the disk is of 32GB with only 4GB used, the 32GB image file is waste of space-time. Is there any way or tool to create images with only valid data?

但是如果磁盘是32GB的,只用了4GB,那么32GB的镜像文件就是浪费空间。有什么方法或工具可以创建仅包含有效数据的图像?

采纳答案by Necktwi

The best thing to do is

最好的办法是

  1. Copy all the files from all the partitions preserving meta data

    mkdir -p myimage/partition1

    mkdir myimage/partition2

    sudo cp -rf --preserve=all /media/mount_point_partition1/* myimage/partition1/

    sudo cp -rf --preserve=all /media/mount_point_partition2/* myimage/partition2/

  2. Extract the MBR

    sudo dd if=/dev/sdX of=myimage/mbr.img bs=446 count=1

    replace /dev/sdXwith the corresponding device.

  3. Partition the destination disk into partitions with sizes greater than copied data and should be of the same format and same flags using gparted. Google how to partition a disk.

  4. Mount the freshly formatted and partitioned disk. On most computers, you just need to connect the disk and you can find the mounted partitions in /mediafolder.

  5. Copy the previously copied data to destination partitions using following commands

    sudo cp -rf --preserve=all myimage/partition1/* /media/mount_point_partition1/sudo cp -rf --preserve=all myimage/partition2/* /media/mount_point_partition2/

  6. Copy back the MBR

    sudo dd if=myimage/mbr.img of=/dev/sdX bs=446 count=1

  1. 从保留元数据的所有分区复制所有文件

    mkdir -p myimage/partition1

    mkdir myimage/partition2

    sudo cp -rf --preserve=all /media/mount_point_partition1/* myimage/partition1/

    sudo cp -rf --preserve=all /media/mount_point_partition2/* myimage/partition2/

  2. 提取 MBR

    sudo dd if=/dev/sdX of=myimage/mbr.img bs=446 count=1

    更换/dev/sdX相应的设备。

  3. 将目标磁盘分区为大小大于复制数据的分区,并且应该使用相同的格式和相同的标志gparted。谷歌如何分区磁盘。

  4. 挂载新格式化和分区的磁盘。在大多数计算机上,您只需要连接磁盘即可在/media文件夹中找到已挂载的分区。

  5. 使用以下命令将之前复制的数据复制到目标分区

    sudo cp -rf --preserve=all myimage/partition1/* /media/mount_point_partition1/sudo cp -rf --preserve=all myimage/partition2/* /media/mount_point_partition2/

  6. 复制回 MBR

    sudo dd if=myimage/mbr.img of=/dev/sdX bs=446 count=1

Now njoy Ur new disk!

现在享受你的新磁盘!

回答by mvp

Pretty good and simple way to deal with this is simply pipe it via gzip, something like this:

处理这个问题的非常好的和简单的方法就是通过 gzip 管道它,像这样:

# dd if=/dev/sdb | gzip > backup.img.gz

This way your image will be compressed and most likely unused space will be squeezed to almost nothing.

这样您的图像将被压缩,并且很可能未使用的空间将被压缩到几乎没有。

You would use this to restore such image back:

您可以使用它来恢复这样的图像:

# cat backup.img.gz | gunzip | dd of=/dev/sdb

One note: if you had a lot of files which were recently deleted, image size may be still large (deleting file does not necessarily zeroes underlying sectors). You can wipe free space by creating and immediately deleting large file containing zeros:

一个注意事项:如果您最近删除了很多文件,则图像大小可能仍然很大(删除文件不一定会将底层扇区归零)。您可以通过创建并立即删除包含零的大文件来擦除可用空间:

# cd /media/flashdrive
# dd if=/dev/zero of=bigfile bs=1M     # let it run and quit by disk full error
# rm bigfile

回答by Crumeniferus

Using the bsand countparameters of dd, you can limit the size of the image, as seen in step 2 of answer 1665017.

使用 的bscount参数dd,您可以限制图像的大小,如答案 1665017 的步骤 2 中所示

You may already know what size image you want to create. If not, you can get a good idea from df:

您可能已经知道要创建多大尺寸的图像。如果没有,您可以从df以下方面获得一个好主意:

df -H --total /

Substitute /with a space-separated list of all the mount points relating to the disk partitions.

替换/为与磁盘分区相关的所有挂载点的空格分隔列表。

A more accurate way might be to use fdiskor your preferred partition editor and get busy with a calculator.

更准确的方法可能是使用fdisk或您喜欢的分区编辑器并忙于使用计算器。

$ fdisk -l /dev/mmcblk0

Disk /dev/mmcblk0: 7.4 GiB, 7948206080 bytes, 15523840 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
Disklabel type: dos
Disk identifier: 0x00057540

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk0p1        2048  186367  184320   90M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      186368 3667967 3481600  1.7G  5 Extended
/dev/mmcblk0p5      188416 3667967 3479552  1.7G 83 Linux

Total used space in bytes = end sector of last partition X sector size (here that's 3667967 x 512).

以字节为单位的总已用空间 = 最后一个分区的结束扇区 X 扇区大小(这里是 3667967 x 512)。

Total used space in GB = total used space in bytes / 10243(here that's 1.749023 GB).

以 GB 为单位的总已用空间 = 以字节为单位的总已用空间 / 1024 3(此处为 1.749023 GB)。

If you decide, for example, that your image should be exactly 2 GB, the following command will do that:

例如,如果您决定您的图像应该正好是 2 GB,则以下命令将执行此操作:

dd if=/dev/mmcblk0 of=/path/to/pi_updated.img bs=1M count=2048

The resulting image will also include the random noise beyond the greatest extent of the last partition.

生成的图像还将包括超出最后一个分区最大范围的随机噪声。

Don't forget to sudothe above commands if your account doesn't already have sufficient privileges.

sudo如果您的帐户还没有足够的权限,请不要忘记执行上述命令。

For my purposes, I don't need an image that is perfectly trimmed down to the last bit of data so when the real size is 1.75 GB then a 2 GB image is near enough for me. This cuts out the other 6 GB (or 30 GB or whatever the device has spare) of unused space that would otherwise be in the image.

出于我的目的,我不需要完美修剪到最后一位数据的图像,因此当实际大小为 1.75 GB 时,2 GB 的图像对我来说已经足够了。这会减少图像中其他 6 GB(或 30 GB 或任何设备的空闲空间)的未使用空间。

I have seen advice in many places that ddshould notbe performed on a mounted partition and I followed that because it seems intuitively correct; it does seem rather like trying to sketch yourself making a sketch in a mirror with the sketch you're making also visible in the sketch. It's a bit sketchy.

我在很多地方看到的建议dd应该上挂载的分区,我遵循的,因为它似乎直觉正确执行; 这似乎更像是试图在镜子中绘制自己的草图,而您正在制作的草图也在草图中可见。这有点粗略。

回答by jturi

If you have a big SD card 16 GB, 32 GB etc but you want to save space with backup you can use:

如果您有 16 GB、32 GB 等大 SD 卡,但您想通过备份节省空间,您可以使用:

sudo apt-get install gnome-disk-utility

Open disk utility to check witch letter is your usb drive actually has:

打开磁盘实用程序检查女巫字母是您的 USB 驱动器实际上有:

gnome-disks

In my case a 32GB SD card with Raspbian image on it recognised as: /dev/sde

在我的例子中,一个带有 Raspbian 图像的 32GB SD 卡被识别为:/dev/sde

So I run with /dev/sde:

所以我运行/dev/sde

sudo dd bs=4M status=progress if=/dev/sde | gzip > \
/you-selected-full-path-here/raspberry-images/`date +%Y%m%d`_rpi_image_backup.gz
status=progress gives you progress bar indication
| gzip > compresses the 32 GB total size and not writing the empty space from the 32 GB
`date +%Y%m%d` writes today date in the filename

Output: 20190529_rpi_image_backup.gz

输出:20190529_rpi_image_backup.gz

And the size is only 3.5GB. If you want to write this image to a new SD card use:

而且大小只有3.5GB。如果要将此图像写入新的 SD 卡,请使用:

https://www.balena.io/etcher/

https://www.balena.io/etcher/

Also you can write this image made from 32 GB to a 16 GB or 8 GB disk, it is not complaining that the image is too large anymore.

您也可以将这个由 32 GB 制作的图像写入 16 GB 或 8 GB 磁盘,不再抱怨图像太大了。

回答by hakskel

After trying multiple different methods, I found the following article:

在尝试了多种不同的方法后,我找到了以下文章:

https://medium.com/platformer-blog/creating-a-custom-raspbian-os-image-for-production-3fcb43ff3630

https://medium.com/platformer-blog/creating-a-custom-raspbian-os-image-for-production-3fcb43ff3630

It's created to shrink and resize (on first boot) a raspberry-pi image but could be easily adjusted for any other Linux distribution. I successfully got it working with Debian 9 on a custom arm based chip.

它的创建是为了缩小和调整大小(在第一次启动时)一个 raspberry-pi 映像,但可以很容易地针对任何其他 Linux 发行版进行调整。我成功地让它在基于定制 arm 的芯片上与 Debian 9 一起工作。

The rc.localcreated by the pishrinkscript first uses raspi-configto resize the rootfs and then falls back to a method using parted(which I had to install ahead of time on my machine). I commented out the section of codewhere raspi-config.

rc.local由创建pishrink脚本首先用途raspi-config来调整根文件系统,然后回退到使用的方法parted(这我有我的机器上的提前安装)。我注释掉的代码部分地方raspi-config

My sd-card image was shrunk from 15 GB to 1.1 GB. I flashed the shrunk sd-card image with etcher. Took less than 5 mins as opposed to over half an hour for the full 15 GB image

我的 SD 卡映像从 15 GB 缩小到 1.1 GB。我用etcher 刷新了缩小的 SD 卡图像。花了不到 5 分钟,而完整的 15 GB 图像需要半小时以上