挂载磁盘映像

挂载磁盘映像

我正在尝试创建磁盘映像并安装分区以进行进一步的工作,但遇到了令人沮丧的错误消息:“错误的 fs 类型、错误的选项、错误的超级块”。以下是我采取的步骤和收到的输出:

$ dd if=/dev/zero of=test.img status=progress bs=200M count=1
1+0 records in
1+0 records out
209715200 bytes (210 MB, 200 MiB) copied, 0.191841 s, 1.1 GB/s

$ mkfs.ext4 test.img
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done                            
Creating filesystem with 204800 1k blocks and 51200 inodes
Filesystem UUID: b4c4d7d0-0341-4f48-880f-de2110f36825
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729

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

$ fdisk test.img

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

The device contains 'ext4' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.

Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0xca60123f.

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

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

Command (m for help): w
The partition table has been altered.
Syncing disks.

$ fdisk -lu test.img
Disk test.img: 200 MiB, 209715200 bytes, 409600 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: 0xca60123f

Device     Boot Start    End Sectors  Size Id Type
test.img1        2048 409599  407552  199M 83 Linux

$ sudo mount test.img mount-point/ -o loop,offset=$((512*2048))
mount: /home/reyuki/code/linux-system/mount-point: wrong fs type, bad option, bad superblock on /dev/loop1, missing codepage or helper program, or other error.
       dmesg(1) may have more information after failed mount system call.

$ sudo dmesg
[ 8616.381787] loop1: detected capacity change from 0 to 407552
[ 8616.384637] EXT4-fs (loop1): VFS: Can't find ext4 filesystem
[ 8616.384782] EXT4-fs (loop1): VFS: Can't find ext4 filesystem
[ 8616.384874] EXT4-fs (loop1): VFS: Can't find ext4 filesystem
[ 8616.385973] ISOFS: Unable to identify CD-ROM format.

我是否错过了流程中的关键步骤?任何见解或建议都将不胜感激!

答案1

您使用文件系统格式化了整个映像,然后创建了一个涵盖整个相同映像的主分区,并假定文件系统仍然存在...好吧,当在其上创建主分区时,文件系统就被破坏了,而映像剩下一个未格式化的分区。

对于包含分区的磁盘映像,可以使用更简单的方法,使用官方存储库提供的工具可以读取磁盘映像上的分区表,例如kpartx...

因此,基本上,您将使用 eg 创建图像文件dd,然后像您所做的那样使用 eg 对其进行分区fdisk...然后使用类似的工具kpartx读取分区表并将其映射到系统上创建块设备,然后您可以像平常一样处理、格式化甚至挂载它:

$ sudo kpartx -av test.img
add map loop1p1 (252:0): 0 407552 linear 7:1 2048

loop1p1...请注意您可以访问的分区的块设备文件的报告名称,/dev/mapper/例如将其格式化如下:

$ sudo mkfs.ext4 /dev/mapper/loop1p1 
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done                            
Creating filesystem with 50944 4k blocks and 50944 inodes
Filesystem UUID: d02236d0-27c3-46ab-8806-a9f936f6d947
Superblock backups stored on blocks: 
    32768

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

...然后像这样安装它:

$ sudo mount /dev/mapper/loop1p1  mount-point/
$
$
$ findmnt -T mount-point
TARGET                        SOURCE              FSTYPE OPTIONS
/home/ubuntu/test/mount-point /dev/mapper/loop1p1 ext4   rw,relatime

答案2

我遇到了类似的问题,USB 硬盘显示坏文件系统等。我使用 Ubuntu 的“磁盘”实用程序检查文件系统(没问题),然后修复文件系统(做了一些更正)。使用磁盘安装(左下角的黑色方块,齿轮旁边,这就是我得到上面的结果)。
一切看起来都很好,但我的脚本仍然不起作用(直到出现错误之前工作正常。我还收到一条错误消息,提示硬盘为 RO(只读)。chmod - 未修复。然后我注意到一个细节:有两个外部驱动器:一个是“WD_10TB”(我在命名方面没有什么创意!),另一个是 WD_10TB1”。某些东西更改/更新了卷名。将挂载点、备份脚本指向“新”驱动器,为我消除了“错误的 fs 类型、错误的选项、错误的超级块”错误消息。

答案3

$ losetup -f test.img
$ losetup -a
/dev/loop1: []: (/home/reyuki/code/linux-system/test.img)
/dev/loop0: []: (/home/reyuki/code/distro/boot.img)
$ sudo fsck -fv /dev/loop1
fsck from util-linux 2.39.3
e2fsck 1.47.0 (5-Feb-2023)
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
Block bitmap differences:  +(40961--41219) +(57345--57603) +(73729--73987)
Fix<y>? yes
Free blocks count wrong for group #3 (8192, counted=7933).
Fix<y>? yes

/dev/loop1: ***** FILE SYSTEM WAS MODIFIED *****

          12 inodes used (0.02%, out of 51200)
           0 non-contiguous files (0.0%)
           0 non-contiguous directories (0.0%)
             # of inodes with ind/dind/tind blocks: 0/0/0
             Extent depth histogram: 4
       18565 blocks used (9.06%, out of 204800)
           0 bad blocks
           0 large files

           0 regular files
           2 directories
           0 character device files
           0 block device files
           0 fifos
           0 links
           0 symbolic links (0 fast symbolic links)
           0 sockets
------------
           2 files
$ sudo mount test.img mount-point/ -o loop
$ ls mount-point/
lost+found

fsck由此想到使用回答 但仍然不知道为什么新创建的磁盘映像已损坏?这似乎很奇怪,因为该文件刚刚创建

相关内容