Skip to content

Commit 52efec8

Browse files
committed
make the path that is allowed to use /dev/zvol configurable via command-line parameter
1 parent 1d53d82 commit 52efec8

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

daemon/albatrossd.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ let write_reply name fd txt (hdr, cmd) =
220220

221221
let m = conn_metrics "unix"
222222

223-
let jump _ systemd influx tmpdir dbdir no_drop =
223+
let jump _ systemd influx tmpdir dbdir no_drop dev_zvol =
224224
Sys.(set_signal sigpipe Signal_ignore);
225225
Albatross_cli.set_tmpdir tmpdir;
226226
Albatross_cli.set_dbdir dbdir;
227227
Vmm_unix.drop_path := not no_drop;
228+
state := Vmm_vmmd.allow_dev_zvol !state dev_zvol;
228229
state := Vmm_vmmd.init_block_devices !state;
229230
(match Vmm_unix.check_commands () with
230231
| Error `Msg m -> invalid_arg m
@@ -331,6 +332,15 @@ let no_drop_path =
331332
let doc = "Do not drop unikernel path for --name (use --name=path:hello instead of --name=hello)" in
332333
Arg.(value & flag & info [ "no-drop-path" ] ~doc)
333334

335+
let path_c =
336+
Arg.conv
337+
(Name.Path.of_string,
338+
fun ppf p -> Name.pp ppf (Name.make_of_path p))
339+
340+
let allow_dev_zvol =
341+
let doc = "Allow /dev/zvol access to a certain arc" in
342+
Arg.(value & opt (some path_c) None & info [ "allow-zvol-for" ] ~doc)
343+
334344
let cmd =
335345
let doc = "Albatross daemon" in
336346
let man = [
@@ -352,7 +362,7 @@ let cmd =
352362
creation and attaching tap devices to bridges."
353363
] in
354364
let term =
355-
Term.(const jump $ (Albatross_cli.setup_log Albatrossd_utils.syslog) $ Albatrossd_utils.systemd_socket_activation $ Albatrossd_utils.influx $ Albatross_cli.tmpdir $ Albatross_cli.dbdir $ no_drop_path)
365+
Term.(const jump $ (Albatross_cli.setup_log Albatrossd_utils.syslog) $ Albatrossd_utils.systemd_socket_activation $ Albatrossd_utils.influx $ Albatross_cli.tmpdir $ Albatross_cli.dbdir $ no_drop_path $ allow_dev_zvol)
356366
and info = Cmd.info "albatrossd" ~version:Albatross_cli.version ~doc ~man
357367
in
358368
Cmd.v info term

src/vmm_resources.ml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type t = {
88
policies : Policy.t Vmm_trie.t ;
99
block_devices : (int * bool) Vmm_trie.t ;
1010
unikernels : Unikernel.t Vmm_trie.t ;
11+
dev_zvol : Name.Path.t option ;
1112
}
1213

1314
let pp ppf t =
@@ -21,10 +22,11 @@ let pp ppf t =
2122
(fun id unikernel () ->
2223
Fmt.pf ppf "unikernel %a: %a@." Name.pp id Unikernel.pp_config unikernel.Unikernel.config) ()
2324

24-
let empty = {
25+
let empty dev_zvol = {
2526
policies = Vmm_trie.empty ;
2627
block_devices = Vmm_trie.empty ;
27-
unikernels = Vmm_trie.empty
28+
unikernels = Vmm_trie.empty ;
29+
dev_zvol ;
2830
}
2931

3032
let policy_metrics =
@@ -95,9 +97,15 @@ let find_policy t path =
9597

9698
let find_block t name = Vmm_trie.find name t.block_devices
9799

98-
let set_block_usage t name active =
99-
let lbl = Option.value ~default:"" (Option.map Name.Label.to_string (Name.name name)) in
100-
if String.starts_with ~prefix:"/dev/zvol" lbl then
100+
let zvol_allowed dev_zvol name =
101+
match dev_zvol with
102+
| None -> false
103+
| Some x ->
104+
let lbl = Option.value ~default:"" (Option.map Name.Label.to_string (Name.name name)) in
105+
String.starts_with ~prefix:"/dev/zvol" lbl && Name.Path.equal (Name.path name) x
106+
107+
let set_block_usage ?dev_zvol t name active =
108+
if zvol_allowed dev_zvol name then
101109
t
102110
else
103111
match Vmm_trie.find name t with
@@ -107,7 +115,7 @@ let set_block_usage t name active =
107115
then invalid_arg ("block device " ^ Name.to_string name ^ " already in state " ^ (if curr then "active" else "inactive"))
108116
else fst (Vmm_trie.insert name (size, active) t)
109117

110-
let use_blocks t name unikernel active =
118+
let use_blocks ?dev_zvol t name unikernel active =
111119
match unikernel.Unikernel.config.Unikernel.block_devices with
112120
| [] -> t
113121
| blocks ->
@@ -117,12 +125,12 @@ let use_blocks t name unikernel active =
117125
Name.block_name name bd)
118126
blocks
119127
in
120-
List.fold_left (fun t' n -> set_block_usage t' n active) t block_names
128+
List.fold_left (fun t' n -> set_block_usage ?dev_zvol t' n active) t block_names
121129

122130
let remove_unikernel t name = match find_unikernel t name with
123131
| None -> Error (`Msg "unknown unikernel")
124132
| Some unikernel ->
125-
let block_devices = use_blocks t.block_devices name unikernel false in
133+
let block_devices = use_blocks ?dev_zvol:t.dev_zvol t.block_devices name unikernel false in
126134
let unikernels = Vmm_trie.remove name t.unikernels in
127135
let t' = { t with block_devices ; unikernels } in
128136
report_unikernels t' name;
@@ -180,10 +188,10 @@ let check_unikernel t name unikernel =
180188
List.fold_left (fun r (block, dev, _sector_size) ->
181189
let* () = r in
182190
let bl = match dev with Some b -> b | None -> block in
183-
if String.starts_with ~prefix:"/dev/zvol" bl then
191+
let block_name = Name.block_name name bl in
192+
if zvol_allowed t.dev_zvol block_name then
184193
Ok ()
185194
else
186-
let block_name = Name.block_name name bl in
187195
match find_block t block_name with
188196
| None ->
189197
Error (`Msg (Fmt.str "block device %s not found" (Name.to_string block_name)))

src/vmm_resources.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ type t = private {
1818
policies : Policy.t Vmm_trie.t ;
1919
block_devices : (int * bool) Vmm_trie.t ;
2020
unikernels : Unikernel.t Vmm_trie.t ;
21+
dev_zvol : Name.Path.t option ;
2122
}
2223

2324

2425
(** [empty] is the empty tree. *)
25-
val empty : t
26+
val empty : Name.Path.t option -> t
2627

2728
(** [find_unikernel t name] is either [Some unikernel] or [None]. *)
2829
val find_unikernel : t -> Name.t -> Unikernel.t option

src/vmm_vmmd.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,14 @@ let killall t create =
108108
let empty = {
109109
console_counter = 1L ;
110110
stats_counter = 1L ;
111-
resources = Vmm_resources.empty ;
111+
resources = Vmm_resources.empty None ;
112112
waiters = String_map.empty ;
113113
restarting = String_set.empty ;
114114
}
115115

116+
let allow_dev_zvol t dev_zvol =
117+
{ t with resources = Vmm_resources.empty dev_zvol }
118+
116119
let init_block_devices t =
117120
match Vmm_unix.find_block_devices () with
118121
| Error (`Msg msg) ->

src/vmm_vmmd.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type 'a t
66

77
val empty : 'a t
88

9+
val allow_dev_zvol : 'a t -> Name.Path.t option -> 'a t
10+
911
val init_block_devices : 'a t -> 'a t
1012

1113
val waiter : 'a t -> Name.t -> 'a t * 'a option

0 commit comments

Comments
 (0)