客户端配置
-
加载nbd模块
[root@centos sm]# rmmod nbd [root@centos sm]# modprobe nbd max_part=8
-
映射服务器的块设备到本地nbd设备
[root@centos data]# nbd-client 192.168.100.25 8888 /dev/nbd0
-
挂载本地nbd设备到文件系统
[root@centos sm]# mount /dev/nbd0 ./data/ [root@centos sm]# cd data [root@centos sm]# touch mytest.txt
-
使用完成后断开设备
[root@centos data]# cd .. [root@centos sm]# umount ./data/ [root@centos sm]# nbd-client -d /dev/nbd0 Disconnecting: que, disconnect, sock, done
本地使用qemu-nbd访问qcow2文件
-
加载nbd模块
[root@centos sm]# rmmod nbd [root@centos sm]# modprobe nbd max_part=8
-
映射qcow2文件到本地nbd设备上
root@ubuntu-virtual-machine:~# qemu-nbd --connect=/dev/nbd0 /home/ubuntu/laboratory/data.qcow2 root@ubuntu-virtual-machine:~# mount /dev/nbd0 ./data/ root@ubuntu-virtual-machine:~# cd data/ root@ubuntu-virtual-machine:~/data# ls mytest.txt
-
卸载设备并断开链接
root@ubuntu-virtual-machine:~/data# cd root@ubuntu-virtual-machine:~# umount /home/ubuntu/data root@ubuntu-virtual-machine:~# qemu-nbd -d /dev/nbd0 /dev/nbd0 disconnected
qemu-nbd在有的系统上叫kvm-nbd,qemu-nbd-xen等。基本上都一样啦。
用qemu-nbd实现mount虚拟硬盘到Host上的功能,需要这么做:
rmmod nbd
modprobe nbd max_part=8
因为默认,nbd的增加分区的功能是关闭的,需要指定max_part这个参数。这个参数指定了一个nbd设备可以有多少个分区。
qemu-nbd --connect=/dev/nbd0 ./testxp.img
现在已经在localhost的1024端口上安装上该设备了。
ls /dev/ | grep nbd
nbd0
nbd0p1
上面这样显示就对了。因为我的那个虚拟磁盘只有一个分区。如果你的虚拟磁盘有多个分区,应该显示更多。
fdisk -l /dev/nbd0 查看虚拟磁盘和分区状况:
Disk /dev/nbd0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6f106f10
Device Boot Start End Blocks Id System
/dev/nbd0p1 * 1 1304 10474348+ 7 HPFS/NTFS
我们可以把/dev/nbd0p1这个ntfs格式的设备mount到我们的Host上。
fdisk /dev/nbd0 可以进入交互式命令行。
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
可以列出,增加,删除分区。
-----------------------------------------------------
SYNOPSIS
usage: qemu-nbd [OPTION]... filename
OPTIONS
filename <-- is a disk image filename
-p, --port=PORT port to listen on (default `1024')
-o, --offset=OFFSET offset into the image
-b, --bind=IFACE interface to bind to (default `0.0.0.0')
-k, --socket=PATH path to the unix socket (default '/var/lock/qemu-nbd-DEVICE')
-r, --read-only export read-only -----------> 只读访问
-P, --partition=NUM only expose partition NUM -----------> 建立磁盘分区到NBD设备的连接
-s, --snapshot use snapshot file -----------> 挂目标磁盘的snapshot文件。
-n, --nocache disable host cache -----------> ?
-c, --connect=DEV connect FILE to the local NBD device DEV -----------> 建立磁盘文件到NBD设备的连接
-d, --disconnect disconnect the specified device ------------------> 断开磁盘文件到NBD设备的连接
-e, --shared=NUM device can be shared by NUM clients (default '1')
-t, --persistent don't exit on the last connection -----------> ?
-v, --verbose display extra debugging information
-h, --help display this help and exit
-V, --version output version information and exit
-------------------------------------------------------------------------------------------
$ qemu-nbd --read-only --partition=1 --snapshot winxp.img &
$ sudo nbd-client localhost 1024 /dev/nbd0
$ sudo mount /dev/nbd0 -oloop ~/empty/
$ ls -l /home/muelli/empty/