挂载 Xen 的镜像文件

由于 domU 跟 dom0 的 IP 产生了冲突,导致一开虚拟机就会出现假死的情况,后来考虑能不能像 ISO 文件一样先挂载到某个目录,对其网络参数进行直接修改,发现可以通过 mount 的 offset 完成。

通过下面可以看到,由于该镜像有多于一个的分区,导致其不可以直接以 ISO 文件的方式进行挂载:
# ls
initrd.gz  ubuntu_10.04.3_x86_64.img  ubuntu_xen  vmlinuz
# mount -o loop ubuntu_10.04.3_x86_64.img  /mnt/
mount: you must specify the filesystem type

通过 file 查看发现不是一个简单的镜像文件:
# file ubuntu_10.04.3_x86_64.img
ubuntu_10.04.3_x86_64.img: x86 boot sector; partition 1: ID=0x83, starthead 32, startsector 2048, 19980288 sectors; partition 2: ID=0x5, star
thead 254, startsector 19984382, 985090 sectors, extended partition table, code offset 0x63

# fdisk  -ul ubuntu_10.04.3_x86_64.img
last_lba(): I don't know how to handle files with mode 81a4
You must set cylinders.
You can do this from the extra functions menu.

Disk ubuntu_10.04.3_x86_64.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
ubuntu_10.04.3_x86_64.img1            2048    19982335     9990144   83  Linux
Partition 1 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1243, 214, 59)
ubuntu_10.04.3_x86_64.img2        19984382    20969471      492545    5  Extended
Partition 2 has different physical/logical beginnings (non-Linux?):
     phys=(1023, 254, 63) logical=(1243, 247, 27)
Partition 2 has different physical/logical endings:
     phys=(1023, 254, 63) logical=(1305, 73, 48)
ubuntu_10.04.3_x86_64.img5        19984384    20969471      492544   82  Linux swap / Solaris

-u 表示以 sectors 而不是以 cylinders 的方式显示
-u[=unit]
    When  listing  partition  tables, show sizes in 'sectors' or in 'cylinders'.  The default is to show sizes in sectors.  For
    backward compatibility, it is possible to use the option without the <units> argument — then the default  is  used.   Note
    that  the  optional  <unit>  argument  cannot  be  separated from the -u option by a space, the correct form is for example
    '-u=cylinders'.

为了挂载特定的分区,需要知道该分区的起始 Byte,通过下面这个公式:
start_sector * sector_byte_size

因此,如果需要挂载 / 目录,其起始位置是 2048*512=1048576:
# mount -o loop,offset=1048576 ububtu_10.04.3_x86_64.img /mnt

上面的情况在启动需要修改 grub(TMOUT 时间太短) 的时候也很有用。

ref:
http://www.jefferyfernandez.id.au/2007/06/14/mounting-partitions-that-are-within-a-disk-image/