qvm-prefs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 = 19
  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 ("autostart", vm.autostart)
  43. print fmt.format ("installed_by_rpm", vm.installed_by_rpm)
  44. print fmt.format ("include_in_backups", vm.include_in_backups)
  45. print fmt.format ("last_backup", vm.backup_timestamp)
  46. print fmt.format ("dir", vm.dir_path)
  47. print fmt.format ("config", vm.conf_file)
  48. print fmt.format ("pcidevs", vm.pcidevs)
  49. if vm.template is None:
  50. print fmt.format ("root_img", vm.root_img)
  51. if hasattr(vm, "rootcow_img") and vm.rootcow_img is not None:
  52. print fmt.format ("root_cow_img", vm.rootcow_img)
  53. if vm.template is not None:
  54. print fmt.format ("root_img", vm.template.root_img)
  55. if hasattr(vm, 'volatile_img') and vm.volatile_img is not None:
  56. print fmt.format ("root_volatile_img", vm.volatile_img)
  57. if hasattr(vm, 'private_img') and vm.private_img is not None:
  58. print fmt.format ("private_img", vm.private_img)
  59. print fmt.format ("vcpus", str(vm.vcpus))
  60. print fmt.format ("memory", vm.memory)
  61. if hasattr(vm, 'maxmem'):
  62. print fmt.format ("maxmem", vm.maxmem)
  63. print fmt.format ("MAC", "%s%s" % (vm.mac, " (auto)" if vm._mac is None else ""))
  64. if hasattr(vm, 'kernel'):
  65. if vm.uses_default_kernel:
  66. print fmt.format ("kernel", "%s (default)" % vm.kernel)
  67. else:
  68. print fmt.format ("kernel", vm.kernel)
  69. if hasattr(vm, 'kernelopts'):
  70. if vm.uses_default_kernelopts:
  71. print fmt.format ("kernelopts", "%s (default)" % vm.kernelopts)
  72. else:
  73. print fmt.format ("kernelopts", vm.kernelopts)
  74. if hasattr(vm, 'debug'):
  75. print fmt.format("debug", "on" if vm.debug else "off")
  76. if hasattr(vm, 'default_user'):
  77. print fmt.format("default_user", str(vm.default_user))
  78. if hasattr(vm, 'qrexec_installed'):
  79. print fmt.format("qrexec_installed", str(vm.qrexec_installed))
  80. if hasattr(vm, 'qrexec_timeout'):
  81. print fmt.format("qrexec_timeout", str(vm.qrexec_timeout))
  82. if hasattr(vm, 'guiagent_installed'):
  83. print fmt.format("guiagent_installed", str(vm.guiagent_installed))
  84. if hasattr(vm, 'seamless_gui_mode'):
  85. print fmt.format("seamless_gui_mode", str(vm.seamless_gui_mode))
  86. if hasattr(vm, 'drive'):
  87. print fmt.format("drive", str(vm.drive))
  88. if hasattr(vm, 'timezone'):
  89. print fmt.format("timezone", str(vm.timezone))
  90. print fmt.format ("internal", vm.internal)
  91. def set_label(vms, vm, args):
  92. if len (args) != 1:
  93. print >> sys.stderr, "Missing label name argument!"
  94. return False
  95. label = args[0]
  96. if label not in QubesVmLabels:
  97. print >> sys.stderr, "Wrong label name, supported values are the following:"
  98. for l in QubesVmLabels.values():
  99. print >> sys.stderr, "* {0}".format(l.name)
  100. return False
  101. vm.label = QubesVmLabels[label]
  102. return True
  103. def set_memory(vms, vm, args):
  104. if len (args) != 1:
  105. print >> sys.stderr, "Missing memory argument!"
  106. return False
  107. new_memory = int(args[0])
  108. if new_memory <= 0:
  109. print >>sys.stderr, "Memory size must be positive"
  110. return False
  111. qubes_host = QubesHost()
  112. if new_memory > qubes_host.memory_total/1024:
  113. print >> sys.stderr, "This host has only {0} MB of RAM".format(qubes_host.memory_total/1024)
  114. return False
  115. vm.memory = new_memory
  116. return True
  117. def set_maxmem(vms, vm, args):
  118. if len (args) != 1:
  119. print >> sys.stderr, "Missing maxmem argument!"
  120. exit (1)
  121. new_maxmem = int(args[0])
  122. if new_maxmem <= 0:
  123. print >>sys.stderr, "Memory size must be positive"
  124. return False
  125. qubes_host = QubesHost()
  126. if new_maxmem > qubes_host.memory_total/1024:
  127. print >> sys.stderr, "This host has only {0} MB of RAM".format(qubes_host.memory_total/1024)
  128. return False
  129. if new_maxmem < vm.memory:
  130. print >> sys.stderr, "WARNING: new maxmem smaller than memory property - VM will be able to use only 'maxmem' memory amount"
  131. vm.maxmem = new_maxmem
  132. return True
  133. def set_mac(vms, vm, args):
  134. if len (args) != 1:
  135. print >> sys.stderr, "Missing MAC argument!"
  136. return False
  137. if not re.match("[0-9a-fA-F:]{17}|auto", args[0]):
  138. print >> sys.stderr, "Invalid MAC argument!"
  139. print >> sys.stderr, "Possible values:"
  140. print >> sys.stderr, "1) auto"
  141. print >> sys.stderr, "2) MAC in format: XX:XX:XX:XX:XX:XX"
  142. return False
  143. mac = args[0]
  144. if mac == "auto":
  145. mac = None
  146. vm.mac = mac
  147. return True
  148. def set_pcidevs(vms, vm, args):
  149. if len (args) != 1:
  150. print >> sys.stderr, "Missing pcidevs argument!"
  151. return False
  152. if vm.is_running():
  153. print >>sys.stderr, "Cannot modify PCI devices of running VM, " \
  154. "use qvm-pci instead"
  155. return False
  156. vm.pcidevs = list(eval(args[0]))
  157. return True
  158. def set_netvm(vms, vm, args):
  159. if len (args) != 1:
  160. print >> sys.stderr, "Missing netvm name argument!"
  161. print >> sys.stderr, "Possible values:"
  162. print >> sys.stderr, "1) default"
  163. print >> sys.stderr, "2) none"
  164. print >> sys.stderr, "3) <vmaname>"
  165. return
  166. netvm = args[0]
  167. if netvm == "none":
  168. netvm = None
  169. vm.uses_default_netvm = False
  170. elif netvm == "default":
  171. netvm = vms.get_default_netvm()
  172. vm.uses_default_netvm = True
  173. else:
  174. netvm = vms.get_vm_by_name (netvm)
  175. if netvm is None:
  176. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(netvm)
  177. return False
  178. if not netvm.is_netvm():
  179. print >> sys.stderr, "VM '{0}' is not a NetVM".format(netvm)
  180. return False
  181. vm.uses_default_netvm = False
  182. vm.netvm = netvm
  183. return True
  184. def set_kernel(vms, vm, args):
  185. if len (args) != 1:
  186. print >> sys.stderr, "Missing kernel version argument!"
  187. print >> sys.stderr, "Possible values:"
  188. print >> sys.stderr, "1) default"
  189. print >> sys.stderr, "2) none (kernels subdir in VM)"
  190. print >> sys.stderr, "3) <kernel version>, one of:"
  191. for k in os.listdir(system_path["qubes_kernels_base_dir"]):
  192. print >> sys.stderr, " -", k
  193. return False
  194. kernel = args[0]
  195. if kernel == "default":
  196. kernel = vms.get_default_kernel()
  197. vm.uses_default_kernel = True
  198. elif kernel == "none":
  199. kernel = None
  200. vm.uses_default_kernel = False
  201. else:
  202. if not os.path.exists(os.path.join(system_path["qubes_kernels_base_dir"], kernel)):
  203. print >> sys.stderr, "Kernel version {0} not installed.".format(kernel)
  204. return False
  205. vm.uses_default_kernel = False
  206. vm.kernel = kernel
  207. return True
  208. def set_template(vms, vm, args):
  209. if len (args) != 1:
  210. print >> sys.stderr, "Missing template name argument!"
  211. return False
  212. template_name = args[0];
  213. template = vms.get_vm_by_name(template_name)
  214. if template is None or template.qid not in vms:
  215. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(template_name)
  216. return False
  217. if not template.is_template():
  218. print >> sys.stderr, "VM '{0}' is not a TemplateVM".format(template_name)
  219. return False
  220. print >> sys.stderr, "Setting template for VM '{0}' to '{1}'...".format (vm.name, template_name)
  221. vm.template = template
  222. return True
  223. def set_vcpus(vms, vm, args):
  224. if len (args) != 1:
  225. print >> sys.stderr, "Missing vcpus count argument!"
  226. return False
  227. vcpus = int(args[0])
  228. if vcpus <= 0:
  229. print >> sys.stderr, "A vcpus count must be positive."
  230. return False
  231. qubes_host = QubesHost()
  232. if vcpus > qubes_host.no_cpus:
  233. print >> sys.stderr, "This host has only {0} cpus".format(ubes_host.no_cpus)
  234. return False
  235. print >> sys.stderr, "Setting vcpus count for VM '{0}' to '{1}'...".format (vm.name, vcpus)
  236. vm.vcpus = vcpus
  237. return True
  238. def set_kernelopts(vms, vm, args):
  239. if len (args) != 1:
  240. print >> sys.stderr, "Missing kernel opts argument!"
  241. print >> sys.stderr, "Possible values:"
  242. print >> sys.stderr, "1) default"
  243. print >> sys.stderr, "2) <opts>"
  244. return False
  245. if args[0] == 'default':
  246. vm.uses_default_kernelopts = True
  247. else:
  248. vm.uses_default_kernelopts = False
  249. vm.kernelopts = args[0]
  250. return True
  251. def set_name(vms, vm, args):
  252. if len (args) != 1:
  253. print >> sys.stderr, "Missing new name!"
  254. return False
  255. if args[0] == vm.name:
  256. return False
  257. vm.set_name(args[0])
  258. return True
  259. def set_drive(vms, vm, args):
  260. if len (args) != 1:
  261. print >> sys.stderr, "Missing new drive content (file/device)!"
  262. return False
  263. if args[0] == '' or args[0].lower() == 'none':
  264. vm.drive = None
  265. else:
  266. vm.drive = args[0]
  267. return True
  268. def set_debug(vms, vm, args):
  269. if len (args) != 1:
  270. print >> sys.stderr, "Missing value (True/False or on/off)!"
  271. return False
  272. if args[0].lower() == "on":
  273. vm.debug = True
  274. elif args[0].lower() == "off":
  275. vm.debug = False
  276. else:
  277. vm.debug = bool(eval(args[0].capitalize()))
  278. return True
  279. def set_default_user(vms, vm, args):
  280. if len (args) != 1:
  281. print >> sys.stderr, "Missing user name!"
  282. return False
  283. vm.default_user = args[0]
  284. return True
  285. def set_include_in_backups(vms, vm, args):
  286. if len (args) != 1:
  287. print >> sys.stderr, "Missing value (True/False)!"
  288. return False
  289. vm.include_in_backups = bool(eval(args[0].capitalize()))
  290. return True
  291. def set_qrexec_installed(vms, vm, args):
  292. if len (args) != 1:
  293. print >> sys.stderr, "Missing value (True/False)!"
  294. return False
  295. vm.qrexec_installed = bool(eval(args[0].capitalize()))
  296. return True
  297. def set_internal(vms, vm, args):
  298. if len (args) != 1:
  299. print >> sys.stderr, "Missing value (True/False)!"
  300. return False
  301. vm.internal = bool(eval(args[0].capitalize()))
  302. return True
  303. def set_guiagent_installed(vms, vm, args):
  304. if len (args) != 1:
  305. print >> sys.stderr, "Missing value (True/False)!"
  306. return False
  307. vm.guiagent_installed = bool(eval(args[0].capitalize()))
  308. return True
  309. def set_seamless_gui_mode(vms, vm, args):
  310. if len(args) != 1:
  311. print >> sys.stderr, "Missing value (true/false)!"
  312. return False
  313. if not args[0].lower() in ['true', 'false']:
  314. print >> sys.stderr, "Invalid value, expected 'true' or 'false'"
  315. return False
  316. if args[0].lower() == 'true':
  317. vm.seamless_gui_mode = True
  318. else:
  319. vm.seamless_gui_mode = False
  320. return True
  321. def set_autostart(vms, vm, args):
  322. if len (args) != 1:
  323. print >> sys.stderr, "Missing value (True/False)!"
  324. return False
  325. vm.autostart = bool(eval(args[0].capitalize()))
  326. return True
  327. def set_qrexec_timeout(vms, vm, args):
  328. if len (args) != 1:
  329. print >> sys.stderr, "Missing timeout value (seconds)!"
  330. return False
  331. vm.qrexec_timeout = int(args[0])
  332. return True
  333. def set_timezone(vms, vm, args):
  334. if len (args) != 1:
  335. print >> sys.stderr, "Missing value ('localtime' or timeoffset in seconds)!"
  336. return False
  337. if not args[0].isdigit() and args[0].lower() == 'localtime':
  338. print >> sys.stderr, "Invalid timezone value!"
  339. return False
  340. vm.timezone = args[0]
  341. return True
  342. properties = {
  343. "include_in_backups": set_include_in_backups,
  344. "pcidevs": set_pcidevs,
  345. "label" : set_label,
  346. "netvm" : set_netvm,
  347. "maxmem" : set_maxmem,
  348. "memory" : set_memory,
  349. "kernel" : set_kernel,
  350. "template" : set_template,
  351. "vcpus" : set_vcpus,
  352. "kernelopts": set_kernelopts,
  353. "name": set_name,
  354. "drive": set_drive,
  355. "mac": set_mac,
  356. "debug": set_debug,
  357. "default_user": set_default_user,
  358. "qrexec_installed": set_qrexec_installed,
  359. "guiagent_installed": set_guiagent_installed,
  360. "seamless_gui_mode": set_seamless_gui_mode,
  361. "qrexec_timeout": set_qrexec_timeout,
  362. "timezone": set_timezone,
  363. "internal": set_internal,
  364. "autostart": set_autostart,
  365. }
  366. def do_set(vms, vm, property, args):
  367. if property not in properties.keys():
  368. print >> sys.stderr, "ERROR: Wrong property name: '{0}'".format(property)
  369. return False
  370. if not hasattr(vm, property):
  371. print >> sys.stderr, "ERROR: Property '{0}' not available for this VM".format(property)
  372. return False
  373. try:
  374. return properties[property](vms, vm, args)
  375. except Exception as err:
  376. print >> sys.stderr, "ERROR: %s" % str(err)
  377. return False
  378. def main():
  379. usage = "usage: %prog -l [options] <vm-name>\n"\
  380. "usage: %prog -s [options] <vm-name> <property> [...]\n"\
  381. "List/set various per-VM properties."
  382. parser = OptionParser (usage)
  383. parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
  384. parser.add_option ("-s", "--set", action="store_true", dest="do_set", default=False)
  385. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  386. help="Force to run, even with root privileges")
  387. (options, args) = parser.parse_args ()
  388. if (len (args) < 1):
  389. parser.error ("You must provide at least the vmname!")
  390. vmname = args[0]
  391. if os.geteuid() == 0:
  392. if not options.force_root:
  393. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  394. print >> sys.stderr, "Retry as unprivileged user."
  395. print >> sys.stderr, "... or use --force-root to continue anyway."
  396. exit(1)
  397. if options.do_list and options.do_set:
  398. print >> sys.stderr, "You cannot provide -l and -s at the same time!"
  399. exit (1)
  400. if options.do_set:
  401. qvm_collection = QubesVmCollection()
  402. qvm_collection.lock_db_for_writing()
  403. qvm_collection.load()
  404. else:
  405. qvm_collection = QubesVmCollection()
  406. qvm_collection.lock_db_for_reading()
  407. qvm_collection.load()
  408. qvm_collection.unlock_db()
  409. vm = qvm_collection.get_vm_by_name(vmname)
  410. if vm is None or vm.qid not in qvm_collection:
  411. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  412. exit(1)
  413. if options.do_set:
  414. if len (args) < 2:
  415. print >> sys.stderr, "You must specify the property you wish to set..."
  416. print >> sys.stderr, "Available properties:"
  417. for p in properties.keys():
  418. if hasattr(vm, p):
  419. print >> sys.stderr, "--> '{0}'".format(p)
  420. exit (1)
  421. property = args[1]
  422. if do_set(qvm_collection, vm, property, args[2:]):
  423. qvm_collection.save()
  424. qvm_collection.unlock_db()
  425. else:
  426. qvm_collection.unlock_db()
  427. exit(1)
  428. else:
  429. # do_list
  430. do_list(vm)
  431. main()