/usr/bin/mic-vm-launcher is in mic2 0.24.12-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #!/usr/bin/python
#
# mic-chroot : chroot into an image or file system in order to change it
#
# Copyright 2009, Intel Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
import sys
import optparse
import subprocess
import uuid
import re
import time
import mic.imgcreate as imgcreate
import mic.utils.misc as misc
import mic.utils.bootstrap as bootstrap
def error(msg):
print("Error: %s" % msg)
sys.exit(-1)
def launch_vm(imgfile):
format = imgcreate.get_image_type(imgfile)
if format in ("livecd", "liveusb", "raw"):
qemu = "/usr/bin/kvm"
if not os.path.exists(qemu):
qemu = "/usr/bin/qemu-kvm"
if not os.path.exists(qemu):
qemu = "/usr/bin/qemu"
if not os.path.exists(qemu):
error("Please install qemu or kvm beforehand.")
bootdisk = {"livecd":"-cdrom", "liveusb":"-hda", "raw":"-hda"}
bootsrc = {"livecd":"d", "liveusb":"c", "raw":"c"}
args = [ qemu, bootdisk[format], imgfile, "-boot", bootsrc[format], "-m", "%d" % 512, "-std-vga", "-k", "en-us" ]
elif format == "vmdk":
vmplayer = "/usr/bin/vmplayer"
if not os.path.exists(vmplayer):
error("Please install vmplayer beforehand.")
args = [ vmplayer, imgfile[0:-5] + ".vmx" ]
elif format == "vdi":
vboxmanage = "/usr/bin/VBoxManage"
if not os.path.exists(vboxmanage):
error("Please install VirtualBox beforehand.")
""" Add root to group vboxusers """
if bootstrap.get_distribution().strip() == "openSUSE":
args = [ "/usr/sbin/usermod", "-A", "vboxusers", "root" ]
else:
args = [ "/usr/sbin/usermod", "-G", "vboxusers", "-a", "root" ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
if p.returncode != 0:
error("Failed to add root to group vboxusers")
""" Create and register a VirtualBox VM """
myuuid = "%s" % uuid.uuid4()
args = [ vboxmanage, "createvm", "--register", "--name", myuuid, "--uuid", myuuid, "--ostype", "Linux" ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
if p.returncode != 0:
error("Failed to create a VirtualBox VM: %s\n%s" % (output, errorout))
args = [ vboxmanage, "storagectl", myuuid, "--name", "IDE Controller", "--add", "ide" ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
if p.returncode != 0:
error("Failed to add a stroage controller: %s\n%s" % (output, errorout))
""" Set some properties of this VM """
args = [ vboxmanage, "modifyvm", myuuid, "--memory", "512", "--pae", "on", "--hda", imgfile ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
if p.returncode != 0:
error("Failed to set properties of VirtualBox VM")
""" Start this VirtualBox VM """
args = [ vboxmanage, "startvm", myuuid ]
else:
error("Invalid image file.")
try:
if format in ("livecd", "liveusb", "raw"):
subprocess.call(["/sbin/modprobe", "kvm_intel"])
elif format in ("vmdk", "vdi"):
subprocess.call(["/sbin/modprobe", "-r", "kvm_intel"])
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
print output
print errorout
if p.returncode != 0:
if format in ("livecd", "liveusb", "raw"):
# New kvm changed option -std-vga to "-vga std", so try new one
args = [ qemu, bootdisk[format], imgfile, "-boot", bootsrc[format], "-m", "%d" % 512, "-vga", "std", "-k", "en-us" ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
if p.returncode != 0:
raise OSError(-p.returncode, errorout)
else:
raise OSError(-p.returncode, errorout)
if format == "vdi":
poweroff = -3
vmstate = "running"
while vmstate != "powered off":
args = [ vboxmanage, "showvminfo", myuuid ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
lines = output.split("\n")
for line in lines:
if re.match(r"^State:.*powered off|State:.*saved", line):
""" Maybe it isn't started, need to check it in a few seconds """
poweroff = poweroff + 1
if poweroff >= 0:
vmstate = "powered off"
break
time.sleep(2)
except OSError, (err, msg):
error("Failed to launch VM using %(imagefile)s: %(msg)s" % {"imagefile":imgfile, "msg":msg})
finally:
if format == "vdi":
""" Discard VM state """
args = [ vboxmanage, "discardstate", myuuid ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
""" Detach disk """
args = [ vboxmanage, "modifyvm", myuuid, "--hda", "none" ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
""" Remove VM """
args = [ vboxmanage, "unregistervm", myuuid, "--delete" ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
""" Close medium """
args = [ vboxmanage, "closemedium", "disk", imgfile ]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, errorout) = p.communicate()
def parse_options(args):
parser = optparse.OptionParser("Usage: %prog [options] [image file]")
(options, args) = parser.parse_args()
if len(args) != 1:
error("Error: too few arguments.")
if not os.path.isfile(args[0]):
error("Error: invalid image file.")
options.imgfile = os.path.abspath(os.path.expanduser(args[0]))
return options
if __name__ == "__main__":
misc.setlocale()
if os.geteuid () != 0:
error("You must run mic-vm-launcher as root")
if not os.environ.has_key("DISPLAY") or not os.environ["DISPLAY"]:
error("No env $DISPLAY (add 'Defaults env_keep += \"DISPLAY\"'to \"/etc/sudoers\")")
options = parse_options(sys.argv[1:])
if options.imgfile:
launch_vm(options.imgfile)
|