[qemu] 挂载qcow2文件,qcow2里边还有个lvm

时间:2023-03-09 03:32:14
[qemu] 挂载qcow2文件,qcow2里边还有个lvm

环境:archlinux

背景:在虚拟机里玩dpdk,把挂载HugePage(hugetlbfs)的命令写入fstab的时候,写错了,无法启动,需要把qcow2挂起来改一下。

方法:使用qemu-nbd

来源与参考与鸣谢:http://smilejay.com/2012/11/how-to-mount-a-qcow2-image/

http://blog.csdn.net/eyf0917/article/details/38339159

一:要查看内核参数是否支持了nbd,可是完全不知道上哪里去看内核参数(又不是我自己编译的,囧)(什么开始学习内核知识!!!)

  我知道了(10个小时后)。。。 不过好想,并没有神马帮助。。。

[root@dpdk ~]# grep -i NBD /boot/config-3.10.-.el7.x86_64
# CONFIG_BLK_DEV_NBD is not set
[root@dpdk ~]#

  我有知道了更靠谱的一招(又过去了N天之后。。。)

[tong@T7 dpdk]$ zcat /proc/config.gz  |grep -i nbd
CONFIG_BLK_DEV_NBD=m
[tong@T7 dpdk]$

  再修正!(几个小时之后) 以上两个方法,得到的只不过是系统编译时的内核参数状态,传给grub的参数变化并不能体现在这里,也许?

  以上基于经验未求证,经验出处见(dpdk读文档2)

二:关于一,从天而降的答案默认内核参数肯定是个’M‘,所以看看有没有加载,没有就载进去,嗯好棒!

[tong@T7 src]$ lsmod |grep nbd
[tong@T7 src]$ modinfo nbd
filename: /lib/modules/4.7.--ARCH/kernel/drivers/block/nbd.ko.gz
license: GPL
description: Network Block Device
depends:
intree: Y
vermagic: 4.7.--ARCH SMP preempt mod_unload modversions
parm: nbds_max:number of network block devices to initialize (default: ) (int)
parm: max_part:number of partitions per device (default: ) (int)
[tong@T7 src]$ sudo modprobe nbd max_part=
[tong@T7 src]$ lsmod |grep nbd
nbd
[tong@T7 src]$

三:连接nbd设备

[tong@T7 dpdk]$ sudo qemu-nbd -f qcow2 -c /dev/nbd0 disk.img
[tong@T7 dpdk]$ sudo fdisk -l /dev/nbd0
Disk /dev/nbd0: GiB, bytes, sectors
Units: sectors of * = bytes
Sector size (logical/physical): bytes / bytes
I/O size (minimum/optimal): bytes / bytes
Disklabel type: dos
Disk identifier: 0x000a11b2 Device Boot Start End Sectors Size Id Type
/dev/nbd0p1 * 500M Linux
/dev/nbd0p2 .5G 8e Linux LVM

然后我们发现,咦,竟然没有创建设备nbd0p1与nbd0p2 。然后机智的我去查看了驱动参数是否设置正确。

[tong@T7 dev]$ cat /sys/module/nbd/parameters/max_part 

[tong@T7 dev]$ cat /sys/module/nbd/parameters/nbds_max

虽然我搞不清楚它为什么是31,但是反正不是0我就可以忍了。

然后我又机智的想到了partx,对,这都基于我多年来强大的知识积累,虽然我并不太理解这个命令的机制(早就要你去学习内核拉)

也可以用partprobe, 应该说最好用partprobe,如果不行再用partx(仿佛记得partx是比较危险的,有一定可能让内核挂掉。?!)

[tong@T7 dev]$ sudo partx -a /dev/nbd0
[tong@T7 dev]$ ll /dev/nbd0*
brw-rw---- root disk , Sep : /dev/nbd0
brw-rw---- root disk , Sep : /dev/nbd0p1
brw-rw---- root disk , Sep : /dev/nbd0p2

搞定 : )

四:挂吧! 本来故事应该到此结束,然而不行,我这块盘是lvm,可是我不会挂lvm,所以还要插叙一个挂载lvm

快速学习了一下 http://blog.csdn.net/eyf0917/article/details/38339159

然后,并不行,人家的结果是那样的,我的却是这样的,这让人很尬尴。。。 系统死活就是认不得我的lvm。。。

[tong@T7 dpdk]$ sudo lvdisplay
[tong@T7 dpdk]$ sudo vgdisplay
[tong@T7 dpdk]$ sudo pvdisplay
[tong@T7 dpdk]$ sudo lvscan
[tong@T7 dpdk]$ sudo pvdisplay
[tong@T7 dpdk]$ sudo lvmdiskscan
/dev/sda1 [ 50.00 GiB]
/dev/nbd0p1 [ 500.00 MiB]
/dev/sda2 [ 150.00 GiB]
/dev/nbd0p2 [ 9.51 GiB] LVM physical volume
/dev/sda3 [ 731.51 GiB]
/dev/sdb1 [ 512.00 MiB]
/dev/sdb2 [ 14.41 GiB]
disks
partitions
LVM physical volume whole disks
LVM physical volume
[tong@T7 dpdk]$ sudo pvdisplay
[tong@T7 dpdk]$ sudo pvscan  
 No matching physical volumes found
