Skip to content

Instantly share code, notes, and snippets.

@mping-exo
Last active July 1, 2022 08:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mping-exo/dc50f1e8e6676c1fe6ef0ac0139867bb to your computer and use it in GitHub Desktop.
Save mping-exo/dc50f1e8e6676c1fe6ef0ac0139867bb to your computer and use it in GitHub Desktop.
nbd example

setup

virt-install --name ubuntu18 --vcpus 4 --memory 2048 --disk path=ubuntu18.iso,bus=virtio,cache=writeback --graphics vnc,listen=0.0.0.0  --noautoconsole --os-variant=ubuntu18.10 --import --virt-type kvm

;; virsh 8.0.0
virsh attach-disk ubuntu18 --source foo --source-protocol nbd --source-host-name localhost:6666 --target vde

;; xml
<disk type='network'>
  <source protocol='nbd' name='foo'>
    <host name='localhost' port='6666'/>
  </source>
  <target dev='vde'/>
</disk>

;; virsh 6.0.0 doesn't support source-protocol, etc
virsh attach-device ubuntu18 --file nbd.xml --persistent --live ;; do we need persistent+live?

;; to cleanup
virsh destroy ubuntu18; virsh undefine ubuntu18  

session exchange

gonbdserver:[INFO] Loaded configuration. Available backends: aiofile, file, rbd.
gonbdserver:[INFO] Starting server tcp:127.0.0.1:6666
gonbdserver:[INFO] Starting listening on tcp:127.0.0.1:6666
gonbdserver:[INFO] Connect to tcp:127.0.0.1:6666 from 127.0.0.1:47774
> writing newStyleHeader:  {5639144782019643715 5280542401877725268 3} 24
< reading client flags...
< reading client opt...
=> Read opt: 8
> write NBD_REP_ERR_UNSUP:  {1100100111001001 8 2147483649 0} 24
< reading client opt...
=> Read opt: 7
<read NBD_OPT_GO> 
< reading name length...
< reading numInfoElements...
< reading one infoElement...
find export [102 111 111]
Export: foo

|
> writing NBD_REP_INFO pt1:  {1100100111001001 7 3 12} 24
> writing NBD_INFO_EXPORT:  {0 4096000000 333} 24
> writing NBD_REP_INFO:  {1100100111001001 7 3 5} 24
> writing NBD_INFO_NAME uint16(NBD_INFO_NAME) 1
> writing name [102 111 111] 24
> writing NBD_REP_INFO for description:  {1100100111001001 7 3 2} 24
> writing NBD_INFO_DESCRIPTION uint16(NBD_INFO_DESCRIPTION) 2
> writing description [] 24
> writing NBD_REP_INFO:  {1100100111001001 7 3 14} 24
> writing NBD_INFO_BLOCK_SIZE:  {3 1 32768 134217728} 16
> writing ACK or error:  {1100100111001001 7 1 0} 24

gonbdserver:[INFO] Negotiation succeeded with 127.0.0.1:47774/foo, serving with 5 worker(s)
...
;; edit gonbdserver.conf to see backing file loc, create backing file manually
;; something like
;; dd if=/dev/zero of=/tmp/file.nbd/foo bs=4096 count=1000000
;; ^dir ^export ^blockisze (doesnt really matter)
❯ go build && ./gonbdserver -c gonbdserver.conf -f
gonbdserver:[INFO] Loaded configuration. Available backends: aiofile, file, rbd.
gonbdserver:[INFO] Starting server tcp:127.0.0.1:6666
gonbdserver:[INFO] Starting listening on tcp:127.0.0.1:6666
gonbdserver:[INFO] Connect to tcp:127.0.0.1:6666 from 127.0.0.1:37836
find export [102 111 111]
gonbdserver:[INFO] Negotiation succeeded with 127.0.0.1:37836/foo, serving with 5 worker(s)
❯ sudo modprobe nbd
❯ sudo nbd-client -N foo localhost 6666 /dev/nbd0 -b 4096
[sudo] password for mping:
Negotiation: ..size = 0MB
Connected /dev/nbd0
;; running as superuser
root@3700x:~# mkfs.ext4 -b 4096 /dev/nbd0
root@3700x:~# mkdir -p /mnt/nbd-mount
root@3700x:~# mount /dev/nbd0 /mnt/nbd-mount/
;; to disconnect:
❯ sudo umount /mnt/nbd-mount
❯ sudo nbd-client -d /dev/nbd0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment