Compare commits
17 commits
killall
...
systemd-so
Author | SHA1 | Date | |
---|---|---|---|
Reynir Björnsson | 549a70b2a5 | ||
Reynir Björnsson | f7a3c4fdac | ||
Reynir Björnsson | f280892894 | ||
Reynir Björnsson | e6eba35a97 | ||
Reynir Björnsson | 9f317f2638 | ||
9afe691de2 | |||
c3cd5bd5ff | |||
Reynir Björnsson | 362ff7b27a | ||
Reynir Björnsson | 04ed59202b | ||
Reynir Björnsson | c67bafa063 | ||
Reynir Björnsson | b2b9ddcdef | ||
Reynir Björnsson | 99a992b3c4 | ||
Reynir Björnsson | f79ed78a2b | ||
Reynir Björnsson | d9c572109c | ||
Reynir Björnsson | 0013e55d71 | ||
Reynir Björnsson | 0508465bba | ||
Reynir Björnsson | 0c29e2b90d |
19
albatross-systemd.opam
Normal file
19
albatross-systemd.opam
Normal 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)"
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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/.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
16
packaging/Linux/dune
Normal 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)))
|
51
packaging/Linux/gen_socket.ml
Normal file
51
packaging/Linux/gen_socket.ml
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue