如何格式化 img 文件内的分区?

Mik*_*kov 17 filesystems partition disk-image

img通过以下命令创建了一个文件:

dd if=/dev/zero bs=2M count=200 > binary.img
Run Code Online (Sandbox Code Playgroud)

它只是一个带零的文件,但我可以使用它fdisk并创建一个分区表:

# fdisk binary.img

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

Command (m for help): p
Disk binary.img: 400 MiB, 419430400 bytes, 819200 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: 0x51707f21
Run Code Online (Sandbox Code Playgroud)

并且,让我们说,一个分区:

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): 
First sector (2048-819199, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-819199, default 819199): 

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

Command (m for help): w
The partition table has been altered.
Syncing disks.
Run Code Online (Sandbox Code Playgroud)

当我检查分区表时,我得到以下结果:

Command (m for help): p
Disk binary.img: 400 MiB, 419430400 bytes, 819200 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: 0x7f3a8a6a

Device      Boot Start    End Sectors  Size Id Type
binary.img1       2048 819199  817152  399M 83 Linux
Run Code Online (Sandbox Code Playgroud)

所以分区存在。当我尝试通过 gparted 格式化此分区时,出现以下错误:

在此处输入图片说明

我不知道它为什么要查找binary.img1,也不知道如何从命令 live 格式化分区。

有谁知道如何使用 ext4 文件系统格式化它?

roa*_*ima 19

您可以通过环回功能访问磁盘映像及其各个分区。您已经发现某些磁盘实用程序可以(合理地)在磁盘映像上愉快地运行。然而,mkfs不是其中之一(但奇怪的mount是)。

这里是输出fdisk -lu binary.img

Disk binary.img: 400 MiB, 419430400 bytes, 819200 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
...

Device           Boot Start    End Sectors  Size Id Type
binary.img1            2048 819199  817152  399M 83 Linux
Run Code Online (Sandbox Code Playgroud)

要访问您创建的分区,您有几个选择

  1. 显式路由

    losetup --offset $((512*2048)) --sizelimit $((512*817152)) --show --find binary.img
    /dev/loop0
    
    Run Code Online (Sandbox Code Playgroud)

    输出/dev/loop0是已分配的循环设备的名称。该--offset参数只是分区的偏移量 ( Start) 乘以扇区大小 ( 512)。而--sizelimit是分区的大小,您可以通过以下方式计算它: End-Start+1,即 819199-2048+1=817152 ,并且该数字还必须乘以扇区大小。

    You can then use /dev/loop0 as your reference to the partition:

    mkfs -t ext4 -L img1 /dev/loop0
    mkdir -p /mnt/img1
    mount /dev/loop0 /mnt/img1
    ...
    umount /mnt/img1
    losetup -d /dev/loop0
    
    Run Code Online (Sandbox Code Playgroud)
  2. The implicit route

    losetup --partscan --show --find binary.img
    /dev/loop0
    
    Run Code Online (Sandbox Code Playgroud)

    The output /dev/loop0 is the name of the primary loop device that has been allocated. In addition, the --partscan option tells the kernel to scan the device for a partition table and assign subsidiary loop devices automatically. In your case with the one partition you also get /dev/loop0p1, which you can then use as your reference to the partition:

    mkfs -t ext4 -L img1 /dev/loop0p1
    mkdir -p /mnt/img1
    mount /dev/loop0p1 /mnt/img1
    ...
    umount /mnt/img1
    losetup -d /dev/loop0
    
    Run Code Online (Sandbox Code Playgroud)

  • 有些数学有什么问题?此外,很高兴知道您可以通过这种方式轻松获得正确的扇区编号,以防万一...... (2认同)

sol*_*iCe 14

还有另一种方法在一般情况下,使用要做到这一点kpartxKDE的)

sudo kpartx -a binary.img
Run Code Online (Sandbox Code Playgroud)

现在您应该将所有分区设备定义/dev/mapperloop0p1 , loop0p2 , ...

进而

sudo mkfs.ext4 /dev/mapper/loop0p1
Run Code Online (Sandbox Code Playgroud)

Optionnaly,当你完成后,你也可以运行

sudo kpartx -d binary.img
Run Code Online (Sandbox Code Playgroud)

摆脱loop0p?设备

  • 不知道为什么这没有更多选票。海事组织这是最好的答案......! (2认同)