Add --cli switch that disables the GUI window. (#20)

This commit is contained in:
msm-code 2020-04-05 14:47:10 +00:00 committed by GitHub
parent a583335865
commit e588479701
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 22 deletions

View file

@ -173,14 +173,14 @@ func isRunning(l *libvirt.Libvirt, name string) bool {
func generateAppVM(l *libvirt.Libvirt,
nixName, vmName, appvmPath, sharedDir string,
verbose, online bool) (err error) {
verbose, online, gui bool) (err error) {
realpath, reginfo, qcow2, err := generateVM(appvmPath, nixName, verbose)
if err != nil {
return
}
xml := generateXML(vmName, online, realpath, reginfo, qcow2, sharedDir)
xml := generateXML(vmName, online, gui, realpath, reginfo, qcow2, sharedDir)
_, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate)
return
}
@ -209,7 +209,7 @@ func isAppvmConfigurationExists(appvmPath, name string) bool {
return fileExists(appvmPath + "/nix/" + name + ".nix")
}
func start(l *libvirt.Libvirt, name string, verbose, online, stateless bool,
func start(l *libvirt.Libvirt, name string, verbose, online, gui, stateless bool,
args, open string) {
appvmPath := configDir
@ -267,15 +267,17 @@ func start(l *libvirt.Libvirt, name string, verbose, online, stateless bool,
}
err := generateAppVM(l, name, vmName, appvmPath, sharedDir,
verbose, online)
verbose, online, gui)
if err != nil {
log.Fatal(err)
}
}
if gui {
cmd := exec.Command("virt-viewer", "-c", "qemu:///system", vmName)
cmd.Start()
}
}
func stop(l *libvirt.Libvirt, name string) {
dom, err := l.DomainLookupByName("appvm_" + name)
@ -450,6 +452,7 @@ func main() {
startArgs := startCommand.Flag("args", "Command line arguments").String()
startOpen := startCommand.Flag("open", "Pass file to application").String()
startOffline := startCommand.Flag("offline", "Disconnect").Bool()
startCli := startCommand.Flag("cli", "Disable graphics mode, enable serial").Bool()
startStateless := startCommand.Flag("stateless", "Do not use default state directory").Bool()
stopName := kingpin.Command("stop", "Stop application").Arg("name", "Application name").Required().String()
@ -496,7 +499,7 @@ func main() {
*generateBuildVM)
case "start":
start(l, *startName,
!*startQuiet, !*startOffline, *startStateless,
!*startQuiet, !*startOffline, !*startCli, *startStateless,
*startArgs, *startOpen)
case "stop":
stop(l, *stopName)

40
xml.go
View file

@ -5,9 +5,15 @@ import "fmt"
// You may think that you want to rewrite to proper golang structures.
// Believe me, you shouldn't.
func generateXML(vmName string, online bool,
func generateXML(vmName string, online, gui bool,
vmNixPath, reginfo, img, sharedDir string) string {
devices := ""
if gui {
devices = guiDevices
}
qemuParams := `
<qemu:commandline>
<qemu:arg value='-device'/>
@ -27,9 +33,25 @@ func generateXML(vmName string, online bool,
}
return fmt.Sprintf(xmlTmpl, vmName, vmNixPath, vmNixPath, vmNixPath,
reginfo, img, sharedDir, sharedDir, sharedDir, qemuParams)
reginfo, img, sharedDir, sharedDir, sharedDir, devices, qemuParams)
}
var guiDevices = `
<!-- Graphical console -->
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<!-- Guest additionals support -->
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
</channel>
<video>
<model type='qxl' ram='524288' vram='524288' vgamem='262144' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
`
var xmlTmpl = `
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
<name>%s</name>
@ -50,25 +72,12 @@ var xmlTmpl = `
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<!-- Graphical console -->
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<!-- Guest additionals support -->
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
</channel>
<!-- Fake (because -snapshot) writeback image -->
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback' error_policy='report'/>
<source file='%s'/>
<target dev='vda' bus='virtio'/>
</disk>
<video>
<model type='qxl' ram='524288' vram='524288' vgamem='262144' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<!-- filesystems -->
<filesystem type='mount' accessmode='passthrough'>
<source dir='/nix/store'/>
@ -87,6 +96,7 @@ var xmlTmpl = `
<source dir='%s'/>
<target dir='home'/>
</filesystem>
%s
</devices>
%s
</domain>