Commit a0854e99 authored by Maria Matejka's avatar Maria Matejka

INIT

parents
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
NAME=$1
ID=$2
if [ -z "$NAME" ] || [ -z "$ID" ]; then
echo "Usage: $0 NAME ID"
exit 2
fi
socat UNIX-CONNECT:$ROOT/run/$NAME.sock - <<EOF
netdev_add type=tap,id=$ID,ifname=$ID,script=no
device_add e1000,netdev=$ID
EOF
#!/bin/bash
. $(dirname $0)/virt-lib
[ -f $ROOT/run/dhcpd.pid ] && kill $(cat $ROOT/run/dhcpd.pid)
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
iptables -D FORWARD -i eth0 -o $VIRTCTRL -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i eth0 -o $VIRTCTRL -j ACCEPT
iptables -D FORWARD -i $VIRTCTRL -o eth0 -j ACCEPT
iptables -D FORWARD -o eth0 -j REJECT
ovs-vsctl del-br $VIRTBR
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
NETLIST=$ROOT/run/networks
NET=$1
if [ -z "$NET" ] || [ -z "$2" ]; then
echo "Usage: $0 NET hosts"
exit 2
fi
shift
if grep -q $NET $NETLIST; then
echo "Network $NET exists"
exit 2
fi
MAXID=$(sort -k1,1nr $NETLIST | head -n1|cut -f1 -d' ')
MAXID=$((MAXID+1))
echo "$MAXID $NET" >> $NETLIST
i=0
idprefix=auto-$MAXID
for H in "$@"; do
$ROOT/bin/addnic $H $idprefix-$i
ip link set $idprefix-$i up
ovs-vsctl add-port $VIRTBR $idprefix-$i tag=$MAXID
i=$((i+1))
done
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
HOST=$2
TYPE=$1
case ${TYPE^^} of
FREEBSD)
# OPENBSD)
# NETBSD)
LINUX)
type=${type,,}
;;
*)
echo "Unknown type of host: $TYPE"
exit 2
;;
esac
gethost $HOST
AUTH=`mktemp`
cat /home/birdlab/.ssh/authorized_keys > $AUTH
cat /root/.ssh/virt-rsa.pub >> $AUTH
scp $AUTH root@$HOST_IP4:/root/.ssh/authorized_keys
SCRIPT=`mktemp`
FSTAB_FREEBSD="192.168.192.1:/var/lib/virt/nfs /mnt/nfs nfs rw 0 0"
FSTAB_LINUX="192.168.192.1:/var/lib/virt/nfs /mnt/nfs nfs defaults,intr,_netdev 0 0"
FSTAB=${FSTAB_${TYPE^^}}
cat >$SCRIPT <<EOF
echo "$FSTAB" >> /etc/fstab
mkdir -p /mnt/nfs
mount /mnt/nfs
EOF
ssh root@$HOST_IP4 <$SCRIPT
#!/bin/bash
. $(dirname $0)/virt-lib
vsctl add-br $VIRTBR
ip link set $VIRTBR up
ip addr add 192.168.10.1/28 dev $VIRTBR
ip link add $VIRTCTRL type veth peer name $VIRTUPLINK
ip link set $VIRTCTRL up
ip link set $VIRTUPLINK up
ip addr add 192.168.192.1/18 dev $VIRTCTRL
vsctl add-port $VIRTBR $VIRTUPLINK tag=1
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o $VIRTCTRL -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth0 -o $VIRTCTRL -j ACCEPT
iptables -A FORWARD -i $VIRTCTRL -o eth0 -j ACCEPT
iptables -A FORWARD -o eth0 -j REJECT
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
egrep -i '^\s*Network' <$VIRTUALS | sed -r 's/.*[Nn][Ee][Tt][Ww][Oo][Rr][Kk]\s+(.*)\s+(.*)\s+(.*)$/host \1 { hardware ethernet \2; fixed-address \3; }/' >$ROOT/etc/dhcpd-hosts.conf
[ -f $ROOT/run/dhcpd.pid ] && kill $(cat $ROOT/run/dhcpd.pid)
dhcpd -cf $ROOT/etc/dhcpd.conf -pf $ROOT/run/dhcpd.pid $VIRTCTRL
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
NAME=$1
shift
if [ -z "$NAME" ]; then
echo "Usage: $0 name [custom options for qemu]"
exit 2
fi
if [ -e $ROOT/run/$NAME.pid ]; then
echo -n "Host $NAME pidfile found"
if [ -d /proc/$(<$ROOT/run/$NAME.pid) ]; then
echo " running with PID $(<$ROOT/run/$NAME.pid)"
exit 1
else
echo " stale ... removing"
rm $ROOT/run/$NAME.pid
fi
fi
gethost $NAME
if [ "$?" != 0 ]; then
echo "Host $NAME not configured"
exit 2
fi
#echo -e "Image:\t\t$HOSTIMAGE"
#echo -e "CD:\t\t$HOSTCDROM"
#echo -e "Arch:\t\t$HOSTARCH"
#echo -e "Network:\t$HOSTNETWORK"
#echo -e "MAC:\t\t$HOSTMAC"
#echo -e "IP4:\t\t$HOSTIP4"
#echo -e "TAP:\t\t$HOSTTAP"
qemu-system-$HOSTARCH -enable-kvm -m $HOSTMEMORY ${HOSTCDROM:+-cdrom $ROOT/iso/$HOSTCDROM -boot d} -hda $ROOT/img/$HOSTIMAGE $HOSTCOW -display none ${HOSTVNCID:+-vnc 10.0.0.11:$HOSTVNCID} -netdev tap,id=ctl,ifname=$HOSTTAP,script=no -device e1000,netdev=ctl,mac=$HOSTMAC -pidfile $ROOT/run/$NAME.pid -monitor unix:$ROOT/run/$NAME.sock,server,nowait -daemonize
#-runas $VIRTUSER
ip link set $HOSTTAP up
vsctl add-port $VIRTBR $HOSTTAP tag=1
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
declare -A seen
seen[dhcpd]=1
for S in `find $ROOT/run -name '*.sock'`; do
S=$(basename $S)
gethost ${S%%.sock}
echo "${S%%.sock} running, IPv4 $HOSTIP4, monitor $S"
seen["${S%%.sock}"]=1
done
for P in `find $ROOT/run -name '*.pid'`; do
P=$(basename $P)
if [ "${seen[${P%%.pid}]}" = "1" ]; then continue; fi
gethost ${P%%.pid}
if ps -p $(<$P) >/dev/null; then
echo "${P%%.pid} probably running at PID $(<$P), no socket, IPv4 $HOSTIP4"
else
echo "${P%%.pid} stale pidfile found, removing"
rm $ROOT/run/$P
fi
seen["${P%%.pid}"]=1
done
for H in `grep -i '^Host' $VIRTUALS| sed 's/^[Hh][Oo][Ss][Tt] //'`; do
if [ "${seen[$H]}" = "1" ]; then continue; fi
echo "$H not running"
done
#!/bin/bash
. $(dirname $(readlink -f $0))/virt-lib
NAME=$1
shift
if [ -z "$NAME" ]; then
echo "Usage: $0 name [custom options for qemu]"
exit 2
fi
function ok() {
echo
rm -f $ROOT/run/$NAME.sock $ROOT/run/$NAME.pid
if gethost $NAME; then
vsctl del-port $VIRTBR $HOSTTAP
virtns ip link del $HOSTTAP
else
echo "Warning: Config not found for this host"
fi
echo "OK"
exit 0
}
if [ -e $ROOT/run/$NAME.sock ]; then
echo -n "Graceful powerdown "
echo "system_powerdown" | socat - UNIX-CONNECT:$ROOT/run/$NAME.sock >/dev/null
if wait_pidfile $ROOT/run/$NAME.pid 30; then ok; fi
else
echo "No monitor socket found."
fi
if [ -e $ROOT/run/$NAME.pid ]; then
if [ ! -d /proc/$(<$ROOT/run/$NAME.pid) ]; then
echo "Stale pidfile found, cleaning up"
rm $ROOT/run/$NAME.pid
return 0
fi
echo -n "Sending TERM "
kill -15 $(<$ROOT/run/$NAME.pid)
if wait_pidfile $ROOT/run/$NAME.pid 30; then ok; fi
echo -n "Sending KILL "
kill -9 $(<$ROOT/run/$NAME.pid)
if wait_pidfile $ROOT/run/$NAME.pid 30; then ok; fi
echo "Did not stop even after KILL, strange."
exit 1
fi
#!/bin/bash
ROOT=$(readlink -f $(dirname $(readlink -f $0))/../)
VIRTBR=virtbr
VIRTCTRL=virtctrl
VIRTUPLINK=virtuplink
VIRTUALS=$ROOT/etc/virtlist
VIRTUSER=virt-qemu
function virtns() {
"$@"
}
function vsctl() {
virtns ovs-vsctl "$@"
}
function wait_pidfile() {
CNT=0
FILE=$1
TIMEOUT=${2:-30}
while [ -d /proc/$(<$FILE) ]; do
echo -n "."
sleep 1
CNT=$((CNT+1))
if [ $CNT -eq $TIMEOUT ]; then
echo
echo "Timeout ${TIMEOUT}s reached."
return 1
fi
done
return 0
}
function gethost() {
HOSTIMAGE=
HOSTNETWORK=
HOSTCDROM=
HOSTARCH=
HOSTMEMORY=
HOSTCOW=
HOSTVNCID=
HOSTTAP=
HOSTIP4=
HOSTMAC=
HOSTNIC=()
HOSTNICCMD=
NAME=$1
_HOST_READING=false
while read item value; do
if $_HOST_READING; then
case ${item^^} in
#) ;;
HOST) hostdefaults
return $? ;;
IMAGE) HOSTIMAGE="$value" ;;
NETWORK)HOSTNETWORK="$value" ;;
CDROM) HOSTCDROM="$value" ;;
ARCH) HOSTARCH="$value" ;;
MEMORY) HOSTMEMORY="$value" ;;
COW) HOSTCOW="$value" ;;
VNC) HOSTVNCID="$value" ;;
NIC) HOSTNIC+=("$value")
esac
else
if [ "${item^^}" = "HOST" ] \
&& [ "$value" = "$NAME" ]; then
_HOST_READING=true
HOSTNAME=$NAME
fi
fi
done <$VIRTUALS
if $_HOST_READING; then
hostdefaults
return $?
else
return 1
fi
}
function hostdefaults() {
if [ -z "$HOSTIMAGE" ]; then echo "Error: Image not set"; return 1; fi
if [ -z "$HOSTNETWORK" ]; then echo "Error: No control network set"; return 1; fi
if [ -z "$HOSTARCH" ]; then HOSTARCH=x86_64; fi
if [ -z "$HOSTMEMORY" ]; then HOSTMEMORY=64M; fi
if [ "${HOSTCOW^^}" = "YES" ]; then HOSTCOW=-snapshot; else HOSTCOW= ; fi
for NIC in "${HOSTNIC[@]}"; do
NICNAME=${NIC%% *}
NICMAC=${NIC##* }
done
read HOSTTAP HOSTMAC HOSTIP4 <<<"$HOSTNETWORK"
return 0
}
host tci-freebsd10 { hardware ethernet 00:11:22:00:05:01; fixed-address 192.168.197.1; }
host tci-debian8 { hardware ethernet 00:11:22:00:01:01; fixed-address 192.168.193.1; }
host tci-debian5 { hardware ethernet 00:11:22:00:01:02; fixed-address 192.168.193.2; }
host tci-fb10-1 { hardware ethernet 00:11:22:00:15:01; fixed-address 192.168.200.1; }
host tci-fb10-2 { hardware ethernet 00:11:22:00:15:02; fixed-address 192.168.200.2; }
host tcd8-1 { hardware ethernet 00:11:22:00:11:01; fixed-address 192.168.210.1; }
host tcd8-2 { hardware ethernet 00:11:22:00:11:11; fixed-address 192.168.210.2; }
host tcd8-3 { hardware ethernet 00:11:22:00:11:21; fixed-address 192.168.210.3; }
option domain-name "bird-virtual.nic.cz";
option domain-name-servers 8.8.8.8;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 192.168.192.0 netmask 255.255.192.0 {
range 192.168.193.0 192.168.254.0;
option routers 192.168.192.1;
default-lease-time 15555555;
}
include "/var/lib/virt/etc/dhcpd-hosts.conf";
Host freebsd10-install
Image freebsd10
Network tci-freebsd10 00:11:22:00:05:01 192.168.197.1
Memory 512M
# Cdrom freebsd10.iso
VNC 51
Host debian8-install
Image debian8
Network tci-debian8 00:11:22:00:01:01 192.168.193.1
Memory 512M
# Cdrom debian8.iso
VNC 11
Host debian5-install
Image debian5
Network tci-debian5 00:11:22:00:01:02 192.168.193.2
Memory 512M
# Cdrom debian5.iso
VNC 12
Host fb10-1
Image freebsd10
Network tci-fb10-1 00:11:22:00:15:01 192.168.200.1
Memory 128M
VNC 101
Cow yes
Host fb10-2
Image freebsd10
Network tci-fb10-2 00:11:22:00:15:02 192.168.200.2
Memory 2G
VNC 102
Cow yes
Host d8-1
Image debian8
Network tcd8-1 00:11:22:00:11:01 192.168.210.1
Memory 128M
VNC 121
Cow yes
Host d8-2
Image debian8
Network tcd8-2 00:11:22:00:11:11 192.168.210.2
Memory 128M
VNC 122
Cow yes
Host d8-3
Image debian8
Network tcd8-3 00:11:22:00:11:21 192.168.210.3
Memory 128M
VNC 123
Cow yes
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