Compare commits

...

17 commits

Author SHA1 Message Date
Reynir Björnsson 549a70b2a5 Add albatross-systemd package 2020-11-26 10:02:47 +01:00
Reynir Björnsson f7a3c4fdac Generate systemd.socket files 2020-11-26 09:19:58 +01:00
Reynir Björnsson f280892894 Linux: Rename albatross_stat -> albatross_stats 2020-11-26 09:17:11 +01:00
Reynir Björnsson e6eba35a97 Linux README: add comment about socket paths 2020-11-26 08:09:24 +01:00
Reynir Björnsson 9f317f2638 Only add --systemd-socket-activation flag on Linux 2020-11-26 07:40:09 +01:00
Reynir Björnsson 9afe691de2 Apply suggestions from code review
Co-authored-by: Hannes Mehnert <hannes@mehnert.org>
2020-11-26 07:36:18 +01:00
Reynir Björnsson c3cd5bd5ff
albatross_log.service: don't depend on albatross_console
Co-authored-by: Hannes Mehnert <hannes@mehnert.org>
2020-11-26 07:29:04 +01:00
Reynir Björnsson 362ff7b27a Linux: Fix socket permissions 2020-11-24 14:41:56 +01:00
Reynir Björnsson 04ed59202b Set FD_CLOEXEC in sd_listen_fds 2020-11-18 21:30:29 +01:00
Reynir Björnsson c67bafa063 Implement sd_listen_fds in OCaml 2020-11-18 17:36:36 +01:00
Reynir Björnsson b2b9ddcdef Systemd services depend on their sockets 2020-11-11 14:31:26 +01:00
Reynir Björnsson 99a992b3c4 AssertPathExists belongs under [Unit]
And not [Service].
2020-11-11 14:18:07 +01:00
Reynir Björnsson f79ed78a2b packaging/Linux/albatross_stat.service fix typo 2020-11-11 14:11:25 +01:00
Reynir Björnsson d9c572109c Install .socket files 2020-11-11 13:54:06 +01:00
Reynir Björnsson 0013e55d71 Add missing systemd.socket files
Also pass the new --systemd-socket-activation to the daemons.
2020-11-11 13:02:48 +01:00
Reynir Björnsson 0508465bba Refactor socket activation, use for all daemons 2020-11-11 12:17:08 +01:00
Reynir Björnsson 0c29e2b90d Experimental systemd socket activation 2020-11-11 11:41:07 +01:00
18 changed files with 164 additions and 35 deletions

19
albatross-systemd.opam Normal file
View file

@ -0,0 +1,19 @@
opam-version: "2.0"
maintainer: "Hannes Mehnert <hannes@mehnert.org>"
authors: ["Hannes Mehnert <hannes@mehnert.org>" "Reynir Björnsson <reynir@reynir.dk>"]
homepage: "https://github.com/hannesm/albatross"
dev-repo: "git+https://github.com/hannesm/albatross.git"
bug-reports: "https://github.com/hannesm/albatross/issues"
license: "ISC"
available: os = "linux"
depends: [
"albatross" {= version}
"fmt"
"fpath"
]
build: [
["dune" "subst"] {pinned}
["dune" "build" "-p" name "-j" jobs]
]
synopsis: "Albatross - orchestrate and manage MirageOS unikernels with Solo5 (SystemD files)"

View file

@ -345,6 +345,13 @@ let retry_connections =
let doc = "Number of retries when connecting to other daemons (log, console, stats etc). 0 aborts after one failure, -1 is unlimited retries." in
Arg.(value & opt int 0 & info [ "retry-connections" ] ~doc)
let systemd_socket_activation =
match Lazy.force Vmm_unix.uname with
| FreeBSD -> Term.const false
| Linux ->
let doc = "Pass this flag when systemd socket activation is being used" in
Arg.(value & flag & info [ "systemd-socket-activation" ] ~doc)
let exit_status = function
| Ok () -> Ok Success
| Error e -> Ok e

