• Stars
    star
    326
  • Rank 123,977 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 1 year ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pure Go NBD server and client library.

go-nbd

Logo

Pure Go NBD server and client library.

hydrun CI Go Version Go Reference Matrix

Overview

go-nbd is a lean NBD server and client library supporting the baseline protocol.

It enables you to ...

  • Build NBD servers and clients in Go: Develop Network Block Device servers and clients using the efficient and easy-to-understand Go programming language, without having to fallback to CGo.
  • Expose any io.ReadWriter as a block device: Effortlessly turn a file, byte slice, S3 bucket or other io.ReadWriter into a fully-fledged block device.
  • Bridge with legacy services: If you need to make your application's dynamic data available to a legacy system, providing a NBD interface can be the perfect solution.

Installation

You can add go-nbd to your Go project by running the following:

$ go get github.com/pojntfx/go-nbd/...@latest

Usage

TL;DR: Define a backend, expose it with a server, connect a block device with the client and setup/mount the filesystem.

1. Define a Backend

First, define a backend; it should conform to this simple interface:

type Backend interface {
	ReadAt(p []byte, off int64) (n int, err error)
	WriteAt(p []byte, off int64) (n int, err error)
	Size() (int64, error)
	Sync() error
}

A simple file-based backend could look like this:

// server/main.go

type FileBackend struct {
	file *os.File
	lock sync.RWMutex
}

func NewFileBackend(file *os.File) *FileBackend {
	return &FileBackend{file, sync.RWMutex{}}
}

func (b *FileBackend) ReadAt(p []byte, off int64) (n int, err error) {
	b.lock.RLock()

	n, err = b.file.ReadAt(p, off)

	b.lock.RUnlock()

	return
}

func (b *FileBackend) WriteAt(p []byte, off int64) (n int, err error) {
	b.lock.Lock()

	n, err = b.file.WriteAt(p, off)

	b.lock.Unlock()

	return
}

func (b *FileBackend) Size() (int64, error) {
	stat, err := b.file.Stat()
	if err != nil {
		return -1, err
	}

	return stat.Size(), nil
}

func (b *FileBackend) Sync() error {
	return b.file.Sync()
}

See pkg/backend for more backend examples.

2. Expose the Backend With a Server

Next, create the backend and expose it with a server:

// server/main.go

b := NewFileBackend(f)

for {
	conn, err := l.Accept()
	if err != nil {
		continue
	}

	go func() {
		if err := server.Handle(
			conn,
			[]server.Export{
				{
					Name:        *name,
					Description: *description,
					Backend:     b,
				},
			},
			&server.Options{
				ReadOnly:           *readOnly,
				MinimumBlockSize:   uint32(*minimumBlockSize),
				PreferredBlockSize: uint32(*preferredBlockSize),
				MaximumBlockSize:   uint32(*maximumBlockSize),
			}); err != nil {
			panic(err)
		}
	}()
}

See cmd/go-nbd-example-server-file/main.go for the full example.

3. Connect to the Server with a Client

In a new main package, connect to the server by creating a client; note that you'll have to modprobe ndb and run the command as root:

// client/main.go

if err := client.Connect(conn, f, &client.Options{
	ExportName: *name,
	BlockSize:  uint32(*blockSize),
}); err != nil {
	panic(err)
}

See cmd/go-nbd-example-client/main.go for the full example.

4. Setup and Mount the Filesystem

Lastly, create a filesystem on the block device and mount it:

$ sudo mkfs.ext4 /dev/nbd0
$ sudo mkdir -p /mnt
$ sudo mount -t ext4 /dev/nbd0 /mnt

You should now be able to use the mounted filesystem by navigating to /mnt.

πŸš€ That's it! We can't wait to see what you're going to build with go-nbd.

Examples

To make getting started with go-nbd easier, take a look at the following examples:

Acknowledgements

Contributing

To contribute, please use the GitHub flow and follow our Code of Conduct.

To build and start a development version of one of the examples locally, run the following:

