From 5f81e7ac3d8d4dcf9401f4ce8d81767bd4b7d462 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sun, 15 Jul 2018 19:08:28 +0000 Subject: [PATCH] Move out VM generation --- appvm.go | 66 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/appvm.go b/appvm.go index 9941148..37769d9 100644 --- a/appvm.go +++ b/appvm.go @@ -166,6 +166,39 @@ func prepareTemplates(appvmPath string) (err error) { return } +func generateVM(name string) (realpath, reginfo, qcow2 string, err error) { + stdout, stderr, ret, err := system.System("nix-build", "", "-A", "config.system.build.vm", + "-I", "nixos-config=nix/"+name+".nix", "-I", ".") + if err != nil { + log.Println(err, stdout, stderr, ret) + return + } + + realpath, err = filepath.EvalSymlinks("result/system") + if err != nil { + return + } + + // TODO: Use go regex + reginfo, _, _, err = system.System("sh", "-c", "cat result/bin/run-nixos-vm | grep -o 'regInfo=.*/registration'") + if err != nil { + return + } + + syscall.Unlink("result") + + qcow2 = "/tmp/.appvm.fake.qcow2" + if _, err = os.Stat(qcow2); os.IsNotExist(err) { + system.System("qemu-img", "create", "-f", "qcow2", qcow2, "512M") + err = os.Chmod(qcow2, 0400) // qemu run with -snapshot, we only need it for create /dev/vda + if err != nil { + return + } + } + + return +} + func start(l *libvirt.Libvirt, name string) { // Currently binary-only installation is not supported, because we need *.nix configurations gopath := os.Getenv("GOPATH") @@ -176,48 +209,19 @@ func start(l *libvirt.Libvirt, name string) { } // Copy templates - err := prepareTemplates(name) + err = prepareTemplates(name) if err != nil { log.Fatal(err) } - stdout, stderr, ret, err := system.System("nix-build", "", "-A", "config.system.build.vm", - "-I", "nixos-config=nix/"+name+".nix", "-I", ".") - if err != nil { - log.Fatalln(err, stdout, stderr, ret) - } - - realpath, err := filepath.EvalSymlinks("result/system") + realpath, reginfo, qcow2, err := generateVM(name) if err != nil { log.Fatal(err) } - // TODO: Use go regex - reginfo, _, _, err := system.System("sh", "-c", "cat result/bin/run-nixos-vm | grep -o 'regInfo=.*/registration'") - if err != nil { - log.Fatal(err) - } - - syscall.Unlink("result") - - qcow2 := "/tmp/.appvm.fake.qcow2" - if _, err := os.Stat(qcow2); os.IsNotExist(err) { - system.System("qemu-img", "create", "-f", "qcow2", qcow2, "512M") - err := os.Chmod(qcow2, 0400) // qemu run with -snapshot, we only need it for create /dev/vda - if err != nil { - log.Fatal(err) - } - } - sharedDir := fmt.Sprintf(os.Getenv("HOME") + "/appvm/" + name) os.MkdirAll(sharedDir, 0700) - // TODO: Search go libraries for manipulate ACL - _, _, _, err = system.System("setfacl", "-R", "-m", "u:qemu:rwx", os.Getenv("HOME")+"/appvm/") - if err != nil { - log.Fatal(err) - } - xml := generateXML(name, realpath, reginfo, qcow2, sharedDir) _, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate) if err != nil {