View file

@ -158,12 +158,12 @@ let handle s addr =
let m = Vmm_core.conn_metrics "unix"
let jump _ influx tmpdir =
let jump _ systemd influx tmpdir =
Sys.(set_signal sigpipe Signal_ignore) ;
Albatross_cli.set_tmpdir tmpdir;
Lwt_main.run
(Albatross_cli.init_influx "albatross_console" influx;
Vmm_lwt.server_socket `Console >>= fun s ->
Vmm_lwt.server_socket ~systemd `Console >>= fun s ->
let rec loop () =
Lwt_unix.accept s >>= fun (cs, addr) ->
m `Open;
@ -177,7 +177,7 @@ open Cmdliner
open Albatross_cli
let cmd =
Term.(term_result (const jump $ setup_log $ influx $ tmpdir)),
Term.(term_result (const jump $ setup_log $ systemd_socket_activation $ influx $ tmpdir)),
Term.info "albatross_console" ~version
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1

View file

@ -148,7 +148,7 @@ let handle mvar ring s addr =
let m = Vmm_core.conn_metrics "unix"
let jump _ file read_only influx tmpdir =
let jump _ systemd file read_only influx tmpdir =
Sys.(set_signal sigpipe Signal_ignore) ;
Albatross_cli.set_tmpdir tmpdir;
Lwt_main.run
@ -161,7 +161,7 @@ let jump _ file read_only influx tmpdir =
Lwt.return_unit
end else begin
Albatross_cli.init_influx "albatross_log" influx;
Vmm_lwt.server_socket `Log >>= fun s ->
Vmm_lwt.server_socket ~systemd `Log >>= fun s ->
let ring = Vmm_ring.create `Startup () in
List.iter (Vmm_ring.write ring) entries ;
let mvar = Lwt_mvar.create_empty () in
@ -192,7 +192,7 @@ let read_only =
Arg.(value & flag & info [ "read-only" ] ~doc)
let cmd =
Term.(const jump $ setup_log $ file $ read_only $ influx $ tmpdir),
Term.(const jump $ setup_log $ systemd_socket_activation $ file $ read_only $ influx $ tmpdir),
Term.info "albatross_log" ~version
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1

View file

@ -135,7 +135,7 @@ let write_reply name fd txt (hdr, cmd) =
let m = conn_metrics "unix"
let jump _ influx tmpdir dbdir retries enable_stats =
let jump _ systemd influx tmpdir dbdir retries enable_stats =
Sys.(set_signal sigpipe Signal_ignore);
Albatross_cli.set_tmpdir tmpdir;
Albatross_cli.set_dbdir dbdir;
@ -165,7 +165,7 @@ let jump _ influx tmpdir dbdir retries enable_stats =
else
Lwt.return_none) >>= fun s ->
Lwt.catch
(fun () -> Vmm_lwt.server_socket `Vmmd)
(fun () -> Vmm_lwt.server_socket ~systemd `Vmmd)
(fun e ->
let str =
Fmt.strf "unable to create server socket %a: %s"
@ -218,7 +218,7 @@ let jump _ influx tmpdir dbdir retries enable_stats =
open Cmdliner
let cmd =
Term.(const jump $ setup_log $ influx $ tmpdir $ dbdir $ retry_connections $ enable_stats),
Term.(const jump $ setup_log $ systemd_socket_activation $ influx $ tmpdir $ dbdir $ retry_connections $ enable_stats),
Term.info "albatrossd" ~version:Albatross_cli.version
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1

View file

@ -1,6 +1,8 @@
# systemd service scripts
these are preliminary and just here to let people play with `solo5-spt`, the seccomp-enabled backend for [Solo5](https://github.com/Solo5/solo5) on Linux.
Note: The socket paths are hardcoded relative to the RuntimeDirectory (tmpdir).
If you modify `Vmm_core.socket_path` you must modify the corresponding `.socket` file(s) in this directory.
1) You need to build the `albatross` tooling in this repository
2) To run unikernels, you need to build and install solo5-elftool and at least one of the tenders: solo5-hvt and solo5-spt. They can be installed somewhere in PATH or in /var/lib/albatross/.

