How can I mount a block device from one computer to another via the network as a block device?

25,967

Solution 1

I think you might be able to accomplish what you want using network block devices (NBD). Looking at the wikipedia page on the subject there is mention of a tool called nbd. It's comprised of a client and server component.

Example

In this scenario I'm setting up a CDROM on my Fedora 19 laptop (server) and I'm sharing it out to an Ubuntu 12.10 system (client).

installing
$ apt-cache search ^nbd-
nbd-client - Network Block Device protocol - client
nbd-server - Network Block Device protocol - server

$ sudo apt-get install nbd-server nbd-client
sharing a CD

Now back on the server (Fedodra 19) I do a similar thing using its package manager YUM. Once complete I pop a CD in and run this command to share it out as a block device:

$ sudo nbd-server 2000 /dev/sr0

** (process:29516): WARNING **: Specifying an export on the command line is deprecated.

** (process:29516): WARNING **: Please use a configuration file instead.
$

A quick check to see if it's running:

$ ps -eaf | grep nbd
root     29517     1  0 12:02 ?        00:00:00 nbd-server 2000 /dev/sr0
root     29519 29071  0 12:02 pts/6    00:00:00 grep --color=auto nbd
Mounting the CD

Now back on the Ubuntu client we need to connect to the nbd-server using nbd-client like so. NOTE: the name of the nbd-server is greeneggs in this example.

$ sudo nbd-client greeneggs 2000 /dev/nbd0
Negotiation: ..size = 643MB
bs=1024, sz=674983936 bytes

(On some systems - e.g. Fedora - one has to modprobe nbd first.)

We can confirm that there's now a block device on the Ubuntu system using lsblk:

$ sudo lsblk -l
NAME                 MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                    8:0    0 465.8G  0 disk 
sda1                   8:1    0   243M  0 part /boot
sda2                   8:2    0     1K  0 part 
sda5                   8:5    0 465.5G  0 part 
ubuntu-root (dm-0)   252:0    0 461.7G  0 lvm  /
ubuntu-swap_1 (dm-1) 252:1    0   3.8G  0 lvm  [SWAP]
sr0                   11:0    1 654.8M  0 rom  
nbd0                  43:0    0   643M  1 disk 
nbd0p1                43:1    0   643M  1 part 

And now we mount it:

$ sudo mount /dev/nbd0p1 /mnt/
mount: block device /dev/nbd0p1 is write-protected, mounting read-only
$
did it work?

The suspense is killing me, and we have liftoff:

$ sudo ls /mnt/
EFI  GPL  isolinux  LiveOS

There's the contents of a LiveCD of CentOS that I mounted in the Fedora 19 laptop and was able to mount it as a block device of the network on Ubuntu.

Solution 2

