Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 197 lines (167 sloc) 4.16 KB
#!/bin/sh -e
OVMF_CODE_PATH="/usr/share/ovmf/x64/OVMF_CODE.fd"
rand() {
od -vAn -tu2 -N1 </dev/urandom | sed -z 's/[ \n]//gi'
}
abort() {
echo >&2 "$*"
exit 1;
}
setup_network() {
network_name="$1"
test -z "$network_name" && abort "setup_network: network_name not set"
ip link add "$network_name" type bridge
ip link set "$network_name" up
}
shutdown_network() {
network_name="$1"
test -z "$network_name" && abort "shutdown_network: network_name not set"
ip link delete "$network_name" type bridge || true
}
setup_tap() {
network_name="$1"
tap_name="$2"
test -z "$network_name" && abort "setup_tap: network_name not set"
test -z "$tap_name" && abort "setup_tap: tap_name not set"
ip tuntap add "$tap_name" mode tap
ip link set "$tap_name" master "$network_name"
ip link set "$tap_name" up
}
shutdown_tap() {
tap_name="$1"
test -z "$tap_name" && abort "shutdown_tap: tap_name not set"
ip tuntap delete "$tap_name" mode tap || true
}
assign_network_address() {
network_name=$1
cidr=$2
test -z "$network_name" && abort "assign_network_address: network_name not set"
test -z "$cidr" && abort "assign_network_address: cidr not set"
ip address add "$cidr" dev "$network_name"
}
start_node() {
boot_mode="$1"
serial="$2"
test -z "$boot_mode" && abort "start_node: boot_mode not set"
if [ -z "$serial"]; then
serial=$(printf "%02x%02x%02x%02x" $(rand) $(rand) $(rand) $(rand))
fi
tap_name="$serial"
mac_address=$(printf "52:54:00:%02x:%02x:%02x" $(rand) $(rand) $(rand))
qemu_opts="-nodefaults
-enable-kvm
-boot n
-m 128
-nographic
-serial stdio
-smbios type=1,serial=$serial
-netdev tap,id=net0,ifname=${tap_name},script=no,downscript=no
-device virtio-net-pci,netdev=net0,romfile=,mac=${mac_address}"
case $boot_mode in
uefi)
qemu_opts="$qemu_opts -drive if=pflash,file=${OVMF_CODE_PATH},format=raw,readonly"
;;
ipxe)
qemu_opts="$qemu_opts -option-rom pxe-virtio.rom"
;;
*)
abort "unknown boot mode \`${boot_mode}'"
esac
setup_tap "$network_name" "$serial"
trap "shutdown_tap $tap_name" SIGHUP SIGTERM SIGINT EXIT
qemu-system-x86_64 $qemu_opts
}
usage() {
echo >&2 "Usage: $0 network --name NETWORK_NAME [--address BRIDGE_ADDRESS]"
echo >&2 " $0 node --network NETWORK_NAME [--serial SERIAL]"
echo >&2 " [--boot-mode ipxe|uefi] [--ovmf-path OVMF_CODE_PATH]"
}
usage_network() {
echo >&2 "Usage: $0 network --name NETWORK_NAME [--address BRIDGE_ADDRESS]"
}
usage_node() {
echo >&2 "Usage: $0 node --network NETWORK_NAME [--serial SERIAL]"
echo >&2 " [--boot-mode ipxe|uefi] [--ovmf-path OVMF_CODE_PATH]"
}
cmd_network() {
while [ "$#" -ne 0 ]; do
case "$1" in
--name)
network_name=$2
shift; shift
;;
--address)
bridge_address=$2
shift; shift
;;
-h|--help)
usage_network
return 0
;;
*)
abort "unknown parameter $1"
;;
esac
done
test -z "$network_name" && abort "missing --name parameter"
setup_network "$network_name"
trap "shutdown_network $network_name" SIGHUP SIGTERM SIGINT EXIT
if [ -n "$bridge_address" ]; then
assign_network_address "$network_name" "$bridge_address"
fi
echo "Network $network_name created. Press Ctrl+C to shutdown."
tail -F /dev/null
}
cmd_node() {
boot_mode=ipxe
while [ "$#" -ne 0 ]; do
case "$1" in
--boot-mode)
boot_mode=$2
shift; shift
;;
--ovmf-path)
OVMF_CODE_PATH="$2"
shift; shift
;;
--network)
network_name=$2
shift; shift
;;
--serial)
serial=$2
shift; shift
;;
-h|--help)
usage_node
return 0
;;
*)
abort "unknown parameter $1"
;;
esac
done
test -z "$network_name" && abort "missing --network parameter"
start_node "$boot_mode" "$serial"
}
mode="$1"
if [ -z "$mode" ]; then
usage
exit 2
fi
case "$mode" in
network)
shift
cmd_network $@
;;
node)
shift
cmd_node $@
;;
-h|--help)
usage
;;
*)
abort "unknown mode \`${mode}'"
exit 2
esac
You can’t perform that action at this time.