Add per-VM NAT and DNS config

This commit is contained in:
Thales Maciel 2026-01-29 16:22:28 -03:00
parent 60b1865ece
commit 68cf5f2cbb
No known key found for this signature in database
GPG key ID: 33112E6833C34679
3 changed files with 165 additions and 4 deletions

23
run.sh
View file

@ -42,6 +42,7 @@ MAX_VCPU=16
MIN_RAM=256
MAX_RAM=32768
MAX_DISK_BYTES=$((128 * 1024 * 1024 * 1024))
DNS_SERVERS="${DNS_SERVERS:-1.1.1.1}"
VCPU_COUNT="$DEFAULT_VCPU"
RAM_MIB="$DEFAULT_RAM"
@ -192,7 +193,7 @@ sudo -v
VM_STARTED=0
CLEANUP_ON_EXIT=0
KEEP_VM_DIR_ON_FAIL=1
DISK_PATH="$ROOTFS"
DISK_PATH="$VM_DIR/rootfs.ext4"
cleanup() {
local exit_code=$?
@ -241,13 +242,13 @@ else
log "setcap not available; firecracker may need root to open TAP"
fi
cp --reflink=auto "$ROOTFS" "$DISK_PATH"
if [[ -n "$DISK_BYTES" ]]; then
if ! command -v resize2fs >/dev/null 2>&1; then
log "resize2fs required for --disk-size"
exit 1
fi
DISK_PATH="$VM_DIR/rootfs.ext4"
cp --reflink=auto "$ROOTFS" "$DISK_PATH"
BASE_BYTES="$(stat -c%s "$ROOTFS")"
if (( DISK_BYTES < BASE_BYTES )); then
log "disk-size must be >= base image size"
@ -260,6 +261,20 @@ if [[ -n "$DISK_BYTES" ]]; then
fi
fi
if ! command -v debugfs >/dev/null 2>&1; then
log "debugfs required to set resolv.conf"
exit 1
fi
RESOLV_TMP="$VM_DIR/resolv.conf"
printf '' >"$RESOLV_TMP"
for ns in ${DNS_SERVERS//,/ }; do
printf 'nameserver %s\n' "$ns" >>"$RESOLV_TMP"
done
debugfs -w -R "write $RESOLV_TMP /etc/resolv.conf" "$DISK_PATH" >/dev/null 2>&1 || {
log "failed to write /etc/resolv.conf into rootfs"
exit 1
}
# Host bridge
if ! ip link show "$BR_DEV" >/dev/null 2>&1; then
log "creating host bridge $BR_DEV ($BR_IP/$CIDR)"
@ -325,7 +340,7 @@ log "configuring machine"
# Boot source
log "configuring boot source"
KCMD="console=ttyS0 reboot=k panic=1 pci=off root=/dev/vda rw ip=${GUEST_IP}::${BR_IP}:255.255.255.0::eth0:off"
KCMD="console=ttyS0 reboot=k panic=1 pci=off root=/dev/vda rw ip=${GUEST_IP}::${BR_IP}:255.255.255.0::eth0:off hostname=${VM_NAME}"
"${CURL_CMD[@]}" --unix-socket "$API_SOCK" -X PUT http://localhost/boot-source \
-H "Content-Type: application/json" \