2017-05-26 14:30:34 +00:00
|
|
|
(* (c) 2017 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
|
|
|
open Vmm_provision
|
|
|
|
|
|
|
|
open Rresult.R.Infix
|
|
|
|
|
|
|
|
open Vmm_asn
|
|
|
|
|
2018-03-18 18:07:14 +00:00
|
|
|
let vm_csr key name image cpu mem args block net force compression =
|
2017-05-26 14:30:34 +00:00
|
|
|
let block = match block with
|
|
|
|
| None -> []
|
|
|
|
| Some x -> [ (false, `Unsupported (Oid.block_device, string_to_cstruct x)) ]
|
|
|
|
and arg = match args with
|
|
|
|
| [] -> []
|
|
|
|
| xs -> [ (false, `Unsupported (Oid.argv, strings_to_cstruct xs)) ]
|
|
|
|
and net = match net with
|
|
|
|
| [] -> []
|
|
|
|
| xs -> [ (false, `Unsupported (Oid.network, strings_to_cstruct xs)) ]
|
2018-09-19 19:16:44 +00:00
|
|
|
and cmd = if force then `Force_create_vm else `Create_vm
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
2018-03-18 18:07:14 +00:00
|
|
|
let image = match compression with
|
2018-09-21 20:31:04 +00:00
|
|
|
| 0 -> image_to_cstruct (`Hvt_amd64, image)
|
2018-03-18 18:07:14 +00:00
|
|
|
| level ->
|
|
|
|
let img = Vmm_compress.compress ~level (Cstruct.to_string image) in
|
2018-09-21 20:31:04 +00:00
|
|
|
image_to_cstruct (`Hvt_amd64_compressed, Cstruct.of_string img)
|
2018-03-18 18:07:14 +00:00
|
|
|
in
|
2017-05-26 14:30:34 +00:00
|
|
|
let exts =
|
|
|
|
[ (false, `Unsupported (Oid.version, version_to_cstruct asn_version)) ;
|
|
|
|
(false, `Unsupported (Oid.cpuid, int_to_cstruct cpu)) ;
|
|
|
|
(false, `Unsupported (Oid.memory, int_to_cstruct mem)) ;
|
2018-03-18 18:07:14 +00:00
|
|
|
(false, `Unsupported (Oid.vmimage, image)) ;
|
2018-09-19 19:16:44 +00:00
|
|
|
(false, `Unsupported (Oid.command, command_to_cstruct cmd)) ;
|
2017-05-26 14:30:34 +00:00
|
|
|
] @ block @ arg @ net
|
|
|
|
and name = [ `CN name ]
|
|
|
|
in
|
|
|
|
X509.CA.request name ~extensions:[`Extensions exts] key
|
|
|
|
|
2018-03-18 18:07:14 +00:00
|
|
|
let jump _ name key image mem cpu args block net force compression =
|
2017-05-26 14:30:34 +00:00
|
|
|
Nocrypto_entropy_unix.initialize () ;
|
|
|
|
match
|
|
|
|
priv_key key name >>= fun key ->
|
|
|
|
(Bos.OS.File.read (Fpath.v image) >>= fun s ->
|
|
|
|
Ok (Cstruct.of_string s)) >>= fun image ->
|
2018-03-18 18:07:14 +00:00
|
|
|
let csr = vm_csr key name image cpu mem args block net force compression in
|
2017-05-26 14:30:34 +00:00
|
|
|
let enc = X509.Encoding.Pem.Certificate_signing_request.to_pem_cstruct1 csr in
|
|
|
|
Bos.OS.File.write Fpath.(v name + ".req") (Cstruct.to_string enc)
|
|
|
|
with
|
|
|
|
| Ok () -> `Ok ()
|
|
|
|
| Error (`Msg m) -> `Error (false, m)
|
|
|
|
|
|
|
|
open Cmdliner
|
|
|
|
|
|
|
|
let cpu =
|
|
|
|
let doc = "CPUid" in
|
|
|
|
Arg.(required & pos 3 (some int) None & info [] ~doc)
|
|
|
|
|
|
|
|
let image =
|
|
|
|
let doc = "Image file to provision" in
|
|
|
|
Arg.(required & pos 1 (some file) None & info [] ~doc)
|
|
|
|
|
|
|
|
let args =
|
|
|
|
let doc = "Boot arguments" in
|
|
|
|
Arg.(value & opt_all string [] & info [ "arg" ] ~doc)
|
|
|
|
|
|
|
|
let block =
|
|
|
|
let doc = "Block device name" in
|
|
|
|
Arg.(value & opt (some string) None & info [ "block" ] ~doc)
|
|
|
|
|
|
|
|
let net =
|
|
|
|
let doc = "Network device" in
|
|
|
|
Arg.(value & opt_all string [] & info [ "net" ] ~doc)
|
|
|
|
|
2018-01-16 00:10:22 +00:00
|
|
|
let force =
|
|
|
|
let doc = "Force creation (destroy VM with same name if it exists)" in
|
|
|
|
Arg.(value & flag & info [ "force" ] ~doc)
|
|
|
|
|
2018-03-18 18:07:14 +00:00
|
|
|
let compress_level =
|
|
|
|
let doc = "Compression level (0 for no compression)" in
|
|
|
|
Arg.(value & opt int 4 & info [ "compression-level" ] ~doc)
|
|
|
|
|
2017-05-26 14:30:34 +00:00
|
|
|
let cmd =
|
2018-03-18 18:07:14 +00:00
|
|
|
Term.(ret (const jump $ setup_log $ nam $ key $ image $ mem $ cpu $ args $ block $ net $ force $ compress_level)),
|
2017-05-26 14:30:34 +00:00
|
|
|
Term.info "vmm_req_vm" ~version:"%%VERSION_NUM%%"
|
|
|
|
|
|
|
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|