Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
Fix pxe install again
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Dec 1, 2021
1 parent d358e63 commit c35d85d
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
FROM opensuse/leap:15.3 as base
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf && \
sed -i 's/download/provo-mirror/g' /etc/zypp/repos.d/*repo
RUN zypper ref

FROM base AS build
RUN zypper in -y squashfs xorriso go1.16 upx busybox-static curl tar git gzip
RUN curl -Lo /usr/bin/luet https://github.com/mudler/luet/releases/download/0.20.10/luet-0.20.10-linux-$(go env GOARCH) && \
chmod +x /usr/bin/luet && \
upx /usr/bin/luet
RUN curl -Lo /usr/bin/rancherd https://github.com/rancher/rancherd/releases/download/v0.0.1-alpha11/rancherd-$(go env GOARCH) && \
RUN curl -Lo /usr/bin/rancherd https://github.com/rancher/rancherd/releases/download/v0.0.1-alpha13/rancherd-$(go env GOARCH) && \
chmod +x /usr/bin/rancherd && \
upx /usr/bin/rancherd
RUN curl -L https://get.helm.sh/helm-v3.7.1-linux-$(go env GOARCH).tar.gz | tar xzf - -C /usr/bin --strip-components=1 && \
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.dapper
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM opensuse/leap:15.3
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf && \
sed -i 's/download/provo-mirror/g' /etc/zypp/repos.d/*repo
RUN zypper ref

ARG DAPPER_HOST_ARCH
Expand Down
12 changes: 11 additions & 1 deletion Dockerfile.kvm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM opensuse/leap:15.3
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf
RUN sed -i -s 's/^# rpm.install.excludedocs/rpm.install.excludedocs/' /etc/zypp/zypp.conf && \
sed -i 's/download/provo-mirror/g' /etc/zypp/repos.d/*repo
RUN zypper ref
RUN zypper install -y socat net-tools-deprecated libtasn1-devel gnutls-devel libseccomp-devel json-glib-devel system-user-tss git
RUN zypper install -y autoconf
Expand All @@ -13,6 +14,7 @@ RUN zypper install -y gcc-c++
RUN zypper install -y make
RUN zypper install -y expect
RUN zypper install -y sudo
RUN zypper install -y bridge-utils
RUN cd /usr/src/libtpms && \
./autogen.sh --with-openssl --with-tpm2 && \
make -j4 && \
Expand All @@ -37,8 +39,16 @@ RUN cd /usr/src/ipxe/src && \
mkdir -p /usr/share/ipxe/ && \
cp bin/ipxe.iso /usr/share/ipxe/

RUN zypper in -y iproute2
RUN zypper in -y dnsmasq
RUN zypper in -y bind-utils

COPY scripts/qemu-in-container /usr/bin/
COPY scripts/startvm /usr/bin/
ENTRYPOINT ["/usr/bin/qemu-in-container"]

RUN chmod +s /usr/lib/qemu-bridge-helper
RUN echo 'allow all' > /etc/qemu/bridge.conf

VOLUME /tmp/emulated_tpm
VOLUME /image
19 changes: 19 additions & 0 deletions framework/files/system/oem/05_network.yaml
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
4 changes: 0 additions & 4 deletions framework/files/system/oem/05_rancheros-installer.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: "RancherOS Installer"
stages:
initramfs:
- commands:
- |
source /usr/lib/rancheros-release
echo COS_IMAGE=${IMAGE} > /etc/cos-upgrade-image
- if: '[ -f /run/cos/live_mode ]'
files:
- path: /etc/motd
Expand Down
5 changes: 4 additions & 1 deletion scripts/package
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ set url ${RELEASE_URL}/\${version}
set kernel rancheros-${PXE_ASSET_VERSION}-kernel
set initrd rancheros-${PXE_ASSET_VERSION}-initrd
set rootfs rancheros-${PXE_ASSET_VERSION}.squashfs
kernel \${url}/\${kernel} initrd=\${initrd} ip=dhcp rd.cos.disable root=live:\${url}/\${rootfs} rancheros.install.automatic=true rancheros.install.config_url=\${config} console=tty1 console=ttyS0 \${cmdline}
set iso rancheros-${PXE_ASSET_VERSION}.iso
# set config http://example.com/machine-config
# set cmdline extra.values=1
kernel \${url}/\${kernel} initrd=\${initrd} ip=dhcp rd.cos.disable root=live:\${url}/\${rootfs} rancheros.install.automatic=true rancheros.install.iso_url=\${url}/\${iso} rancheros.install.config_url=\${config} console=tty1 console=ttyS0 \${cmdline}
initrd \${url}/\${initrd}
boot
EOF
Expand Down
199 changes: 199 additions & 0 deletions scripts/startvm
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

0 comments on commit c35d85d

Please sign in to comment.