如何在 Linux 中挂载多分区磁盘映像?

如何在 Linux 中挂载多分区磁盘映像?

我下载了一个有两个分区的原始 SD 卡映像。

当我尝试将其直接放入 SD 卡时,它导致了一些文件系统错误。我不确定是卡有问题还是图像有问题。

有没有办法检查此图像而不将其写入物理卡?比如尝试单独安装分区或检查表格?

答案1

您可以使用陣容或者部分为映像上的分区创建循环设备,然后挂载它们。因此,请执行以下操作之一:

$ sudo kpartx -v -a file.iso
add map loop0p1 (253:17): 0 8382464 linear 7:1 2048
$ mount /dev/mapper/loop0p1 ./mnt_point
...  do something with the partition  ...
$ umount ./mnt_point
$ kpartx -d -v file.iso
del devmap : loop0p1
loop deleted : /dev/loop0

或者:

$ sudo partx -a -v file.iso
partition: none, disk: file.iso, lower: 0, upper: 0
Trying to use '/dev/loop0' for the loop device
/dev/loop0: partition table type 'dos' detected
range recount: max partno=1, lower=0, upper=0
/dev/loop0: partition #1 added
$ mount /dev/loop0p1 ./mnt_point
...  do something with the partition  ...
$ umount /dev/loop0p1 ./mnt_point
$ sudo partx -d -v /dev/loop0
partition: none, disk: /dev/loop0, lower: 0, upper: 0
/dev/loop0: partition #1 removed

也可以看看我如何装载磁盘映像?

答案2

losetup -Pf在 util-linux >= 2.21(Ubuntu 16.04)中

sudo losetup -Pf disk.img
sudo mkdir /mnt/loop0p1
sudo mount /dev/loop0p1 /mnt/loop0p1

查看失败(8)手册页以及https://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device/673257#673257

losetup -P自动化

以下是进一步实现自动化的功能。用法:

$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2

$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there

$ sudo losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                                                      DIO
/dev/loop1         0      0         0  0 /full/path/to/my.img

$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0
loop0

来源:

los() (
  img="$1"
  dev="$(sudo losetup --show -f -P "$img")"
  echo "$dev"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    echo "$dst"
    sudo mkdir -p "$dst"
    sudo mount "$part" "$dst"
  done
)
losd() (
  dev="/dev/loop$1"
  for part in "$dev"?*; do
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  done
  sudo losetup -d "$dev"
)

答案3

@Catskul 和 @Cristian Ciupitu 的回答非常好,但是它缺少 loop unmount 命令。因此,如果您必须制作第二幅图像,那么您最终将使用 loop1、loop2 等。

你可以通过调用losetup来检查哪些循环设备连接到了哪些图像:

pk:~# partx -v -a /home/pkolmann/img/Test.img
partition: none, disk: /home/pkolmann/img/Test.img, lower: 0, upper: 0
Trying to use '/dev/loop1' for the loop device
/dev/loop1: partition table type 'dos' detected
range recount: max partno=2, lower=0, upper=0
/dev/loop1: partition #1 added
/dev/loop1: partition #2 added
pk:~# losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                    DIO LOG-SEC
/dev/loop1         0      0         0  0 /home/pkolmann/img/Test.img   0     512
/dev/loop0         0      0         0  0 /home/pkolmann/img/Test.img   0     512

卸载分区后

pk:~# partx -v -d /dev/loop0
partition: none, disk: /dev/loop0, lower: 0, upper: 0
/dev/loop0: partition #1 removed
/dev/loop0: partition #2 removed
pk:~# partx -v -d /dev/loop1
partition: none, disk: /dev/loop1, lower: 0, upper: 0
/dev/loop1: partition #1 removed
/dev/loop1: partition #2 removed

循环设备仍在使用:

pk:~# losetup
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                    DIO LOG-SEC
/dev/loop1         0      0         0  0 /home/pkolmann/img/Test.img   0     512
/dev/loop0         0      0         0  0 /home/pkolmann/img/Test.img   0     512

这些需要额外删除:

wspk:~# losetup -d /dev/loop0
wspk:~# losetup -d /dev/loop1
wspk:~# losetup

答案4

您可以尝试:

安装-t类型-o循环./image/mnt

其中“type”= fs type,“image”是下载文件的名称

相关内容