Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions slemicro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,13 @@
<!-- vscode-markdown-toc -->

- [create_vm_with_eib.sh](#create_vm_with_eib.sh)
- [Prerequisites](#Prerequisites)
- [Enviroment variables](#Enviromentvariables)
- [Usage](#Usage)
- [create_vm_with_image.sh](#create_vm_with_image.sh)
- [Prerequisites](#Prerequisites-1)
- [Enviroment variables](#Enviromentvariables-1)
- [Usage](#Usage-1)
- [create_eib.sh](#create_eib.sh)
- [Prerequisites](#Prerequisites-1)
- [Enviroment variables](#Enviromentvariables-1)
- [Usage](#Usage-1)
- [create_empty_vm.sh](#create_empty_vm.sh)
- [delete_vm.sh](#delete_vm.sh)
- [Prerequisites](#Prerequisites-1)
- [Enviroment variables](#Enviromentvariables-1)
- [Usage](#Usage-1)
- [get_kubeconfig.sh](#get_kubeconfig.sh)
- [Prerequisites](#Prerequisites-1)
- [Enviroment variables](#Enviromentvariables-1)
- [Usage](#Usage-1)
- [get_ip.sh](#get_ip.sh)
- [Prerequisites](#Prerequisites-1)
- [Enviroment variables](#Enviromentvariables-1)
- [Usage](#Usage-1)
- [getvmsip.sh](#getvmsip.sh)
- [Prerequisites](#Prerequisites-1)
- [Usage](#Usage-1)
- [What's next?](#Whatsnext)

<!-- vscode-markdown-toc-config
Expand Down Expand Up @@ -289,6 +270,40 @@ Kernel Params ................ [SKIPPED]
Build complete, the image can be found at: 32-mgmt-cluster.raw
```

## <a name='create_empty_vm.sh'></a>create_empty_vm.sh

This script just creates an empty VM. Then it can be used with Metal3.

### <a name='Prerequisites-1'></a>Prerequisites

- `qemu-img`

NOTE: They can be installed using `brew`.

### <a name='Enviromentvariables-1'></a>Enviroment variables

It requires a few enviroment variables to be set to customize it, the bare minimum is just:

```
# Folder where the VM will be hosted
VMFOLDER="${HOME}/VMs"
MACADDRESS="00:00:00:00:00:00"
```

The rest of them can be observed in the script itself.

The variables can be stored in the script basedir as `.env` or in any file and use the `-f path/to/the/variables` flag.

**NOTE**: There is a `vm*` pattern already in the `.gitignore` file so you can conviniently name your VM parameters file as `vm-foobar` and they won't be added to git.

### <a name='Usage-1'></a>Usage

For a simple example like:

```bash
$ ./create_empty_vm.sh -f vm-slmicro62-eib -s 40
```

## <a name='delete_vm.sh'></a>delete_vm.sh

This script is intended to easily delete the previously SLE Micro VM created with the `create_vm.sh` script.
Expand Down
90 changes: 55 additions & 35 deletions slemicro/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ check_os(){
fi
}

create_empty_image_file(){
SIZE="${1:-20}"
mkdir -p ${VMFOLDER}
qemu-img create -f qcow2 ${VMFOLDER}/${VMNAME}.qcow2 ${SIZE}G
}

create_image_file(){
# Create the image file
mkdir -p ${VMFOLDER}
Expand All @@ -38,6 +44,7 @@ create_extra_disks(){
}

create_vm(){
POWERON="${1:-true}"
if [ $(uname -o) == "Darwin" ]; then
# See if there are extra disks to be created
UTMEXTRADISKS=""
Expand All @@ -56,38 +63,47 @@ create_vm(){
UTMDISKMAPPING="{removable:false, source:rootfs}${UTMEXTRADISKMAPPING}}"

# Create the VM
OUTPUT=$(osascript <<-END
VMCREATION=$(osascript <<-END
tell application "UTM"
-- specify the RAW file
set rootfs to POSIX file "${VMFOLDER}/${VMNAME}.qcow2"
-- specify extra disks
${UTMEXTRADISKS}
--- create a new QEMU VM
set vm to make new virtual machine with properties {backend:qemu, configuration:{cpu cores:${CPUS}, memory: ${MEMORY}, name:"${VMNAME}", network interfaces:{{hardware:"virtio-net-pci", mode:shared, index:0, address:"${MACADDRESS}", port forwards:{}, host interface:""}}, architecture:"aarch64", drives:${UTMDISKMAPPING}}
start vm
repeat
if status of vm is started then exit repeat
end repeat
get address of first serial port of vm
end tell
END
)

echo "VM ${VMNAME} started. You can connect to the serial terminal as: screen ${OUTPUT}"
if [ ${POWERON} == "true" ]; then
# Create the VM
OUTPUT=$(osascript <<-END
tell application "UTM"
set vm virtual machine named "${VMNAME}"
start vm
repeat
if status of vm is started then exit repeat
end repeat
get address of first serial port of vm
end tell
END
)
echo "VM ${VMNAME} started. You can connect to the serial terminal as: screen ${OUTPUT}"

VMMAC=$(echo ${MACADDRESS} | sed 's/0\([0-9A-Fa-f]\)/\1/g')
timeout=180
count=0
echo -n "Waiting for IP: "
until grep -q -i "${VMMAC}" -B1 -m1 /var/db/dhcpd_leases | head -1 | awk -F= '{ print $2 }'; do
count=$((count + 1))
if [[ ${count} -ge ${timeout} ]]; then
break
fi
sleep 1
echo -n "."
done
VMIP=$(grep -i "${VMMAC}" -B1 -m1 /var/db/dhcpd_leases | head -1 | awk -F= '{ print $2 }')
VMMAC=$(echo ${MACADDRESS} | sed 's/0\([0-9A-Fa-f]\)/\1/g')
timeout=180
count=0
echo -n "Waiting for IP: "
until grep -q -i "${VMMAC}" -B1 -m1 /var/db/dhcpd_leases | head -1 | awk -F= '{ print $2 }'; do
count=$((count + 1))
if [[ ${count} -ge ${timeout} ]]; then
break
fi
sleep 1
echo -n "."
done
VMIP=$(grep -i "${VMMAC}" -B1 -m1 /var/db/dhcpd_leases | head -1 | awk -F= '{ print $2 }')
fi
elif [ $(uname -o) == "GNU/Linux" ]; then
# By default virt-install powers off the VM when rebooted once.
# As a workaround, create the VM definition, change the on_reboot behaviour
Expand All @@ -110,22 +126,26 @@ create_vm(){
--print-xml 1 > ${VIRTFILE}
sed -i -e 's#<on_reboot>destroy</on_reboot>#<on_reboot>restart</on_reboot>#g' ${VIRTFILE}
virsh define ${VIRTFILE}
virsh start ${VMNAME}
rm -f ${VIRTFILE}
echo "VM ${VMNAME} started. You can connect to the serial terminal as: virsh console ${VMNAME}"
echo -n "Waiting for IP..."
timeout=180
count=0
VMIP=""
while [ -z "${VMIP}" ]; do
sleep 1
count=$((count + 1))
if [[ ${count} -ge ${timeout} ]]; then
break
fi
echo -n "."
VMIP=$(vm_ip ${VMNAME})
done

if [ ${POWERON} == "true" ]; then
virsh start ${VMNAME}
echo "VM ${VMNAME} started. You can connect to the serial terminal as: virsh console ${VMNAME}"
echo -n "Waiting for IP..."
timeout=180
count=0
VMIP=""
while [ -z "${VMIP}" ]; do
sleep 1
count=$((count + 1))
if [[ ${count} -ge ${timeout} ]]; then
break
fi
echo -n "."
VMIP=$(vm_ip ${VMNAME})
done
fi

else
die "VM not deployed. Unsupported operating system" 2
fi
Expand Down
69 changes: 69 additions & 0 deletions slemicro/create_empty_vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
set -euo pipefail
BASEDIR="$(dirname "$0")"
source ${BASEDIR}/common.sh

usage(){
cat <<-EOF
Usage: ${0} [-f <path/to/variables/file>] [-s <size_in_GB>] [-n <vmname>]
EOF
}

ENVFILE=""
SIZE=""

while getopts 'f:s:n:h' OPTION; do
case "${OPTION}" in
f)
[ -f "${OPTARG}" ] && ENVFILE="${OPTARG}" || die "Parameters file ${OPTARG} not found" 2
;;
s)
SIZE="${OPTARG}"
;;
n)
NAMEOPTION="${OPTARG}"
;;
h)
usage && exit
;;
?)
usage && exit 2
;;
esac
done

[ -z "${SIZE}" ] && { usage && die "\"-s <size_in_GB>\" required" 2;}
[ -z "${ENVFILE}" ] && { usage && die "\"-f <path/to/variables/file>\" required" 2;}

set -a
# Get the env file
source ${ENVFILE:-${BASEDIR}/.env}
# Some defaults just in case
CPUS="${CPUS:-2}"
MEMORY="${MEMORY:-2048}"
VMNAME="${NAMEOPTION:-${VMNAME:-slemicro}}"
MACADDRESS="${MACADDRESS:-null}"
EXTRADISKS="${EXTRADISKS:-false}"
VM_NETWORK=${VM_NETWORK:-default}
LIBVIRT_DISK_SETTINGS="${LIBVIRT_DISK_SETTINGS:-bus=virtio}"
set +a

check_os

# Check if the commands required exist
command -v qemu-img > /dev/null 2>&1 || die "qemu-img not found" 2

# Check if the image file exist
[ -f ${VMFOLDER}/${VMNAME}.qcow2 ] && die "Image file ${VMFOLDER}/${VMNAME}.qcow2 already exists" 2

# Check if the MAC address has been set
[ "${MACADDRESS}" != "null" ] || die "MAC Address needs to be specified and it should match the one defined on EIB at \"<eibfolder>/network/${VMNAME}.yaml\"" 2

create_empty_image_file ${SIZE}

create_extra_disks

# Create the VM powered off
create_vm "off"

exit 0
17 changes: 12 additions & 5 deletions slemicro/delete_vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ elif [ $(uname -o) == "GNU/Linux" ]; then
[ "$(virsh domstate ${VMNAME} 2>/dev/null)" == "running" ] && virsh destroy ${VMNAME}
virsh undefine ${VMNAME} --nvram --remove-all-storage
else
die "${VMNAME} not found" 2
echo "${VMNAME} not found"
fi
else
die "Unsupported operating system" 2
fi

[ -f ${VMFOLDER}/${VMNAME}.raw ] && rm -f ${VMFOLDER}/${VMNAME}.raw
[ -f ${VMFOLDER}/${VMNAME}.qcow2 ] && rm -f ${VMFOLDER}/${VMNAME}.qcow2
[ "${EXTRADISKS}" != false ] && rm -f ${VMFOLDER}/${VMNAME}-extra-disk-*.raw
for ext in raw qcow2; do
if [ -f ${VMFOLDER}/${VMNAME}.${ext} ]; then
echo "Deleting ${VMFOLDER}/${VMNAME}.${ext}"
rm -f ${VMFOLDER}/${VMNAME}.${ext}
fi
done

if [ "${EXTRADISKS}" != false ]; then
rm -f ${VMFOLDER}/${VMNAME}-extra-disk-*.raw
fi

exit 0
exit 0
Loading