One alternative to nbd (if you're interested) is using iSCSI. tgtd can be configured to have a /dev device as its backing storage for a particular iSCSI IQN.

If you're on a RHEL system so you just need to install scsi-target-utils and then configure/start tgtd on the source system. Configuration of tgtd can get involved but Red Hat provides plenty of different examples for the various scenarios.

For Example:

<target iqn.2008-09.com.example:server.target4>
    direct-store /dev/sdb      # Becomes LUN 1
    direct-store /dev/sdc      # Becomes LUN 2
    direct-store /dev/sdd      # Becomes LUN 3
    write-cache off
    vendor_id MyCompany Inc.
</target>

To start it up on Fedora/RHEL:

# systemctl start tgtd.service
# firewall-cmd --add-service iscsi-target

You would install iscsi-initiator-utils on the client system and use iscsiadm to send targets then to "log into" the enumerated targets. For Example:

# iscsiadm -m discovery -t sendtargets -p <remoteHost>
# iscsiadm -m node -T <Complete Target IQN> -l -p <remoteHost>

The iSCSI LUN's will then present to the system as regular block devices. On RHEL, you can check the transport a particular device is coming over you can just do an ls -l /dev/disk/by-path | grep iscsi to see what storage is coming over iSCSI. The path will also list the IQN of the target you logged into above.

When the iscsi device is not needed anymore one can remove it via:

# iscsiadm -m node -T <Complete Target IQN> -u -p <remoteHost>

Solution 3

You obviously prefer the SAN solution. Beside the already mentioned iSCSI and NBD, you have also the AoE (ATA over ethernet) approach.

This is very easy to do:

On the serving side you need to

modprobe aoe
vbladed 0 0 eth0 /dev/sdc

On the client side

modprobe aoe
aoe-discover
aoe-stat
e0.0      1000.204GB       eth0 1024  up

Your devices are in

ls -l /dev/etherd/
c-w--w----  1 root disk 152, 3 Mar 12 22:47 discover
brw-rw----  1 root disk 152, 0 Mar 12 22:47 e0.0
brw-rw----  1 root disk 152, 1 Mar 12 22:47 e0.0p1
cr--r-----  1 root disk 152, 2 Mar 12 22:47 err
c-w--w----  1 root disk 152, 6 Mar 12 22:47 flush
c-w--w----  1 root disk 152, 4 Mar 12 22:47 interfaces
c-w--w----  1 root disk 152, 5 Mar 12 22:47 revalidate

Where e0.0 is your /dev/sdc and e0.0.p1 is /dev/sdc1

dmesg on server:

[221384.454447] aoe: AoE v85 initialised.

dmesg output on client:

[ 1923.225832] aoe: AoE v85 initialised.
[ 1923.226379] aoe: e0.0: setting 1024 byte data frames
[ 1923.226910] aoe: 38607725d8b1 e0.0 v4014 has 1953525168 sectors
[ 1923.653820]  etherd/e0.0: p1

Pretty easy.

Additional Notes

  • vbladed is part of the package vblade on Fedora & Ubuntu, likely the same in other distros as well.
  • aoe-discover & aoe-stat are part of the package aoetools on Fedora & Ubuntu as well.
  • Device shows up in fdisk as a block device, for example, /dev/etherd/e0.0.
  • The version of vblade that's available in the F19 and F20 repositories is pretty dated, it's version 14. The ATAoE project page has version 21 available. There's a updated RPM available for Fedora 19 x86_64 here.

Solution 4

Although this solution works, it is presented only as a method to show that TMWTDI.

For this setup we need a unix inspired system to host the block device, and a system with a loop back block device for the system to view it. Linux will work for both.

Next we need a dumb network file system (it must not make optimizations of the data storage), like samba or webdav. Specifically NFS, and AFS will not work.

Place the block device in a folder that is exported. (man mknod will help.) disable block device checking if applicable. mount the exported filesystem on the client computer, create a loop device using the imported file as it's backing store and mount as normal. (losetup or mount -o loop)

How and why it works, and why the performance sucks: One of the core ideas behind unix is that everything is a file, so devices are files only they are a little special (that is why they are called special files). There are several types of special files that mknod can produce, but we are only going to look at block devices. block devices exist in the file system like any other file and like any other file they have owners, permissions, inodes and names; but they have two differences: they do not store their data in the filesystem, and they have extra capabilities so they can be mounted. If the file server is dumb enough not to pay attention to these differences, you can ignore them and export them as regular files. Next the loop device takes an ordinary file (local or remote) and adds the special properties and capabilities so that you can mount regular files as if they are block devices. The big drawback is performance.

Share:
25,967

Related videos on Youtube

slm
Author by

slm

Worked in the tech field for over 20+ years. Started out learning basic on an Apple IIe then on a TRS-80. Been interested in computer hardware and software my entire life. Consider myself lucky that my hobby as a kid/adult is what I get to do everyday earning a living. You can learn more about me here. ============================================================ Stolen from @Mokubai: First, please put down the chocolate-covered banana and step away from the European currency systems. You may consider how to ask a question.

Updated on September 18, 2022

Comments

  • slm
    slm almost 2 years

    Is it possible to export a block device such as a DVD or CDROM and make it so that it's mountable on another computer as a block device?

    NOTE: I'm not interested in doing this using NFS or Samba, I actually want the optical drive to show up as a optical drive on a remote computer.

  • slm
    slm over 10 years
    If you want you can add your A to this AU Q too, it's what prompted me to write this one up: askubuntu.com/questions/433231/…
  • Bratchley
    Bratchley over 10 years
    I would, but I don't really have an Ubuntu system handy to ensure the package names are the same or if the configuration examples I referenced are upstream or just something Red Hat does. The configuration itself should be the same, though.
  • slm
    slm over 10 years
    Joel you're the best...no Ubuntu system 8-)
  • slm
    slm over 10 years
    Tried doing this b/w Fedora and Ubuntu, it's not working.
  • Admin
    Admin over 10 years
    I usually check everything before I post. Q: is aoe loaded? Did you give proper arguments? On receiver: did aoe found the opponent? What says dmesg?
  • slm
    slm over 10 years
    I know you do, I wasn't implying that it was wrong only that there might be some extra steps 8-)
  • Admin
    Admin over 10 years
    but there are no extra steps, this is really an easy task.
  • Admin
    Admin over 10 years
    it doesnt work on one machine, you need to have two separate machines.
  • Admin
    Admin over 10 years
    what? just only two?
  • slm
    slm over 10 years
    Ha, yeah...right...I have 8...at home. 8-). That's the physical ones, I'd have to look at my Nagio/Cacti graphs to tell you how many VM's I have at home.
  • slm
    slm over 10 years
    Ironically I have no smart phones, I hate them 8-). I have a old school burner type of phone that I can call and text and nothing else. No tablets either. You guys think it's easy to answer all these Q's and know all this stuff about the different distros and architectures 8-)
  • Admin
    Admin over 10 years
    :-), You will be assimilated!
  • slm
    slm over 10 years
    Are you able to export a DVD, such as /dev/sr0 using ATAoE? vblade 0 0 wlp3s0 /dev/sr0 gives me this message: "open: Read-only file system", but doesn't open it.
  • Admin
    Admin over 10 years
    same thing here. vblade exits. but it works with: vblade -d -r 0 0 eth0 /dev/sr0
  • slm
    slm over 10 years
    What version of vblade do you have? I have 14 Release 10.fc19 on Fedora 19, doesn't have -r.
  • Admin
    Admin over 10 years
    the version didnt change since 2009 it's 20
  • guettli
    guettli almost 10 years
    I get this: "WARNING Specifying an export on the command line is deprecated. Please use a configuration file instead". Version: 3.3
  • slm
    slm almost 10 years
    @guettli - thanks for the feedback. What distro was this on?
  • guettli
    guettli almost 10 years
    @sim openSUSE 13.1
  • devius
    devius over 9 years
    Note: greeneggs is the server's host name.
  • slm
    slm over 9 years
    @devius - thanks added a note to that effect in the A.
  • maxschlepzig
    maxschlepzig over 8 years
    Note that the old-style protocol (i.e. using one port for each device) was marked as deprecated for several versions and was dropped in nbd 3.10.