qvm-add-netvm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/python2.6
  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 optparse import OptionParser
  23. import subprocess
  24. import shutil
  25. import re
  26. from qubes.qubes import QubesVmCollection
  27. from qubes.qubes import QubesException
  28. def find_net_devices():
  29. p = subprocess.Popen (["lspci", "-mm", "-n"], stdout=subprocess.PIPE)
  30. result = p.communicate()
  31. retcode = p.returncode
  32. if (retcode != 0):
  33. print "ERROR when executing lspci!"
  34. raise IOError
  35. net_devices = set()
  36. rx_netdev = re.compile (r"^([0-9][0-9]:[0-9][0-9].[0-9]) \"02")
  37. for dev in str(result[0]).splitlines():
  38. match = rx_netdev.match (dev)
  39. if match is not None:
  40. dev_bdf = match.group(1)
  41. assert dev_bdf is not None
  42. net_devices.add (dev_bdf)
  43. return net_devices
  44. def main():
  45. usage = "usage: %prog [options] <netvm-name>"
  46. parser = OptionParser (usage)
  47. parser.add_option ("-p", "--path", dest="dir_path",
  48. help="Specify path to the template directory")
  49. parser.add_option ("-c", "--conf", dest="conf_file",
  50. help="Specify the Xen VM .conf file to use\
  51. (relative to the template dir path)")
  52. (options, args) = parser.parse_args ()
  53. if (len (args) != 1):
  54. parser.error ("You must specify a NetVM name!")
  55. netvmname = args[0]
  56. qvm_collection = QubesVmCollection()
  57. qvm_collection.lock_db_for_writing()
  58. qvm_collection.load()
  59. if qvm_collection.get_vm_by_name(netvmname) is not None:
  60. print "ERROR: A VM with the name '{0}' already exists in the system.".format(netvmname)
  61. exit(1)
  62. vm = qvm_collection.add_new_netvm(netvmname,
  63. conf_file=options.conf_file,
  64. dir_path=options.dir_path)
  65. try:
  66. vm.verify_files()
  67. except QubesException as err:
  68. print "ERROR: {0}".format(err)
  69. qvm_collection.pop(vm.qid)
  70. exit (1)
  71. net_devices = find_net_devices()
  72. print "Found the following net devices in your system:"
  73. dev_str = ''
  74. for dev in net_devices:
  75. print "--> {0}".format(dev)
  76. dev_str += '"{0}", '.format(dev)
  77. print "Assigning them to the netvm '{0}'".format(netvmname)
  78. rx_pcidevs = re.compile (r"%NETVMPCIDEVS%")
  79. conf_template = open (vm.conf_file, "r")
  80. conf_vm = open(vm.conf_file + ".processed", "w")
  81. for line in conf_template:
  82. line = rx_pcidevs.sub(dev_str, line)
  83. conf_vm.write(line)
  84. conf_template.close()
  85. conf_vm.close()
  86. shutil.move (vm.conf_file + ".processed", vm.conf_file)
  87. try:
  88. pass
  89. vm.add_to_xen_storage()
  90. except (IOError, OSError) as err:
  91. print "ERROR: {0}".format(err)
  92. qvm_collection.pop(vm.qid)
  93. exit (1)
  94. qvm_collection.save()
  95. qvm_collection.unlock_db()
  96. main()