---
layout: post
title: "Mount a Zvol under linux"
date: 2024-01-12
comments: true
tags: zfs, zvol, mount, kpartx, linux, nixos
---

Zvol are volumes backed by a zfs pool. They are quite popular as vm disk images. 

Sometimes you just want to examine them in the host; here is how to mount them.

When the pool is imported, they show up under `/dev/zvol/<path to zvol
dataset>`. At this point they are just raw drives, and kernel knows nothing
about its structure. To be able to mount them, we first need to read its
partition structure and create partition specific `/dev/` entires with
`kpartx(8)`[^1].

```shell
# -a for add
kpartx -a /dev/zvol/<pathtozvol>
```

This will create corresponding devices for partitions under `/dev/mapper`

```
$:/dev/mapper]# ls -al
total 0
drwxr-xr-x  2 root root     120 Jan 12 17:04 .
drwxr-xr-x 20 root root    3980 Jan 12 17:04 ..
crw-------  1 root root 10, 236 Jan 12 17:00 control
lrwxrwxrwx  1 root root       7 Jan 12 17:04 root-zvol1 -> ../dm-0
lrwxrwxrwx  1 root root       7 Jan 12 17:04 root-zvol2 -> ../dm-1
lrwxrwxrwx  1 root root       7 Jan 12 17:04 root-zvol3 -> ../dm-2
```

Now you can mount them as a regular disk with `mount`.

```
mount /dev/mapper/root-zvol1 /mnt
```

[^1]: In nixos, `kpartx` is part of package [`multipath-tools`](https://search.nixos.org/packages?channel=23.11&show=multipath-tools&from=0&size=50&sort=relevance&type=packages&query=kpartx)