View file

@ -3,12 +3,13 @@
# to create an override configuration:
# systemctl edit albatross_console.service
Description=Albatross console daemon (albatross_console)
Requires=albatross_console.socket
After=syslog.target
[Service]
Type=simple
User=albatross
ExecStart=/usr/local/sbin/albatross-console --tmpdir="%t/albatross/" -vv
ExecStart=/usr/local/sbin/albatross-console --systemd-socket-activation --tmpdir="%t/albatross/" -vv
RuntimeDirectoryPreserve=yes
RuntimeDirectory=albatross
ExecStartPre=/bin/mkdir -p %t/albatross/fifo
@ -18,4 +19,5 @@ PIDFile=%t/albatross/console.pid
RestrictAddressFamilies=AF_UNIX
[Install]
Also=albatross_console.socket
WantedBy=multi-user.target

View file

@ -1,6 +1,6 @@
[Unit]
Description=Albatross VMM daemon (albatrossd)
Requires=albatross_console.service albatross_log.service
Requires=albatross_console.socket albatross_log.socket albatross_daemon.socket
After=syslog.target albatross_console.service albatross_log.service
[Service]
@ -8,7 +8,7 @@ Type=simple
# TODO not necessarily needs to be run as root, anything that can solo5-spt/hvt,
# create tap interfaces should be fine!
User=root
ExecStart=/usr/local/sbin/albatrossd --tmpdir="%t/albatross/" -vv
ExecStart=/usr/local/sbin/albatrossd --systemd-socket-activation --tmpdir="%t/albatross/" -vv
#RuntimeDirectoryPreserve=yes
#RuntimeDirectory=albatross
PIDFile=%t/albatross/daemon.pid
@ -27,4 +27,5 @@ IgnoreSIGPIPE=true
#RuntimeDirectoryMode=0700
[Install]
Also=albatross_daemon.socket
WantedBy=multi-user.target

View file

@ -4,17 +4,18 @@
# systemctl edit albatross_log.service
Description=Albatross log daemon (albatross_log)
After=syslog.target albatross_console.service
Requires=albatross_console.service
Requires=albatross_log.socket
AssertPathExists=/var/lib/albatross/albatross.log
[Service]
Type=simple
User=albatross
AssertPathExists=/var/lib/albatross/albatross.log
ExecStart=/usr/local/sbin/albatross-log --logfile="/var/lib/albatross/albatross.log" --tmpdir="%t/albatross/" -vv
ExecStart=/usr/local/sbin/albatross-log --systemd-socket-activation --logfile="/var/lib/albatross/albatross.log" --tmpdir="%t/albatross/" -vv
RuntimeDirectory=albatross albatross/util
#RuntimeDirectoryPreserve=yes # avoid albatross.log being cleaned up
PIDFile=%t/albatross/log.pid
RestrictAddressFamilies=AF_UNIX
[Install]
Also=albatross_log.socket
WantedBy=multi-user.target

View file

@ -3,16 +3,18 @@
# to create an override configuration:
# systemctl edit albatross_stat.service
Description=Albatross stat daemon (albatross_stat)
Requires=albatross_stat.socket
After=syslog.target
[Service]
Type=simple
User=albatross
ExecStart=/usr/local/sbin/albatross-stats --tmpdir="%t/albatross/" -vv
ExecStart=/usr/local/sbin/albatross-stats --systemd-socket-activation --tmpdir="%t/albatross/" -vv
RuntimeDirectoryPreserve=yes
RuntimeDirectory=albatross albatross/util
PIDFile=%t/albatross/stat.pid
RestrictAddressFamilies=AF_UNIX
[Install]
Also=albatross_stat.socket
WantedBy=multi-user.target

16
packaging/Linux/dune Normal file
View file

@ -0,0 +1,16 @@
(executable
(name gen_socket)
(libraries albatross))
(install
(files albatross_console.service albatross_console.socket
albatross_daemon.service albatross_daemon.socket
albatross_log.service albatross_log.socket
albatross_stats.service albatross_stats.socket)
(section share)
(package albatross-systemd))
(rule
(targets albatross_console.socket albatross_daemon.socket
albatross_log.socket albatross_stats.socket)
(action (run ./gen_socket.exe)))

View file

@ -0,0 +1,51 @@
let pp_service ppf = function
| `Console -> Fmt.pf ppf "console"
| `Vmmd -> Fmt.pf ppf "daemon"
| `Stats -> Fmt.pf ppf "stats"
| `Log -> Fmt.pf ppf "log"
let unprivileged_services = [ `Console; `Log; `Stats ]
let privileged_services = [ `Vmmd ]
let unprivileged_format : _ format= {|[Unit]
Description=Albatross %s socket
PartOf=%s
[Socket]
ListenStream=%s
SocketUser=albatross
SocketMode=0600
Accept=no
[Install]
WantedBy=sockets.target
|}
let privileged_format : _ format = {|[Unit]
Description=Albatross %s socket
PartOf=%s
[Socket]
ListenStream=%s
SocketMode=0600
Accept=no
[Install]
WantedBy=sockets.target
|}
let write_socket privileged service =
let out_file = Fmt.str "albatross_%a.socket" pp_service service in
let systemd_service = Fmt.str "albatross_%a.service" pp_service service in
let service_name = Fmt.str "%a" pp_service service in
let socket_path = Vmm_core.socket_path (service :> Vmm_core.service) in
let oc = open_out out_file in
let ppf = Format.formatter_of_out_channel oc in
Fmt.pf ppf (if privileged then privileged_format else unprivileged_format)
service_name systemd_service socket_path
let () =
let tmpdir = Fpath.(v "%t" / "albatross") in
let () = Vmm_core.set_tmpdir tmpdir in
List.iter (write_socket false) unprivileged_services;
List.iter (write_socket true) privileged_services

View file

