Linux/NBD

From Omnia
Revision as of 21:06, 4 May 2017 by Kenneth (talk | contribs) (→‎Server)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Linux Network Block Device

nbd - Tools for the Linux Kernel's network block device, allowing you to use remote block devices over a TCP/IP network.

Install

Centos: (server, client, but no kernel module)

# epel repo
yum install nbd

Ubuntu: (server, client and kernel module)

apt-get install nbd-server nbd-client
# I answer 'yes' to disconnect all on stop

Server

Create disk file: (or use block device)

dd if=/dev/zero of=/root/nbd.img bs=1M count=100

Start Server:

sudo nbd-server 6000 /root/nbd.img
# Note: This uses the old style, and is no longer supported.

Recommended to use config file, instead of specifying on command line

sudo nbd-server -C nbd.cfg

Config file:

/etc/nbd-server/config

Example:

[generic]
        user = nbd
        group = nbd
[export]
        exportname = /root/sdb

The default port is 10809

Watch output: (don't deamonize)

nbd-server -C test.cfg -d

Client

Load module (if not already loaded):

modprobe -l | grep nbd
modinfo nbd
modprobe nbd

Start client:

sudo nbd-client localhost 6000 /dev/nbd0

Write traffic:

dd if=/dev/zero of=/dev/nbd0

Kill client:

sudo nbd-client -d /dev/nbd0

CentOS Kernel Module

yum install kernel-devel kernel-headers

# wget ftp://ftp.redhat.com/pub/redhat/linux/enterprise/6ComputeNode/en/os/SRPMS/kernel-2.6.32.220.4.1.el6.src.rpm
KERNEL=$(uname -r | sed "s/.`uname -m`//")
# my test was on 2.6.32-573.26.1.el6.x86_64
wget ftp://ftp.redhat.com/pub/redhat/linux/enterprise/6ComputeNode/en/os/SRPMS/kernel-$KERNEL.src.rpm

# rpm -ivh kernel-2.6.32.220.4.1.el6.src.rpm
rpm -ivh kernel-$KERNEL.src.rpm
cd ~/rpmbuild/SOURCES
# tar jxvf linux-$KERNEL.tar.bz2 -C /usr/src/kernels/
tar jxf linux-$KERNEL.tar.bz2 -C /usr/src/kernels/

cd /usr/src/kernels
mv $(uname -r) $(uname -r)-old
# mv linux-2.6.32-220.4.1.el6 $(uname -r)
mv cd linux-$KERNEL $(uname -r)
cd $(uname -r)

make mrproper
cp ../$(uname -r)-old/Module.symvers ./
cp /boot/config-$(uname -r) ./.config
make oldconfig
make prepare
make scripts
make CONFIG_BLK_DEV_NBD=m M=drivers/block

cp drivers/block/nbd.ko /lib/modules/$(uname -r)/kernel/drivers/block/

depmod -a
modprobe nbd

Based on reference from here: http://jamyy.us.to/blog/2012/02/3582.html

yum install kernel-devel kernel-headers
cd /tmp
wget ftp://ftp.redhat.com/pub/redhat/linux/enterprise/6ComputeNode/en/os/SRPMS/kernel-2.6.32.220.4.1.el6.src.rpm
rpm -ivh kernel-2.6.32.220.4.1.el6.src.rpm
cd ~/rpmbuild/SOURCES
tar jxf linux-2.6.32-220.4.1.el6.tar.bz2 -C /usr/src/kernels/
cd /usr/src/kernels
mv $(uname -r) $(uname -r)-old
mv linux-2.6.32-220.4.1.el6 $(uname -r)
cd $(uname -r)
make mrproper
cp ../$(uname -r)-old/Module.symvers ./
cp /boot/config-$(uname -r) ./.config
make oldconfig
make prepare
make scripts
make CONFIG_BLK_DEV_NBD=m M=drivers/block
cp drivers/block/nbd.ko /lib/modules/$(uname -r)/kernel/drivers/block/
depmod -a

Issues

Negotiation: Error: Server closed connection

The user running nbd did not have permission to access device. The /etc/nbd-server/config may specify "user = nbd". Just comment this out, or give user "nbd" permissions.