Last active
March 2, 2024 15:29
-
-
Save mortn/95b21a10bbe3210202d30d29f60a9496 to your computer and use it in GitHub Desktop.
Systemd template unit for controlling Cloud-hypervisor guests
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
[Unit] | |
Description=Cloud-Hypervisor for %i | |
After=network.target | |
After=local-fs.target | |
After=remote-fs.target | |
After=systemd-logind.service | |
After=systemd-machined.service | |
Wants=network.target | |
[Service] | |
SyslogLevel=debug | |
Type=simple | |
LogsDirectory=ch | |
StateDirectory=ch | |
StandardOutput=append:/var/log/ch/%i.stdout | |
WorkingDirectory=/var/lib/ch | |
RuntimeDirectory=ch | |
RuntimeDirectoryMode=0775 | |
RuntimeDirectoryPreserve=yes | |
EnvironmentFile=/var/lib/ch/%i.env | |
ExecStartPre=/bin/bash -c 'for IF in ${CH_BRS};do ip l sh ${IF%:*} >/dev/null 2>&1 || (ip tuntap add ${IF%:*} mode tap && ip l s ${IF%:*} master ${IF#*:});done' | |
ExecStart=/bin/bash -c 'cloud-hypervisor --api-socket $RUNTIME_DIRECTORY/%i-sock ${CH_CONFIG}' | |
ExecStop=/bin/bash -c 'ch-remote --api-socket $RUNTIME_DIRECTORY/%i-sock shutdown-vmm' | |
ExecStop=/bin/bash -c 'for IF in ${CH_BRS};do ip l sh ${IF%:*} >/dev/null 2>&1 && ip l del ${IF%:*};done' | |
#ExecStop=/bin/bash -c 'rm -fv $RUNTIME_DIRECTORY/%i-sock' | |
ExecReload=/bin/bash -c 'ch-remote --api-socket $RUNTIME_DIRECTORY/%i-sock reboot' | |
[Install] | |
WantedBy=multi-user.target |
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
#!/bin/bash | |
set -e | |
#set -x | |
#[[ "$EUID" -ne 0 ]] && echo "Please run as root" && exit | |
# ./create-cloud-init.sh trax 44:c1 10.0.1.41/26 | |
usage(){ | |
echo "Usage: $0 [name] [mac] [ip] [gw] [nssearch] [nsaddr] " | |
} | |
[[ $# -lt 3 ]] && (usage && exit) | |
[[ $1 == *.* ]] && (echo "No dots in arg1" && usage && exit) | |
_dir="/var/lib/ch" | |
vm="$1" | |
c_init="${_dir}/${vm}-init.img" | |
#set -x | |
bridge="brvirt" | |
[[ $2 == *:* ]] && mac="22:22:22:22:$2" || mac="22:22:22:22:aa:a1" | |
[[ $3 == *.*.*.* ]] && ip="$3" || ip="10.0.1.60/26" | |
[[ $4 == *.*.*.* ]] && gw="$4" || gw="10.0.1.1" | |
[[ $5 == *.* ]] && nssearch="$5" || nssearch="h3m,h3m.li" | |
[[ $6 == *.*.*.* ]] && nsaddr="$6" || nsaddr="10.0.1.5" | |
gen_nw_cfg(){ | |
nw_cfg="version: 2 | |
ethernets: | |
ens2: | |
match: | |
macaddress: "$1" | |
addresses: ["$2"] | |
nameservers: | |
search: ["$4"] | |
addresses: ["$5"] | |
routes: | |
- to: default | |
via: "$3" | |
" | |
printf "${nw_cfg}" > network-config | |
} | |
gen_nw_cfg $mac $ip $gw $nssearch $nsaddr | |
printf "CH_CONFIG=--kernel ./hypervisor-fw \ | |
--cpus boot=2 \ | |
--memory size=2G,shared=on \ | |
--disk path=$vm.raw --disk path=${c_init##*/} \ | |
--net tap=${vm},mac=${mac} \ | |
--serial tty --console off \ | |
--log-file /var/log/ch/${vm}.log -v | |
CH_BRS="${vm}:${bridge}" | |
" > "${_dir}/${vm}.env" | |
printf "instance-id: $vm \nlocal-hostname: $vm\n" > meta-data | |
## cloud-localds -v -H $vm -N network-config.${vm} $c_init user-data.${vm} | |
[[ -f "${c_init}" ]] && sudo rm -vf "${c_init}" | |
#mkdosfs -n CIDATA -C "${c_init}" 4096 > /dev/null | |
mkdosfs -n CIDATA -C "${c_init}" 64 > /dev/null | |
mcopy -oi "${c_init}" -s user-data :: | |
mcopy -oi "${c_init}" -s meta-data :: | |
mcopy -oi "${c_init}" -s network-config :: | |
cat meta-data network-config | |
rm -f meta-data network-config |
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
CH_CONFIG=--kernel ./hypervisor-fw --cpus boot=2 --memory size=8G,shared=on --disk path=px7.raw --net tap=px7,mac=22:22:22:14:fa:a0 --serial tty --console off --log-file /var/log/ch/px7.log -v | |
CH_BRS=px7:brvirt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One idea I had wanted to approach was using a generator, although it seems too complex. As you can see (if you click through to the github link), there is alot of stuff going on when I launch virtual machines for using
VFIO
.