Commit d3d65916 authored by Alain Takoudjou's avatar Alain Takoudjou

Allow to create tap and nat interface

A new parameter 'use-nat', True by default will specify if we will setup a nat interface.
if 'use-tap' is Truei (false by default), a tap interface will be connected with a new interface in kvm.
if 'use-tap' and 'use-nat' are True, then virtual machine will have two interfaces, one with tap and
one with nat.
parent 832015ea
...@@ -45,7 +45,7 @@ class Recipe(GenericBaseRecipe): ...@@ -45,7 +45,7 @@ class Recipe(GenericBaseRecipe):
path_list = [] path_list = []
if not self.isTrueValue(self.options.get('use-tap')): if self.isTrueValue(self.options.get('use-nat')):
# XXX This could be done using Jinja. # XXX This could be done using Jinja.
for port in self.options['nat-rules'].split(): for port in self.options['nat-rules'].split():
tunnel_port = int(port) + 10000 tunnel_port = int(port) + 10000
......
...@@ -24,9 +24,11 @@ virtual_hard_drive_md5sum = '%(virtual-hard-drive-md5sum)s'.strip() ...@@ -24,9 +24,11 @@ virtual_hard_drive_md5sum = '%(virtual-hard-drive-md5sum)s'.strip()
virtual_hard_drive_gzipped = '%(virtual-hard-drive-gzipped)s'.strip() virtual_hard_drive_gzipped = '%(virtual-hard-drive-gzipped)s'.strip()
nat_rules = '%(nat-rules)s'.strip() nat_rules = '%(nat-rules)s'.strip()
use_tap = '%(use-tap)s' use_tap = '%(use-tap)s'
use_nat = '%(use-nat)s'
tap_interface = '%(tap-interface)s' tap_interface = '%(tap-interface)s'
listen_ip = '%(ipv4)s' listen_ip = '%(ipv4)s'
mac_address = '%(mac-address)s' mac_address = '%(mac-address)s'
tap_mac_address = '%(tap-mac-address)s'
smp_count = '%(smp-count)s' smp_count = '%(smp-count)s'
ram_size = '%(ram-size)s' ram_size = '%(ram-size)s'
pid_file_path = '%(pid-file-path)s' pid_file_path = '%(pid-file-path)s'
...@@ -98,15 +100,17 @@ if not os.path.exists(disk_path): ...@@ -98,15 +100,17 @@ if not os.path.exists(disk_path):
# Generate network parameters # Generate network parameters
# XXX: use_tap should be a boolean # XXX: use_tap should be a boolean
tap_network_parameter = []
nat_network_parameter = []
if use_tap == 'True': if use_tap == 'True':
qemu_network_parameter = 'tap,ifname=%%s,script=no,downscript=no' %% tap_interface tap_network_parameter = ['-net', 'nic,macaddr=%%s' %% tap_mac_address, '-net',
else: 'tap,ifname=%%s,script=no,downscript=no' %% tap_interface]
qemu_network_parameter = 'user,' + ','.join('hostfwd=tcp:%%s:%%s-:%%s' %% (listen_ip, int(port) + 10000, port) for port in nat_rules.split()) if use_nat == 'True':
rules = 'user,' + ','.join('hostfwd=tcp:%%s:%%s-:%%s' %% (listen_ip, int(port) + 10000, port) for port in nat_rules.split())
nat_network_parameter = ['-net', 'nic,macaddr=%%s' %% mac_address, '-net', rules]
kvm_argument_list = [qemu_path, kvm_argument_list = [qemu_path,
'-enable-kvm', '-net', 'nic,macaddr=%%s' %% mac_address, '-enable-kvm', '-smp', smp_count,
'-net', qemu_network_parameter,
'-smp', smp_count,
'-m', ram_size, '-m', ram_size,
'-drive', 'file=%%s,if=%%s' %% (disk_path, disk_type), '-drive', 'file=%%s,if=%%s' %% (disk_path, disk_type),
'-vnc', '%%s:1,ipv4,password' %% listen_ip, '-vnc', '%%s:1,ipv4,password' %% listen_ip,
...@@ -114,6 +118,10 @@ kvm_argument_list = [qemu_path, ...@@ -114,6 +118,10 @@ kvm_argument_list = [qemu_path,
'-qmp', 'unix:%%s,server' %% socket_path, '-qmp', 'unix:%%s,server' %% socket_path,
'-pidfile', pid_file_path, '-pidfile', pid_file_path,
] ]
if tap_network_parameter == [] and nat_network_parameter == []:
print 'Warning : No network interface defined.'
else:
kvm_argument_list += tap_network_parameter + nat_network_parameter
# Try to connect to NBD server (and second nbd if defined). # Try to connect to NBD server (and second nbd if defined).
# If not available, don't even specify it in qemu command line parameters. # If not available, don't even specify it in qemu command line parameters.
......
...@@ -93,7 +93,7 @@ mode = 0644 ...@@ -93,7 +93,7 @@ mode = 0644
recipe = hexagonit.recipe.download recipe = hexagonit.recipe.download
url = ${:_profile_base_location_}/instance-kvm.cfg.jinja2 url = ${:_profile_base_location_}/instance-kvm.cfg.jinja2
mode = 644 mode = 644
md5sum = 717f22f56ca1afbe736489c15a332013 md5sum = 9872925dafa10419b067392f2d1e10ce
download-only = true download-only = true
on-update = true on-update = true
...@@ -101,7 +101,7 @@ on-update = true ...@@ -101,7 +101,7 @@ on-update = true
recipe = hexagonit.recipe.download recipe = hexagonit.recipe.download
url = ${:_profile_base_location_}/instance-kvm-cluster.cfg.jinja2.in url = ${:_profile_base_location_}/instance-kvm-cluster.cfg.jinja2.in
mode = 644 mode = 644
md5sum = 1c8ba5a64b53a182001f553fb5f6fff8 md5sum = ca88ddc054ccb107f2f1f7e348c75818
download-only = true download-only = true
on-update = true on-update = true
......
...@@ -35,6 +35,7 @@ config-cpu-count = {{ dumps(kvm_parameter_dict.get('cpu-count', 1)) }} ...@@ -35,6 +35,7 @@ config-cpu-count = {{ dumps(kvm_parameter_dict.get('cpu-count', 1)) }}
{% set nat_rules_list = kvm_parameter_dict.get('nat-rules', [22, 80, 443]) -%} {% set nat_rules_list = kvm_parameter_dict.get('nat-rules', [22, 80, 443]) -%}
config-nat-rules = {{ nat_rules_list | join(' ') }} config-nat-rules = {{ nat_rules_list | join(' ') }}
config-use-nat = {{ dumps(kvm_parameter_dict.get('use-nat', True)) }}
config-use-tap = {{ dumps(kvm_parameter_dict.get('use-tap', False)) }} config-use-tap = {{ dumps(kvm_parameter_dict.get('use-tap', False)) }}
config-virtual-hard-drive-url = {{ dumps(kvm_parameter_dict.get('virtual-hard-drive-url', '')) }} config-virtual-hard-drive-url = {{ dumps(kvm_parameter_dict.get('virtual-hard-drive-url', '')) }}
config-virtual-hard-drive-md5sum = {{ dumps(kvm_parameter_dict.get('virtual-hard-drive-md5sum', '')) }} config-virtual-hard-drive-md5sum = {{ dumps(kvm_parameter_dict.get('virtual-hard-drive-md5sum', '')) }}
......
...@@ -41,6 +41,10 @@ cronstamps = ${:etc}/cronstamps ...@@ -41,6 +41,10 @@ cronstamps = ${:etc}/cronstamps
recipe = slapos.cookbook:generate.mac recipe = slapos.cookbook:generate.mac
storage-path = ${directory:srv}/mac storage-path = ${directory:srv}/mac
[create-tap-mac]
recipe = slapos.cookbook:generate.mac
storage-path = ${directory:srv}/tap_mac
[gen-passwd] [gen-passwd]
recipe = slapos.cookbook:generate.password recipe = slapos.cookbook:generate.password
storage-path = ${directory:srv}/passwd storage-path = ${directory:srv}/passwd
...@@ -79,12 +83,14 @@ pid-file-path = ${directory:run}/pid_file ...@@ -79,12 +83,14 @@ pid-file-path = ${directory:run}/pid_file
smp-count = ${slap-parameter:cpu-count} smp-count = ${slap-parameter:cpu-count}
ram-size = ${slap-parameter:ram-size} ram-size = ${slap-parameter:ram-size}
mac-address = ${create-mac:mac-address} mac-address = ${create-mac:mac-address}
tap-mac-address = ${create-tap-mac:mac-address}
# XXX-Cedric: should be named runner-wrapper-path and controller-wrapper-path # XXX-Cedric: should be named runner-wrapper-path and controller-wrapper-path
runner-path = ${directory:services}/kvm runner-path = ${directory:services}/kvm
controller-path = ${directory:scripts}/kvm_controller controller-path = ${directory:scripts}/kvm_controller
use-tap = ${slap-parameter:use-tap} use-tap = ${slap-parameter:use-tap}
use-nat = ${slap-parameter:use-nat}
nat-rules = ${slap-parameter:nat-rules} nat-rules = ${slap-parameter:nat-rules}
6tunnel-wrapper-path = ${directory:services}/6tunnel 6tunnel-wrapper-path = ${directory:services}/6tunnel
...@@ -250,6 +256,7 @@ disk-type = virtio ...@@ -250,6 +256,7 @@ disk-type = virtio
cpu-count = 1 cpu-count = 1
nat-rules = 22 80 443 nat-rules = 22 80 443
use-nat = True
use-tap = False use-tap = False
virtual-hard-drive-url = virtual-hard-drive-url =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment