Add repo guidelines and verify script
This commit is contained in:
parent
dba2f327f5
commit
5b1de19cf5
3 changed files with 129 additions and 9 deletions
48
run.sh
48
run.sh
|
|
@ -40,6 +40,24 @@ log "allocated guest ip: $GUEST_IP"
|
|||
|
||||
sudo -v
|
||||
|
||||
FC_USE_SUDO="${FC_USE_SUDO:-1}"
|
||||
FC_RUN=("$FC_BIN")
|
||||
CURL_CMD=(curl)
|
||||
if [[ "$FC_USE_SUDO" == "1" ]]; then
|
||||
log "running firecracker with sudo (FC_USE_SUDO=1)"
|
||||
FC_RUN=(sudo -E "$FC_BIN")
|
||||
CURL_CMD=(sudo -E curl)
|
||||
fi
|
||||
|
||||
if command -v setcap >/dev/null 2>&1; then
|
||||
if ! getcap "$FC_BIN" 2>/dev/null | rg -q "cap_net_admin"; then
|
||||
log "granting cap_net_admin to firecracker binary"
|
||||
sudo setcap cap_net_admin+ep "$FC_BIN"
|
||||
fi
|
||||
else
|
||||
log "setcap not available; firecracker may need root to open TAP"
|
||||
fi
|
||||
|
||||
# Host bridge
|
||||
if ! ip link show "$BR_DEV" >/dev/null 2>&1; then
|
||||
log "creating host bridge $BR_DEV ($BR_IP/$CIDR)"
|
||||
|
|
@ -48,20 +66,24 @@ if ! ip link show "$BR_DEV" >/dev/null 2>&1; then
|
|||
sudo ip link set "$BR_DEV" up
|
||||
else
|
||||
log "host bridge $BR_DEV already exists"
|
||||
# Ensure existing bridge is up in case it was left down.
|
||||
sudo ip link set "$BR_DEV" up
|
||||
fi
|
||||
|
||||
# Per-VM TAP
|
||||
log "creating tap device $TAP_DEV"
|
||||
sudo ip tuntap add dev "$TAP_DEV" mode tap
|
||||
TAP_USER="${SUDO_UID:-$(id -u)}"
|
||||
TAP_GROUP="${SUDO_GID:-$(id -g)}"
|
||||
sudo ip tuntap add dev "$TAP_DEV" mode tap user "$TAP_USER" group "$TAP_GROUP"
|
||||
sudo ip link set "$TAP_DEV" master "$BR_DEV"
|
||||
sudo ip link set "$TAP_DEV" up
|
||||
sudo ip link set "$BR_DEV" up
|
||||
|
||||
# Start Firecracker
|
||||
log "starting firecracker process"
|
||||
rm -f "$API_SOCK"
|
||||
nohup "$FC_BIN" --api-sock "$API_SOCK" >"$LOG_FILE" 2>&1 &
|
||||
nohup "${FC_RUN[@]}" --api-sock "$API_SOCK" >"$LOG_FILE" 2>&1 &
|
||||
FC_PID="$!"
|
||||
echo "$FC_PID" > "$VM_DIR/pid"
|
||||
log "firecracker pid: $FC_PID"
|
||||
|
||||
# Wait for API socket
|
||||
|
|
@ -73,9 +95,18 @@ done
|
|||
[[ -S "$API_SOCK" ]] || { log "firecracker api socket not ready"; exit 1; }
|
||||
log "api socket ready"
|
||||
|
||||
if [[ "$FC_USE_SUDO" == "1" ]]; then
|
||||
SUDO_CHILD_PID="$(pgrep -n -f "$API_SOCK" || true)"
|
||||
if [[ -n "$SUDO_CHILD_PID" ]]; then
|
||||
FC_PID="$SUDO_CHILD_PID"
|
||||
log "firecracker child pid: $FC_PID"
|
||||
fi
|
||||
fi
|
||||
echo "$FC_PID" > "$VM_DIR/pid"
|
||||
|
||||
# Machine config
|
||||
log "configuring machine"
|
||||
curl --unix-socket "$API_SOCK" -X PUT http://localhost/machine-config \
|
||||
"${CURL_CMD[@]}" --unix-socket "$API_SOCK" -X PUT http://localhost/machine-config \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"vcpu_count": 2,
|
||||
|
|
@ -87,7 +118,7 @@ curl --unix-socket "$API_SOCK" -X PUT http://localhost/machine-config \
|
|||
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"
|
||||
|
||||
curl --unix-socket "$API_SOCK" -X PUT http://localhost/boot-source \
|
||||
"${CURL_CMD[@]}" --unix-socket "$API_SOCK" -X PUT http://localhost/boot-source \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"kernel_image_path\": \"$KERNEL\",
|
||||
|
|
@ -96,7 +127,7 @@ curl --unix-socket "$API_SOCK" -X PUT http://localhost/boot-source \
|
|||
|
||||
# Root filesystem
|
||||
log "attaching root filesystem"
|
||||
curl --unix-socket "$API_SOCK" -X PUT http://localhost/drives/rootfs \
|
||||
"${CURL_CMD[@]}" --unix-socket "$API_SOCK" -X PUT http://localhost/drives/rootfs \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"drive_id\": \"rootfs\",
|
||||
|
|
@ -107,7 +138,7 @@ curl --unix-socket "$API_SOCK" -X PUT http://localhost/drives/rootfs \
|
|||
|
||||
# Network interface
|
||||
log "configuring network interface"
|
||||
curl --unix-socket "$API_SOCK" -X PUT http://localhost/network-interfaces/eth0 \
|
||||
"${CURL_CMD[@]}" --unix-socket "$API_SOCK" -X PUT http://localhost/network-interfaces/eth0 \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"iface_id\": \"eth0\",
|
||||
|
|
@ -116,7 +147,7 @@ curl --unix-socket "$API_SOCK" -X PUT http://localhost/network-interfaces/eth0 \
|
|||
|
||||
# Start VM
|
||||
log "starting virtual machine"
|
||||
curl --unix-socket "$API_SOCK" -X PUT http://localhost/actions \
|
||||
"${CURL_CMD[@]}" --unix-socket "$API_SOCK" -X PUT http://localhost/actions \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{ "action_type": "InstanceStart" }' >/dev/null
|
||||
|
||||
|
|
@ -133,4 +164,3 @@ log "vm started successfully"
|
|||
log "guest ip: $GUEST_IP"
|
||||
log "ssh: ssh -i \"$SSH_KEY\" root@$GUEST_IP"
|
||||
log "logs: $LOG_FILE"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue