import Base import ./hvf.bend as H import ../src/vmm/snapdir.bend as Sd import ../src/vmm/cmds.bend as Cm import ../src/vmm/disksnap.bend as Ds import ../src/vmm/opts.bend as Op import ../src/vmm/hostfiles.bend as Hf import ../src/vmm/cli.bend as Cli import ../src/lib/u64.bend as U import ../src/disk/ext4free.bend as Ex import ../src/disk/access.bend as Ac # bend-vmm as a library: typed calls on a VM from another process (this # one runs no VM). A VM is named by its folder (bend-vmm create; its # control socket is DIR/control.sock) or by its --control socket path. The # decisions are src/vmm/{snapdir,cmds,disksnap}.bend's; here they are # carried out. Nothing here prints or ends the process: every call answers # a Result. Vmm.run_dir and Vmm.restore (host/main.bend) run a VM. # Why a call failed. ENoSocket: no socket there (a folder: no VM ever ran); # ENotRunning: a socket nobody answers on (the VM stopped); ETimeout: no # reply in time; EBadName, EExists, ENoSnapshot: snapshot names; ENoVm: not # a VM folder; EFailed: the VM answered an error, or the host failed. type Err is Data: ENoSocket{} ENotRunning{} ETimeout{} EBadName{name: String} EExists{name: String} ENoSnapshot{name: String} ENoVm{path: String} EFailed{why: String} def Err.text(e: Err) -> String: match e: case ENoSocket{}: "no control socket there (is the VM running?)" case ENotRunning{}: "the VM is not running" case ETimeout{}: "the VM did not answer in time" case EBadName{n}: "bad snapshot name " ++ n ++ " (1-64 of A-Z a-z 0-9 . _ -, not starting with .)" case EExists{n}: "a snapshot " ++ n ++ " exists already" case ENoSnapshot{n}: "no snapshot " ++ n case ENoVm{p}: p ++ " is not a VM folder (no vm.conf; bend-vmm create makes one)" case EFailed{w}: w # A VM's state: running, paused or stopped, and its forwards (vm.conf's # --forward; "" for a socket). type Status is Data: Status{state: String, forwards: String} # A VM as a call reaches it: its control socket, and its folder ("" when # named by its socket). type Vm is Data: Vm{sock: String, dir: String} def Api.fail(-A: Type, e: Err) -> IO(Result<&1, &1, Err, A>): IO.pure(Result<&1, &1, Err, A>, Fail{e}) def Api.done(-A: Type, x: A) -> IO(Result<&1, &1, Err, A>): IO.pure(Result<&1, &1, Err, A>, Done{x}) def Api.vm.of(+path: String, +ws: +List) -> Vm: Bool.pick(Vm, U32.is_eq(H.Host.first(ws), 1), Vm{path, ""}, Vm{path ++ "/control.sock", path}) # A socket path is the VM's socket; anything else its folder. def Api.vm(+path: String) -> IO(Vm): do IO: +ws : +List <- H.Hvf.sock_facts(path) return Api.vm.of(path, ws) def Api.sock(v: Vm) -> String: match v: case Vm{s, d}: s def Api.dir(v: Vm) -> String: match v: case Vm{s, d}: d # Text files (vm.conf, a snapshot's info): "" when missing. def Api.read.got(got: File & Result<&1, &1, U32 & String, String>) -> IO(String): match got: case (f, Done{s}): do IO: File.close(f) return s case (f, Fail{e}): do IO: File.close(f) return "" def Api.read.opened(r: Result<&1, &1, U32 & String, File>) -> IO(String): match r: case Fail{e}: IO.pure(String, "") case Done{f}: do IO: got : File & Result<&1, &1, U32 & String, String> <- File.read(f, 1048576) Api.read.got(got) def Api.read(path: String) -> IO(String): do IO: r : Result<&1, &1, U32 & String, File> <- File.open(path, "r") Api.read.opened(r) def Api.write.got(got: File & Result<&1, &1, U32 & String, Unit>) -> IO(Bool): match got: case (f, Done{u}): do IO: File.close(f) return True{} case (f, Fail{e}): do IO: File.close(f) return False{} def Api.write.opened(r: Result<&1, &1, U32 & String, File>, +text: String) -> IO(Bool): match r: case Fail{e}: IO.pure(Bool, False{}) case Done{f}: do IO: got : File & Result<&1, &1, U32 & String, Unit> <- File.write(f, text) Api.write.got(got) def Api.write(path: String, +text: String) -> IO(Bool): do IO: r : Result<&1, &1, U32 & String, File> <- File.open(path, "w") Api.write.opened(r, text) def Api.chars(bs: +List) -> String: match bs: case Nil{}: "" case Con{b, rest}: SCon{Char.from_u32(b), Api.chars(rest)} # The VM's answer to one control line. ENOENT (2): no socket; any other # connect error (ECONNREFUSED): a socket nobody answers on. def Api.reply(+ws: +List) -> Result<&1, &1, Err, String>: +tag = H.Host.first(ws) +rest = List.drop(&2, U32, ws, 1n) +text = Api.chars(rest) Bool.pick(Result<&1, &1, Err, String>, U32.is_eq(tag, 2), Fail{ETimeout{}}, Bool.pick(Result<&1, &1, Err, String>, U32.is_eq(tag, 1), Fail{Bool.pick(Err, U32.is_eq(H.Host.first(rest), 2), ENoSocket{}, ENotRunning{})}, Bool.pick(Result<&1, &1, Err, String>, String.starts_with(text, "error"), Fail{EFailed{"the VM answered: " ++ text}}, Done{text}))) def Api.call.at(+sock: String, +line: String, +ms: U32) -> IO(Result<&1, &1, Err, String>): do IO>: +ws : +List <- H.Hvf.ctl_call(sock, line, ms) return Api.reply(ws) # One control line to a VM (its folder or socket), its reply. def Vmm.ctl(+vm: String, +line: String) -> IO(Result<&1, &1, Err, String>): do IO>: +v : Vm <- Api.vm(vm) Api.call.at(Api.sock(v), line, 30000) def Vmm.pause(+vm: String) -> IO(Result<&1, &1, Err, String>): Vmm.ctl(vm, "pause") def Vmm.resume(+vm: String) -> IO(Result<&1, &1, Err, String>): Vmm.ctl(vm, "resume") # stop ends the VM at once (quit); powerdown asks the guest to shut down. def Vmm.stop(+vm: String) -> IO(Result<&1, &1, Err, String>): Vmm.ctl(vm, "quit") def Vmm.powerdown(+vm: String) -> IO(Result<&1, &1, Err, String>): Vmm.ctl(vm, "powerdown") # The folder's VM options (vm.conf, paths made the folder's). def Api.conf(+dir: String) -> IO(List<&2, String>): do IO>: +text : String <- Api.read(dir ++ "/vm.conf") return Cm.Conf.flags(dir, text) def Api.status.of(r: Result<&1, &1, Err, String>, +fw: String) -> Result<&1, &1, Err, Status>: match r: case Done{+t}: Done{Status{Bool.pick(String, String.ends_with(t, "\"paused\"}"), "paused", "running"), fw}} case Fail{e}: match e: case ENoSocket{}: Done{Status{"stopped", fw}} case ENotRunning{}: Done{Status{"stopped", fw}} case ETimeout{}: Fail{ETimeout{}} case EBadName{n}: Fail{EBadName{n}} case EExists{n}: Fail{EExists{n}} case ENoSnapshot{n}: Fail{ENoSnapshot{n}} case ENoVm{p}: Fail{ENoVm{p}} case EFailed{w}: Fail{EFailed{w}} # running, paused or stopped (a VM that is not running is stopped, not an # error), with its forwards. def Vmm.status(+vm: String) -> IO(Result<&1, &1, Err, Status>): do IO>: +v : Vm <- Api.vm(vm) +d : String = Api.dir(v) +flags : List<&2, String> <- Bool.pick(IO(List<&2, String>), String.is_empty(d), IO.pure(List<&2, String>, []), Api.conf(d)) r : Result<&1, &1, Err, String> <- Api.call.at(Api.sock(v), "status json", 30000) return Api.status.of(r, Cm.Sub.opt(flags, "--forward", "")) # Snapshots # ========= def Api.names(xs: List) -> List<&2, String>: match xs: case Nil{}: [] case Con{+x, rest}: +more : List<&2, String> = Api.names(rest) Bool.pick(List<&2, String>, String.starts_with(x, "."), more, Con{x, more}) def Api.has(xs: List<&2, String>, +n: String) -> Bool: match xs: case Nil{}: False{} case Con{+x, rest}: String.eq(x, n) || Api.has(rest, n) # A folder and everything in it (snapshot folders hold only files). def Api.rm_files(xs: List, +dir: String) -> IO(Unit): match xs: case Nil{}: IO.pure(Unit, Unit{}) case Con{x, rest}: do IO: _ : U32 <- H.Hvf.remove(dir ++ "/" ++ x) Api.rm_files(rest, dir) def Api.rm_dir(+dir: String) -> IO(U32): do IO: xs : List <- H.Hvf.ls(dir) Api.rm_files(xs, dir) H.Hvf.rmdir(dir) # The KiB a snapshot's disk copies hold on their own (Hvf.size_kib: blocks # not shared with the disk they were cloned from, nor other copies). def Api.disk_kib(xs: List, +dir: String) -> IO(U32): match xs: case Nil{}: IO.pure(U32, 0) case Con{+x, rest}: do IO: +k : U32 <- Bool.pick(IO(U32), String.starts_with(x, "disk"), H.Hvf.size_kib(dir ++ "/" ++ x), IO.pure(U32, 0)) +more : U32 <- Api.disk_kib(rest, dir) return (k + more : U32) # A snapshot's info with what its memory and disks hold now, on their own: # a disk copy cloned from the disk, and a memory file cloned from the # snapshot before it (Snapf.rebase), share nearly all of it at first and # take more as the VM moves on (the space removing the snapshot gives # back). def Api.info.now(i: Sd.Info, +mk: U32, +dk: U32) -> Sd.Info: match i: case Sd.Info{n, t, p, m, d, note}: Sd.Info{n, t, p, mk, dk, note} def Api.infos(ns: List<&2, String>, +dir: String) -> IO(List<&2, Sd.Info>): match ns: case Nil{}: IO.pure(List<&2, Sd.Info>, []) case Con{+n, rest}: do IO>: +sd : String = Sd.Sdir.dir(dir, n) +text : String <- Api.read(Sd.Sdir.info(sd)) xs : List <- H.Hvf.ls(sd) +dk : U32 <- Api.disk_kib(xs, sd) +mk : U32 <- H.Hvf.size_kib(sd ++ "/memory") more : List<&2, Sd.Info> <- Api.infos(rest, dir) return Con{Api.info.now(Sd.Sdir.info.parse(text), mk, dk), more} def Api.no_dir() -> Err: EFailed{"snapshots need a VM folder (for a socket: bend-vmm ctl SOCKET snapshot PATH)"} # The VM's snapshots, oldest first. def Api.snapshots.in(none: Bool, +d: String) -> IO(Result<&1, &1, Err, List<&2, Sd.Info>>): match none: case True{}: Api.fail(List<&2, Sd.Info>, Api.no_dir()) case False{}: do IO>>: xs : List <- H.Hvf.ls(Sd.Sdir.root(d)) infos : List<&2, Sd.Info> <- Api.infos(Api.names(xs), d) return Done{Sd.Sdir.sorted(infos, [])} def Vmm.snapshots(+vm: String) -> IO(Result<&1, &1, Err, List<&2, Sd.Info>>): do IO>>: +v : Vm <- Api.vm(vm) Api.snapshots.in(String.is_empty(Api.dir(v)), Api.dir(v)) def Api.rm.go(ok: Bool, +d: String, +name: String) -> IO(Result<&1, &1, Err, String>): match ok: case False{}: Api.fail(String, ENoSnapshot{name}) case True{}: do IO>: +rc : U32 <- Api.rm_dir(Sd.Sdir.dir(d, name)) return Bool.pick(Result<&1, &1, Err, String>, U32.is_eq(rc, 0), Done{name}, Fail{EFailed{"cannot remove " ++ Sd.Sdir.dir(d, name) ++ " (" ++ U32.show(rc) ++ ")"}}) def Api.rm.in(none: Bool, +d: String, +name: String) -> IO(Result<&1, &1, Err, String>): match none: case True{}: Api.fail(String, Api.no_dir()) case False{}: do IO>: xs : List <- H.Hvf.ls(Sd.Sdir.root(d)) Api.rm.go(Sd.Sname.valid(name) && Api.has(Api.names(xs), name), d, name) def Vmm.snapshot_rm(+vm: String, +name: String) -> IO(Result<&1, &1, Err, String>): do IO>: +v : Vm <- Api.vm(vm) Api.rm.in(String.is_empty(Api.dir(v)), Api.dir(v), name) def Api.prune.go(ns: List<&2, String>, +d: String) -> IO(List<&2, String>): match ns: case Nil{}: IO.pure(List<&2, String>, []) case Con{+n, rest}: do IO>: _ : U32 <- Api.rm_dir(Sd.Sdir.dir(d, n)) more : List<&2, String> <- Api.prune.go(rest, d) return Con{n, more} # prune --keep N: removes the oldest past the newest N; their names. def Api.prune.in(none: Bool, +d: String, +keep: U32) -> IO(Result<&1, &1, Err, List<&2, String>>): match none: case True{}: Api.fail(List<&2, String>, Api.no_dir()) case False{}: do IO>>: xs : List <- H.Hvf.ls(Sd.Sdir.root(d)) infos : List<&2, Sd.Info> <- Api.infos(Api.names(xs), d) gone : List<&2, String> <- Api.prune.go(Sd.Sdir.prune(infos, keep), d) return Done{gone} def Vmm.prune(+vm: String, +keep: U32) -> IO(Result<&1, &1, Err, List<&2, String>>): do IO>>: +v : Vm <- Api.vm(vm) Api.prune.in(String.is_empty(Api.dir(v)), Api.dir(v), keep) # disksnap's steps, in order, until one fails (0, or its errno). def Api.step(s: Ds.Step) -> IO(U32): match s: case Ds.DClone{from, to}: H.Hvf.clone(from, to, Bool.to_u32(Ds.Dsnap.copy_fallback())) case Ds.DRename{from, to}: H.Hvf.rename(from, to) case Ds.DRemove{p}: do IO: _ : U32 <- H.Hvf.remove(p) return 0 def Api.steps(ss: List, +rc: U32) -> IO(U32): match ss: case Nil{}: IO.pure(U32, rc) case Con{s, rest}: do IO: +r : U32 <- Bool.pick(IO(U32), U32.is_eq(rc, 0), Api.step(s), IO.pure(U32, rc)) Api.steps(rest, r) # The disks of a stopped VM copied into a snapshot folder (its memory is not # running anywhere: a snapshot of a stopped VM is its disks). def Api.copy_disks(+tmp: String, +d: String) -> IO(U32): do IO: +flags : List<&2, String> <- Api.conf(d) +rc : U32 <- Api.steps(Ds.Dsnap.save(tmp ++ "/memory", Ds.Dsnap.folder_gen(), Ds.Dsnap.writable(Cm.Sub.opt(flags, "--disk", ""))), 0) Api.steps(Ds.Dsnap.place(tmp ++ "/memory", Ds.Dsnap.folder_gen(), Ds.Dsnap.writable(Cm.Sub.opt(flags, "--disk", ""))), rc) def Api.stopped(e: Err) -> Bool: match e: case ENoSocket{}: True{} case ENotRunning{}: True{} case _: False{} def Api.snap.renamed(+rc: U32, +info: Sd.Info, +dst: String) -> Result<&1, &1, Err, Sd.Info>: Bool.pick(Result<&1, &1, Err, Sd.Info>, U32.is_eq(rc, 0), Done{info}, Fail{EFailed{"cannot place " ++ dst ++ " (" ++ U32.show(rc) ++ ")"}}) # A snapshot folder written whole: its info, then renamed into place; the # VM's current snapshot (the next one's parent) is now this one. def Api.snap.place(ok: Bool, +rc: U32, +d: String, +name: String, +note: String, +t: U32, +tmp: String) -> IO(Result<&1, &1, Err, Sd.Info>): match ok: case False{}: do IO>: _ : U32 <- Api.rm_dir(tmp) return Fail{EFailed{"cannot copy the disks (" ++ U32.show(rc) ++ ")"}} case True{}: do IO>: +parent : String <- Api.read(Sd.Sdir.root(d) ++ "/.current") +mem : U32 <- H.Hvf.size_kib(tmp ++ "/memory") xs : List <- H.Hvf.ls(tmp) +dk : U32 <- Api.disk_kib(xs, tmp) +info : Sd.Info = Sd.Info{name, t, String.trim(parent), mem, dk, note} _ : Bool <- Api.write(Sd.Sdir.info(tmp), Sd.Sdir.info.text(info)) +dst : String = Sd.Sdir.dir(d, name) +r : U32 <- H.Hvf.rename(tmp, dst) _ : Bool <- Bool.pick(IO(Bool), U32.is_eq(r, 0), Api.write(Sd.Sdir.root(d) ++ "/.current", name), IO.pure(Bool, False{})) return Api.snap.renamed(r, info, dst) # The VM's answer to `snapshot TMP/`; a VM that is not running has its # disks copied instead. def Api.snap.failed(stopped: Bool, +e: Err, +d: String, +name: String, +note: String, +t: U32, +tmp: String) -> IO(Result<&1, &1, Err, Sd.Info>): match stopped: case True{}: do IO>: +rc : U32 <- Api.copy_disks(tmp, d) Api.snap.place(U32.is_eq(rc, 0), rc, d, name, note, t, tmp) case False{}: do IO>: _ : U32 <- Api.rm_dir(tmp) return Fail{e} def Api.snap.after(r: Result<&1, &1, Err, String>, +d: String, +name: String, +note: String, +t: U32, +tmp: String) -> IO(Result<&1, &1, Err, Sd.Info>): match r: case Done{x}: Api.snap.place(True{}, 0, d, name, note, t, tmp) case Fail{+e}: Api.snap.failed(Api.stopped(e), e, d, name, note, t, tmp) def Api.snap.take(+d: String, +name: String, +note: String, +t: U32) -> IO(Result<&1, &1, Err, Sd.Info>): do IO>: _ : U32 <- H.Hvf.mkdir(Sd.Sdir.root(d), Hf.Files.private_dir()) +tmp : String = Sd.Sdir.tmp(d, name) _ : U32 <- Api.rm_dir(tmp) _ : U32 <- H.Hvf.mkdir(tmp, Hf.Files.private_dir()) r : Result<&1, &1, Err, String> <- Api.call.at(d ++ "/control.sock", "snapshot " ++ tmp ++ "/", 3600000) Api.snap.after(r, d, name, note, t, tmp) def Api.snap.check(valid: Bool, exists: Bool, +d: String, +name: String, +note: String, +t: U32) -> IO(Result<&1, &1, Err, Sd.Info>): match valid exists: case False{} _: Api.fail(Sd.Info, EBadName{name}) case True{} True{}: Api.fail(Sd.Info, EExists{name}) case True{} False{}: Api.snap.take(d, name, note, t) # A snapshot of the VM in its folder: name "" takes the time's # (Sname.of_time). A running VM keeps its memory and disks; a stopped one # its disks. Answers what was saved. def Api.snap.in(none: Bool, +d: String, +base: String, +gen: Bool, +note: String, +t: U32) -> IO(Result<&1, &1, Err, Sd.Info>): match none: case True{}: Api.fail(Sd.Info, Api.no_dir()) case False{}: do IO>: xs : List <- H.Hvf.ls(Sd.Sdir.root(d)) +names : List<&2, String> = Api.names(xs) +name : String = Bool.pick(String, gen, Sd.Sname.unique(base, names), base) Api.snap.check(Sd.Sname.valid(name), Api.has(names, name), d, name, note, t) # A snapshot of the VM in its folder: name "" takes the time's # (Sname.of_time; with -2, -3 ... when taken). A running VM keeps its # memory and disks; a stopped one its disks. Answers what was saved. def Vmm.snapshot(+vm: String, +name0: String, +note: String) -> IO(Result<&1, &1, Err, Sd.Info>): do IO>: +v : Vm <- Api.vm(vm) +now : U.U64 <- H.Host.now() +t : U32 = U.U64.lo(now) +gen : Bool = String.is_empty(name0) Api.snap.in(String.is_empty(Api.dir(v)), Api.dir(v), Bool.pick(String, gen, Sd.Sname.of_time(t), name0), gen, note, t) # The snapshot a restore takes first (Sname.undo, unique). def Vmm.snapshot_undo(+vm: String, +note: String) -> IO(Result<&1, &1, Err, Sd.Info>): do IO>: +v : Vm <- Api.vm(vm) +now : U.U64 <- H.Host.now() +t : U32 = U.U64.lo(now) Api.snap.in(String.is_empty(Api.dir(v)), Api.dir(v), Sd.Sname.undo(t), True{}, note, t) # A VM folder: DIR/vm.conf with these options (checked as the command line # would be); refused when DIR has one already. def Api.create.go(+why: String, there: Bool, +dir: String, +text: String) -> IO(Result<&1, &1, Err, String>): match there: case True{}: Api.fail(String, EFailed{dir ++ " has a vm.conf already"}) case False{}: Bool.pick(IO(Result<&1, &1, Err, String>), Bool.not(String.is_empty(why)), Api.fail(String, EFailed{why}), IO.bind(U32, Result<&1, &1, Err, String>, H.Hvf.mkdir(dir, Hf.Files.private_dir()), rc => IO.bind(Bool, Result<&1, &1, Err, String>, Api.write(dir ++ "/vm.conf", text), ok => IO.pure(Result<&1, &1, Err, String>, Bool.pick(Result<&1, &1, Err, String>, ok, Done{dir ++ "/vm.conf"}, Fail{EFailed{"cannot write " ++ dir ++ "/vm.conf"}}))))) def Vmm.create(+dir: String, +flags: List<&2, String>) -> IO(Result<&1, &1, Err, String>): do IO>: +old : String <- Api.read(dir ++ "/vm.conf") Api.create.go(Cli.Cli.check(Cm.Sub.l1(flags)), Bool.not(String.is_empty(old)), dir, Cm.Conf.text(flags)) # The command line's commands: their results printed (host/main.bend's cli) def vmdir.undo_said(r: Result<&1, &1, Err, Sd.Info>) -> IO(Unit): match r: case Done{+i}: IO.print("bend-vmm: saved " ++ Sd.Sdir.name(i) ++ " first (to undo: bend-vmm restore " ++ Sd.Sdir.name(i) ++ ")") case Fail{e}: IO.die(Unit, 1, "bend-vmm: cannot save the current state before restoring: " ++ Err.text(e)) def cli.kib(+k: U32) -> String: Bool.pick(String, U32.is_lt(k, 10240), U32.show(k) ++ " KiB", U32.show((k / 1024 : U32)) ++ " MiB") def cli.info(i: Sd.Info) -> String: match i: case Sd.Info{+n, +t, +p, +m, +d, +note}: n ++ " " ++ Sd.Sname.of_time(t) ++ " memory " ++ Bool.pick(String, U32.is_eq(m, 0), "-", cli.kib(m)) ++ " disks " ++ cli.kib(d) ++ Bool.pick(String, String.is_empty(p), "", " from " ++ p) ++ Bool.pick(String, String.is_empty(note), "", " (" ++ note ++ ")") def cli.infos(xs: List<&2, Sd.Info>) -> IO(Unit): match xs: case Nil{}: IO.pure(Unit, Unit{}) case Con{x, rest}: do IO: IO.print(cli.info(x)) cli.infos(rest) def cli.lines(xs: List<&2, String>, +pre: String) -> IO(Unit): match xs: case Nil{}: IO.pure(Unit, Unit{}) case Con{x, rest}: do IO: IO.print(pre ++ x) cli.lines(rest, pre) def cli.die(e: Err) -> IO(Unit): IO.die(Unit, 1, "bend-vmm: " ++ Err.text(e)) def cli.text(r: Result<&1, &1, Err, String>, +pre: String) -> IO(Unit): match r: case Done{t}: IO.print(pre ++ t) case Fail{e}: cli.die(e) def cli.snap(r: Result<&1, &1, Err, Sd.Info>) -> IO(Unit): match r: case Done{i}: IO.print("saved " ++ cli.info(i)) case Fail{e}: cli.die(e) def cli.snaps(r: Result<&1, &1, Err, List<&2, Sd.Info>>) -> IO(Unit): match r: case Done{+xs}: Bool.pick(IO(Unit), List.is_empty(&2, Sd.Info, xs), IO.print("no snapshots"), cli.infos(xs)) case Fail{e}: cli.die(e) def cli.names(r: Result<&1, &1, Err, List<&2, String>>) -> IO(Unit): match r: case Done{+xs}: Bool.pick(IO(Unit), List.is_empty(&2, String, xs), IO.print("nothing to remove"), cli.lines(xs, "removed ")) case Fail{e}: cli.die(e) def cli.status(r: Result<&1, &1, Err, Status>) -> IO(Unit): match r: case Done{Status{+st, +fw}}: IO.print(st ++ Bool.pick(String, String.is_empty(fw), "", " forwards " ++ fw)) case Fail{e}: cli.die(e) def cli.created(r: Result<&1, &1, Err, String>) -> IO(Unit): match r: case Done{+p}: IO.print("created " ++ p ++ " (run it: bend-vmm run " ++ String.take(p, Nat.sub(String.length(p), 8n)) ++ ")") case Fail{e}: cli.die(e) def cli.ctl(+vm: String, +line: String) -> IO(Unit): Bool.pick(IO(Unit), String.eq(line, "status json"), IO.bind(Result<&1, &1, Err, Status>, Unit, Vmm.status(vm), cli.status), IO.bind(Result<&1, &1, Err, String>, Unit, Vmm.ctl(vm, line), r => cli.text(r, ""))) def cli.none(s: Cm.Sub) -> Bool: match s: case Cm.SubNone{}: True{} case _: False{} # Running a VM folder's VM (run: host/main.bend's Vmm.run, passed in) def die_if(bad: Bool, what: String) -> IO(Unit): match bad: case False{}: IO.pure(Unit, Unit{}) case True{}: IO.die(Unit, 1, "bend-vmm: " ++ what) def vmdir.sock(+dir: String) -> String: dir ++ "/control.sock" # Runs the VM of a folder: its vm.conf's options, its control socket in it. @unsafe def Vmm.run_dir(run: List -> List -> IO(Unit), +prog: List, +dir: String) -> IO(Unit): do IO: +text : String <- Api.read(dir ++ "/vm.conf") die_if(String.is_empty(text), Err.text(ENoVm{dir})) run(prog, Cm.Sub.l1(List.append(&2, String, Cm.Conf.flags(dir, text), ["--control", vmdir.sock(dir)]))) def vmdir.stopped(st: Result<&1, &1, Err, Status>) -> Bool: match st: case Done{Status{state, fw}}: String.eq(state, "stopped") case Fail{e}: True{} # Waits (at most fuel x 100 ms) for the VM to stop. def vmdir.wait_stop(fuel: Nat, +vm: String) -> IO(Unit): match fuel: case 0n: IO.pure(Unit, Unit{}) case 1n+k: do IO: st : Result<&1, &1, Err, Status> <- Vmm.status(vm) Bool.pick(IO(Unit), vmdir.stopped(st), IO.pure(Unit, Unit{}), IO.bind(Unit, Unit, IO.sleep(100), u => vmdir.wait_stop(k, vm))) # Runs a folder's VM from one of its snapshots. A snapshot with memory # restores (its disks set back from its copies, src/vmm/disksnap.bend); one # of disks only sets them back and boots. @unsafe def vmdir.restore.go(mem: Bool, run: List -> List -> IO(Unit), +prog: List, +vm: String, +snap: String, +flags: List<&2, String>) -> IO(Unit): match mem: case True{}: run(prog, Cm.Sub.l1(List.append(&2, String, Cm.Conf.for_restore(flags), ["--restore", snap ++ "/memory", "--control", vmdir.sock(vm)]))) case False{}: do IO: +rc : U32 <- Api.steps(Ds.Dsnap.restore(snap ++ "/memory", Ds.Dsnap.folder_gen(), Ds.Dsnap.writable(Cm.Sub.opt(flags, "--disk", ""))), 0) die_if(U32.is_ne(rc, 0), "cannot set the disks back from " ++ snap ++ " (" ++ U32.show(rc) ++ ")") run(prog, Cm.Sub.l1(List.append(&2, String, flags, ["--control", vmdir.sock(vm)]))) # bend-vmm restore NAME: the current state is saved first (memory and disks # when running, disks when stopped: Sname.undo), the VM stopped, then run # from NAME in this process. @unsafe def Vmm.restore(run: List -> List -> IO(Unit), +prog: List, +vm: String, +name: String) -> IO(Unit): do IO: +text : String <- Api.read(vm ++ "/vm.conf") die_if(String.is_empty(text), Err.text(ENoVm{vm})) +snap : String = Sd.Sdir.dir(vm, name) +info : String <- Api.read(Sd.Sdir.info(snap)) die_if(Bool.not(Sd.Sname.valid(name)) || String.is_empty(info), Err.text(ENoSnapshot{name})) undo : Result<&1, &1, Err, Sd.Info> <- Vmm.snapshot_undo(vm, "saved before restoring " ++ name) vmdir.undo_said(undo) _ : Result<&1, &1, Err, String> <- Vmm.stop(vm) vmdir.wait_stop(300n, vm) _ : Bool <- Api.write(Sd.Sdir.root(vm) ++ "/.current", name) +ws : +List <- H.Hvf.sock_facts(snap ++ "/memory") vmdir.restore.go(U32.is_eq(H.Host.first(ws), 2), run, prog, vm, snap, Cm.Conf.flags(vm, text)) # bend-vmm sparsify: each image's ext4 free space punched (src/disk/ext4free.bend # decides where; the host reads and punches). The image opens as a writer # does (an exclusive lock: a running VM's disk is refused). def Spr.punch(+id: U32, rg: U.U64 & U.U64) -> IO(U.U64): match rg: case (+off, +len): Bool.pick(IO(U.U64), U.U64.is_zero(len), IO.pure(U.U64, U.U64.zero()), IO.bind(U32, U.U64, H.Host.disk_discard(id, off, len), e => IO.pure(U.U64, Bool.pick(U.U64, U32.is_zero(e), len, U.U64.zero())))) # Group g's free runs punched; the bytes freed so far. def Spr.runs(rs: +List, +id: U32, +f: Ex.Fs, +acc: U.U64) -> IO(U.U64): match rs: case Nil{}: IO.pure(U.U64, acc) case Con{r, rest}: do IO: +n : U.U64 <- Spr.punch(id, Ex.Fs.range(f, r)) Spr.runs(rest, id, f, U.U64.add(acc, n)) def Spr.group(m: Maybe<&2, U32>, +id: U32, +f: Ex.Fs, +g: U32) -> IO(U.U64): match m: case None{}: IO.pure(U.U64, U.U64.zero()) case Some{+b}: do IO: +bs : +List <- H.Host.disk_read(id, Ex.Fs.block_at(f, b), Ex.Fs.bsize(f)) Spr.runs(Ex.Fs.free_runs(f, g, bs), id, f, U.U64.zero()) def Spr.groups(fuel: Nat, +id: U32, +f: Ex.Fs, +ds: +List, +g: U32, +acc: U.U64) -> IO(U.U64): match fuel: case 0n: IO.pure(U.U64, acc) case 1n+k: Bool.pick(IO(U.U64), U32.is_le(Ex.Fs.groups(f), g), IO.pure(U.U64, acc), IO.bind(U.U64, U.U64, Spr.group(Ex.Fs.bitmap(f, ds, g), id, f, g), n => Spr.groups(k, id, f, ds, (g + 1 : U32), U.U64.add(acc, n)))) # One file system at base: (found, bytes freed); "skipped" when it is ext4 # but not cleanly unmounted (or has errors, needs recovery, or META_BG). def Spr.fs.go(+id: U32, +f: Ex.Fs) -> IO(U.U64): do IO: +ds : +List <- H.Host.disk_read(id, Ex.Fs.descs_at(f), Ex.Fs.descs_len(f)) Spr.groups(1048576n, id, f, ds, 0, U.U64.zero()) def Spr.mib(+n: U.U64) -> String: U32.show(U.U64.lo(U.U64.shr(20n, n))) ++ " MiB" def Spr.at(+base: U.U64) -> String: Bool.pick(String, U.U64.is_zero(base), "the disk", "the partition at " ++ Spr.mib(base)) def Spr.fs(+id: U32, +base: U.U64) -> IO(String): do IO: +sb : +List <- H.Host.disk_read(id, U.U64.add_u32(base, 1024), 1024) +f : Ex.Fs = Ex.Ext.fs(base, sb) Bool.pick(IO(String), Bool.not(Ex.Ext.is_ext(sb)), IO.pure(String, ""), Bool.pick(IO(String), Bool.not(Ex.Fs.ok(f)), IO.pure(String, " ext4 on " ++ Spr.at(base) ++ ": skipped (not cleanly unmounted, errors, a journal to replay, META_BG, or past 2^32 blocks)\n"), IO.bind(U.U64, String, Spr.fs.go(id, f), n => IO.pure(String, " ext4 on " ++ Spr.at(base) ++ ": " ++ Spr.mib(n) ++ " free, given back\n")))) def Spr.bases(bs: +List, +id: U32) -> IO(String): match bs: case Nil{}: IO.pure(String, "") case Con{b, rest}: do IO: +a : String <- Spr.fs(id, b) +more : String <- Spr.bases(rest, id) return a ++ more def Spr.gpt.entries(+id: U32, t: Ex.GptTab) -> IO(+List): match t: case Ex.GptTab{+at, +n, +size}: do IO<+List>: +es : +List <- H.Host.disk_read(id, at, (n * size : U32)) return Ex.Part.gpt_entries(es, n, size) # The partitions: an MBR's, or a GPT's (its header at sector 1). def Spr.gpt(+id: U32, gpt: Bool) -> IO(+List): match gpt: case False{}: IO.pure(+List, []) case True{}: do IO<+List>: +hdr : +List <- H.Host.disk_read(id, U.U64.from_u32(512), 512) Spr.gpt.entries(id, Ex.Part.gpt_table(hdr)) def Spr.image.go(+id: U32, +path: String) -> IO(String): do IO: +before : U32 <- H.Hvf.size_kib(path) +s0 : +List <- H.Host.disk_read(id, U.U64.zero(), 512) +gp : +List <- Spr.gpt(id, Ex.Part.gpt(s0)) +found : String <- Spr.bases(Con{U.U64.zero(), List.append(&2, U.U64, Ex.Part.mbr(s0), gp)}, id) +after : U32 <- H.Hvf.size_kib(path) return path ++ ": " ++ Bool.pick(String, String.is_empty(found), "no ext4 file system found\n", U32.show((before / 1024 : U32)) ++ " -> " ++ U32.show((after / 1024 : U32)) ++ " MiB on the host's disk\n" ++ found) def Spr.image(+path: String) -> IO(String): do IO: +id : U32 <- H.Hvf.disk_open_with(path, Ac.Access.flags(Ac.Writer{})) Bool.pick(IO(String), U32.is_eq(id, 4294967295), IO.pure(String, path ++ ": cannot open it for writing (in use by a running VM?)\n"), Spr.image.go(id, path)) def Spr.images(ps: List<&2, String>) -> IO(String): match ps: case Nil{}: IO.pure(String, "") case Con{p, rest}: do IO: +a : String <- Spr.image(p) +more : String <- Spr.images(rest) return a ++ more # The images named, or the writable --disk images of the VM folder. def Spr.paths(ds: List) -> List<&2, String>: match ds: case Nil{}: [] case Con{d, rest}: match d: case Ds.DiskAt{i, p}: Con{p, Spr.paths(rest)} def Vmm.sparsify(+vm: String, +paths: List<&2, String>) -> IO(String): Bool.pick(IO(String), Bool.not(List.is_empty(&2, String, paths)), Spr.images(paths), IO.bind(String, String, Api.read(vm ++ "/vm.conf"), text => Spr.images(Spr.paths(Ds.Dsnap.writable(Cm.Sub.opt(Cm.Conf.flags(vm, text), "--disk", "")))))) def cli.sparsified.go(+t: String) -> IO(Unit): IO.print(String.trim(Bool.pick(String, String.is_empty(t), "nothing to sparsify (no images named, and no writable --disk in the VM folder)", t))) def cli.sparsified(t: String) -> IO(Unit): cli.sparsified.go(t) @unsafe def cli(run: List -> List -> IO(Unit), +prog: List, s: Cm.Sub) -> IO(Unit): match s: case Cm.SubNone{}: IO.pure(Unit, Unit{}) case Cm.SubBad{why}: IO.die(Unit, 2, "bend-vmm: " ++ why) case Cm.SubCreate{dir, flags}: IO.bind(Result<&1, &1, Err, String>, Unit, Vmm.create(dir, flags), cli.created) case Cm.SubRun{dir}: Vmm.run_dir(run, prog, dir) case Cm.SubSnap{vm, name, note}: IO.bind(Result<&1, &1, Err, Sd.Info>, Unit, Vmm.snapshot(vm, name, note), cli.snap) case Cm.SubSnaps{vm}: IO.bind(Result<&1, &1, Err, List<&2, Sd.Info>>, Unit, Vmm.snapshots(vm), cli.snaps) case Cm.SubRestore{vm, name}: Vmm.restore(run, prog, vm, name) case Cm.SubRm{vm, name}: IO.bind(Result<&1, &1, Err, String>, Unit, Vmm.snapshot_rm(vm, name), r => cli.text(r, "removed ")) case Cm.SubPrune{vm, keep}: IO.bind(Result<&1, &1, Err, List<&2, String>>, Unit, Vmm.prune(vm, keep), cli.names) case Cm.SubCtl{vm, line}: cli.ctl(vm, line) case Cm.SubSparsify{vm, paths}: IO.bind(String, Unit, Vmm.sparsify(vm, paths), cli.sparsified) case Cm.SubRaw{sock, line}: IO.bind(Result<&1, &1, Err, String>, Unit, Vmm.ctl(sock, line), r => cli.text(r, "")) # Files for a program that embeds the VMM (main.bend's Fs, asked by # bend-vk8s): the host's own calls, each answering 0 or an errno. # mkdir -p: each directory of path that is not there yet (17, EEXIST, is # no error); the first other errno stops it. def Fs.mkdir_p.ok(+rc: U32) -> Bool: U32.is_zero(rc) || U32.is_eq(rc, 17) def Fs.mkdir_p.go(dirs: List<&2, String>, +mode: U32, +prev: U32) -> IO(U32): match dirs: case Nil{}: IO.pure(U32, Bool.pick(U32, Fs.mkdir_p.ok(prev), 0, prev)) case Con{d, rest}: do IO: +rc : U32 <- Bool.pick(IO(U32), Fs.mkdir_p.ok(prev), H.Hvf.mkdir(d, mode), IO.pure(U32, prev)) Fs.mkdir_p.go(rest, mode, rc) def Fs.mkdir_p(+path: String, +mode: U32) -> IO(U32): Fs.mkdir_p.go(Hf.Files.prefixes(path), mode, 0) def Fs.facts(+path: String) -> IO(Hf.Facts): do IO: +ws : +List <- H.Hvf.path_facts(path) return Hf.Files.facts(ws) def Fs.set_mtime(+path: String, t: U.U64) -> IO(U32): match t: case U.U64{hi, lo}: H.Hvf.set_mtime(path, hi, lo)