Skip to content
Snippets Groups Projects
Commit 32869e01 authored by Juan Antonio Osorio's avatar Juan Antonio Osorio Committed by Pekka Enberg
Browse files

run.py: Remove global variable usage


Previously, the variables image_file, cmd_args and opt_path were used
globally along the run.py script, I did not consider this convenient and
thus, opted to convert these global variables, into one variable that is
passed as a parameter.

This variables was renamed to options, with the mindset that, if
desired, this variable could come from a configuration file, thus making
the passing of command line arguments optional. So, if this
functionality for a configuration file... Or perhaps default values, be
added to the script, there would not be much need for refactoring (or
renaming) as there would with these global variables.

Reviewed-by: default avatarTomasz Grabiec <tgrabiec@gmail.com>
Signed-off-by: default avatarJuan Antonio Osorio Robles <jaosorior@gmail.com>
Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
parent 02abeaae
No related branches found
No related tags found
No related merge requests found
...@@ -24,11 +24,11 @@ def cleanups(): ...@@ -24,11 +24,11 @@ def cleanups():
"cleanups after execution" "cleanups after execution"
stty_restore() stty_restore()
def set_imgargs(): def set_imgargs(options):
if (not cmdargs.execute): if (not options.execute):
return return
args = ["setargs", image_file, cmdargs.execute] args = ["setargs", options.image_file, options.execute]
subprocess.call(["scripts/imgedit.py"] + args) subprocess.call(["scripts/imgedit.py"] + args)
def is_direct_io_supported(path): def is_direct_io_supported(path):
...@@ -44,51 +44,53 @@ def is_direct_io_supported(path): ...@@ -44,51 +44,53 @@ def is_direct_io_supported(path):
return False; return False;
raise raise
def start_osv_qemu(): def start_osv_qemu(options):
if (cmdargs.unsafe_cache):
if (options.unsafe_cache):
cache = 'unsafe' cache = 'unsafe'
else: else:
cache = 'none' if is_direct_io_supported(image_file) else 'unsafe' cache = 'none' if is_direct_io_supported(options.image_file) else 'unsafe'
args = [ args = [
"-vnc", ":1", "-vnc", ":1",
"-gdb", "tcp::1234,server,nowait", "-gdb", "tcp::1234,server,nowait",
"-m", cmdargs.memsize, "-m", options.memsize,
"-smp", cmdargs.vcpus, "-smp", options.vcpus,
"-drive", "file=%s,if=virtio,cache=%s" % (image_file, cache)] "-drive", "file=%s,if=virtio,cache=%s" % (options.image_file, cache)]
if (cmdargs.no_shutdown): if (options.no_shutdown):
args += ["-no-reboot", "-no-shutdown"] args += ["-no-reboot", "-no-shutdown"]
if (cmdargs.wait): if (options.wait):
args += ["-S"] args += ["-S"]
if (cmdargs.networking): if (options.networking):
if (cmdargs.vhost): if (options.vhost):
args += ["-netdev", "tap,id=hn0,script=scripts/qemu-ifup.sh,vhost=on"] args += ["-netdev", "tap,id=hn0,script=scripts/qemu-ifup.sh,vhost=on"]
args += ["-device", "virtio-net-pci,netdev=hn0,id=nic1"] args += ["-device", "virtio-net-pci,netdev=hn0,id=nic1"]
else: else:
args += ["-netdev", "bridge,id=hn0,br=%s,helper=/usr/libexec/qemu-bridge-helper" % (cmdargs.bridge)] args += ["-netdev", "bridge,id=hn0,br=%s,helper=/usr/libexec/qemu-bridge-helper" % (options.bridge)]
args += ["-device", "virtio-net-pci,netdev=hn0,id=nic1"] args += ["-device", "virtio-net-pci,netdev=hn0,id=nic1"]
else: else:
args += ["-netdev", "user,id=un0,net=192.168.122.0/24,host=192.168.122.1"] args += ["-netdev", "user,id=un0,net=192.168.122.0/24,host=192.168.122.1"]
args += ["-device", "virtio-net-pci,netdev=un0"] args += ["-device", "virtio-net-pci,netdev=un0"]
args += ["-redir", "tcp:8080::8080"] args += ["-redir", "tcp:8080::8080"]
args += ["-redir", "tcp:2222::22"] args += ["-redir", "tcp:2222::22"]
for rule in cmdargs.forward:
for rule in options.forward:
args += ['-redir', rule] args += ['-redir', rule]
args += ["-device", "virtio-rng-pci"] args += ["-device", "virtio-rng-pci"]
if cmdargs.hypervisor == "kvm": if options.hypervisor == "kvm":
args += ["-enable-kvm", "-cpu", "host,+x2apic"] args += ["-enable-kvm", "-cpu", "host,+x2apic"]
elif (cmdargs.hypervisor == "none") or (cmdargs.hypervisor == "qemu"): elif (options.hypervisor == "none") or (options.hypervisor == "qemu"):
pass pass
if (cmdargs.detach): if (options.detach):
args += ["-daemonize"] args += ["-daemonize"]
else: else:
signal_option = ('off', 'on')[cmdargs.with_signals] signal_option = ('off', 'on')[options.with_signals]
args += ["-chardev", "stdio,mux=on,id=stdio,signal=%s" % signal_option] args += ["-chardev", "stdio,mux=on,id=stdio,signal=%s" % signal_option]
args += ["-mon", "chardev=stdio,mode=readline,default"] args += ["-mon", "chardev=stdio,mode=readline,default"]
args += ["-device", "isa-serial,chardev=stdio"] args += ["-device", "isa-serial,chardev=stdio"]
...@@ -99,7 +101,8 @@ def start_osv_qemu(): ...@@ -99,7 +101,8 @@ def start_osv_qemu():
# Launch qemu # Launch qemu
qemu_env = os.environ.copy() qemu_env = os.environ.copy()
qemu_env['OSV_BRIDGE'] = cmdargs.bridge
qemu_env['OSV_BRIDGE'] = options.bridge
subprocess.call(["qemu-system-x86_64"] + args, env = qemu_env) subprocess.call(["qemu-system-x86_64"] + args, env = qemu_env)
except OSError, e: except OSError, e:
if e.errno == errno.ENOENT: if e.errno == errno.ENOENT:
...@@ -110,8 +113,8 @@ def start_osv_qemu(): ...@@ -110,8 +113,8 @@ def start_osv_qemu():
finally: finally:
cleanups() cleanups()
def start_osv_xen(): def start_osv_xen(options):
if cmdargs.hypervisor == "xen": if options.hypervisor == "xen":
args = [ args = [
"builder='hvm'", "builder='hvm'",
"xen_platform_pci=1", "xen_platform_pci=1",
...@@ -120,12 +123,13 @@ def start_osv_xen(): ...@@ -120,12 +123,13 @@ def start_osv_xen():
"boot='c'", "boot='c'",
] ]
else: else:
args = [ "kernel='%s/build/%s/loader.elf'" % (os.getcwd(), opt_path) ] args = [ "kernel='%s/build/%s/loader.elf'" % (os.getcwd(), options.opt_path) ]
try: try:
memory = int(cmdargs.memsize) memory = int(options.memsize)
except ValueError: except ValueError:
memory = cmdargs.memsize memory = options.memsize
if memory[-1:].upper() == "M": if memory[-1:].upper() == "M":
memory = int(memory[:-1]) memory = int(memory[:-1])
...@@ -140,19 +144,20 @@ def start_osv_xen(): ...@@ -140,19 +144,20 @@ def start_osv_xen():
print >> sys.stderr, "Unrecognized memory size" print >> sys.stderr, "Unrecognized memory size"
return; return;
args += [ args += [
"memory=%d" % (memory), "memory=%d" % (memory),
"vcpus=%s" % (cmdargs.vcpus), "vcpus=%s" % (options.vcpus),
"maxcpus=%s" % (cmdargs.vcpus), "maxcpus=%s" % (options.vcpus),
"name='osv-%d'" % (os.getpid()), "name='osv-%d'" % (os.getpid()),
"disk=['%s,qcow2,hda,rw']" % image_file, "disk=['%s,qcow2,hda,rw']" % options.image_file,
"serial='pty'", "serial='pty'",
"paused=0", "paused=0",
"on_crash='preserve'" "on_crash='preserve'"
] ]
if cmdargs.networking: if options.networking:
args += [ "vif=['bridge=%s']" % (cmdargs.bridge)] args += [ "vif=['bridge=%s']" % (options.bridge)]
# Using xm would allow us to get away with creating the file, but it comes # Using xm would allow us to get away with creating the file, but it comes
# with its set of problems as well. Stick to xl. # with its set of problems as well. Stick to xl.
...@@ -166,7 +171,7 @@ def start_osv_xen(): ...@@ -166,7 +171,7 @@ def start_osv_xen():
# Launch qemu # Launch qemu
cmdline = ["xl", "create" ] cmdline = ["xl", "create" ]
if not cmdargs.detach: if not options.detach:
cmdline += [ "-c" ] cmdline += [ "-c" ]
cmdline += [ xenfile.name ] cmdline += [ xenfile.name ]
subprocess.call(cmdline) subprocess.call(cmdline)
...@@ -176,7 +181,7 @@ def start_osv_xen(): ...@@ -176,7 +181,7 @@ def start_osv_xen():
xenfile.close() xenfile.close()
cleanups() cleanups()
def start_osv(): def start_osv(options):
launchers = { launchers = {
"xen" : start_osv_xen, "xen" : start_osv_xen,
"xenpv" : start_osv_xen, "xenpv" : start_osv_xen,
...@@ -185,7 +190,7 @@ def start_osv(): ...@@ -185,7 +190,7 @@ def start_osv():
"kvm" : start_osv_qemu, "kvm" : start_osv_qemu,
} }
try: try:
launchers[cmdargs.hypervisor]() launchers[options.hypervisor](options)
except KeyError: except KeyError:
print >> sys.stderr, "Unrecognized hypervisor selected" print >> sys.stderr, "Unrecognized hypervisor selected"
return; return;
...@@ -199,9 +204,9 @@ def choose_hypervisor(external_networking): ...@@ -199,9 +204,9 @@ def choose_hypervisor(external_networking):
return 'xen' return 'xen'
return 'qemu' return 'qemu'
def main(): def main(options):
set_imgargs() set_imgargs(options)
start_osv() start_osv(options)
if (__name__ == "__main__"): if (__name__ == "__main__"):
# Parse arguments # Parse arguments
...@@ -238,11 +243,11 @@ if (__name__ == "__main__"): ...@@ -238,11 +243,11 @@ if (__name__ == "__main__"):
parser.add_argument("--forward", metavar = "RULE", action = "append", default = [], parser.add_argument("--forward", metavar = "RULE", action = "append", default = [],
help = "add network forwarding RULE (QEMU syntax)") help = "add network forwarding RULE (QEMU syntax)")
cmdargs = parser.parse_args() cmdargs = parser.parse_args()
opt_path = "debug" if cmdargs.debug else "release" cmdargs.opt_path = "debug" if cmdargs.debug else "release"
image_file = os.path.abspath(cmdargs.image or "build/%s/usr.img" % opt_path) cmdargs.image_file = os.path.abspath(cmdargs.image or "build/%s/usr.img" % cmdargs.opt_path)
if(cmdargs.hypervisor == "auto"): if(cmdargs.hypervisor == "auto"):
cmdargs.hypervisor = choose_hypervisor(cmdargs.networking); cmdargs.hypervisor = choose_hypervisor(cmdargs.networking);
# Call main # Call main
main() main(cmdargs)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment