added hackerprefix argument, changed the commandline structure of vm to work better with sudo

This commit is contained in:
Dominique Roux 2020-01-24 17:12:50 +01:00
parent 8cc58726d0
commit 5d05e91335
2 changed files with 26 additions and 15 deletions

View File

@ -25,6 +25,7 @@ arg_parser.add_argument('--use-sudo', help="Use sudo for command requiring root!
arg_parser.add_argument('--memory', help="Size of memory (GB)", type=int)
arg_parser.add_argument('--cores', help="Amount of CPU cores", type=int)
arg_parser.add_argument('--no-db', help="Disable connection to etcd. For local testing only!", action='store_true')
arg_parser.add_argument('--hackprefix', help="hackprefix, if you need it you know it (it's where the iso is located and ifup/down.sh")
log = logging.getLogger(__name__)

View File

@ -23,10 +23,14 @@
import subprocess
import uuid
import os
import logging
from uncloud.hack.db import DB
from uncloud.hack.mac import MAC
log = logging.getLogger(__name__)
class VM(object):
def __init__(self, config):
self.config = config
@ -36,8 +40,9 @@ class VM(object):
self.db = DB(self.config, prefix="/vm")
#TODO: Select generic
self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud" #TODO: Should be removed midterm
#self.hackprefix="/home/nico/vcs/uncloud/uncloud/hack/hackcloud" #TODO: Should be removed midterm
#self.hackprefix="/home/rouxdo/Work/ungleich/uncloud/uncloud/hack/hackcloud" #TODO: Dominique testing
self.hackprefix=self.config.arguments['hackprefix']
self.qemu="/usr/bin/qemu-system-x86_64" #TODO: should be in config
self.accel="kvm" #TODO: should be config
@ -69,30 +74,36 @@ class VM(object):
#TODO: Enable sudo -- FIXME!
if self.config.arguments['use_sudo']:
self.sudo = "sudo"
self.sudo = "sudo "
else:
self.sudo = ""
self.mac=MAC(self.config)
self.mac.create()
self.vm['mac'] = self.mac
self.vm['ifname'] = "uc{}".format(self.mac.to_str_format())
# FIXME: TODO: turn this into a string and THEN
# .split() it later -- easier for using .format()
#self.vm['commandline'] = [ "{}".format(self.sudo),
self.vm['commandline'] = [ "{}".format(self.sudo),
"{}".format(self.qemu),
"-name", "uncloud-{}".format(self.vm['uuid']),
"-machine", "pc,accel={}".format(self.accel),
"-m", "{}".format(self.vm['memory']),
"-smp", "{}".format(self.vm['cores']),
"-uuid", "{}".format(self.vm['uuid']),
"-drive", "file={},media=cdrom".format(self.vm['os_image']),
"-netdev", "tap,id=netmain,script={},downscript={},ifname={}".format(self.ifup, self.ifdown, self.vm['ifname']),
"-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.mac)
]
self.vm['commandline'] = "{sudo}{qemu} -name uncloud-{uuid} -machine pc,accel={accel} -m {memory} -smp {cores} -uuid {uuid} -drive file={os_image},media=cdrom -netdev tap,id=netmain,script={ifup},downscript={ifdown},ifname={ifname} -device virtio-net-pci,netdev=netmain,id=net0,mac={mac}"
# self.vm['commandline'] = [ "{}".format(self.sudo),
# "{}".format(self.qemu),
# "-name", "uncloud-{}".format(self.vm['uuid']),
# "-machine", "pc,accel={}".format(self.accel),
# "-m", "{}".format(self.vm['memory']),
# "-smp", "{}".format(self.vm['cores']),
# "-uuid", "{}".format(self.vm['uuid']),
# "-drive", "file={},media=cdrom".format(self.vm['os_image']),
# "-netdev", "tap,id=netmain,script={},downscript={},ifname={}".format(self.ifup, self.ifdown, self.vm['ifname']),
# "-device", "virtio-net-pci,netdev=netmain,id=net0,mac={}".format(self.vm['mac'])
# ]
def _execute_cmd(self, cmd_string, **kwargs):
cmd = cmd_string.format(**self.vm, **kwargs)
log.info("Executing: {}".format(cmd))
subprocess.run(cmd.split())
def create(self):
if not self.no_db:
@ -100,6 +111,5 @@ class VM(object):
self.vm,
as_json=True)
print(" ".join(self.vm['commandline']))
subprocess.run(self.vm['commandline']) #TODO: run in background
self._execute_cmd(self.vm['commandline'], sudo=self.sudo, qemu=self.qemu, accel=self.accel, ifup=self.ifup, ifdown=self.ifdown)
#TODO: Add interface ifname to bridge brXX (via net.py: public function add iface to bridge)