如何挂载一个镜像文件(how to mount an image file)
如何挂载一个镜像文件(how to mount an image file)
在使用KVM或Xen虚拟化的情况下,经常需要使用镜像文件(image file),我们可以将Guest系统启动起来,然后对镜像文件进行修改,不过这样有时也是比较麻烦,其实也是可以将镜像文件直接进行mount的,可以用如下两种办法。
(本文写的RAW格式镜像文件的挂载;我的另一篇文章写了“如何挂载qcow2格式的镜像文件”)
方法一:找出分区开始的开始位置,使用mount命令的offset参数偏移掉前面不需要的,即可得到真正的分区。其具体步骤如下:
1. 用“fdisk -lu my.img”查询image信息;
2. 计算image内分区开始的地方(计算offset),用从N号sector(扇区)开始,则offset=N*M (M为一个sector的大小,一般为512)
3. 使用mount命令挂载为loop设备即可。 (如LVM分区,则会较复杂,请见本文最后的介绍)
在Linux系统上具体操作演示如下:
You must set cylinders.
You can do this from the extra functions menu.
Disk rhel6u2.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x00048b34
Device Boot Start End Blocks Id System
rhel6u2.img1 * 2048 1026047 512000 83 Linux
Partition 1 does not end on cylinder boundary.
rhel6u2.img2 1026048 104857599 51915776 8e Linux LVM
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(6527, 21, 22)
[root@jay-linux image]# echo $((2048*512))
1048576
[root@jay-linux image]# mount -o loop,offset=1048576 rhel6u2.img /media/
[root@jay-linux image]# cd /media/
[root@jay-linux media]# ls
config-2.6.32-220.el6.x86_64 initramfs-3.5.0.img System.map-2.6.32-279.el6.x86_64
config-2.6.32-279.el6.x86_64 lost+found System.map-3.5.0
efi symvers-2.6.32-220.el6.x86_64.gz vmlinuz
grub symvers-2.6.32-279.el6.x86_64.gz vmlinuz-2.6.32-220.el6.x86_64
initramfs-2.6.32-220.el6.x86_64.img System.map vmlinuz-2.6.32-279.el6.x86_64
initramfs-2.6.32-279.el6.x86_64.img System.map-2.6.32-220.el6.x86_64 vmlinuz-3.5.0
[root@jay-linux media]# echo $((1026048*512))
525336576
[root@jay-linux media]# umount /media
[root@jay-linux media]# cd /home/image/
[root@jay-linux image]# umount /media
[root@jay-linux image]# mount -o loop,offset=525336576 rhel6u2.img /media/
mount: unknown filesystem type 'LVM2_member'
(镜像文件中的LVM分区mount的问题,本文最后单独说明)
[root@jay-linux image]# fdisk -lu sles11sp2-i386.img
You must set cylinders.
You can do this from the extra functions menu.
Disk sles11sp2-i386.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x0002d3be
Device Boot Start End Blocks Id System
sles11sp2-i386.img1 2048 4208639 2103296 82 Linux swap / Solaris
Partition 1 does not end on cylinder boundary.
sles11sp2-i386.img2 * 4208640 41943039 18867200 83 Linux
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(2610, 212, 34)
[root@jay-linux image]# echo $((4208640*512))
2154823680
[root@jay-linux image]# mount -o loop,offset=2154823680 sles11sp2-i386.img /media
[root@jay-linux image]# cd /media/
[root@jay-linux media]# ls
bin dev home lost+found mnt proc sbin srv sys usr
boot etc lib media opt root selinux success tmp var
[root@jay-linux image]# umount /media/
|
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
[root@jay-linux image]# fdisk -lu rhel6u2.img
You must set cylinders.
You can do this from the extra functions menu.
Disk rhel6u2.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x00048b34
Device Boot Start End Blocks Id System
rhel6u2.img1 * 2048 1026047 512000 83 Linux
Partition 1 does not end on cylinder boundary.
rhel6u2.img2 1026048 104857599 51915776 8e Linux LVM
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(6527, 21, 22)
[root@jay-linux image]# echo $((2048*512))
1048576
[root@jay-linux image]# mount -o loop,offset=1048576 rhel6u2.img /media/
[root@jay-linux image]# cd /media/
[root@jay-linux media]# ls
config-2.6.32-220.el6.x86_64 initramfs-3.5.0.img System.map-2.6.32-279.el6.x86_64
config-2.6.32-279.el6.x86_64 lost+found System.map-3.5.0
efi symvers-2.6.32-220.el6.x86_64.gz vmlinuz
grub symvers-2.6.32-279.el6.x86_64.gz vmlinuz-2.6.32-220.el6.x86_64
initramfs-2.6.32-220.el6.x86_64.img System.map vmlinuz-2.6.32-279.el6.x86_64
initramfs-2.6.32-279.el6.x86_64.img System.map-2.6.32-220.el6.x86_64 vmlinuz-3.5.0
[root@jay-linux media]# echo $((1026048*512))
525336576
[root@jay-linux media]# umount /media
[root@jay-linux media]# cd /home/image/
[root@jay-linux image]# umount /media
[root@jay-linux image]# mount -o loop,offset=525336576 rhel6u2.img /media/
mount: unknown filesystem type 'LVM2_member'
(镜像文件中的LVM分区mount的问题,本文最后单独说明)
[root@jay-linux image]# fdisk -lu sles11sp2-i386.img
You must set cylinders.
You can do this from the extra functions menu.
Disk sles11sp2-i386.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x0002d3be
Device Boot Start End Blocks Id System
sles11sp2-i386.img1 2048 4208639 2103296 82 Linux swap / Solaris
Partition 1 does not end on cylinder boundary.
sles11sp2-i386.img2 * 4208640 41943039 18867200 83 Linux
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(2610, 212, 34)
[root@jay-linux image]# echo $((4208640*512))
2154823680
[root@jay-linux image]# mount -o loop,offset=2154823680 sles11sp2-i386.img /media
[root@jay-linux image]# cd /media/
[root@jay-linux media]# ls
bin dev home lost+found mnt proc sbin srv sys usr
boot etc lib media opt root selinux success tmp var
[root@jay-linux image]# umount /media/
|
方法二:用kpartx建立分区映射后,再mount映射后的设备即可,操作实例如下:
add map loop3p1 (253:2): 0 4206592 linear /dev/loop3 2048
add map loop3p2 (253:3): 0 37734400 linear /dev/loop3 4208640
[root@jay-linux image]# mount /dev/mapper/loop3p2 /media/
[root@jay-linux image]# ls /media/
bin dev home lost+found mnt proc sbin srv sys usr
boot etc lib media opt root selinux success tmp var
[root@jay-linux image]# umount /media/
[root@jay-linux image]# mount /dev/mapper/loop3p1 /media/
/dev/mapper/loop3p1 looks like swapspace - not mounted
mount: you must specify the filesystem type
(其中的交换分区,我也还不知道是否可以mount;其实mount交换分区也没意义)
(使用完成后,卸载挂载点、删除映射关系即可)
[root@jay-linux image]# umount /media/
[root@jay-linux image]# kpartx -d sles11sp2-i386.img
loop deleted : /dev/loop3
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@jay-linux image]# kpartx -av sles11sp2-i386.img
add map loop3p1 (253:2): 0 4206592 linear /dev/loop3 2048
add map loop3p2 (253:3): 0 37734400 linear /dev/loop3 4208640
[root@jay-linux image]# mount /dev/mapper/loop3p2 /media/
[root@jay-linux image]# ls /media/
bin dev home lost+found mnt proc sbin srv sys usr
boot etc lib media opt root selinux success tmp var
[root@jay-linux image]# umount /media/
[root@jay-linux image]# mount /dev/mapper/loop3p1 /media/
/dev/mapper/loop3p1 looks like swapspace - not mounted
mount: you must specify the filesystem type
(其中的交换分区,我也还不知道是否可以mount;其实mount交换分区也没意义)
(使用完成后,卸载挂载点、删除映射关系即可)
[root@jay-linux image]# umount /media/
[root@jay-linux image]# kpartx -d sles11sp2-i386.img
loop deleted : /dev/loop3
|
关于LVM的mount,可以参考文末参考资料中的vpsee的文章(我也是试多次没做成功,才偶然看到这篇文章的),我对镜像文件中LVM分区的mount操作如下供参考。
You must set cylinders.
You can do this from the extra functions menu.
Disk rhel6u2.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x00048b34
Device Boot Start End Blocks Id System
rhel6u2.img1 * 2048 1026047 512000 83 Linux
Partition 1 does not end on cylinder boundary.
rhel6u2.img2 1026048 104857599 51915776 8e Linux LVM
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(6527, 21, 22)
[root@jay-linux image]# echo $((1026048*512))
525336576
[root@jay-linux image]# losetup /dev/loop0 rhel6u2.img -o 525336576
[root@jay-linux image]# pvscan
PV /dev/loop0 VG VolGroup lvm2 [49.51 GiB / 0 free]
Total: 1 [49.51 GiB] / in use: 1 [49.51 GiB] / in no VG: 0 [0 ]
[root@jay-linux image]# vgchange -ay VolGroup
2 logical volume(s) in volume group "VolGroup" now active
[root@jay-linux image]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
lv_root VolGroup -wi-a--- 45.57g
lv_swap VolGroup -wi-a--- 3.94g
[root@jay-linux image]# mount /dev/VolGroup/lv_root /media/
[root@jay-linux image]# ls /media/
bin cgroup etc lib lost+found misc net proc sbin srv tmp var
boot dev home lib64 media mnt opt root selinux sys usr
(使用完后的卸载操作,如下)
[root@jay-linux image]# umount /media/
[root@jay-linux image]# vgchange -an VolGroup
0 logical volume(s) in volume group "VolGroup" now active
[root@jay-linux image]# losetup -d /dev/loop0
|
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
|
[root@jay-linux image]# fdisk -lu rhel6u2.img
You must set cylinders.
You can do this from the extra functions menu.
Disk rhel6u2.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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
Disk identifier: 0x00048b34
Device Boot Start End Blocks Id System
rhel6u2.img1 * 2048 1026047 512000 83 Linux
Partition 1 does not end on cylinder boundary.
rhel6u2.img2 1026048 104857599 51915776 8e Linux LVM
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(6527, 21, 22)
[root@jay-linux image]# echo $((1026048*512))
525336576
[root@jay-linux image]# losetup /dev/loop0 rhel6u2.img -o 525336576
[root@jay-linux image]# pvscan
PV /dev/loop0 VG VolGroup lvm2 [49.51 GiB / 0 free]
Total: 1 [49.51 GiB] / in use: 1 [49.51 GiB] / in no VG: 0 [0 ]
[root@jay-linux image]# vgchange -ay VolGroup
2 logical volume(s) in volume group "VolGroup" now active
[root@jay-linux image]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
lv_root VolGroup -wi-a--- 45.57g
lv_swap VolGroup -wi-a--- 3.94g
[root@jay-linux image]# mount /dev/VolGroup/lv_root /media/
[root@jay-linux image]# ls /media/
bin cgroup etc lib lost+found misc net proc sbin srv tmp var
boot dev home lib64 media mnt opt root selinux sys usr
(使用完后的卸载操作,如下)
[root@jay-linux image]# umount /media/
[root@jay-linux image]# vgchange -an VolGroup
0 logical volume(s) in volume group "VolGroup" now active
[root@jay-linux image]# losetup -d /dev/loop0
|
mount LVM 分区的 参考资料:
Related posts:
如何挂载一个镜像文件(how to mount an image file)的更多相关文章
- 磁盘管理,磁盘挂在mount,挂载光盘镜像文件,挂在U盘,umount 卸载命令, dd
1 mount 命令格式: mount[-t vfstype] -o options device dir 其中: *-t vfstype 指定文件系统的类型,通常不必指定.mount会自动选择正确的 ...
- 攻城狮在路上(叁)Linux(二十四)--- linux设置开机挂载及镜像文件挂载
虽然可以手动进行文件系统的挂载,但是每次都手动挂载就会很麻烦,开机挂载的目的就是实现文件系统的自动挂载. 一.开机挂载:/etc/fstab及/etc/mtab 主要是通过修改/etc/fstab文件 ...
- linux下挂载iso镜像文件(转)
挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式: mount [-t vfstype] [-o optio ...
- linux无需root挂载iso镜像文件
引言 起初,我在针对deepin制作一款appimage安装工具,想要其实现的功能就是自动获取图标,只需要输入软件名称和分类即可,当然以后也会寻找方案省去手动输入的麻烦. 后来我发现一个有趣的问题 o ...
- linux下挂载iso镜像文件
linux挂载iso镜像可以分为两种,直接挂载镜像和上传镜像文件至服务器进行挂载 一.虚拟机镜像挂载 测试时使用虚拟机,此时可以直接在虚拟机的设置中将镜像文件直接选择并进行挂载,无需上传至虚拟机服务器 ...
- [CentOS7] 挂载iso镜像文件到/media目录下
声明:本文主要总结自:鸟哥的Linux私房菜-第七章.Linux 磁碟與檔案系統管理,如有侵权,请通知博主 首先,设置虚拟机让它加载iso镜像文件到CD/DVD设备中,这个设备对应于Linux下的/d ...
- docker中 devicemapper驱动挂载容器镜像文件
详解docker中容器devicemapper设备的挂载流程
- 宿主机mount虚拟机镜像文件
转载 mount挂载虚拟机镜像文件 使用mount挂载ubuntu虚拟机所在的img文件的时候,执行: “sudo mount -o loop xxx.img /mnt/xxx”, 系统提示: “mo ...
- XenServer安装虚拟机---先扩容存放ISO镜像文件
我们都知道xenserver安装后,不管你的盘有多大,只有4G的空间. 故操作是:新建LV卷,可自定义大小 1.vgdisplay #先查看剩余空间 [root@XenServer /]# vgdis ...
随机推荐
- 程序员收藏必看系列:深度解析MySOL优化(一)
说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型….. 你是否真的理解这些优化技巧?是否理解其背后的工作原理? ...
- 关于 Overtrue 的拼音库 overtrue/pinyin 为何 travis 为 error
什么是ThinkSNS ? ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+(简称TS+).Thin ...
- C#代码生成器附百度云盘源码地址
今晚闲着没事,写了个代码生成器,在这里只做个抛砖引玉,后面可以继续扩展功能,下方附百度云盘源码地址. 使用数据库:sqlserver 编译器:vs2015 废话不多说,上界面: 程序主界面: 数据库: ...
- Java工程打包成jar可执行文件
将一个工程中的类打包成jar文件,步骤参考如下: 1.选择file -> project structure 2. 选择Arifacts->JAR->form modules wit ...
- iphone状态栏,导航栏,标签栏高度一览表
iphone状态栏,导航栏,标签栏高度一览表 设备分辨率 状态栏高度 导航栏高度 标签栏高度 iPhone6 plus 1242×2208 px 60px 132px 147px iPhon ...
- 页面出现滚动条时,body里面的内容不能自动居中?
弹窗后允许页面滚动 这种方式通常使用 position: absolute; ,可以看看我做的这个 Demo.主要用来应对弹窗内容很大很多的情况,超过了屏幕的宽高需要产生滚动条来方便浏览者查看.有一些 ...
- Netty(4)Stream by codec(粘包与拆包)
TCP/IP,传输的是byte[],将byte[]放入队列中.可能会发生粘包和拆包. 比如,客户端向服务端发送了2条消息,分别为D1,D2,可能产生的情况,如下图所示: 情况一:正常的. 情况二:粘包 ...
- .NET Core模块化
.NET Core模块化 源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com账户:ad ...
- Linux Ubuntu系统之PPP拨号经验分享
近期,工作需要,我负责开发PPP拨号模块. 说起拨号,算算时间,我已经做过2次了, 暴露年龄了,呵呵. 第一次是刚毕业做的PPOE拨号,给电信做拨号软件,在河北石家庄工作过一段时间,基于windows ...
- 《从0到1学习Flink》—— 如何自定义 Data Source ?
前言 在 <从0到1学习Flink>-- Data Source 介绍 文章中,我给大家介绍了 Flink Data Source 以及简短的介绍了一下自定义 Data Source,这篇 ...