環境: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.0-327.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.1-1-ARCH/kernel/drivers/block/nbd.ko.gz license: GPL description: Network Block Device depends: intree: Y vermagic: 4.7.1-1-ARCH SMP preempt mod_unload modversions parm: nbds_max:number of network block devices to initialize (default: 16) (int) parm: max_part:number of partitions per device (default: 0) (int) [tong@T7 src]$ sudo modprobe nbd max_part=16 [tong@T7 src]$ lsmod |grep nbd nbd 20480 0 [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: 10 GiB, 10737418240 bytes, 20971520 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: 0x000a11b2 Device Boot Start End Sectors Size Id Type /dev/nbd0p1 * 2048 1026047 1024000 500M 83 Linux /dev/nbd0p2 1026048 20971519 19945472 9.5G 8e Linux LVM
然后我們發現,咦,竟然沒有創建設備nbd0p1與nbd0p2 。然后機智的我去查看了驅動參數是否設置正確。
[tong@T7 dev]$ cat /sys/module/nbd/parameters/max_part 31 [tong@T7 dev]$ cat /sys/module/nbd/parameters/nbds_max 16
雖然我搞不清楚它為什么是31,但是反正不是0我就可以忍了。
然后我又機智的想到了partx,對,這都基於我多年來強大的知識積累,雖然我並不太理解這個命令的機制(早就要你去學習內核拉)
也可以用partprobe, 應該說最好用partprobe,如果不行再用partx(仿佛記得partx是比較危險的,有一定可能讓內核掛掉。?!)
[tong@T7 dev]$ sudo partx -a /dev/nbd0 [tong@T7 dev]$ ll /dev/nbd0* brw-rw---- 1 root disk 43, 0 Sep 24 11:03 /dev/nbd0 brw-rw---- 1 root disk 43, 1 Sep 24 11:45 /dev/nbd0p1 brw-rw---- 1 root disk 43, 2 Sep 24 11:45 /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] 0 disks 6 partitions 0 LVM physical volume whole disks 1 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, 2016-09-18 22:22:36 +0800 LV Status NOT available LV Size 1.00 GiB Current LE 256 Segments 1 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, 2016-09-18 22:22:37 +0800 LV Status NOT available LV Size 8.47 GiB Current LE 2168 Segments 1 Allocation inherit Read ahead sectors auto [tong@T7 dpdk]$ sudo vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 9.51 GiB PE Size 4.00 MiB Total PE 2434 Alloc PE / Size 2424 / 9.47 GiB Free PE / Size 10 / 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 2434 Free PE 10 Allocated PE 2424 PV UUID 97pf2w-aFZr-MshR-17b2-Is9X-KqOg-mdlQzo [tong@T7 dpdk]$
嗯,然后我終於跟大家是一樣的了,可以繼續掛載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 2 logical volume(s) in volume group "centos" now active [tong@T7 dpdk]$ ll /dev/centos/ total 0 lrwxrwxrwx 1 root root 7 Sep 24 13:20 root -> ../dm-1 lrwxrwxrwx 1 root root 7 Sep 24 13:20 swap -> ../dm-0 [tong@T7 dpdk]$ ll /dev/dm-0 brw-rw---- 1 root disk 254, 0 Sep 24 13:20 /dev/dm-0
五,呃,終於可以掛了!
[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 0 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 2434 Free PE 10 Allocated PE 2424 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]$
七,全劇終!