如何同时从磁盘映像挂载多个分区?


9

我试图安装根目录和引导分区Raspbian图像:

mount -v -o offset="70254592" -t ext4 /mnt/X/raspbian-jessie.img /tmp/raspbian
mount -v -o offset="4194304" -t vfat /mnt/X/raspbian-jessie.img /tmp/boot

安装引导程序,在安装根目录后会导致:

mount: /mnt/X/raspbian-jessie.img: overlapping loop device exists

如何在一个磁盘映像上同时安装多个分区?(对于磁盘,显然有可能,为什么不对文件?)

Answers:


6

losttup 2.21 -P选项

losetup -P -f --show my.img

/dev/loopXpY每个分区创建一个。

优点:许多发行版中预装了可执行文件(util-linux软件包)。

缺点:最近的选项,在Ubuntu 14.04中不存在,在此之前请使用kpartx:https ://unix.stackexchange.com/a/405639/32558

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"
)

7

您需要指定分区的长度以避免重叠。选项sizelimit,看看man mountman losetup


1

使用kpartx工具。它将映射映像分区/dev/mapper,您可以使用该映像分区直接挂载。

$ sudo kpartx -a disk.img
$ sudo mount -o loop /dev/mapper/loop0p2 /mnt

PS。完成后,请不要忘记删除映射:sudo kpartx -d disk.img

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.