entrypoints root was changed, additional scripts added

This commit is contained in:
Paul Rock 2020-04-14 21:20:44 +03:00
parent 519fc5c563
commit 3b5e15fcd3
6 changed files with 143 additions and 8 deletions

View file

@ -1,8 +0,0 @@
#!/bin/sh
qemu-system-x86_64 \
-vnc 0.0.0.0:0 \
-m 256 \
-hda $ROUTEROS_IMAGE \
-device e1000,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::21-:21,hostfwd=tcp::22-:22,hostfwd=tcp::23-:23,hostfwd=tcp::80-:80,hostfwd=tcp::443-:443,hostfwd=tcp::8291-:8291,hostfwd=tcp::8728-:8728,hostfwd=tcp::8729-:8729

58
scripts/entrypoint.sh Executable file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env bash
# A bridge of this name will be created to host the TAP interface created for
# the VM
QEMU_BRIDGE='qemubr0'
# DHCPD must have an IP address to run, but that address doesn't have to
# be valid. This is the dummy address dhcpd is configured to use.
DUMMY_DHCPD_IP='10.0.0.1'
# These scripts configure/deconfigure the VM interface on the bridge.
QEMU_IFUP='/routeros/qemu-ifup'
QEMU_IFDOWN='/routeros/qemu-ifdown'
# The name of the dhcpd config file we make
DHCPD_CONF_FILE='/routeros/dhcpd.conf'
function default_intf() {
ip -json route show | jq -r '.[] | select(.dst == "default") | .dev'
}
# First step, we run the things that need to happen before we start mucking
# with the interfaces. We start by generating the DHCPD config file based
# on our current address/routes. We "steal" the container's IP, and lease
# it to the VM once it starts up.
/routeros/generate-dhcpd-conf.py $QEMU_BRIDGE > $DHCPD_CONF_FILE
default_dev=`default_intf`
# Now we start modifying the networking configuration. First we clear out
# the IP address of the default device (will also have the side-effect of
# removing the default route)
ip addr flush dev $default_dev
# Next, we create our bridge, and add our container interface to it.
ip link add $QEMU_BRIDGE type bridge
ip link set dev $default_dev master $QEMU_BRIDGE
# Then, we toggle the interface and the bridge to make sure everything is up
# and running.
ip link set dev $default_dev up
ip link set dev $QEMU_BRIDGE up
# Finally, start our DHCPD server
udhcpd -I $DUMMY_DHCPD_IP -f $DHCPD_CONF_FILE &
# And run the VM! A brief explanation of the options here:
# -enable-kvm: Use KVM for this VM (much faster for our case).
# -nographic: disable SDL graphics.
# -serial mon:stdio: use "monitored stdio" as our serial output.
# -nic: Use a TAP interface with our custom up/down scripts.
# -drive: The VM image we're booting.
exec qemu-system-x86_64 \
-enable-kvm -nographic -serial mon:stdio \
-vnc 0.0.0.0:0 \
-m 256 \
-nic tap,id=qemu0,script=$QEMU_IFUP,downscript=$QEMU_IFDOWN \
"$@" \
-hda $ROUTEROS_IMAGE

73
scripts/generate-dhcpd-conf.py Executable file
View file

@ -0,0 +1,73 @@
#!/usr/bin/env python3
import argparse
import ipaddress
import json
import re
import socket
import subprocess
from typing import List, Iterable
DEFAULT_ROUTE = 'default'
DEFAULT_DNS_IPS = ('8.8.8.8', '8.8.4.4')
DHCP_CONF_TEMPLATE = """
start {host_addr}
end {host_addr}
# avoid dhcpd complaining that we have
# too many addresses
maxleases 1
interface {dhcp_intf}
option dns {dns}
option router {gateway}
option subnet {subnet}
option hostname {hostname}
"""
def default_route(routes):
"""Returns the host's default route"""
for route in routes:
if route['dst'] == DEFAULT_ROUTE:
return route
raise ValueError('no default route')
def addr_of(addrs, dev : str) -> ipaddress.IPv4Interface:
"""Finds and returns the IP address of `dev`"""
for addr in addrs:
if addr['ifname'] != dev:
continue
info = addr['addr_info'][0]
return ipaddress.IPv4Interface((info['local'], info['prefixlen']))
raise ValueError('dev {0} not found'.format(dev))
def generate_conf(intf_name : str, dns : Iterable[str]) -> str:
"""Generates a dhcpd config. `intf_name` is the interface to listen on."""
with subprocess.Popen(['ip', '-json', 'route'], stdout=subprocess.PIPE) as proc:
routes = json.load(proc.stdout)
with subprocess.Popen(['ip', '-json', 'addr'], stdout=subprocess.PIPE) as proc:
addrs = json.load(proc.stdout)
droute = default_route(routes)
host_addr = addr_of(addrs, droute['dev'])
return DHCP_CONF_TEMPLATE.format(
dhcp_intf = intf_name,
dns = ' '.join(dns),
gateway = droute['gateway'],
host_addr = host_addr.ip,
hostname = socket.gethostname(),
subnet = host_addr.network.netmask,
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('intf_name')
parser.add_argument('dns_ips', nargs='*')
args = parser.parse_args()
dns_ips = args.dns_ips
if not dns_ips:
dns_ips = DEFAULT_DNS_IPS
print(generate_conf(args.intf_name, dns_ips))

6
scripts/qemu-ifdown Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
QEMU_BRIDGE='qemubr0'
ip link set dev $1 nomaster
ip link set dev $1 down

6
scripts/qemu-ifup Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
QEMU_BRIDGE='qemubr0'
ip link set dev $1 up
ip link set dev $1 master $QEMU_BRIDGE