1. 介绍
NBD(Network Block Device),即网络块设备。可以将一个远程主机的磁盘空间,当作一个块设备来使用。就像一块硬盘一样使用它,你可以很方便的将另一台服务器的硬盘空间,增加到本地服务器上。NBD与NFS有所不同,NFS只是提供一个挂载点供客户端使用,客户端无法改变这个挂载点的分区格式,而NBD提供的是一个块设备,客户端可以把这个块设备格式化成各种类型的分区,更便于用户的使用。NBD是一个内核模块,大部分Linux发行版都已包含它。
在centos中, 编译成模块NBD, 保存在 /lib/modules/$version/kernel/driver/block/
远程服务器是一段轻量级的守护进程代码,它提供对远程设备的访问,而且该守护进程并不需要运行在linux平台之上。本地操作系统必须是linux,而且要支持Linux内核NBD驱动,并且能够运行本地客户端进程。
图1.NBD在本地客户端代表远程资源
文件系统能被挂载到NBD上(图1),而且NBD设备也能够用来组成冗余磁盘阵列(RAID)。在本地Linux上挂载一个EXT2文件系统到NBD,其传输率要比在同样的远程机器上挂载一个NFS快。
图2.使用NBD的冗余磁盘阵列
2. 加载 nbd 驱动
某些版本的 Linux 不加 max_part 参数会导致没有设备节点 /dev/nbd0p{1,2,3,4…} 等.用 kpartx 也不行. /dev/nbd0p{1,2,3,4} 分区。
# modprobe nbd max_part=8
1
备注:查看Linux 模块信息(modinfo),例如查看nbd模块的参数。 加载后,在/dev/目录下,根据参数创建nbdxx块设备文件。
# modinfo nbd
filename: /lib/modules/4.18.0-147.el8.x86_64/kernel/drivers/block/nbd.ko.xz
license: GPL
description: Network Block Device
rhelversion: 8.1
srcversion: 6A98CFDAB8374FFFD7FCD25
depends:
intree: Y
name: nbd
vermagic: 4.18.0-147.el8.x86_64 SMP mod_unload modversions
sig_id: PKCS#7
signer: CentOS Linux kernel signing key
sig_key: 79:05:D0:5C:21:6F:8A:C5:DD:6E:19:BB:77:9D:05:F6:F2:21:B8:17
sig_hashalgo: sha256
signature: AB:3E:49:FE:84:52:F2:52:6B:AB:C8:D7:A5:71:06:40:87:C8:E6:17:
E2:EF:0E:B9:2C:ED:F4:D8:FA:82:0B:31:70:37:7D:A1:3D:7A:79:32:
1D:89:9C:F2:2E:89:D9:9B:D4:86:EC:33:56:60:60:A3:64:2F:B1:26:
08:9E:47:EF:76:48:96:67:2B:3A:83:A5:8E:5D:F2:4B:D7:83:05:86:
FD:54:AC:ED:38:2E:8B:E8:EE:66:6B:F7:4B:E2:D7:CE:63:A7:4A:2B:
E6:CD:DA:24:04:CC:56:61:CC:84:6D:0B:0E:E6:A3:D2:90:37:E5:FA:
1B:36:91:57:F7:37:B3:FC:5F:44:93:24:9A:B3:A5:8F:8E:BE:08:FA:
35:58:61:9B:C2:4D:02:29:AC:A0:75:8D:3E:0B:75:77:93:31:44:90:
39:BF:C7:ED:39:CE:38:E3:44:30:CA:D4:29:9F:57:E5:86:75:C0:A5:
F0:88:76:A6:37:00:43:A2:10:98:E5:89:38:22:89:25:AE:35:BE:00:
F4:5B:C6:3F:68:C5:49:06:15:6D:58:AF:34:2C:C5:A0:A4:E6:55:49:
10:BD:B1:1A:E5:F2:07:BE:26:AC:80:2B:2D:2C:00:B2:AA:59:79:90:
7B:15:95:FC:28:E7:FE:D0:9D:F3:2F:FE:7C:37:B4:93:99:77:B1:E8:
FD:3D:4B:3A:51:D6:53:FA:DF:FE:47:52:08:14:4B:4A:C8:F3:B9:FD:
A0:98:3F:3F:92:1A:66:3A:D9:5F:80:5E:26:5C:11:3C:E2:38:4D:D0:
D1:DB:DF:16:1B:02:C2:88:AA:5D:24:12:AE:1A:CB:B7:32:2E:50:42:
C7:E1:E5:6F:47:D7:8A:F3:C9:7E:F5:56:B8:38:3C:B1:58:76:1A:CA:
51:E7:1B:E5:CA:6A:6F:72:C7:08:DC:DC:CC:1B:86:C8:8F:8D:38:22:
4C:07:AE:C2:2C:3D:D3:13:8C:FF:3E:BD:27:D8:03:7E:45:B1:2C:85:
13:EE:02:B7
parm: nbds_max:number of network block devices to initialize (default: 16) (int)
parm: max_part:number of partitions per device (default: 16) (int)
3. 挂载
连接:
# qemu-nbd -c /dev/nbd0 test.qcow2
1
查看分区:
# fdisk -l /dev/nbd0
Disk /dev/nbd0: 1 GiB, 1073741824 bytes, 2097152 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
格式化:
# mkfs.ext4 /dev/nbd0
mke2fs 1.44.6 (5-Mar-2019)
Discarding device blocks: done
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 361767db-1aae-4f07-bac2-e93e7903d6c8
挂载:
# mount /dev/nbd0 /root/fs
4. 卸载
卸载文件系统:
# umount /root/fs
断开连接:
# qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
原文链接:https://blog.csdn.net/hbuxiaofei/article/details/106732500