Skip to content

Commit b5a5980

Browse files
committed
nixvm: automatically mount when --vm-dir is used
changed the share arg from viofs to virtiofs as it is closer to the actual name. Signed-off-by: Joel Granados <[email protected]>
1 parent 2775681 commit b5a5980

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ add the following text to your configruation file (usually /etc/nix/nix.conf):
129129
experimental-features = nix-command flakes
130130
131131
132+
### Mounting directories
133+
134+
This is useful when selftests or modules are needed within the VM.
135+
136+
1. Early mount: Add a `qemu_share_add` to your vmctl config
137+
138+
```sh
139+
qemu_share_add \
140+
--shared-dir "/path/to/modules/lib/modules" \
141+
--tag "modules" \
142+
--share-type "virtiofs" \
143+
--vm-dir "/lib/modules"
144+
```
145+
146+
If you want to share but not mount, remove the --vm-dir arg.
147+
148+
2. Systemd mount: Add a "service" to your nix configuration:
149+
150+
```nix
151+
systemd.services.mount-user-virtiofs = {
152+
description = "Mount virtiofsd tag 'selftests' to /usr/lib/kselftests";
153+
after = [ "local-fs.target" ];
154+
wantedBy = [ "multi-user.target" ];
155+
serviceConfig = {
156+
Type = "oneshot";
157+
ExecStart = "/run/current-system/sw/bin/mount -t virtiofs user /usr/lib/kselftests";
158+
RemainAfterExit = true;
159+
};
160+
};
161+
```
162+
132163
## Prep boot img
133164
134165
The base configruation `*-base.conf` will look for a base image in

lib/qemu/nix

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ qemu_nix_add_noimgnix () {
103103

104104
_qemu_nix_create_nix_init "${VMSTATE}"
105105

106+
qemu_share_add --shared-dir "${NIX_STORE_PATH}" \
107+
--tag "nixstore" \
108+
--share-type "virtiofs" \
109+
--vm-dir "/nix/store"
110+
106111
local nix_init_path=""
107112
nix_init_path="$(readlink "${dst_path}")/init"
108113
local cmd_line="root=/dev/ram0 console=${GUEST_KERNEL_CONSOLE}"
@@ -111,13 +116,14 @@ qemu_nix_add_noimgnix () {
111116
fi
112117
cmd_line="${cmd_line} ${cmd_line_extra}"
113118
cmd_line="${cmd_line} -- --init-path ${nix_init_path}"
114-
cmd_line="${cmd_line} --mount nixstore,/nix/store,virtiofs"
119+
if [[ -v NIX_MOUNTS ]]; then
120+
for nmount in "${NIX_MOUNTS[@]}"; do
121+
cmd_line="${cmd_line} --mount ${nmount}"
122+
done
123+
fi
115124

116125
QEMU_PARAMS+=("-kernel" "${kernel_dir}/${GUEST_KERNEL_IMAGE}")
117126
QEMU_PARAMS+=("-append" "${cmd_line}")
118127
QEMU_PARAMS+=("-initrd" "${VMSTATE}/init.img")
119128

120-
qemu_share_add --shared-dir "${NIX_STORE_PATH}" \
121-
--tag "nixstore" \
122-
--share-type "viofs"
123129
}

lib/qemu/share

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ qemu_share_add_9p() {
4343
}
4444

4545
qemu_share_add() {
46-
local long="shared-dir:,tag:,share-type:"
46+
local long="shared-dir:,tag:,share-type:,vm-dir:"
4747

4848
if ! tmp=$(getopt -o "" --long "$long" -n "${FUNCNAME[0]}" -- "$@"); then
4949
exit 1
@@ -63,6 +63,9 @@ qemu_share_add() {
6363
'--tag' )
6464
local tag="$2"; shift 2;
6565
;;
66+
'--vm-dir' )
67+
local vm_dir="$2"; shift 2;
68+
;;
6669
'--' )
6770
shift; break
6871
;;
@@ -90,10 +93,17 @@ qemu_share_add() {
9093

9194
if [[ "${share_type}" == "9p" ]]; then
9295
qemu_share_add_9p "${shared_dir}" "${tag}"
93-
elif [[ "${share_type}" == "viofs" ]]; then
96+
elif [[ "${share_type}" == "virtiofs" ]]; then
9497
qemu_share_add_virtiofsd "${shared_dir}" "${tag}"
9598
else
9699
_fatal 1 "${FUNCNAME[0]}: Unknown directory share type (${share_type})"
97100
fi
101+
102+
if [[ -v vm_dir ]]; then
103+
if [[ ! -v NIX_STORE_PATH ]]; then
104+
_log "vm-dir ignored! it's only active in NIX"
105+
fi
106+
NIX_MOUNTS+=("${tag},${vm_dir},${share_type}")
107+
fi
98108
}
99109

0 commit comments

Comments
 (0)