Implements command line flag to disable internet connection

This commit is contained in:
dump_stack() 2020-01-02 20:34:35 +00:00
parent 0c35a66606
commit 13226a6a79
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
2 changed files with 32 additions and 14 deletions

View File

@ -165,7 +165,9 @@ func isRunning(l *libvirt.Libvirt, name string) bool {
return err == nil return err == nil
} }
func generateAppVM(l *libvirt.Libvirt, appvmPath, name string, verbose bool) (err error) { func generateAppVM(l *libvirt.Libvirt, appvmPath, name string, verbose,
online bool) (err error) {
err = os.Chdir(appvmPath) err = os.Chdir(appvmPath)
if err != nil { if err != nil {
return return
@ -179,7 +181,7 @@ func generateAppVM(l *libvirt.Libvirt, appvmPath, name string, verbose bool) (er
sharedDir := fmt.Sprintf(os.Getenv("HOME") + "/appvm/" + name) sharedDir := fmt.Sprintf(os.Getenv("HOME") + "/appvm/" + name)
os.MkdirAll(sharedDir, 0700) os.MkdirAll(sharedDir, 0700)
xml := generateXML(name, realpath, reginfo, qcow2, sharedDir) xml := generateXML(name, online, realpath, reginfo, qcow2, sharedDir)
_, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate) _, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate)
return return
} }
@ -208,7 +210,9 @@ func isAppvmConfigurationExists(appvmPath, name string) bool {
return fileExists(appvmPath + "/nix/" + name + ".nix") return fileExists(appvmPath + "/nix/" + name + ".nix")
} }
func start(l *libvirt.Libvirt, name string, verbose bool, args, open string) { func start(l *libvirt.Libvirt, name string, verbose, online bool,
args, open string) {
appvmPath := configDir appvmPath := configDir
vmHomePath := os.Getenv("HOME") + "/appvm/" + name + "/" vmHomePath := os.Getenv("HOME") + "/appvm/" + name + "/"
@ -251,7 +255,7 @@ func start(l *libvirt.Libvirt, name string, verbose bool, args, open string) {
if !verbose { if !verbose {
go stupidProgressBar() go stupidProgressBar()
} }
err = generateAppVM(l, appvmPath, name, verbose) err = generateAppVM(l, appvmPath, name, verbose, online)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -399,6 +403,7 @@ func main() {
startQuiet := startCommand.Flag("quiet", "Less verbosity").Bool() startQuiet := startCommand.Flag("quiet", "Less verbosity").Bool()
startArgs := startCommand.Flag("args", "Command line arguments").String() startArgs := startCommand.Flag("args", "Command line arguments").String()
startOpen := startCommand.Flag("open", "Pass file to application").String() startOpen := startCommand.Flag("open", "Pass file to application").String()
startOffline := startCommand.Flag("offline", "Disconnect").Bool()
stopName := kingpin.Command("stop", "Stop application").Arg("name", "Application name").Required().String() stopName := kingpin.Command("stop", "Stop application").Arg("name", "Application name").Required().String()
dropName := kingpin.Command("drop", "Remove application data").Arg("name", "Application name").Required().String() dropName := kingpin.Command("drop", "Remove application data").Arg("name", "Application name").Required().String()
@ -421,7 +426,8 @@ func main() {
case "generate": case "generate":
generate(l, *generateName, *generateBin, *generateVMName) generate(l, *generateName, *generateBin, *generateVMName)
case "start": case "start":
start(l, *startName, !*startQuiet, *startArgs, *startOpen) start(l, *startName, !*startQuiet, !*startOffline,
*startArgs, *startOpen)
case "stop": case "stop":
stop(l, *stopName) stop(l, *stopName)
case "drop": case "drop":

30
xml.go
View File

@ -5,9 +5,27 @@ import "fmt"
// You may think that you want to rewrite to proper golang structures. // You may think that you want to rewrite to proper golang structures.
// Believe me, you shouldn't. // Believe me, you shouldn't.
func generateXML(name, vmNixPath, reginfo, img, sharedDir string) string { func generateXML(name string, online bool, vmNixPath, reginfo, img, sharedDir string) string {
qemuParams := `
<qemu:commandline>
<qemu:arg value='-device'/>
<qemu:arg value='e1000,netdev=net0'/>
<qemu:arg value='-netdev'/>
<qemu:arg value='user,id=net0'/>
<qemu:arg value='-snapshot'/>
</qemu:commandline>
`
if !online {
qemuParams = `
<qemu:commandline>
<qemu:arg value='-snapshot'/>
</qemu:commandline>
`
}
return fmt.Sprintf(xmlTmpl, "appvm_"+name, vmNixPath, vmNixPath, vmNixPath, return fmt.Sprintf(xmlTmpl, "appvm_"+name, vmNixPath, vmNixPath, vmNixPath,
reginfo, img, sharedDir, sharedDir, sharedDir) reginfo, img, sharedDir, sharedDir, sharedDir, qemuParams)
} }
var xmlTmpl = ` var xmlTmpl = `
@ -68,12 +86,6 @@ var xmlTmpl = `
<target dir='home'/> <target dir='home'/>
</filesystem> </filesystem>
</devices> </devices>
<qemu:commandline> %s
<qemu:arg value='-device'/>
<qemu:arg value='e1000,netdev=net0'/>
<qemu:arg value='-netdev'/>
<qemu:arg value='user,id=net0'/>
<qemu:arg value='-snapshot'/>
</qemu:commandline>
</domain> </domain>
` `