如何找到一个img文件的类型并挂载它?

Lui*_*igi 46 filesystems partition mount

我必须挂载一个 .img 文件,但我不知道它是什么类型的 .img。我怎样才能弄清楚它是什么类型的 .img 文件?

# mount -t auto -o ro,loop gmapsupp.img /mnt/iso/
mount: you must specify the filesystem type
# file -k gmapsupp.img 
gmapsupp.img: x86 boot sector, code offset 0x0
#
Run Code Online (Sandbox Code Playgroud)

slm*_*slm 51

尝试运行命令fdisk -l <img file>。通常,如果.img文件是来自 KVM VM 的整个磁盘,那么它们在技术上是一个虚拟磁盘。

例子

我有一个 CentOS KVM VM,它显示如下file命令:

$ file centostest.img 
centostest.img: x86 boot sector; partition 1: ID=0x83, active, starthead 1, startsector 63, 208782 sectors; partition 2: ID=0x8e, starthead 0, startsector 208845, 20755980 sectors, code offset 0x48
Run Code Online (Sandbox Code Playgroud)

运行fdisk它:

$ sudo /sbin/fdisk -lu /kvm/centostest.img
last_lba(): I don't know how to handle files with mode 81ed
You must set cylinders.
You can do this from the extra functions menu.

Disk /kvm/centostest.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

              Device Boot      Start         End      Blocks   Id  System
/kvm/centostest.img1   *          63      208844      104391   83  Linux
/kvm/centostest.img2          208845    20964824    10377990   8e  Linux LVM
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1304, 254, 63)
Run Code Online (Sandbox Code Playgroud)

如果您想挂载这些分区之一,您可以按如下方式进行:

fdisk(柱面输出)
  • 块大小为 512 字节,起始块为 63。
  • 偏移量为 512 * 63 = 32256。
fdisk(扇区输出)
  • 块大小为 512 字节,起始块为 1。
  • 偏移量为 512 * 1 = 512。

所以挂载命令是:

在气缸中
$ mount -o loop,offset=32256 centostest.img /mnt/tmp
Run Code Online (Sandbox Code Playgroud)

挂载另一个分区 (512 * 208845 = 106928640):

$ mount -o loop,offset=106928640 centostest.img /mnt/tmp
Run Code Online (Sandbox Code Playgroud) 在部门
$ mount -o loop,offset=512 centostest.img /mnt/tmp
Run Code Online (Sandbox Code Playgroud)

挂载另一个分区 (512 * 14 = 7168):

$ mount -o loop,offset=7168 centostest.img /mnt/tmp
Run Code Online (Sandbox Code Playgroud)

笔记

这仅在 mount 可以确定您尝试挂载的“分区”内的文件系统类型时才有效。例如,您可能需要包含-t auto,或者具体说明mount就是这样-t ext4

参考


Fay*_*afa 25

使用parted来识别偏移值。

root@mysystem:~/# parted myimage.img
GNU Parted 2.3
Using /root/myimage.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) u
Unit?  [compact]? B
(parted) print
Model:  (file)
Disk /root/myimage.img: 8589934592B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start        End          Size         Type     File system     Flags
 1      32256B       254983679B   254951424B   primary  ext3            boot
 2      254983680B   1274918399B  1019934720B  primary  linux-swap(v1)
 3      1274918400B  3323013119B  2048094720B  primary  ext3
 4      3323013120B  8587192319B  5264179200B  primary  ext3

(parted) 
Run Code Online (Sandbox Code Playgroud)

现在您有了偏移值,您可以使用它们来挂载文件系统。

# mount -o loop,offset=32256 myimage.img /mnt/disk1 
# mount -o loop,offset=1274918400 myimage.img /mnt/disk2
# mount -o loop,offset=3323013120 myimage.img /mnt/disk3
Run Code Online (Sandbox Code Playgroud)


Lee*_*low 7

我意识到这个线程很旧,但如果您的系统较新,例如 Ubuntu 20.04,您可以使用 udiskctl:

udisksctl loop-setup --file something.img
Run Code Online (Sandbox Code Playgroud)

它将为 img 中的分区创建循环设备并安装它们。干杯。

  • 确认的。这在 Ubuntu 20.10 中非常有效。 (2认同)

Pie*_*erz 6

挂载 img 文件的另一种非常简单的方法是使用kpartx工具(来自kpartx包)。kpartx 手册页的解释(使用 sudo/root 运行):

要挂载原始磁盘映像中的所有分区:

          kpartx -av disk.img
Run Code Online (Sandbox Code Playgroud)

这将输出如下行:

          loop3p1 : 0 20964762 /dev/loop3 63
Run Code Online (Sandbox Code Playgroud)

Loop3p1 是 /dev/mapper 下的设备文件的名称,您可以使用它来访问分区,例如对其进行 fsck:

          fsck /dev/mapper/loop3p1
Run Code Online (Sandbox Code Playgroud)

将该设备安装在/mnt

mount /dev/mapper/loop3p1 /mnt
Run Code Online (Sandbox Code Playgroud)
   When you're done, you need to remove the devices:

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