服务端

服务器环境

已经安装过qemu-img的32位ubuntu

ubuntu@ubuntu-virtual-machine:~/laboratory$ uname -a
Linux ubuntu-virtual-machine 3.13.0-46-generic #76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015 i686 i686 i686 GNU/Linux

服务端配置

创建一个文件系统为ext3的qcow2文件

ubuntu@ubuntu-virtual-machine:~/laboratory$ qemu-img create data2.img 20M
ubuntu@ubuntu-virtual-machine:~/laboratory$ mkfs.ext3 data.img
ubuntu@ubuntu-virtual-machine:~/laboratory$ qemu-img convert -O qcow2 data.img data.qcow2

不需要加载nbd模块

root@ubuntu-virtual-machine:~# ls /dev/ | grep nbd
root@ubuntu-virtual-machine:~#

发布文件

root@ubuntu-virtual-machine:~# qemu-nbd -p 8888 -b 0.0.0.0 /home/ubuntu/laboratory/data.qcow2

客户端

客户端环境

安装了libvirt的64位CentOS

[sm@centos ~]$ uname -a
Linux centos 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

客户端配置

加载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