Initial commit
All checks were successful
Build package / build-package (push) Successful in 30s

This commit is contained in:
2024-12-16 20:28:52 +08:00
commit d6c5a20053
9 changed files with 229 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
name: Build package
on: [push]
jobs:
build-package:
runs-on: imgbuilder
container:
image: gitea.konchin.com/image/archmakepkg
credentials:
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
options: --dns 192.168.68.254 --dns-search konchin.com --dns-option ndots:15
env:
REPO_NAME: custom
MINIO_BUCKET: archrepo
MINIO_ENDPOINT: https://minio.konchin.com
MINIO_ACCESSKEY: ${{ secrets.MINIO_ACCESSKEY }}
MINIO_SECRETKEY: ${{ secrets.MINIO_SECRETKEY }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build package
run: |
chown -R builder .
sudo -u builder makepkg -sc --needed --noconfirm
- name: Setup MinIO
run: |
mcli alias set m "${{ env.MINIO_ENDPOINT }}" \
"${{ env.MINIO_ACCESSKEY }}" "${{ env.MINIO_SECRETKEY }}"
echo "Set endpoint to ${{ env.MINIO_ENDPOINT }}"
- name: Copy repo db from MinIO
run: |
mkdir repo
mcli cp "m/${{ env.MINIO_BUCKET }}/${{ env.REPO_NAME }}.db" "repo/${{ env.REPO_NAME }}.db.tar.zst"
mcli cp "m/${{ env.MINIO_BUCKET }}/${{ env.REPO_NAME }}.files" "repo/${{ env.REPO_NAME }}.files.tar.zst"
echo "Copy ${{ env.REPO_NAME }}.db and ${{ env.REPO_NAME }}.files from MinIO"
- name: Add pkgs to repo db
run: |
repo-add "repo/${{ env.REPO_NAME }}.db.tar.zst" *.pkg.tar.zst
mv *.pkg.tar.zst repo
echo "Add $(ls *.pkg.tar.zst) to repo"
- name: Update repo to MinIO
run: |
mcli mv repo/${{ env.REPO_NAME }}.db.tar.zst "m/${{ env.MINIO_BUCKET }}/${{ env.REPO_NAME }}.db"
mcli mv repo/${{ env.REPO_NAME }}.files.tar.zst "m/${{ env.MINIO_BUCKET }}/${{ env.REPO_NAME }}.files"
mcli mv repo/*.pkg.tar.zst "m/${{ env.MINIO_BUCKET }}"
echo "Update ${{ env.REPO_NAME }}.db adn ${{ env.REPO_NAME }}.files to MinIO"

30
PKGBUILD Normal file
View File

@@ -0,0 +1,30 @@
# Maintainer: Yi-Ting Shih <ytshih@it.cs.nycu.edu.tw>
pkgname=vm
pkgver=1.0.0rc1
pkgrel=1
pkgdesc="A QEMU VM management tool"
arch=("x86_64")
url="https://gitea.konchin.com/package/vm"
license=("Apache")
makedepends=()
depends=('jsonnet' 'qemu-base')
provides=('vm')
source=()
package() {
cd "$srcdir" || exit 1
install -Dm755 share/startvm.sh share/stopvm.sh \
-t "$pkgdir/usr/share/$pkgname/"
install -Dm644 lib/qemu.libsonnet \
-t "$pkgdir/usr/lib/$pkgname/"
install -Dm755 vm "$pkgdir/usr/bin/vm"
install -Dm644 "${pkgname}@.service" \
"$pkgdir/var/lib/.config/systemd/user/${pkgname}@.service"
install -Dm644 "${pkgname}.sysusers" "$pkgdir/usr/lib/sysusers.d/${pkgname}.conf"
install -Dm755 -d "$pkgdir/var/lib/$pkgname/pflash"
install -Dm755 -d "$pkgdir/var/lib/$pkgname/iso"
install -Dm755 -d "$pkgdir/var/lib/$pkgname/img"
}

41
src/etc/config.jsonnet Normal file
View File

@@ -0,0 +1,41 @@
import 'qemu.libsonnet';
{
[obj.name]:
default(obj.name)
+ resource(obj.cpu, obj.ram)
+ net(obj.mac)
+ monitor(obj.mac)
for obj in [
{
name: 'k0scontroller' + i,
cpu: 2,
ram: '2G',
mac: '08:00:00:20:01:1' + i,
}
for i in std.range(1, 3)
] + [
{
name: 'k0sworker' + i,
cpu: 4,
ram: '4G',
mac: '08:00:00:20:01:2' + i,
}
for i in std.range(1, 3)
] + [
{
name: 'ostest',
cpu: 4,
ram: '8G',
mac: '08:00:00:20:01:73',
},
] + [
{
name: 'test' + i,
cpu: 1,
ram: '2G',
mac: '08:00:00:20:00:7' + i,
}
for i in std.range(1, 9)
]
}

37
src/lib/qemu.libsonnet Normal file
View File

@@ -0,0 +1,37 @@
local default(name) = [
'-enable-kvm',
'-cpu host',
'-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd',
'-drive if=pflash,format=raw,file=pflash/' + name + '.4m.fd',
'-drive file=img/' + name + '.qcow2,format=qcow2',
];
local resource(cpu, ram) = [
'-smp ' + cpu + ',sockets=1',
'-m ' + ram,
];
local monitor(mac) = [
'-monitor telnet:localhost:2' + std.split(mac, ':')[4] + std.split(mac, ':')[5] + ',server,nowait,nodelay',
];
local net(mac, area='test') = [
'-nic bridge,br=' + area + ',mac=' + mac,
];
local spice() = [
'-vga virtio',
'-device virtio-serial-pci',
'-spice port=5555,disable-ticketing=on',
'-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0',
'-chardev spicevmc,id=spicechannel0,name=vdagent',
];
local vnc(display=0) = [
'-vnc :' + display,
];
local iso(file) = [
'-boot d',
'-cdrom ' + file,
];

24
src/share/startvm.sh Normal file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env -S bash
CONFIG_FILE='/etc/vm/config.jsonnet'
LIB_DIR='/usr/lib/vm/'
vm="$1"
shift
settings=$(jsonnet "$CONFIG_FILE" -J "$LIB_DIR")
args=()
for i in $(seq 0 $(($(jq -r ".${vm} | length" <<< "$settings") - 1))); do
arg="$(jq -r ".${vm}[${i}]" <<< "$settings")"
args+=("${arg}")
done
while [[ "$#" -ge 1 ]]; do
args+=("$1")
shift
done
echo qemu-system-x86_64 ${args[*]}
exec qemu-system-x86_64 ${args[@]}

12
src/share/stopvm.sh Normal file
View File

@@ -0,0 +1,12 @@
#!/usr/bin/env -S bash
CONFIG_FILE='/etc/vm/config.jsonnet'
vm="$1"
shift
mapfile -t endPoint <<< "$(jsonnet "$CONFIG_FILE" | jq -r ".${vm}[]" | sed -nr 's/^-monitor telnet:(\w+):(\w+).*$/\1\n\2/p')"
nc "${endPoint[0]}" "${endPoint[1]}" <<'EOF'
system_powerdown
EOF

15
src/vm Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env -S bash
helpmsg(){
echo 'usage [image] [option]'
}
if [[ "$1" == "--help" ]]; then
helpmsg
exit 0
fi
image="$1"
option="$2"
systemctl --user -M vm@ "$option" "vm@$image"

1
src/vm.sysusers Normal file
View File

@@ -0,0 +1 @@
u vm - - /var/lib/vm

17
src/vm@.service Normal file
View File

@@ -0,0 +1,17 @@
[Unit]
Description=Virtual Machine %i
After=network.target
[Service]
WorkingDirectory=%h
ExecStart=/usr/share/vm/startvm.sh %i -nographic
ExecStop=/usr/share/vm/stopvm.sh %i
#ExecStop=/usr/bin/kill -INT $MAINPID
StandardError=journal
StandardOutput=null
StandardInput=null
Restart=on-failure
RestartSec=300s
[Install]
WantedBy=default.target