@ -5,7 +5,8 @@ sudo mkdir -m 0700 -p /var/lib/albatross/block
sudo install -o "$ALBATROSS_USER" -- /dev/null /var/lib/albatross/albatross.log
sudo cp ../../_build/install/default/bin/* /usr/local/sbin/
sudo cp ./albatross_*.service /etc/systemd/system/
sudo cp ../../_build/install/default/share/albatross-systemd/albatross_*.service /etc/systemd/system/
sudo cp ../../_build/install/default/share/albatross-systemd/albatross_*.socket /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl stop albatross_console
sudo systemctl start albatross_console

View file

@ -12,20 +12,25 @@ let safe_close fd =
(fun () -> Lwt_unix.close fd)
(fun _ -> Lwt.return_unit)
let server_socket sock =
let name = Vmm_core.socket_path sock in
(Lwt_unix.file_exists name >>= function
| true -> Lwt_unix.unlink name
| false -> Lwt.return_unit) >>= fun () ->
let s = Lwt_unix.(socket PF_UNIX SOCK_STREAM 0) in
Lwt_unix.set_close_on_exec s ;
let old_umask = Unix.umask 0 in
let _ = Unix.umask (old_umask land 0o707) in
Lwt_unix.(bind s (ADDR_UNIX name)) >|= fun () ->
Logs.app (fun m -> m "listening on %s" name);
let _ = Unix.umask old_umask in
Lwt_unix.listen s 1 ;
s
let server_socket ~systemd sock =
if systemd
then match Vmm_unix.sd_listen_fds () with
| Some [fd] -> Lwt.return (Lwt_unix.of_unix_file_descr fd)
| _ -> failwith "Systemd socket activation error" (* FIXME *)
else
let name = Vmm_core.socket_path sock in
(Lwt_unix.file_exists name >>= function
| true -> Lwt_unix.unlink name
| false -> Lwt.return_unit) >>= fun () ->
let s = Lwt_unix.(socket PF_UNIX SOCK_STREAM 0) in
Lwt_unix.set_close_on_exec s ;
let old_umask = Unix.umask 0 in
let _ = Unix.umask (old_umask land 0o707) in
Lwt_unix.(bind s (ADDR_UNIX name)) >|= fun () ->
Logs.app (fun m -> m "listening on %s" name);
let _ = Unix.umask old_umask in
Lwt_unix.listen s 1 ;
s
let connect addrtype sockaddr =
let c = Lwt_unix.(socket addrtype SOCK_STREAM 0) in

View file

@ -2,7 +2,7 @@
val pp_sockaddr : Format.formatter -> Lwt_unix.sockaddr -> unit
val server_socket : Vmm_core.service -> Lwt_unix.file_descr Lwt.t
val server_socket : systemd:bool -> Vmm_core.service -> Lwt_unix.file_descr Lwt.t
val connect : Lwt_unix.socket_domain -> Lwt_unix.sockaddr -> Lwt_unix.file_descr option Lwt.t

View file

@ -26,6 +26,26 @@ let check_solo5_cmd name =
| Ok cmd, _ | _, Ok cmd -> Ok cmd
| _ -> R.error_msgf "%s does not exist" name
(* Pure OCaml implementation of SystemD's sd_listen_fds.
* Note: this implementation does not unset environment variables. *)
let sd_listen_fds () =
let fd_of_int (fd : int) : Unix.file_descr = Obj.magic fd in
let sd_listen_fds_start = 3 in
match Sys.getenv_opt "LISTEN_PID", Sys.getenv_opt "LISTEN_FDS" with
| None, _ | _, None -> None
| Some listen_pid, Some listen_fds ->
match int_of_string_opt listen_pid, int_of_string_opt listen_fds with
| None, _ | _, None -> None
| Some listen_pid, Some listen_fds ->
if listen_pid = Unix.getpid ()
then Some (List.init listen_fds
(fun i ->
let fd = fd_of_int (sd_listen_fds_start + i) in
let () = Unix.set_close_on_exec fd in
fd))
else None
(* here we check that the binaries we use in this file are actually present *)
let check_commands () =
let uname_cmd = Bos.Cmd.v "uname" in

View file

@ -8,6 +8,8 @@ type supported = FreeBSD | Linux
val uname : supported Lazy.t
val sd_listen_fds : unit -> Unix.file_descr list option
val set_dbdir : Fpath.t -> unit
val check_commands : unit -> (unit, [> R.msg ]) result

View file

@ -66,13 +66,13 @@ let timer () =
let m = Vmm_core.conn_metrics "unix"
let jump _ interval influx tmpdir =
let jump _ systemd interval influx tmpdir =
Sys.(set_signal sigpipe Signal_ignore);
Albatross_cli.set_tmpdir tmpdir;
let interval = Duration.(to_f (of_sec interval)) in
Lwt_main.run
(Albatross_cli.init_influx "albatross_stats" influx;
Vmm_lwt.server_socket `Stats >>= fun s ->
Vmm_lwt.server_socket ~systemd `Stats >>= fun s ->
let _ev = Lwt_engine.on_timer interval true (fun _e -> Lwt.async timer) in
let rec loop () =
Lwt_unix.accept s >>= fun (cs, addr) ->
@ -90,7 +90,7 @@ let interval =
Arg.(value & opt int 10 & info [ "interval" ] ~doc)
let cmd =
Term.(term_result (const jump $ setup_log $ interval $ influx $ tmpdir)),
Term.(term_result (const jump $ setup_log $ systemd_socket_activation $ interval $ influx $ tmpdir)),
Term.info "albatross_stats" ~version
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1