您现在的位置是:首页 >其他 >centos7 nbd 挂在qcow2或qcow,raw,虚机镜像,virsh,virt,使用qemu-nbd挂载qcow2镜像文件网站首页其他
centos7 nbd 挂在qcow2或qcow,raw,虚机镜像,virsh,virt,使用qemu-nbd挂载qcow2镜像文件
[root@localhost linux-3.10.63]# cat ../../../readme
https://blog.51cto.com/mshxuyi/5857760
https://blog.csdn.net/weixin_42097108/article/details/111414501 错误解决
1、nbd 模块未安装
登录后复制
[root@localhost ~]# modprobe nbd
modprobe: FATAL: Module nbd not found.
2、查看 linux 版本
登录后复制
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) #查看系统版本
[root@localhost ~]# uname -r #查看linux的内核信息
3.10.0-957.el7.x86_64
3、安装相关包
yum install kernel-devel kernel-headers elfutils-libelf-devel gcc+ gcc-c++
4、下载内核包
登录后复制
http://vault.centos.org/7.6.1810/os/Source/SPackages/kernel-3.10.0-957.el7.src.rpm
#根据自己的系统版本去下载
#7.6.1810 为redhat-release 系统版本
#3.10.0-957.el7 为uname -r 内核信息
5、编译
登录后复制
rpm -ivh kernel-3.10.0-957.el7.src.rpm
cd rpmbuild/SOURCES/
#解压 -C 指定目录 /usr/src/kernels
tar jxvf linux-3.10.0-957.el7.tar.xz -C /usr/src/kernels/
#解压后生成两个文件
ll /usr/src/kernels/
drwxr-xr-x. 22 root root 4096 9月 6 08:07 3.10.0-957.27.2.el7.x86_64
drwxrwxr-x. 24 root root 4096 9月 6 08:17 linux-3.10.0-957.el7
编译
登录后复制
cd /usr/src/kernels/linux-3.10.0-957.el7
#删除所有编译生成文件,内核配置文件
make mrproper
cp /usr/src/kernels/3.10.0-957.27.2.el7.x86_64/Module.symvers ./
cp /boot/config-3.10.0-957.el7.x86_64 ./.config
#备份当前.config文件为.config.old
make oldconfig
make prepare
make scripts
6、编译
登录后复制
make CONFIG_BLK_DEV_NBD=m M=drivers/block
错误:
登录后复制
drivers/block/nbd.c: 在函数‘__nbd_ioctl’中:
drivers/block/nbd.c:619:19: 错误:‘REQ_TYPE_SPECIAL’未声明(在此函数内第一次使用)
sreq.cmd_type = REQ_TYPE_SPECIAL;
^
drivers/block/nbd.c:619:19: 附注:每个未声明的标识符在其出现的函数内只报告一次
make[1]: *** [drivers/block/nbd.o] 错误 1
make: *** [_module_drivers/block] 错误 2
解决:
vi drivers/block/nbd.c
//sreq.cmd_type = REQ_TYPE_SPECIAL;
sreq.cmd_type = 7; #修改为7
#重新执行一次
make CONFIG_BLK_DEV_NBD=m M=drivers/block
7、复制文件
cp drivers/block/nbd.ko /lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/block/
depmod -a
8、查看模块
登录后复制
[root@localhost linux-3.10.0-957.el7]# modinfo nbd
filename: /lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/block/nbd.ko
license: GPL
description: Network Block Device
retpoline: Y
rhelversion: 7.6
srcversion: EDE909A294AC5FE08E81957
depends:
vermagic: 3.10.0 SMP 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)
parm: debugflags:flags for controlling debug output (int)
编译内核出现的问题解决办法
1.cc1: error: code model kernel does not support PIC mode
2
include/linux/compiler-gcc.h:106:1: fatal error: linux/compiler-gcc7.h: 没有那个文件或目录
直接将gcc4重命名为gcc7
mv include/linux/compiler-gcc4.h include/linux/compiler-gcc7.h
3.li@li-vir:~/dq/drivers/hello3$ insmod hello.ko
insmod: ERROR: could not insert module hello.ko: Operation not permitted
4.
insmod: ERROR: could not insert module hello.ko: Invalid module format
这里是引用
版本不对,
使用uname -r查看一下
make时要确保一致
方法1 挂在qcow2
1. 加载 nbd 驱动
某些版本的 linux 不加 max_part 参数会导致没有没有设备节点 /dev/nbd0p{1,2,3,4…} 等. 用 kpartx 也不行.
$ sudo modprobe nbd max_part=8
备注信息:查看Linux 模块信息(modinfo),例如查看nbd模块的参数
$ modinfo nbd
filename: /lib/modules/3.14.12/kernel/drivers/block/nbd.ko
license: GPL
description: Network Block Device
srcversion: 910DE7DFCE8A0AE76D3D428
depends:
intree: Y
vermagic: 3.14.12 SMP 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)
parm: debugflags:flags for controlling debug output (int)
2.连接 qemu-nbd
$ sudo qemu-nbd -c /dev/nbd0 centos.qcow2
查看分区:
$ sudo fdisk -l /dev/nbd0
Disk /dev/nbd0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 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
Disk identifier: 0x0005d1fdDevice Boot Start End Blocks Id System
/dev/nbd0p1 * 2048 1026047 512000 83 Linux
/dev/nbd0p2 1026048 20971519 9972736 8e Linux LVM
vgdisplay
$ sudo lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup/lv_root
VG Name VolGroup
LV UUID v60jyE-1lDm-7dMW-hkVf-YWBH-OKXI-ySHDkb
LV Write Access read/write
LV Status NOT available
LV Size 8.51 GiB
Current LE 2178
Segments 1
Allocation inherit
Read ahead sectors auto
--- Logical volume ---
LV Name /dev/VolGroup/lv_swap
VG Name VolGroup
LV UUID b1BctN-vPtc-8n6F-tokb-75pN-w34N-HXG1cB
LV Write Access read/write
LV Status NOT available
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
Active VolGroup
$ sudo vgchange -a y VolGroup
mount vg
$ sudo mount /dev/VolGroup/lv_root /mnt/test2
3.挂载
$ sudo mount /dev/nbd0p1 /mnt/test1
4.umount disconnect
$ sudo mount /dev/nbd0p1 /mnt/test1
$ sudo qemu-nbd -d /dev/nbd0p1
方法2 挂在qcow2
方法二:libguestfs
1.安装libguestfs工具
$ sudo apt install libguestfs-tools
2.挂载磁盘镜像中的分区到挂载点
语法:guestmount -a 镜像文件 -m 镜像文件内包含的某个分区 [选项] 挂载点
$ sudo mkdir /mnt/a #创建挂载点目录
sudo guestmount -a /XXX/XXX/imgname.qcow2 -m /dev/sda1 /mnt/a #尝试挂载/dev/sda分区,将提示有两个有效的分区/dev/sda1(/boot)和/dev/sda2(/root),
libguestfs: error: mount_options: mount exited with status 32: mount: /sysroot: wrong fs type, bad option, bad superblock on /dev/sda, missing codepage or helper program, or other error.
guestmount: ‘/dev/sda’ could not be mounted.
guestmount: Did you mean to mount one of these filesystems?
guestmount: /dev/sda1 (vfat)
guestmount: /dev/sda2 (ext4)
$ sudo guestmount -a /XXX/XXX/imgname.qcow2 -m /dev/sda2 /mnt/a #将磁盘镜像中包含的/dev/sda2分区挂载到/mnt/a目录
sudo ls /mnt/a
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
也可以通过-i参数替代-m来实现自动挂载有效分区
$ sudo mkdir /mnt/a #创建挂载点目录
$ sudo guestmount -a /XXX/XXX/imgname.qcow2 -i /mnt/a #将自动将磁盘镜像中最有效的/dev/sda2分区挂载到/mnt/a目录
sudo ls /mnt/a
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
3.取消挂载
$ sudo guestunmount /mnt/a
- 1