qubes-dom0-network-via-netvm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/python2
  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. qvm_collection = None
  27. def get_netvm():
  28. global qvm_collection
  29. qvm_collection = QubesVmCollection()
  30. qvm_collection.lock_db_for_reading()
  31. qvm_collection.load()
  32. qvm_collection.unlock_db()
  33. netvm = qvm_collection.get_default_netvm()
  34. while netvm.netvm is not None:
  35. netvm = netvm.netvm
  36. if netvm is None or netvm.name == 'dom0':
  37. print >> sys.stderr, 'There seems to be no dedicated default netvm, aborting.'
  38. sys.exit(1)
  39. return netvm
  40. def vif_eth0_exists():
  41. if not os.path.islink('/sys/class/net/eth0'):
  42. return False
  43. if not os.path.isdir('/sys/devices/vif-0/net/eth0'):
  44. print >> sys.stderr, 'There is a dedicated netvm, but device eth0 is present'
  45. print >> sys.stderr, 'and it is not a Xen interface. Refusing to continue.'
  46. sys.exit(1)
  47. return True
  48. def bringup_eth0(netvm):
  49. resolv_conf = open('/etc/resolv.conf', "w")
  50. resolv_conf.write('nameserver ' + netvm.gateway + '\n')
  51. resolv_conf.write('nameserver ' + netvm.secondary_dns + '\n')
  52. resolv_conf.close()
  53. return os.system('ip link set eth0 up && ip addr add 10.137.0.2/32 dev eth0 && ' +
  54. 'ip route add 10.137.0.1 dev eth0 && ip route add via 10.137.0.1') == 0
  55. def netup():
  56. netvm = get_netvm()
  57. if os.path.isfile('/var/lock/subsys/NetworkManager'):
  58. os.system('/etc/init.d/NetworkManager stop')
  59. if not vif_eth0_exists():
  60. cmd = 'modprobe xen-netfront'
  61. if os.system(cmd) != 0:
  62. print >> sys.stderr, 'Error creating network device'
  63. sys.exit(1)
  64. qvm_collection[0].attach_network(verbose=True, netvm=netvm, wait=True)
  65. if not bringup_eth0(netvm):
  66. sys.exit(1)
  67. def netdown():
  68. netvm = get_netvm()
  69. if not vif_eth0_exists():
  70. print >> sys.stderr, 'There is no eth0 that is a Xen vif device, aborting.'
  71. sys.exit(1)
  72. resolv_conf = open('/etc/resolv.conf', "w")
  73. resolv_conf.close()
  74. os.system('ifconfig eth0 down')
  75. os.system('xl network-detach 0 0')
  76. def usage():
  77. print >> sys.stderr, 'Usage: {0} [up|down]'.format(sys.argv[0])
  78. sys.exit(1)
  79. def main():
  80. if len(sys.argv) != 2:
  81. usage()
  82. if os.getuid() != 0:
  83. print >> sys.stderr, 'This script must be run as root'
  84. sys.exit(1)
  85. if sys.argv[1] == 'up':
  86. netup()
  87. sys.exit(0)
  88. if sys.argv[1] == 'down':
  89. netdown()
  90. sys.exit(0)
  91. usage()
  92. main()