Aliens

挂载img文件

May 22, 2021

本文主要讲解如何在Ubuntu下挂载raw image镜像,以raspberry pi官方镜像2019-04-08-raspbian-stretch-lite.img 为例

计算偏移量并手动挂载

1.使用fdisk查看分区结构

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# fdisk -l

Disk 2019-04-08-raspbian-stretch-lite.img: 1.7 GiB, 1803550720 bytes, 3522560 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc1dc39e5

Device                                Boot Start     End Sectors  Size Id Type
2019-04-08-raspbian-stretch-lite.img1       8192   96042   87851 42.9M  c W95 FA
2019-04-08-raspbian-stretch-lite.img2      98304 3522559 3424256  1.6G 83 Linux

可以看出,这份镜像的sectors size = 512 bytes,而第一个分区的偏移量是8192 sectors,故偏移的字节为512 * 8192 = 4194304 bytes,下面用mount命令挂载

2.使用mount手动挂载

1
# mount -o offset=$((512 * 8192)) 2019-04-08-raspbian-stretch-lite.img tmp/

第二个分区同理

自动挂载

可以使用losetup这个工具自动挂载

1
# losetup -f -P 2019-04-08-raspbian-stretch-lite.img

查看挂载结果

1
2
3
$ losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                    DIO LOG-SEC
/dev/loop0         0      0         0  0 /home/michael/Downloads/2019-04-08-raspbian-stretch-lite.img   0     512

或者使用lsblk查看

1
2
3
4
5
$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0         7:0    0   1.7G  0 loop
├─loop0p1   259:6    0  42.9M  0 loop
└─loop0p2   259:7    0   1.6G  0 loop

挂载

1
# mount /dev/loop0p1 tmp

附上losetup的参数说明

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
$ losetup --help
Usage:
 losetup [options] [<loopdev>]
 losetup [options] -f | <loopdev> <file>

Set up and control loop devices.

Options:
 -a, --all                     list all used devices
 -d, --detach <loopdev>...     detach one or more devices
 -D, --detach-all              detach all used devices
 -f, --find                    find first unused device
 -c, --set-capacity <loopdev>  resize the device
 -j, --associated <file>       list all devices associated with <file>
 -L, --nooverlap               avoid possible conflict between devices

 -o, --offset <num>            start at offset <num> into file
     --sizelimit <num>         device is limited to <num> bytes of the file
 -b  --sector-size <num>       set the logical sector size to <num>
 -P, --partscan                create a partitioned loop device
 -r, --read-only               set up a read-only loop device
     --direct-io[=<on|off>]    open backing file with O_DIRECT
     --show                    print device name after setup (with -f)
 -v, --verbose                 verbose mode

 -J, --json                    use JSON --list output format
 -l, --list                    list info about all or specified (default)
 -n, --noheadings              don't print headings for --list output
 -O, --output <cols>           specify columns to output for --list
     --raw                     use raw --list output format

 -h, --help                    display this help
 -V, --version                 display version

Available output columns:
         NAME  loop device name
    AUTOCLEAR  autoclear flag set
    BACK-FILE  device backing file
     BACK-INO  backing file inode number
 BACK-MAJ:MIN  backing file major:minor device number
      MAJ:MIN  loop device major:minor number
       OFFSET  offset from the beginning
     PARTSCAN  partscan flag set
           RO  read-only device
    SIZELIMIT  size limit of the file in bytes
          DIO  access backing file with direct-io
      LOG-SEC  logical sector size in bytes

For more details see losetup(8).

参考链接

https://major.io/2010/12/14/mounting-a-raw-partition-file-made-with-dd-or-dd_rescue-in-linux/

https://blog.tinned-software.net/mount-raw-image-of-entire-disc/