Categorygithub.com/anatol/devmapper.go
modulepackage
0.0.0-20230829043248-59ac2b9706ba
Repository: https://github.com/anatol/devmapper.go.git
Documentation: pkg.go.dev

# README

Pure Go library for device mapper targets management

devmapper.go is a pure-Go library that helps to deal with device mapper targets.

Here is an example that demonstrates the API usage:

func main() {
    name := "crypttarget"
    uuid := "2f144136-b0de-4b51-b2eb-bd869cc39a6e"
    key := make([]byte, 32)
    c := devmapper.CryptTable{
        Length:        60000 * 512, // size of the device in bytes
        Encryption:    "aes-xts-plain64",
        Key:           key,
        BackendDevice: "/dev/loop0",
        Flags:         []string{devmapper.CryptFlagAllowDiscards},
    }
    if err := devmapper.CreateAndLoad(name, uuid, c); err != nil {
        // handle error
    }
    defer devmapper.Remove(name)

    // at this point a devmapper target named 'crypttarget' should exist
    // you can check it with 'dmsetup info crypttarget'
    // and udev will create /dev/mapper/crypttarget device file
}

Or the same crypttarget initialization using Linux keychain

func main() {
    // load key into keyring
    keyname := fmt.Sprintf("cryptsetup:%s-d%d", uuid, luksDigestId) // an example of keyname used by LUKS framework
    kid, err := unix.AddKey("logon", keyname, key, unix.KEY_SPEC_THREAD_KEYRING)
    if err != nil {
        return err
    }
    defer unlinkKey(kid)
    keyid := fmt.Sprintf(":%v:logon:%v", len(key), keyname)

    name := "crypttarget"
    uuid := "2f144136-b0de-4b51-b2eb-bd869cc39a6e"
    c := devmapper.CryptTable{
        Length:        60000 * 512, // size of the device in bytes
        Encryption:    "aes-xts-plain64",
        KeyID:         keyid,
        BackendDevice: "/dev/loop0",
        Flags:         []string{devmapper.CryptFlagAllowDiscards},
    }
    if err := devmapper.CreateAndLoad(name, uuid, c); err != nil {
        // handle error
    }
    defer devmapper.Remove(name)
}

func unlinkKey(kid int) {
	if _, err := unix.KeyctlInt(unix.KEYCTL_REVOKE, kid, 0, 0, 0); err != nil {
		fmt.Printf("key revoke: %v\n", err)
	}

	if _, err := unix.KeyctlInt(unix.KEYCTL_UNLINK, kid, unix.KEY_SPEC_THREAD_KEYRING, 0, 0); err != nil {
		fmt.Printf("key unlink, thread: %v\n", err)
	}

	// We added key to thread keyring only. But let's try to unlink the key from other keyrings as well just to be safe
	_, _ = unix.KeyctlInt(unix.KEYCTL_UNLINK, kid, unix.KEY_SPEC_PROCESS_KEYRING, 0, 0)
	_, _ = unix.KeyctlInt(unix.KEYCTL_UNLINK, kid, unix.KEY_SPEC_USER_KEYRING, 0, 0)
}

License

See LICENSE.

# Packages

No description provided by the author

# Functions

Create creates a new device.
CreateAndLoad creates, loads the provided tables and resumes the device.
GetVersion returns version for the dm-mapper kernel interface.
InfoByDevno returns device mapper information by its block device number (major/minor).
InfoByName returns device information by its name.
List provides a list of dmsetup devices.
Load loads given table into the device.
Message passes a message string to the target at specific offset of a device.
OpenUserspaceVolume opens a volume that allows to read/write data.
Remove removes the device and destroys its tables.
Rename renames the device.
Resume resumes the given device.
SetUUID sets uuid for a given device.
Suspend suspends the given device.

# Constants

CryptFlagAllowDiscards is an equivalent of 'allow_discards' crypt option.
CryptFlagNoReadWorkqueue is an equivalent of 'no_read_workqueue' crypt option.
CryptFlagNoWriteWorkqueue is an equivalent of 'no_write_workqueue' crypt option.
CryptFlagSameCPUCrypt is an equivalent of 'same_cpu_crypt' crypt option.
CryptFlagSubmitFromCryptCPUs is an equivalent of 'submit_from_crypt_cpus' crypt option.
ReadOnlyFlag is a devmapper readonly flag value.
SectorSize is a device size used for devmapper calculations.

# Structs

CryptTable represents information needed for 'crypt' target creation.
DeviceInfo is a type that holds devmapper device information.
LinearTable represents information needed for 'linear' target creation.
ListItem represents information about a dmsetup device.
VerityTable represents information needed for 'verity' target creation.
ZeroTable represents information needed for 'zero' target creation.

# Interfaces

Table is a type to represent different devmapper targets like 'zero', 'crypt', ...
Volume represents reader/writer for the data handled by the device mapper table.