qvm-dom0-network-via-netvm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python2.6
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Rafal Wojtczuk <rafal@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. import os.path
  24. import os
  25. import sys
  26. def get_netvm():
  27. qvm_collection = QubesVmCollection()
  28. qvm_collection.lock_db_for_reading()
  29. qvm_collection.load()
  30. qvm_collection.unlock_db()
  31. netvm = qvm_collection.get_default_netvm_vm()
  32. if netvm is None or netvm.name == 'dom0':
  33. print 'There seems to be no dedicated default netvm, aborting.'
  34. sys.exit(1)
  35. return netvm
  36. def vif_eth0_exists():
  37. if not os.path.islink('/sys/class/net/eth0'):
  38. return False
  39. if not os.path.isdir('/sys/devices/xen/vif-0/net/eth0'):
  40. print 'There is a dedicated netvm, but device eth0 is present'
  41. print 'and it is not a Xen interface. Refusing to continue.'
  42. sys.exit(1)
  43. return True
  44. def bringup_eth0(netvm):
  45. resolv_conf = open('/etc/resolv.conf', "w")
  46. resolv_conf.write('nameserver ' + netvm.gateway + '\n')
  47. resolv_conf.write('nameserver ' + netvm.secondary_dns + '\n')
  48. resolv_conf.close()
  49. return os.system('ifconfig eth0 10.0.0.1 netmask 255.255.255.255 && route add default dev eth0') == 0
  50. def unpause_all(netvm_name):
  51. os.system('qvm-run --exclude=' + netvm_name + ' --all --unpause')
  52. def netup():
  53. netvm = get_netvm()
  54. if os.path.isfile('/var/lock/subsys/NetworkManager'):
  55. os.system('/etc/init.d/NetworkManager stop')
  56. if not vif_eth0_exists():
  57. cmd = 'modprobe xennet && xm network-attach 0 ip=10.0.0.1 backend='
  58. cmd += netvm.name
  59. cmd += ' script=vif-route-qubes'
  60. if os.system(cmd) != 0:
  61. print 'Error creating network device'
  62. sys.exit(1)
  63. os.system('qvm-run --exclude=' + netvm.name + ' --all --pause')
  64. if not bringup_eth0(netvm):
  65. unpause_all(netvm.name)
  66. sys.exit(1)
  67. def netdown():
  68. netvm = get_netvm()
  69. if not vif_eth0_exists():
  70. print 'There is no eth0 that is a Xen vif device, aborting.'
  71. sys.exit(1)
  72. os.system('ifconfig eth0 down')
  73. unpause_all(netvm.name)
  74. def usage():
  75. print 'Usage: qvm-dom0-network-via-netvm [up|down]'
  76. sys.exit(1)
  77. def main():
  78. if len(sys.argv) != 2:
  79. usage()
  80. if os.getuid() != 0:
  81. print 'This script must be run as root'
  82. sys.exit(1)
  83. if sys.argv[1] == 'up':
  84. netup()
  85. sys.exit(0)
  86. if sys.argv[1] == 'down':
  87. netdown()
  88. sys.exit(0)
  89. usage()
  90. main()