在linux上制作树莓派最小img镜像

    最近在移植debian的arm64位版本到树莓派3B+上,经过多天的努力终于完美的跑起来了。于是想要固化移植成果,需要给移植好的TF卡制作一个img备份镜像,其实最简单的方法就是直接使用dd命令将整个TF卡dd到一个xxx.img文件即可。

    但是这样生成的img文件是整个TF卡的大小,我使用的TF卡有16G,因而直接dd出来的img文件就有16G那么大,但是实际装在TF卡中的有用数据却只有几G,这样做出来的img文件就太浪费空间了,有效负载率太小,而且文件太大也不利于分享。

    最简单的想法就是能不能只把有用的数据备份成一个img文件,如正常img镜像一样,后期能直接将img文件dd到TF卡中就可以直接使用。于是就付诸行动,在网上找相关资料,虽然资料如云,但是重复转载的居多,甚至大多文章应该并没有人真正的验证过,根本就无法正常实施,还有一些就是需要安装一大堆各种工具,用各种复杂的组合操作,看得让人眼花......,所以没有办法,只能自己研究了

    经过自己的实验和研究,终于如愿实现想法,使用了较为简单的方法(仅用到了df、fdisk、e2fsck、resize2fs和dd这几个必要的常用工具命令),成功将16G的TF卡备份到了一个3.5G的img文件中,下面记录操作过程:

    1、查看TF卡中实际有效数据占用:在树莓派上使用df命令查看TF卡的实际占用情况,可以看到我的rootfs分区有15G,但是只使用了1.3G(所以说如果直接dd的话会有太多的无用数据被备份,如果使用的32G的TF卡就更严重,同时后期恢复那么大的文件到TF卡也会极度缓慢),知道了实际数据占用情况后将TF卡取出,并插入到电脑中(我这里使用的是linux主机)。

jiangxianxu@debianOnRpi:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  1.3G   13G  10% /
devtmpfs        455M     0  455M   0% /dev
tmpfs           460M     0  460M   0% /dev/shm
tmpfs           460M   12M  448M   3% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           460M     0  460M   0% /sys/fs/cgroup
/dev/mmcblk0p1  500M   41M  460M   9% /rpi-boot
tmpfs            92M     0   92M   0% /run/user/1001
jiangxianxu@debianOnRpi:~$ 

    2、查看并记录需要缩减分区的start扇区:插入电脑后使用fdisk -l命令查看分区信息,我插入电脑后TF是/dev/sdb,所以fdisk -l 后面的参数是/dev/sdb,可以看出我的TF卡上一共有两个分区,分区1是树莓派的boot分区,只占了500M,可以忽略不计,而我的sdb2分区(即上面的rootfs)大小是14G,所以下面的所有操作都是针对sdb2分区的,即需要对sdb2进行分区重建和分区大小调整。这里需要从信息中记录下/dev/sdb2的start扇区,后面要用到。我的sdb2 start扇区是1026048,每个扇区大小是512字节。

    

jiangxianxu@debian:/mnt$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x0ba16b58

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdb1          2048  1026047  1024000  500M  c W95 FAT32 (LBA)
/dev/sdb2       1026048 30386175 29360128   14G 83 Linux
jiangxianxu@debian:/mnt$ 

    3、调整分区占用大小:使用e2fsck和resize2fs命令缩减sdb2分区,由于前面已经知道sdb2中的实际数据占用只有1.3G,因而我们可以把分区缩减到只要比1.3G大即可,我这里是将其缩减到3G(实际可以更小,只要大于1.3G即可)。e2fsck命令是检查分区信息,resize2fs命令是调整分区大小(注意调用此命令的参数),我给的参数sudo resize2fs /dev/sdb2 3G的意思是将sdb2分区大小调整为3G。调整完成后再用fdisk查看分区的大小,会发现还是显示的是14G,到这里不要慌,虽然是显示14G,但是实际上已经被缩减了,后面第四步会让它现出缩减后的原形。

jiangxianxu@debian:/mnt$ sudo e2fsck -f /dev/sdb2
e2fsck 1.43.4 (31-Jan-2017)
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/sdb2: 43636/899584 files (0.2% non-contiguous), 394487/3670016 blocks
jiangxianxu@debian:/mnt$ sudo resize2fs /dev/sdb2 3G
resize2fs 1.43.4 (31-Jan-2017)
Resizing the filesystem on /dev/sdb2 to 786432 (4k) blocks.
The filesystem on /dev/sdb2 is now 786432 (4k) blocks long.

