从整个磁盘(设备)的映像中挂载单个逻辑分区

从整个磁盘(设备)的映像中挂载单个逻辑分区

下面的方法是从磁盘映像安装主分区,但无法在扩展分区下安装逻辑分区。有办法解决这个问题吗?我在扩展分区下有 3 个逻辑分区,使用下面的步骤无法安装这些分区。

获取镜像的分区布局

$ sudo fdisk -lu sda.img
...
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
...
  Device Boot      Start         End      Blocks   Id  System
sda.img1   *          56     6400000     3199972+   c  W95 FAT32 (LBA)

计算从图像开始到分区开始的偏移量

扇区大小*开始=(在这种情况下)56 * 512 =28672

使用偏移量将其挂载到 /dev/loop0

sudo losetup -o 28672 /dev/loop0 sda.img

现在分区位于/dev/loop0。 你可以文件系统检查它,安装它等等

sudo fsck -fv /dev/loop0
sudo mount /dev/loop0 /mnt

卸载

sudo umount /mnt
sudo losetup -d /dev/loop0

答案1

这是我用来从映像文件挂载分区的脚本。有关使用信息,请参阅脚本开头的注释。

#!/bin/bash

# mount_image, a program that mounts a specific partition from a RAW
# disk image file, such as a full-disk dd copy or a file used by QEMU.
# Note that compressed and other space-saving formats (qcaw2, etc.)
# will NOT work!

# Use:
# mount_image image_file partition_number mount_point
#
# For instance,
#
# mount_image image.raw 2 /mnt/shared
#
# mounts partition 2 from image.raw at /mnt/shared.

# This program relies on my GPT fdisk (gdisk) program to help identify
# partitions. I could have used regular fdisk, but this would have
# limited the program to working with MBR-formatted disks. With gdisk,
# both MBR- and GPT-formatted disks will work.

gdisk -l $1 > /tmp/mount_image.tmp
let StartSector=`egrep "^   $2|^  $2" /tmp/mount_image.tmp | fmt -u -s | sed -e 's/^[ \t]*//' | head -1 | cut -d " " -f 2`

let StartByte=($StartSector*512)

echo "Mounting partition $2, which begins at sector $StartSector"

mount -o loop,offset=$StartByte $1 $3

rm /tmp/mount_image.tmp

相关内容