Skip to content

Commit

Permalink
nixos-modules/microvm/options: add microvm.volumes readOnly option
Browse files Browse the repository at this point in the history
  • Loading branch information
astro committed Dec 5, 2024
1 parent 5792bc1 commit 3374f72
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 13 deletions.
9 changes: 7 additions & 2 deletions lib/runners/alioth.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ in {
"--blk" (lib.escapeShellArg "path=${storeDisk},readonly=true")
]
++
builtins.concatMap ({ image, serial, direct, ... }:
builtins.concatMap ({ image, serial, direct, readOnly, ... }:
lib.warnIf (serial != null) ''
Volume serial is not supported for alioth
''
lib.warnIf direct ''
Volume direct IO is not supported for alioth
''
[ "--blk" (lib.escapeShellArg image) ]
[
"--blk"
(lib.escapeShellArg "path=${image},readOnly=${
lib.boolToString readOnly
}")
]
) volumes
++
builtins.concatMap ({ proto, socket, tag, ... }:
Expand Down
10 changes: 7 additions & 3 deletions lib/runners/cloud-hypervisor.nix
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,17 @@ in {
readonly = "on";
} // mqOps))
++
map ({ image, serial, direct, ... }:
map ({ image, serial, direct, readOnly, ... }:
opsMapped (
{
path = toString image;
direct =
if direct == null then null
else if direct then "on"
if direct
then "on"
else "off";
readonly =
if readOnly
then "on"
else "off";
} //
lib.optionalAttrs (serial != null) {
Expand Down
6 changes: 4 additions & 2 deletions lib/runners/crosvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ in {
"-s" socket
]
++
builtins.concatMap ({ image, direct, serial, ... }:
builtins.concatMap ({ image, direct, serial, readOnly, ... }:
[ "--block"
"${image},o_direct=${
if direct then "true" else "false"
lib.boolToString direct
},ro=${
lib.boolToString readOnly
}${
lib.optionalString (serial != null) ",id=${serial}"
}"
Expand Down
4 changes: 2 additions & 2 deletions lib/runners/firecracker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let
is_root_device = false;
is_read_only = true;
io_engine = "Async";
} ] ++ map ({ image, serial, direct, ... }:
} ] ++ map ({ image, serial, direct, readOnly, ... }:
lib.warnIf (serial != null) ''
Volume serial is not supported for firecracker
''
Expand All @@ -47,7 +47,7 @@ let
drive_id = image;
path_on_host = image;
is_root_device = false;
is_read_only = false;
is_read_only = readOnly;
io_engine = "Async";
}) volumes;
network-interfaces = map ({ type, id, mac, ... }:
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/kvmtool.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ in {
++
lib.optionals (balloonMem > 0) [ "--balloon" ]
++
builtins.concatMap ({ image, serial, direct, ... }:
builtins.concatMap ({ image, serial, direct, readOnly, ... }:
lib.warnIf (serial != null) ''
Volume serial is not supported for kvmtool
''
[ "-d"
(lib.escapeShellArg "image${
lib.optionalString direct ",direct"
}${
lib.optionalString readOnly ",ro"
}")
]
) volumes
Expand Down
4 changes: 2 additions & 2 deletions lib/runners/qemu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ lib.warnIf (mem == 2048) ''
lib.optionals (user != null) [ "-user" user ] ++
lib.optionals (socket != null) [ "-qmp" "unix:${socket},server,nowait" ] ++
lib.optionals (balloonMem > 0) [ "-device" "virtio-balloon" ] ++
builtins.concatMap ({ image, letter, serial, direct, ... }:
builtins.concatMap ({ image, letter, serial, direct, readOnly, ... }:
[ "-drive"
"id=vd${letter},format=raw,file=${image},if=none,aio=io_uring,discard=unmap${
lib.optionalString (direct != null) ",cache=none"
}"
},read-only=${if readOnly then "on" else "off"}"
"-device"
"virtio-blk-${devType},drive=vd${letter}${
lib.optionalString (serial != null) ",serial=${serial}"
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/stratovirt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@ in {
"-device" "virtio-blk-${devType 2},drive=store,id=blk_store"
] ++
lib.optionals (socket != null) [ "-qmp" "unix:${socket},server,nowait" ] ++
builtins.concatMap ({ index, image, letter, serial, direct, ... }: [
builtins.concatMap ({ index, image, letter, serial, direct, readOnly, ... }: [
"-drive"
"id=vd${
letter
},format=raw,if=none,aio=io_uring,file=${
image
},direct=${
if direct then "on" else "off"
},readonly=${
if readOnly then "on" else "off"
}"
"-device"
"virtio-blk-${
Expand Down
5 changes: 5 additions & 0 deletions nixos-modules/microvm/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ in
default = false;
description = "Whether to set O_DIRECT on the disk.";
};
readOnly = mkOption {
type = bool;
default = false;
description = "Turn off write access";
};
label = mkOption {
type = nullOr str;
default = null;
Expand Down

0 comments on commit 3374f72

Please sign in to comment.