$ git clone https://github.com/pojntfx/go-nbd.git
$ cd go-nbd
$ rm -f disk.img && truncate -s 10G disk.img && go run ./cmd/go-nbd-example-server-file .
$ go run ./cmd/go-nbd-example-server-memory .

# With the C NBD client
$ sudo umount ~/Downloads/mnt; sudo nbd-client -d /dev/nbd1 && echo 'NBD starting' | sudo tee /dev/kmsg && sudo nbd-client -N default localhost 10809

# With the Go NBD client
$ sudo umount ~/Downloads/mnt; go build -o /tmp/go-nbd-example-client ./cmd/go-nbd-example-client/ && sudo /tmp/go-nbd-example-client --file /dev/nbd1

$ sudo mkfs.ext4 /dev/nbd1
$ sync -f ~/Downloads/mnt; sudo umount ~/Downloads/mnt; sudo rm -rf ~/Downloads/mnt && sudo mkdir -p ~/Downloads/mnt && sudo mount -t ext4 /dev/nbd1 ~/Downloads/mnt && sudo chown -R "${USER}" ~/Downloads/mnt

Have any questions or need help? Chat with us on Matrix!

License

go-nbd (c) 2023 Felicitas Pojtinger and contributors

SPDX-License-Identifier: Apache-2.0

More Repositories

1

weron

Overlay networks based on WebRTC.
Go
1,375
star
2

multiplex

Watch torrents with your friends.
Go
205
star
3

bofied

Modern network boot server.
Go
162
star
4

liwasc

List, wake and scan nodes in a network.
Go
157
star
5

keygaen

Sign, verify, encrypt and decrypt data with PGP in your browser.
Go
110
star
6

htorrent

HTTP to BitTorrent gateway with seeking support.
Go
110
star
7

octarchive

Simple tool to back up all repos on a GitHub/Gitea account to a local folder.
Go
106
star
8

ram-dl

A tool to download more RAM (yes, seriously!)
Go
86
star
9

gon2n

Go bindings, management daemons and CLIs for n2n edges and supernodes.
Go
76
star
10

pojde

Develop from any device with a browser.
Shell
64
star
11

r3map

High-performance remote memory region mounts and migrations in user space.
Go
64
star
12

tinynet

A `net` implementation for Go and TinyGo based on unisockets, targeting both WebAssembly and native platforms.
Go
47
star
13

hydrapp

Build fast apps that run everywhere with Go and a browser engine of your choice.
Go
38
star
14

stfs

Simple Tape File System (STFS), a file system for tapes and tar files.
Go
34
star
15

unisockets

A universal Berkeley sockets implementation for both WebAssembly (based on WebRTC) and native platforms with bindings for C, Go and TinyGo.
TypeScript
34
star
16

alpimager

Build custom Alpine Linux images with Docker.
Go
29
star
17

panrpc

Language-, transport- and serialization-agnostic RPC framework with remote closure support that allows exposing and calling functions on both clients and servers.
TypeScript
26
star
18

nextcloud-talk-bot-framework

A framework for writing Nextcloud Talk chatbots with every language that supports gRPC.
Go
24
star
19

html2goapp

CLI and web app to convert HTML markup to go-app.dev's syntax.
Go
18
star
20

webnetes

Peer-to-Peer Computing Platform for the Browser and Node.
TypeScript
17
star
21

skysweeper

Automatically delete your old skeets from Bluesky.
TypeScript
16
star
22

networked-linux-memsync

