From 6335756844b2ebf554ee4afdf30e34b42b713c95 Mon Sep 17 00:00:00 2001 From: Sven Dowideit Date: Thu, 18 May 2017 11:56:16 +1000 Subject: [PATCH] Add --qemu-userdata cloud-config Signed-off-by: Sven Dowideit --- qemu.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/qemu.go b/qemu.go index 5a80673..23d2cd0 100644 --- a/qemu.go +++ b/qemu.go @@ -47,7 +47,9 @@ type Driver struct { connectionString string // conn *libvirt.Connect // VM *libvirt.Domain - vmLoaded bool + vmLoaded bool + UserDataFile string + CloudConfigRoot string } func (d *Driver) GetCreateFlags() []mcnflag.Flag { @@ -95,6 +97,10 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag { Usage: "SSH username", Value: defaultSSHUser, }, + mcnflag.StringFlag{ + Name: "qemu-userdata", + Usage: "cloud-config userdata file", + }, /* Not yet implemented mcnflag.Flag{ Name: "qemu-no-share", @@ -152,6 +158,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error { d.SwarmHost = flags.String("swarm-host") d.SwarmDiscovery = flags.String("swarm-discovery") d.SSHUser = flags.String("qemu-ssh-user") + d.UserDataFile = flags.String("qemu-userdata") d.SSHPort = 22 d.DiskPath = d.ResolveStorePath(fmt.Sprintf("%s.img", d.MachineName)) return nil @@ -232,6 +239,13 @@ func (d *Driver) Create() error { return err } + if d.UserDataFile != "" { + log.Infof("Creating Userdata Disk...") + if d.CloudConfigRoot, err = d.generateUserdataDisk(d.UserDataFile); err != nil { + return err + } + } + log.Infof("Starting QEMU VM...") if err := d.Start(); err != nil { return err @@ -288,6 +302,13 @@ func (d *Driver) Start() error { startCmd = append(startCmd, "-enable-kvm") } + if d.CloudConfigRoot != "" { + startCmd = append(startCmd, + "-fsdev", + fmt.Sprintf("local,security_model=passthrough,readonly,id=fsdev0,path=%s", d.CloudConfigRoot)) + startCmd = append(startCmd, "-device", "virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2") + } + // last argument is always the name of the disk image startCmd = append(startCmd, d.diskPath()) @@ -496,6 +517,35 @@ func (d *Driver) generateDiskImage(size int) error { return nil } +func (d *Driver) generateUserdataDisk(userdataFile string) (string, error) { + // Start with virtio, add ISO & FAT format later + // Start with local file, add wget/fetct URL? (or if URL, use datasource..) + log.Infof("1") + userdata, err := ioutil.ReadFile(userdataFile) + if err != nil { + return "", err + } + + log.Infof("2") + machineDir := filepath.Join(d.StorePath, "machines", d.GetMachineName()) + ccRoot := filepath.Join(machineDir, "cloud-config") + os.MkdirAll(ccRoot, 0755) + + log.Infof("3") + userDataDir := filepath.Join(ccRoot, "openstack/latest") + os.MkdirAll(userDataDir, 0755) + + log.Infof("4") + writeFile := filepath.Join(userDataDir, "user_data") + if err := ioutil.WriteFile(writeFile, userdata, 0644); err != nil { + return "", err + } + log.Infof("5") + + return ccRoot, nil + +} + func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) { // connect to monitor