[tong@T7 dpdk]$ sudo pvdisplay
[tong@T7 dpdk]$ man lvmetad

好吧,我只能自己学习,然后我就读到了 “man lvmetad“,细节你们自己去man把,就是说这个daemon cache了各种metadata在它的cache里面,以上命令全部都在读cache。而我的cache根本没有被更新(也许是因为我的systemd配置?没研究),所以人家好使,我的不好使。手动更新使用命令 ” pvscan  --cache“.

[tong@T7 dpdk]$ systemctl list-unit-files |grep lvm
lvm2-lvmetad.service disabled
lvm2-monitor.service disabled
lvm2-pvscan@.service static
lvm2-lvmetad.socket static
[tong@T7 dpdk]$
[tong@T7 dpdk]$ sudo pvscan --cache
[tong@T7 dpdk]$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID p49k51-9Z0H-hvwk-mA3u-Hhq7-kKKr-OkeYNG
LV Write Access read/write
LV Creation host, time dpdk, -- :: +
LV Status NOT available
LV Size 1.00 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto --- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID 1pUpjb-du22-XxqQ-M6fE-o0Tz-leQ7-IgsfjS
LV Write Access read/write
LV Creation host, time dpdk, -- :: +
LV Status NOT available
LV Size 8.47 GiB
Current LE
Segments
Allocation inherit
Read ahead sectors auto [tong@T7 dpdk]$ sudo vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas
Metadata Sequence No
VG Access read/write
VG Status resizable
MAX LV
Cur LV
Open LV
Max PV
Cur PV
Act PV
VG Size 9.51 GiB
PE Size 4.00 MiB
Total PE
Alloc PE / Size / 9.47 GiB
Free PE / Size / 40.00 MiB
VG UUID zt0APb-5ZeI-xyZ5-GTZC-J0GQ-ME4F-c3FqWZ [tong@T7 dpdk]$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/nbd0p2
VG Name centos
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE
Free PE
Allocated PE
PV UUID 97pf2w-aFZr-MshR-17b2-Is9X-KqOg-mdlQzo [tong@T7 dpdk]$

pvscan --cache

嗯,然后我终于跟大家是一样的了,可以继续挂载lvm了。

这个时候,我并没有看见我lvm设备:

[tong@T7 dpdk]$ ll /dev/centos/root
ls: cannot access '/dev/centos/root': No such file or directory
[tong@T7 dpdk]$ ll /dev/centos/swap
ls: cannot access '/dev/centos/swap': No such file or directory
[tong@T7 dpdk]$

然后,我也不知道为啥要这么干,反正人家说这么干。。。

[tong@T7 dpdk]$ man vgchange
[tong@T7 dpdk]$ sudo vgchange -a y centos
logical volume(s) in volume group "centos" now active
[tong@T7 dpdk]$ ll /dev/centos/
total
lrwxrwxrwx root root Sep : root -> ../dm-
lrwxrwxrwx root root Sep : swap -> ../dm-
[tong@T7 dpdk]$ ll /dev/dm-
brw-rw---- root disk , Sep : /dev/dm-

五,呃,终于可以挂了!

[tong@T7 dpdk]$ sudo mount /dev/centos/root mnt/
[tong@T7 dpdk]$ cd mnt/
[tong@T7 mnt]$ ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[tong@T7 dpdk]$ sudo vim mnt/etc/fstab

六,搞定,原路返回。机智的我还参悟了 vgchange -an 这个酷酷的用法!终于,完美退栈!

[tong@T7 dpdk]$ sudo umount mnt/
[tong@T7 dpdk]$ sudo vgchange -an centos
logical volume(s) in volume group "centos" now active
[tong@T7 dpdk]$ sudo ls /dev/centos
ls: cannot access '/dev/centos': No such file or directory
[tong@T7 dpdk]$ sudo qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
[tong@T7 dpdk]$ sudo pvdisplay
WARNING: Device for PV 97pf2w-aFZr-MshR-17b2-Is9X-KqOg-mdlQzo not found or rejected by a filter.
WARNING: Device for PV 97pf2w-aFZr-MshR-17b2-Is9X-KqOg-mdlQzo not found or rejected by a filter.
--- Physical volume ---
PV Name [unknown]
VG Name centos
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE
Free PE
Allocated PE
PV UUID 97pf2w-aFZr-MshR-17b2-Is9X-KqOg-mdlQzo [tong@T7 dpdk]$ sudo pvscan --cache
[tong@T7 dpdk]$ sudo pvdisplay
[tong@T7 dpdk]$ sudo rmmod nbd
[tong@T7 dpdk]$ sudo ls /dev/nbd*
ls: cannot access '/dev/nbd*': No such file or directory
[tong@T7 dpdk]$

七,全剧终!