Efficient Synchronization of Linux Memory Regions over a Network: A Comparative Study and Implementation (Bachelor's Thesis)
Jupyter Notebook
14
star
23

nextcloud-talk-bot-jitsi

A bot for Nextcloud Talk that creates Jitsi meetings.
JavaScript
14
star
24

hydrun

Execute a command for the current directory on multiple architectures and operating systems.
Go
10
star
25

felicitas.pojtinger.com

My personal site.
HTML
9
star
26

connmapper

Visualize your system's internet connections on a globe.
TypeScript
9
star
27

uni-itsec-notes

Notes for the IT security course at HdM Stuttgart.
Makefile
9
star
28

xeus-cling-binaries

Weekly builds of https://github.com/jupyter-xeus/xeus-cling.
Shell
9
star
29

uni-bwl-notes

Notes for the Planung und Kalkulation von IT-Projekten (econ 101) course at HdM Stuttgart.
Makefile
9
star
30

tapisk

Expose a tape drive as a block device.
Go
8
star
31

deliverance

Universal document publishing system.
Makefile
8
star
32

goit

Get a OIDC token from your terminal.
Go
8
star
33

growlapse

Visualize plant growth over time with Go, WebDAV and WASM; @pojntfx's entry for #growlab.
Go
8
star
34

invaentory

Quickly find all IPv6 and IPv4 hosts in a LAN.
Go
7
star
35

bagop

Build for all Go-supported platforms by default, disable those which you don't want.
Go
7
star
36

rpiplay-binaries

Weekly builds of https://github.com/FD-/RPiPlay.
Shell
6
star
37

webpipe

Share files, sockets, pipes and char devices over WebRTC.
Go
6
star
38

uni-appsecurity-notes

Notes for the Anwendungssicherheit (app security) course at HdM Stuttgart.
6
star
39

webnetesctl

Frontend for webnetes, the peer-to-peer computing platform for the browser and node.
TypeScript
6
star
40

adwaita-gtk-ubuntu

A modern Ambiance replacement that keeps Ubuntu's identity while staying upstream.
CSS
6
star
41

ipxe-binaries

Weekly builds of https://ipxe.org/, with an embedded script that chainloads /config.ipxe.
Shell
5
star
42

grpc-examples

gRPC example microservices in Go, Rust, C#, Python and JavaScript.
JavaScript
5
star
43

cadmium-builds

Weekly builds of https://github.com/Maccraft123/Cadmium.
Shell
5
star
44

uni-db1-notes

Personal notes for the DB1 course at HdM Stuttgart.
Makefile
5
star
45

consumat.io

Track, plan and enjoy content.
5
star
46

go-app-grpc-chat-frontend-web

Web frontend for an example chat application using the `go-app` package and gRPC over WebSockets (like gRPC-Web).
Go
5
star
47

appl-alg-interpreter-binaries

Weekly builds and source mirror for Prof. Dr. Fridtjof Toenniessen's ApplAlgInterpreter.
Java
5
star
48

the-commitment

A contract requiring you to do one daily public OSS contribution, while allowing time for rest.
5
star
49

learn-chinese-platform

A modern and libre way to learn Chinese.
TypeScript
4
star
50

uni-algodat-notes

Personal notes and snippets for the Algorithms and Data Structures course at HdM Stuttgart.
Go
4
star
51

box

Setup for my personal (pet) server.
Makefile
4
star
52

uni-distributedsystems-notes

Notes for the distributed systems course at HdM Stuttgart.
4
star
53

go-isc-dhcp

Management daemons and CLIs for the ISC DHCP server and client.
Go
4
star
54

donna

Minimal personal CRM.
Go
3
star
55

atmosfeed

Create fully custom Bluesky feeds with Wasm modules, powered by Scale Functions.
TypeScript
3
star
56

keystoregaen

Generate Java keystores in your browser.
Go
3
star
57

saltpanelo

Fast, smart and secure service mesh.
Go
3
star
58

image-builder

A tool to build KubeVirt disk images. Based on @Tedezed's implementation.
Shell
3
star
59

minitel

Instructions on how to use a Minitel as a Linux terminal.
3
star
60

nebulark

Distribute your workload to the edge.
JavaScript
3
star
61

ppc32-builds

CI/CD build configurations to get binaries for projects on 32-Bit PowerPC (ppc32) machines.
Shell
2
star
62

uni-se1-notes

Assignments for the SE1 course of HDM Stuttgart, as Jupyter notebooks.
Jupyter Notebook
2
star
63

adwaita-gtk-ubuntu-legacy

To test out whether it may be a better idea to use Adwaita instead of Ambiance in LTS if Communitheme won't be done in time.
SCSS
2
star
64

SwiftUIMenuBar

A menu bar app for macOS built with SwiftUI.
Swift
2
star
65

gon2n-frontend

React frontend for @pojntfx/gon2n.
TypeScript
2
star
66

xeus-sql-binaries

Weekly builds of https://github.com/jupyter-xeus/xeus-sql.
Shell
2
star
67

glode

Compute, Network and Storage Node based on WASM, libp2p and IPFS.
TypeScript
2
star
68

bagccgop

Build for all gccgo-supported platforms by default, disable those which you don't want (bagop with CGo support).
Go
2
star
69

weron-archive

Overlay networks based on WebRTC.
Go
2
star
70

consumat.io-api

API spec for https://github.com/alphahorizonio/consumat.io.
2
star
71

gnome-shell-extension-sticky-app

A GNOME shell extension to toggle an app's visibility.
JavaScript
2
star
72

birdid

Bird + Cupid: Find the first interaction between two Twitter users
Go
2
star
73

go-cuse

CUSE bindings for Go.
Go
2
star
74

mepi

Middle Earth Property Insurance, a uni project of @jakwai01, @dentremor, @lucas-goldner and @pojntfx for HdM Stuttgart.
2
star
75

uni-webdev-backend-notes

Notes for the webdev backend course at HdM Stuttgart.
2
star
76

uni-netpractice-notes

Notes for the Praktikum Rechnernetze (networking practice) course at HdM Stuttgart.
2
star
77

consumat.io-frontend

Frontend for https://github.com/alphahorizonio/consumat.io.
TypeScript
2
star
78

jitsi-meet-node-client

A NodeJS client for Jitsi Meet.
JavaScript
2
star
79

ports2packets

Generates a CSV file with the packets that are to be sent in order to check if a UDP port is open.
Go
2
star
80

papilio

CLI and web app to configure the Terminus FE and SL series of USB Hubs.
Go
2
star
81

mosalink-archive

Archive of research from 2017 on AX.25 mesh networks with batman-adv and eoax.
1
star
82

skytheon

Cross-post between Bluesky, Mastodon and Twitter in all directions with ease.
Go
1
star
83

pojde-rs

Experimental Rust implementation of https://github.com/pojntfx/pojde.
Rust
1
star
84

go-auth-utils

Simple auth utilities for Go to secure web APIs with.
Go
1
star
85

webwormhole-binaries

Weekly builds of https://github.com/saljam/webwormhole.
Shell
1
star
86

clusterplatform

The Lean Distributed Cloud Computing System.
JavaScript
1
star
87

gopojde

Experimental Go implementation of https://github.com/pojntfx/pojde.
Go
1
star
88

tftpdd

TFTP server daemon.
Go
1
star
89

go-app-experiments

Experiments with the `go-app` package
Go
1
star
90

uni-hacking-notes

Notes for the hacking (IT-Sicherheit: Angriff & Verteidigung) course at HdM Stuttgart.
1
star
91

csv2go

Generates Go code containing a multi-dimensional array with the contents of a CSV file.
Go
1
star
92

ruby-packer-binaries

Weekly builds of https://github.com/metanorma/ruby-packer.
Shell
1
star
93

connaections

The TCP and UDP connections of a node, as an API. See https://github.com/pojntfx/connaections-frontend-web/ for a web frontend.
Go
1
star
94

jmather

A grpc-java example project for teaching GitHub Flow to the SE1 course at HdM Stuttgart University.
Java
1
star
95

uni-supply-chain-paper

A paper on supply chain security in software development for Uni.
1
star
96

simple-direct-democracy-backend

Fast and minimal backend for a simple direct democracy's suggestion and voting system.
JavaScript
1
star
97

ipxebuilderd

Build daemon and CLI for iPXE.
Go
1
star
98

documatio

In goes Markdown, out come indexed HTML and PDF slides and documents.
Shell
1
star
99

transcommunal-confederation

Flags etc. of the Transcommunal Confederation and it's communes.
1
star
100

uni-webtopics-notes

Notes for the Spezielle Themen fΓΌr Web-Anwendungen (special topics for web applications) course at HdM Stuttgart.
1
star