qvm-prefs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. from qubes.qubes import QubesVmLabels
  24. from qubes.qubes import QubesHost
  25. from qubes.qubes import system_path
  26. from optparse import OptionParser
  27. import subprocess
  28. import os
  29. import sys
  30. import re
  31. def do_list(vm):
  32. label_width = 18
  33. fmt="{{0:<{0}}}: {{1}}".format(label_width)
  34. print fmt.format ("name", vm.name)
  35. print fmt.format ("label", vm.label.name)
  36. print fmt.format ("type", vm.type)
  37. if vm.template is not None:
  38. print fmt.format ("template", vm.template.name)
  39. if vm.netvm is not None:
  40. print fmt.format ("netvm", vm.netvm.name)
  41. print fmt.format ("updateable?", vm.updateable)
  42. print fmt.format ("installed by RPM?", vm.installed_by_rpm)
  43. print fmt.format ("include in backups", vm.include_in_backups)
  44. print fmt.format ("dir", vm.dir_path)
  45. print fmt.format ("config", vm.conf_file)
  46. print fmt.format ("pcidevs", vm.pcidevs)
  47. if vm.template is None:
  48. print fmt.format ("root img", vm.root_img)
  49. if vm.is_template():
  50. print fmt.format ("root COW img", vm.rootcow_img)
  51. if vm.template is not None:
  52. print fmt.format ("root img", vm.template.root_img)
  53. if hasattr(vm, 'volatile_img') and vm.volatile_img is not None:
  54. print fmt.format ("root volatile img", vm.volatile_img)
  55. if hasattr(vm, 'private_img') and vm.private_img is not None:
  56. print fmt.format ("private img", vm.private_img)
  57. print fmt.format ("vcpus", str(vm.vcpus))
  58. print fmt.format ("memory", vm.memory)
  59. if hasattr(vm, 'maxmem'):
  60. print fmt.format ("maxmem", vm.maxmem)
  61. print fmt.format ("MAC", "%s%s" % (vm.mac, " (auto)" if vm._mac is None else ""))
  62. if hasattr(vm, 'kernel'):
  63. if vm.uses_default_kernel:
  64. print fmt.format ("kernel", "%s (default)" % vm.kernel)
  65. else:
  66. print fmt.format ("kernel", vm.kernel)
  67. if hasattr(vm, 'kernelopts'):
  68. if vm.uses_default_kernelopts:
  69. print fmt.format ("kernelopts", "%s (default)" % vm.kernelopts)
  70. else:
  71. print fmt.format ("kernelopts", vm.kernelopts)
  72. if hasattr(vm, 'debug'):
  73. print fmt.format("debug", "on" if vm.debug else "off")
  74. if hasattr(vm, 'default_user'):
  75. print fmt.format("default user", str(vm.default_user))
  76. if hasattr(vm, 'qrexec_installed'):
  77. print fmt.format("qrexec_installed", str(vm.qrexec_installed))
  78. if hasattr(vm, 'guiagent_installed'):
  79. print fmt.format("guiagent_installed", str(vm.guiagent_installed))
  80. if hasattr(vm, 'qrexec_timeout'):
  81. print fmt.format("qrexec timeout", str(vm.qrexec_timeout))
  82. if hasattr(vm, 'drive'):
  83. print fmt.format("drive", str(vm.drive))
  84. if hasattr(vm, 'timezone'):
  85. print fmt.format("timezone", str(vm.timezone))
  86. print fmt.format ("internal", vm.internal)
  87. def set_label(vms, vm, args):
  88. if len (args) != 1:
  89. print >> sys.stderr, "Missing label name argument!"
  90. exit (1)
  91. label = args[0]
  92. if label not in QubesVmLabels:
  93. print >> sys.stderr, "Wrong label name, supported values are the following:"
  94. for l in QubesVmLabels.values():
  95. print >> sys.stderr, "* {0}".format(l.name)
  96. exit (1)
  97. vm.label = QubesVmLabels[label]
  98. def set_memory(vms, vm, args):
  99. if len (args) != 1:
  100. print >> sys.stderr, "Missing memory argument!"
  101. exit (1)
  102. new_memory = int(args[0])
  103. if new_memory <= 0:
  104. print >>sys.stderr, "Memory size must be positive"
  105. return False
  106. qubes_host = QubesHost()
  107. if new_memory > qubes_host.memory_total/1024:
  108. print >> sys.stderr, "This host has only {0} MB of RAM".format(qubes_host.memory_total/1024)
  109. return False
  110. vm.memory = new_memory
  111. def set_maxmem(vms, vm, args):
  112. if len (args) != 1:
  113. print >> sys.stderr, "Missing maxmem argument!"
  114. exit (1)
  115. new_maxmem = int(args[0])
  116. if new_maxmem <= 0:
  117. print >>sys.stderr, "Memory size must be positive"
  118. return False
  119. qubes_host = QubesHost()
  120. if new_maxmem > qubes_host.memory_total/1024:
  121. print >> sys.stderr, "This host has only {0} MB of RAM".format(qubes_host.memory_total/1024)
  122. return False
  123. if new_maxmem < vm.memory:
  124. print >> sys.stderr, "WARNING: new maxmem smaller than memory property - VM will be able to use only 'maxmem' memory amount"
  125. vm.maxmem = new_maxmem
  126. def set_mac(vms, vm, args):
  127. if len (args) != 1:
  128. print >> sys.stderr, "Missing MAC argument!"
  129. exit (1)
  130. if not re.match("[0-9a-fA-F:]{17}|auto", args[0]):
  131. print >> sys.stderr, "Invalid MAC argument!"
  132. print >> sys.stderr, "Possible values:"
  133. print >> sys.stderr, "1) auto"
  134. print >> sys.stderr, "2) MAC in format: XX:XX:XX:XX:XX:XX"
  135. exit (1)
  136. mac = args[0]
  137. if mac == "auto":
  138. mac = None
  139. vm.mac = mac
  140. def set_pcidevs(vms, vm, args):
  141. if len (args) != 1:
  142. print >> sys.stderr, "Missing pcidevs argument!"
  143. exit (1)
  144. if vm.is_running():
  145. print >>sys.stderr, "Cannot modify PCI devices of running VM, " \
  146. "use qvm-pci instead"
  147. vm.pcidevs = list(eval(args[0]))
  148. def set_netvm(vms, vm, args):
  149. if len (args) != 1:
  150. print >> sys.stderr, "Missing netvm name argument!"
  151. print >> sys.stderr, "Possible values:"
  152. print >> sys.stderr, "1) default"
  153. print >> sys.stderr, "2) none"
  154. print >> sys.stderr, "3) <vmaname>"
  155. return
  156. netvm = args[0]
  157. if netvm == "none":
  158. netvm = None
  159. vm.uses_default_netvm = False
  160. elif netvm == "default":
  161. netvm = vms.get_default_netvm()
  162. vm.uses_default_netvm = True
  163. else:
  164. netvm = vms.get_vm_by_name (netvm)
  165. if netvm is None:
  166. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(netvm)
  167. exit(1)
  168. if not netvm.is_netvm():
  169. print >> sys.stderr, "VM '{0}' is not a NetVM".format(netvm)
  170. exit (1)
  171. vm.uses_default_netvm = False
  172. vm.netvm = netvm
  173. def set_kernel(vms, vm, args):
  174. if len (args) != 1:
  175. print >> sys.stderr, "Missing kernel version argument!"
  176. print >> sys.stderr, "Possible values:"
  177. print >> sys.stderr, "1) default"
  178. print >> sys.stderr, "2) none (kernels subdir in VM)"
  179. print >> sys.stderr, "3) <kernel version>, one of:"
  180. for k in os.listdir(system_path["qubes_kernels_base_dir"]):
  181. print >> sys.stderr, " -", k
  182. return
  183. kernel = args[0]
  184. if kernel == "default":
  185. kernel = vms.get_default_kernel()
  186. vm.uses_default_kernel = True
  187. elif kernel == "none":
  188. kernel = None
  189. vm.uses_default_kernel = False
  190. else:
  191. if not os.path.exists(os.path.join(system_path["qubes_kernels_base_dir"], kernel)):
  192. print >> sys.stderr, "Kernel version {0} not installed.".format(kernel)
  193. exit(1)
  194. vm.uses_default_kernel = False
  195. vm.kernel = kernel
  196. def set_template(vms, vm, args):
  197. if len (args) != 1:
  198. print >> sys.stderr, "Missing template name argument!"
  199. return False
  200. template_name = args[0];
  201. template = vms.get_vm_by_name(template_name)
  202. if template is None or template.qid not in vms:
  203. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(template_name)
  204. return False
  205. if not template.is_template():
  206. print >> sys.stderr, "VM '{0}' is not a TemplateVM".format(template_name)
  207. return False
  208. print >> sys.stderr, "Setting template for VM '{0}' to '{1}'...".format (vm.name, template_name)
  209. vm.template = template
  210. return True
  211. def set_vcpus(vms, vm, args):
  212. if len (args) != 1:
  213. print >> sys.stderr, "Missing vcpus count argument!"
  214. return False
  215. vcpus = int(args[0])
  216. if vcpus <= 0:
  217. print >> sys.stderr, "A vcpus count must be positive."
  218. return False
  219. qubes_host = QubesHost()
  220. if vcpus > qubes_host.no_cpus:
  221. print >> sys.stderr, "This host has only {0} cpus".format(ubes_host.no_cpus)
  222. return False
  223. print >> sys.stderr, "Setting vcpus count for VM '{0}' to '{1}'...".format (vm.name, vcpus)
  224. vm.vcpus = vcpus
  225. return True
  226. def set_kernelopts(vms, vm, args):
  227. if len (args) != 1:
  228. print >> sys.stderr, "Missing kernel opts argument!"
  229. print >> sys.stderr, "Possible values:"
  230. print >> sys.stderr, "1) default"
  231. print >> sys.stderr, "2) <opts>"
  232. return False
  233. if args[0] == 'default':
  234. vm.uses_default_kernelopts = True
  235. else:
  236. vm.uses_default_kernelopts = False
  237. vm.kernelopts = args[0]
  238. return True
  239. def set_name(vms, vm, args):
  240. if len (args) != 1:
  241. print >> sys.stderr, "Missing new name!"
  242. return False
  243. if args[0] == vm.name:
  244. return False
  245. vm.set_name(args[0])
  246. return True
  247. def set_drive(vms, vm, args):
  248. if len (args) != 1:
  249. print >> sys.stderr, "Missing new drive content (file/device)!"
  250. return False
  251. if args[0] == '' or args[0].lower() == 'none':
  252. vm.drive = None
  253. else:
  254. vm.drive = args[0]
  255. return True
  256. def set_debug(vms, vm, args):
  257. if len (args) != 1:
  258. print >> sys.stderr, "Missing value (True/False or on/off)!"
  259. return False
  260. if args[0].lower() == "on":
  261. vm.debug = True
  262. elif args[0].lower() == "off":
  263. vm.debug = False
  264. else:
  265. vm.debug = bool(eval(args[0].capitalize()))
  266. return True
  267. def set_default_user(vms, vm, args):
  268. if len (args) != 1:
  269. print >> sys.stderr, "Missing user name!"
  270. return False
  271. vm.default_user = args[0]
  272. return True
  273. def set_include_in_backups(vms, vm, args):
  274. if len (args) != 1:
  275. print >> sys.stderr, "Missing value (True/False)!"
  276. return False
  277. vm.include_in_backups = bool(eval(args[0].capitalize()))
  278. return True
  279. def set_qrexec_installed(vms, vm, args):
  280. if len (args) != 1:
  281. print >> sys.stderr, "Missing value (True/False)!"
  282. return False
  283. vm.qrexec_installed = bool(eval(args[0].capitalize()))
  284. return True
  285. def set_internal(vms, vm, args):
  286. if len (args) != 1:
  287. print >> sys.stderr, "Missing value (True/False)!"
  288. return False
  289. vm.internal = bool(eval(args[0].capitalize()))
  290. return True
  291. def set_guiagent_installed(vms, vm, args):
  292. if len (args) != 1:
  293. print >> sys.stderr, "Missing value (True/False)!"
  294. return False
  295. vm.guiagent_installed = bool(eval(args[0].capitalize()))
  296. return True
  297. def set_qrexec_timeout(vms, vm, args):
  298. if len (args) != 1:
  299. print >> sys.stderr, "Missing timeout value (seconds)!"
  300. return False
  301. vm.qrexec_timeout = int(args[0])
  302. return True
  303. def set_timezone(vms, vm, args):
  304. if len (args) != 1:
  305. print >> sys.stderr, "Missing value ('localtime' or timeoffset in seconds)!"
  306. return False
  307. if not args[0].isdigit() and args[0].lower() == 'localtime':
  308. print >> sys.stderr, "Invalid timezone value!"
  309. return False
  310. vm.timezone = args[0]
  311. return True
  312. properties = {
  313. "include_in_backups": set_include_in_backups,
  314. "pcidevs": set_pcidevs,
  315. "label" : set_label,
  316. "netvm" : set_netvm,
  317. "maxmem" : set_maxmem,
  318. "memory" : set_memory,
  319. "kernel" : set_kernel,
  320. "template" : set_template,
  321. "vcpus" : set_vcpus,
  322. "kernelopts": set_kernelopts,
  323. "name": set_name,
  324. "drive": set_drive,
  325. "mac": set_mac,
  326. "debug": set_debug,
  327. "default_user": set_default_user,
  328. "qrexec_installed": set_qrexec_installed,
  329. "guiagent_installed": set_guiagent_installed,
  330. "qrexec_timeout": set_qrexec_timeout,
  331. "timezone": set_timezone,
  332. "internal": set_internal,
  333. }
  334. def do_set(vms, vm, property, args):
  335. if property not in properties.keys():
  336. print >> sys.stderr, "ERROR: Wrong property name: '{0}'".format(property)
  337. return False
  338. if not hasattr(vm, property):
  339. print >> sys.stderr, "ERROR: Property '{0}' not available for this VM".format(property)
  340. return False
  341. try:
  342. return properties[property](vms, vm, args)
  343. except Exception as err:
  344. print >> sys.stderr, "ERROR: %s" % str(err)
  345. return False
  346. def main():
  347. usage = "usage: %prog -l [options] <vm-name>\n"\
  348. "usage: %prog -s [options] <vm-name> <property> [...]\n"\
  349. "List/set various per-VM properties."
  350. parser = OptionParser (usage)
  351. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  352. parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False)
  353. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  354. help="Force to run, even with root privileges")
  355. (options, args) = parser.parse_args ()
  356. if (len (args) < 1):
  357. parser.error ("You must provide at least the vmname!")
  358. vmname = args[0]
  359. if os.geteuid() == 0:
  360. if not options.force_root:
  361. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  362. print >> sys.stderr, "Retry as unprivileged user."
  363. print >> sys.stderr, "... or use --force-root to continue anyway."
  364. exit(1)
  365. if options.do_list and options.do_set:
  366. print >> sys.stderr, "You cannot provide -l and -s at the same time!"
  367. exit (1)
  368. if options.do_set:
  369. qvm_collection = QubesVmCollection()
  370. qvm_collection.lock_db_for_writing()
  371. qvm_collection.load()
  372. else:
  373. qvm_collection = QubesVmCollection()
  374. qvm_collection.lock_db_for_reading()
  375. qvm_collection.load()
  376. qvm_collection.unlock_db()
  377. vm = qvm_collection.get_vm_by_name(vmname)
  378. if vm is None or vm.qid not in qvm_collection:
  379. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  380. exit(1)
  381. if options.do_set:
  382. if len (args) < 2:
  383. print >> sys.stderr, "You must specify the property you wish to set..."
  384. print >> sys.stderr, "Available properties:"
  385. for p in properties.keys():
  386. if hasattr(vm, p):
  387. print >> sys.stderr, "--> '{0}'".format(p)
  388. exit (1)
  389. property = args[1]
  390. do_set(qvm_collection, vm, property, args[2:])
  391. qvm_collection.save()
  392. qvm_collection.unlock_db()
  393. else:
  394. # do_list
  395. do_list(vm)
  396. main()