jiangxianxu@debian:/mnt$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x0ba16b58

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdb1          2048  1026047  1024000  500M  c W95 FAT32 (LBA)
/dev/sdb2       1026048 30386175 29360128   14G 83 Linux
jiangxianxu@debian:/mnt$ 

    4、重建分区大小为调整后的大小:用fdisk命令重建分区,让分区大小与我们缩减后的大小一致,这里的具体操作步骤为,调用fdisk /dev/sdb后首先用fdisk中的d命令删除sdb2分区,然后用n命令重新建立sdb2分区,按n后选择新建主分区(p),分区号与之前要一致,写2。接着就到了关键的地方了,建立分区需要填分区的start扇区,所以这里就要将步骤2记录下来的start扇区写到这里,这样才能保证数据不丢失。填写完start扇区后还要填end扇区,这里我们直接用+3G的方式让它自己去算结束扇区。马上又要进入关键步骤:回车后它会问你这里原来已经有一个ext4分区存在了,是否需要删除该分区标识?(红色字体部分)。这里一定要输入n,不要让它删除原有分区标识,不然就悲剧了...。接着就回车并用w命令让它写入我们重建分区的修改。完成分区重建后重新挂载分区成功,且用fdisk命令查看也已经变成了3G。到此准备工作就算完成了。

jiangxianxu@debian:/mnt$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1,2, default 2): 
Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 
Using default response p.
Partition number (2-4, default 2): 
First sector (1026048-31116287, default 1026048): 
Last sector, +sectors or +size{K,M,G,T,P} (1026048-31116287, default 31116287): +3G
Created a new partition 2 of type 'Linux' and of size 3 GiB.
Partition #2 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

jiangxianxu@debian:/mnt$ 
jiangxianxu@debian:/mnt$ sudo mount /dev/sdb2 /mnt/iso2/
jiangxianxu@debian:/mnt$ ll iso2/
total 92
drwxr-xr-x  2 root root  4096 Jun  2 00:23 bin
drwxr-xr-x  4 root root  4096 Jun  2 00:25 boot
drwxr-xr-x  2 root root  4096 Jun  2 22:12 boot-rpi
drwxr-xr-x  4 root root  4096 Jan  1  1970 dev
drwxr-xr-x 76 root root  4096 Jun  2 22:12 etc
drwxr-xr-x  3 root root  4096 Nov  4  2016 home
drwxr-xr-x 15 root root  4096 Jun  2 22:12 lib
drwx------  2 root root 16384 Jan  1  1970 lost+found
drwxr-xr-x  3 root root  4096 Jan  1  1970 media
drwxr-xr-x  3 root root  4096 Nov  4  2016 mnt
drwxr-xr-x  2 root root  4096 Jan  1  1970 opt
drwxr-xr-x  2 root root  4096 Feb 24 07:23 proc
drwx------  2 root root  4096 Jun  2 21:43 root
drwxr-xr-x  2 root root  4096 Jan  1  1970 run
drwxr-xr-x  2 root root  4096 Jun  2 00:23 sbin
drwxr-xr-x  2 root root  4096 Jan  1  1970 srv
drwxr-xr-x  2 root root  4096 Feb 24 07:23 sys
drwxrwxrwt  7 root root  4096 Jun  2 22:12 tmp
drwxr-xr-x 10 root root  4096 Jan  1  1970 usr
drwxr-xr-x 11 root root  4096 Jan  1  1970 var
jiangxianxu@debian:/mnt$ 
jiangxianxu@debian:/mnt$ sudo umount /mnt/iso2 
jiangxianxu@debian:/mnt$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x0ba16b58

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdb1          2048 1026047 1024000  500M  c W95 FAT32 (LBA)
/dev/sdb2       1026048 7317503 6291456    3G 83 Linux
jiangxianxu@debian:/mnt$ 

    5、将缩减后的TF卡镜像到一个img文件中:使用dd命令将我们重建并缩减了分区的TF备份,这一步网上其它资料一般会说的过于复杂,使用了各类这样那样的工具来备份,实际上我这里利用了dd工具可以指定从一个地方把指定长度的数据镜像到另一个地方这个特性来简单的达到我的目的。通过fdisk命令查看重建后所有分区占用到的最后一个扇区来计算需要dd镜像的数据长度,我这里sdb2的end扇区是最后一个占用的扇区,为7317503,但是由于扇区是从0开始编号的,所以实际整个TF卡上的分区一共占用了7317504个扇区,每个扇区是512字节,那么实际占用(7317504*512)/(1024*1024) = 3573MB,所以我们需要使用dd命令从TF卡中dd出来3573MB数据到img文件即可(命令在示例中),dd命令的bs参数代表每次dd多少数据,count代表dd多少次,经过前面的计算,我这里填的是每次dd 1MB一共dd 3573次。dd完成后我们缩减后的整个TF卡就已经备份到debian-on-rpi.img文件中了,通过fdisk命令查看该文件实际上与查看缩减后的TF是一样的,同时将该文件挂载到系统中也可以看到数据是真实有效的。到这里备份工作就完成了,我们成功的将一个16G TF卡中的树莓派系统备份到了一个3.5G中的img文件中。后期只需要将该img文件dd恢复到TF卡中即可正常启动(亲自实验过没有问题)。

jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x0ba16b58

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdb1          2048 1026047 1024000  500M  c W95 FAT32 (LBA)
/dev/sdb2       1026048 7317503 6291456    3G 83 Linux
jiangxianxu@debian:/data/work/raspberrypi/debian$ 
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo dd if=/dev/sdb of=debian-on-rpi.img bs=1M count=3573
3573+0 records in
3573+0 records out
3746562048 bytes (3.7 GB, 3.5 GiB) copied, 200.069 s, 18.7 MB/s
jiangxianxu@debian:/data/work/raspberrypi/debian$ 
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo fdisk -l debian-on-rpi.img
Disk debian-on-rpi.img: 3.5 GiB, 3746562048 bytes, 7317504 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: 0x0ba16b58

Device             Boot   Start     End Sectors  Size Id Type
debian-on-rpi.img1         2048 1026047 1024000  500M  c W95 FAT32 (LBA)
debian-on-rpi.img2      1026048 7317503 6291456    3G 83 Linux
jiangxianxu@debian:/data/work/raspberrypi/debian$ 
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo mount -o loop,offset=1048576,sizelimit=524288000 debian-on-rpi.img /mnt/iso1/
jiangxianxu@debian:/data/work/raspberrypi/debian$ ll /mnt/iso1/
total 40080
-rwxr-xr-x 1 root root    24597 Jun  1 23:28 bcm2710-rpi-3-b-plus.dtb
-rwxr-xr-x 1 root root    52064 May 30 22:59 bootcode.bin
-rwxr-xr-x 1 root root      145 May 30 23:00 cmdline.txt
-rwxr-xr-x 1 root root       77 Jun  2 10:42 config.txt
-rwxr-xr-x 1 root root     2599 May 30 22:59 fixup_cd.dat
-rwxr-xr-x 1 root root     6575 May 30 22:59 fixup.dat
-rwxr-xr-x 1 root root     9726 May 30 22:59 fixup_db.dat
-rwxr-xr-x 1 root root     9730 May 30 22:59 fixup_x.dat
-rwxr-xr-x 1 root root 13496832 Jun  1 22:48 Image
drwxr-xr-x 2 root root    16384 May 30 22:59 overlays
-rwxr-xr-x 1 root root   673444 May 30 22:59 start_cd.elf
-rwxr-xr-x 1 root root  4968292 May 30 22:59 start_db.elf
-rwxr-xr-x 1 root root  2825124 May 30 22:59 start.elf
-rwxr-xr-x 1 root root  3912164 May 30 22:59 start_x.elf
-rwxr-xr-x 1 root root 14973440 May 30 22:59 ubuntu-kernel
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo mount -o loop,offset=525336576,sizelimit=3221225472 debian-on-rpi.img /mnt/iso2/ 
jiangxianxu@debian:/data/work/raspberrypi/debian$ ll /mnt/iso2/
total 92
drwxr-xr-x  2 root root  4096 Jun  2 00:23 bin
drwxr-xr-x  4 root root  4096 Jun  2 00:25 boot
drwxr-xr-x  2 root root  4096 Jun  2 22:12 boot-rpi
drwxr-xr-x  4 root root  4096 Jan  1  1970 dev
drwxr-xr-x 76 root root  4096 Jun  2 21:58 etc
drwxr-xr-x  3 root root  4096 Nov  4  2016 home
drwxr-xr-x 15 root root  4096 Jun  2 22:12 lib
drwx------  2 root root 16384 Jan  1  1970 lost+found
drwxr-xr-x  3 root root  4096 Jan  1  1970 media
drwxr-xr-x  3 root root  4096 Nov  4  2016 mnt
drwxr-xr-x  2 root root  4096 Jan  1  1970 opt
drwxr-xr-x  2 root root  4096 Feb 24 07:23 proc
drwx------  2 root root  4096 Jun  2 21:43 root
drwxr-xr-x  2 root root  4096 Jan  1  1970 run
drwxr-xr-x  2 root root  4096 Jun  2 00:23 sbin
drwxr-xr-x  2 root root  4096 Jan  1  1970 srv
drwxr-xr-x  2 root root  4096 Feb 24 07:23 sys
drwxrwxrwt  7 root root  4096 Jun  2 22:16 tmp
drwxr-xr-x 10 root root  4096 Jan  1  1970 usr
drwxr-xr-x 11 root root  4096 Jan  1  1970 var
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo umount /mnt/iso1 
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo umount /mnt/iso2 
jiangxianxu@debian:/data/work/raspberrypi/debian$ 

    6、恢复TF卡原有大小:由于我们调整了原TF卡上的分区,那么它的实际可用空间就只有3G了,所以我们还需要将它恢复成14G,实际上这里就是上面调整分区和重建分区的逆过程,先删除分区,重建一个14G的分区,然后再调整分区的大小即可。这里需要注意一下顺序,要先重建14G的分区后才能调整分区大小为14G。当然,如果你的TF卡不是16G而且你想让这个分区占用你剩余的全部空间,那么在输入end扇区的时候直接使用默认值即可,调整分区的时候就不要加分区大小参数,只需使用命令sudo resize2fs /dev/sdb2即可,后面都用默认值。另外将img文件dd到TF卡上去之后得到的TF卡就是我们缩减分区后的那个TF卡,所以也需要用同样的方法让镜像在新的TF卡恢复到自己希望的空间占用。到此,所有步骤完成。

jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x0ba16b58

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdb1          2048 1026047 1024000  500M  c W95 FAT32 (LBA)
/dev/sdb2       1026048 7317503 6291456    3G 83 Linux
jiangxianxu@debian:/data/work/raspberrypi/debian$ 
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1,2, default 2): 
Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (1026048-31116287, default 1026048): 1026048
Last sector, +sectors or +size{K,M,G,T,P} (1026048-31116287, default 31116287): +14G

Created a new partition 2 of type 'Linux' and of size 14 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo e2fsck -f /dev/sdb2
e2fsck 1.43.4 (31-Jan-2017)
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/sdb2: 43636/192768 files (0.6% non-contiguous), 347813/786432 blocks
jiangxianxu@debian:/data/work/raspberrypi/debian$ 
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo resize2fs /dev/sdb2 14G
resize2fs 1.43.4 (31-Jan-2017)
Resizing the filesystem on /dev/sdb2 to 3670016 (4k) blocks.
The filesystem on /dev/sdb2 is now 3670016 (4k) blocks long.
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo mount /dev/sdb2 /mnt/iso2/
jiangxianxu@debian:/data/work/raspberrypi/debian$ ll /mnt/iso2/
total 92
drwxr-xr-x  2 root root  4096 Jun  2 00:23 bin
drwxr-xr-x  4 root root  4096 Jun  2 00:25 boot
drwxr-xr-x  2 root root  4096 Jun  2 22:12 boot-rpi
drwxr-xr-x  4 root root  4096 Jan  1  1970 dev
drwxr-xr-x 76 root root  4096 Jun  2 21:58 etc
drwxr-xr-x  3 root root  4096 Nov  4  2016 home
drwxr-xr-x 15 root root  4096 Jun  2 22:12 lib
drwx------  2 root root 16384 Jan  1  1970 lost+found
drwxr-xr-x  3 root root  4096 Jan  1  1970 media
drwxr-xr-x  3 root root  4096 Nov  4  2016 mnt
drwxr-xr-x  2 root root  4096 Jan  1  1970 opt
drwxr-xr-x  2 root root  4096 Feb 24 07:23 proc
drwx------  2 root root  4096 Jun  2 21:43 root
drwxr-xr-x  2 root root  4096 Jan  1  1970 run
drwxr-xr-x  2 root root  4096 Jun  2 00:23 sbin
drwxr-xr-x  2 root root  4096 Jan  1  1970 srv
drwxr-xr-x  2 root root  4096 Feb 24 07:23 sys
drwxrwxrwt  7 root root  4096 Jun  2 22:16 tmp
drwxr-xr-x 10 root root  4096 Jan  1  1970 usr
drwxr-xr-x 11 root root  4096 Jan  1  1970 var
jiangxianxu@debian:/data/work/raspberrypi/debian$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: 0x0ba16b58

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdb1          2048  1026047  1024000  500M  c W95 FAT32 (LBA)
/dev/sdb2       1026048 30386175 29360128   14G 83 Linux
jiangxianxu@debian:/data/work/raspberrypi/debian$

 

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值