This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d358e63
commit c35d85d
Showing
7 changed files
with
238 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Default cOS OEM configuration file | ||
# | ||
# This file is part of cOS and will get reset during upgrades. | ||
# | ||
# Before you change this file manually, | ||
# consider copying this file to /usr/local/cloud-config or | ||
# copy the file with a prefix starting by 90, e.g. /oem/91_custom.yaml | ||
name: "Default network configuration" | ||
stages: | ||
initramfs: | ||
- name: "Setup network" | ||
files: | ||
- path: /etc/sysconfig/network/ifcfg-eth0 | ||
content: | | ||
BOOTPROTO='dhcp' | ||
STARTMODE='onboot' | ||
permissions: 0600 | ||
owner: 0 | ||
group: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
#!/bin/bash | ||
|
||
set -x -e | ||
|
||
atoi() | ||
{ | ||
#Returns the integer representation of an IP arg, passed in ascii dotted-decimal notation (x.x.x.x) | ||
IP=$1; IPNUM=0 | ||
for (( i=0 ; i<4 ; ++i )); do | ||
((IPNUM+=${IP%%.*}*$((256**$((3-${i})))))) | ||
IP=${IP#*.} | ||
done | ||
echo $IPNUM | ||
} | ||
|
||
itoa() | ||
{ | ||
#returns the dotted-decimal ascii form of an IP arg passed in integer format | ||
echo -n $(($(($(($((${1}/256))/256))/256))%256)). | ||
echo -n $(($(($((${1}/256))/256))%256)). | ||
echo -n $(($((${1}/256))%256)). | ||
echo $((${1}%256)) | ||
} | ||
|
||
gen_mac() { | ||
local end | ||
local hexchars | ||
hexchars="0123456789ABCDEF" | ||
end=$( for i in {1..8} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' ) | ||
echo 06:FE$end | ||
} | ||
|
||
# These variables can be overwritten | ||
: ${IFACE:="eth0"} | ||
: ${BRIDGE_IFACE:="br0"} | ||
: ${MEMORY_MB:="4096"} | ||
: ${CPUS:="2"} | ||
: ${MAC:=`gen_mac`} | ||
: ${MIGRATE:=false} | ||
: ${MIGRATE_PORT:=4444} | ||
|
||
: ${KVM_BLK_OPTS:="-drive file=\$KVM_IMAGE,if=none,id=drive-disk0,format=qcow2 \ | ||
-device virtio-blk-pci,scsi=off,drive=drive-disk0,id=virtio-disk0,bootindex=1"} | ||
: ${KVM_RAW_BLK_OPTS:="-drive file=\$KVM_IMAGE,if=none,id=drive-disk0,format=raw \ | ||
-device virtio-blk-pci,scsi=off,drive=drive-disk0,id=virtio-disk0,bootindex=1"} | ||
: ${KVM_NET_OPTS:="-netdev bridge,br=\$BRIDGE_IFACE,id=net0 \ | ||
-device virtio-net-pci,netdev=net0,mac=\$MAC"} | ||
|
||
# For debugging | ||
if [ "$1" = "bash" ]; then | ||
exec bash | ||
fi | ||
|
||
# Pass Docker command args to kvm | ||
KVM_ARGS=$@ | ||
|
||
# Create the qcow disk image on the Docker volume named /image, using | ||
# the compressed qcow image that came with Docker image as the base. | ||
# Docker volumes typically perform better than the file system for | ||
# Docker images (no need for overlay fs etc.) | ||
|
||
|
||
if [ ! -d "/image" ]; then | ||
echo "/image directory does not exist, failed to mount volume /image?" | ||
exit 2 | ||
fi | ||
|
||
KVM_IMAGE=/image/sda.qcow2 | ||
if [ -e /root.qcow2 ]; then | ||
if [ ! -e $KVM_IMAGE ]; then | ||
qemu-img create -f qcow2 -b /root.qcow2 $KVM_IMAGE | ||
if [[ $? -ne 0 ]]; then | ||
echo "Failed to create qcow2 image" | ||
exit 3 | ||
fi | ||
fi | ||
elif [ ! -e $KVM_IMAGE ]; then | ||
qemu-img create -f qcow2 $KVM_IMAGE 40g | ||
fi | ||
|
||
# Network setup: | ||
# | ||
# 1. Create a bridge named br0 | ||
# 2. Remove IP from eth0, save eth0 MAC, give eth0 a random MAC | ||
|
||
cidr2mask() { | ||
local i mask="" | ||
local full_octets=$(($1/8)) | ||
local partial_octet=$(($1%8)) | ||
|
||
for ((i=0;i<4;i+=1)); do | ||
if [ $i -lt $full_octets ]; then | ||
mask+=255 | ||
elif [ $i -eq $full_octets ]; then | ||
mask+=$((256 - 2**(8-$partial_octet))) | ||
else | ||
mask+=0 | ||
fi | ||
test $i -lt 3 && mask+=. | ||
done | ||
|
||
echo $mask | ||
} | ||
|
||
setup_bridge_networking() { | ||
TFTP_SERVER=$(dig tftp-server +short) | ||
while [ -z "$TFTP_SERVER" ]; do | ||
TFTP_SERVER=$(dig tftp-server +short) | ||
sleep 1 | ||
done | ||
|
||
MAC=`ip addr show $IFACE | grep ether | sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g' | cut -f2 -d ' '` | ||
IP=`ip addr show dev $IFACE | grep "inet $IP" | awk '{print $2}' | cut -f1 -d/` | ||
CIDR=`ip addr show dev $IFACE | grep "inet $IP" | awk '{print $2}' | cut -f2 -d/` | ||
NETMASK=`cidr2mask $CIDR` | ||
GATEWAY=`ip route get 8.8.8.8 | grep via | cut -f3 -d ' '` | ||
|
||
cat > /etc/dnsmasq.conf << EOF | ||
user=root | ||
dhcp-range=$IP,$IP | ||
dhcp-host=$MAC,$HOSTNAME,$IP,infinite | ||
dhcp-option=option:router,$GATEWAY | ||
dhcp-option=option:netmask,$NETMASK | ||
dhcp-option=option:dns-server,$TFTP_SERVER | ||
dhcp-boot=$PXE,,$TFTP_SERVER | ||
EOF | ||
|
||
if [ -z $NO_DHCP ]; then | ||
dnsmasq | ||
fi | ||
|
||
# Generate random new MAC address | ||
hexchars="0123456789ABCDEF" | ||
end=$( for i in {1..8} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' ) | ||
NEWMAC=`echo 06:FE$end` | ||
|
||
let "NEWCIDR=$CIDR-1" | ||
#NEWNETMASK=`cidr2mask $NEWCIDR` | ||
|
||
i=`atoi $IP` | ||
let "i=$i^(1<<$CIDR)" | ||
NEWIP=`itoa i` | ||
|
||
ip link set dev $IFACE down | ||
ip link set $IFACE address $NEWMAC | ||
ip addr del $IP/$CIDR dev $IFACE | ||
|
||
brctl addbr $BRIDGE_IFACE | ||
brctl addif $BRIDGE_IFACE $IFACE | ||
ip link set dev $IFACE up | ||
ip link set dev $BRIDGE_IFACE up | ||
if [ -z $NO_DHCP ]; then | ||
ip addr add $NEWIP/$NEWCIDR dev $BRIDGE_IFACE | ||
fi | ||
|
||
if [[ $? -ne 0 ]]; then | ||
echo "Failed to bring up network bridge" | ||
exit 4 | ||
fi | ||
|
||
echo allow $BRIDGE_IFACE > /etc/qemu/bridge.conf | ||
} | ||
|
||
setup_bridge_networking | ||
|
||
# Allow qemu to use the bridge | ||
mkdir -p /etc/qemu | ||
echo allow $BRIDGE_IFACE > /etc/qemu/bridge.conf | ||
|
||
# Configure cloud-init | ||
USER_DATA=/user-data | ||
META_DATA=meta-data | ||
CI_ISO=cidata.iso | ||
echo "instance-id: $HOSTNAME; local-hostname: $HOSTNAME" > $META_DATA | ||
|
||
if [ ! -e $USER_DATA ]; then | ||
touch $USER_DATA | ||
fi | ||
|
||
# Create CD-ROM ISO with cloud-init config | ||
echo "$(date -R) Generating ISO for cloud-init..." | ||
mkisofs -output $CI_ISO -volid cidata -joliet -r $USER_DATA $META_DATA | ||
|
||
# Run as PID 1 | ||
exec qemu-in-container qemu-system-x86_64 \ | ||
-nographic \ | ||
-serial mon:stdio \ | ||
-rtc base=utc,clock=rt \ | ||
-chardev socket,path=qga.sock,server,nowait,id=qga0 \ | ||
-device virtio-serial \ | ||
-device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0 \ | ||
-enable-kvm \ | ||
-m size=$MEMORY_MB \ | ||
-smp cpus=$CPUS \ | ||
-drive if=ide,media=cdrom,file=/usr/share/ipxe/ipxe.iso \ | ||
-drive if=ide,media=cdrom,file=$CI_ISO \ | ||
`eval echo $KVM_BLK_OPTS` \ | ||
`eval echo $KVM_NET_OPTS` $KVM_ARGS $KVM_EXTRA_ARGS |