让我们在 Ubuntu 中处理磁盘映像文件。

我在原生 Ubuntu 和 WSL 上的 Ubuntu 上进行了尝试。
我想知道它是否可以用 WSL2 完成......
总而言之,我这次使用的工具,如loopback mounts、fdisk、trancate、dd、mount、mkfs,都没有问题。

环境

  • Ubuntu 20.04.4 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)
  • Ubuntu 22.04 LTS 桌面版

工作

这项工作假设一个用于 RaspberryPi 的 SD 卡映像。
树莓派的 SD 卡有如下配置

  • 两个分区
  • 第一个分区:/boot
    • vfat
    • 存储配置、启动等所需的文件。
    • 尺寸小
  • 第二个分区:/
    • 分机4
    • 根文件系统
    • 通常为除 /boot 分区之外的所有内容分配空间

创建工作区并移动

$ mkdir ~/working
$ cd ~/working

尝试制作图像

dd的使用方法


$ dd bs=1024k count=100 if=/dev/zero of=image.img
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.0015547 s, 674 MB/s
$ ls -al image.img
-rw-r--r-- 1 nanbuwks nanbuwks 1048576 Sep 23 11:53 image.img

尝试用截断来实现

$ truncate -s 100M image.img
$ ls -al image.img
-rw-r--r-- 1 nanbuwks nanbuwks 104857600 Sep 23 12:01 image.img

你可以毫无问题地制作两者!

dd 在很多方面都很危险,所以 truncate 可能会更好。

环回挂载

挂载镜像文件。

使用 losttup 检查免费的环回设备

$ losetup -f
/dev/loop0

将 image.img 分配给它。如果是这样,它就会变成 Permission denied,所以我 sudo。


$ losetup /dev/loop0 image.img
losetup: /dev/loop0: failed to set up loop device: Permission denied
$ sudo losetup /dev/loop0 image.img
[sudo] password for nanbuwks:

使用 fdisk 创建一个分区。
RaspberryPi 的分区包括一个小的 VFAT 分区作为 /boot 和一个 ext4 分区作为 /。相应地设置。

$ sudo fdisk /dev/loop0

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

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x56b28ef8.

Command (m for help): p
Disk /dev/loop0: 100 MiB, 104857600 bytes, 204800 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: 0x56b28ef8

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-204799, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-204799, default 204799): +10M

Created a new partition 1 of type 'Linux' and of size 10 MiB.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (22528-204799, default 22528):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (22528-204799, default 204799):

Created a new partition 2 of type 'Linux' and of size 89 MiB.

Command (m for help): t
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): c

Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'.

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 83

Changed type of partition 'Linux' to 'Linux'.

Command (m for help): a
Partition number (1,2, default 2): 1

The bootable flag on partition 1 is enabled now.

Command (m for help): p
Disk /dev/loop0: 100 MiB, 104857600 bytes, 204800 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: 0x56b28ef8

Device       Boot Start    End Sectors Size Id Type
/dev/loop0p1 *     2048  22527   20480  10M  c W95 FAT32 (LBA)
/dev/loop0p2      22528 204799  182272  89M 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

一次性删除 image.img 的关联

$ sudo losetup -d /dev/loop0

/dev/loop0p1 および /dev/loop0p2 をマウントするためには kpartx を使ったり オフセットを指定してマウントしたりする方法もありますが、今回は以下のようにしました。

$ sudo losetup -P -f --show image.img
/dev/loop0

设备文件会自动创建。

$ ls /dev/loop0*
/dev/loop0  /dev/loop0p1  /dev/loop0p2

创建文件系统。还要指定卷标。

$ sudo mkfs.vfat -F 32 -n boot /dev/loop0p1
mkfs.fat 4.1 (2017-01-24)
mkfs.fat: warning - lowercase labels might not work properly with DOS or Windows
WARNING: Not enough clusters for a 32 bit FAT!
$ sudo mkfs.ext4 -L root /dev/loop0p2
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done
Creating filesystem with 22784 4k blocks and 22784 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

创建一个挂载点来挂载和写入。

$ mkdir root
$ mkdir boot
$ sudo mount /dev/loop0p1 boot
$ sudo mount /dev/loop0p2 root

完成后,像这样清理:

$ sudo umount root
$ sudo umount boot
$ sudo losetup -d /dev/loop0

原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308626883.html

相关文章: