Skip to content

Commit dfd6a3b

Browse files
committed
Do not retain the unikernel image in memory
I guess for 95% of the code, this is fine. I suspect the dump/restore of the internal state blends well with it, also not the restart options (when I suspect the image will be removed, and the invariant is that it is part of the unikernel config).
1 parent 4d35078 commit dfd6a3b

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

src/vmm_unix.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ let free_system_resources name taps =
382382
destroy_tap n)
383383
(Ok ()) taps
384384

385+
let unikernel_image name =
386+
let file = Name.image_file name in
387+
let* exists = Bos.OS.File.exists file in
388+
if exists then
389+
Bos.OS.File.read file
390+
else
391+
Error (`Msg "unikernel image not found")
392+
385393
let cpuset cpu =
386394
let cpustring = string_of_int cpu in
387395
match Lazy.force uname with
@@ -446,6 +454,7 @@ let exec name (config : Unikernel.config) bridge_taps blocks digest =
446454
close_no_err stdout ;
447455
let taps = List.map (fun (_, tap, mac) -> tap, mac) bridge_taps in
448456
let started = Ptime_clock.now () in
457+
let config = { config with image = "" } in
449458
Ok Unikernel.{ config ; cmd = line ; pid ; taps ; digest ; started }
450459
with
451460
Unix.Unix_error (e, _, _) ->

src/vmm_unix.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ val check_commands : unit -> (unit, [> `Msg of string ]) result
1515
val prepare : Name.t -> Unikernel.config ->
1616
((string * string * Macaddr.t option) list * string, [> `Msg of string ]) result
1717

18+
val unikernel_image : Name.t -> (string, [> `Msg of string ]) result
19+
1820
val exec : Name.t -> Unikernel.config -> (string * string * Macaddr.t option) list ->
1921
(string * Name.t * int option) list -> string -> (Unikernel.t, [> `Msg of string ]) result
2022

src/vmm_vmmd.ml

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,13 @@ let handle_unikernel_cmd t id =
295295
function
296296
| `Old_unikernel_info1 ->
297297
Logs.debug (fun m -> m "old info1 %a" Name.pp id) ;
298-
let empty_image unikernel = { unikernel.Unikernel.config with image = "" } in
299298
let unikernels =
300299
match Name.name id with
301300
| None ->
302301
Vmm_trie.fold (Name.path id) t.resources.Vmm_resources.unikernels
303-
(fun id unikernel unikernels -> (id, empty_image unikernel) :: unikernels) []
302+
(fun id unikernel unikernels -> (id, unikernel.config) :: unikernels) []
304303
| Some _ ->
305-
Option.fold ~none:[] ~some:(fun unikernel -> [ id, empty_image unikernel ])
304+
Option.fold ~none:[] ~some:(fun unikernel -> [ id, unikernel.Unikernel.config ])
306305
(Vmm_trie.find id t.resources.Vmm_resources.unikernels)
307306
in
308307
Ok (t, `End (`Success (`Old_unikernels unikernels)))
@@ -311,7 +310,8 @@ let handle_unikernel_cmd t id =
311310
begin match Vmm_trie.find id t.resources.Vmm_resources.unikernels with
312311
| None -> Error (`Msg "get: no unikernel found")
313312
| Some u ->
314-
Ok (t, `End (`Success (`Old_unikernels [ (id, u.Unikernel.config) ])))
313+
let* image = Vmm_unix.unikernel_image id in
314+
Ok (t, `End (`Success (`Old_unikernels [ (id, { u.Unikernel.config with compressed = false ; image }) ])))
315315
end
316316
| `Old_unikernel_info2 ->
317317
Logs.debug (fun m -> m "old info2 %a" Name.pp id) ;
@@ -351,27 +351,15 @@ let handle_unikernel_cmd t id =
351351
Ok (t, `End (`Success (`Unikernel_info infos)))
352352
| `Unikernel_get compress_level ->
353353
Logs.debug (fun m -> m "get %a" Name.pp id) ;
354-
begin match Vmm_trie.find id t.resources.Vmm_resources.unikernels with
355-
| None -> Error (`Msg "get: no unikernel found")
356-
| Some u ->
357-
let cfg = u.Unikernel.config in
358-
let img = cfg.Unikernel.image in
359-
let* compress, img =
360-
if cfg.Unikernel.compressed then
361-
if compress_level > 0 then
362-
Ok (true, img)
363-
else
364-
let* blob = Vmm_compress.uncompress img in
365-
Ok (false, blob)
366-
else
367-
if compress_level = 0 then
368-
Ok (false, img)
369-
else
370-
Ok (true, Vmm_compress.compress ~level:compress_level img)
371-
in
372-
let r = `Unikernel_image (compress, img) in
373-
Ok (t, `End (`Success r))
374-
end
354+
let* image = Vmm_unix.unikernel_image id in
355+
let compress, img =
356+
if compress_level = 0 then
357+
false, image
358+
else
359+
true, Vmm_compress.compress ~level:compress_level image
360+
in
361+
let r = `Unikernel_image (compress, img) in
362+
Ok (t, `End (`Success r))
375363
| `Unikernel_create unikernel_config -> Ok (t, `Create (id, unikernel_config))
376364
| `Unikernel_force_create unikernel_config ->
377365
begin

0 commit comments

Comments
 (0)