feat: add flake to create iso and create VMs
This commit is contained in:
78
nixos/proxmox/create-vm.sh
Executable file
78
nixos/proxmox/create-vm.sh
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
|
||||
|
||||
|
||||
function cleanup() {
|
||||
trap - SIGINT SIGTERM ERR EXIT
|
||||
echo "Cleaning Up"
|
||||
}
|
||||
trap cleanup SIGINT SIGTERM ERR EXIT
|
||||
|
||||
function main() {
|
||||
VMID="$(sudo pvesh get /cluster/nextid)"
|
||||
|
||||
# NODE="$(sudo pvesh get /nodes --output-format json | jq -r '.[].node' | gum choose --select-if-one --cursor="Select Node > ")"
|
||||
NODE="falcon"
|
||||
echo "Selected ${NODE}"
|
||||
|
||||
STORAGE="$(sudo pvesh get /nodes/${NODE}/storage --output-format json --human-readable true | jq -r '.[] | select((.enabled == 1) and (.content | contains("images"))).storage' | gum choose --select-if-one)"
|
||||
echo "Selected ${STORAGE} for storage"
|
||||
|
||||
EXISTING_NODES=$(sudo pvesh get /nodes/${NODE}/qemu --output-format json | jq -r "map(.name | select(startswith(\"premhome-${NODE}\")))")
|
||||
|
||||
printf "\nexisting Nodes: %s\n" "${EXISTING_NODES}"
|
||||
|
||||
DEFAULT_NAME=$(jq -r "map(capture(\"(?<base>.*-)(?<num>[0-9]+)$\") | .num |= (tonumber + 1) | \"\(.base)\(.num)\") | max" <<< "$EXISTING_NODES")
|
||||
|
||||
NAME="$(gum input --prompt="VM Name > " --value="${DEFAULT_NAME}")"
|
||||
echo "creating VM with id: ${VMID} on ${NODE} stored on ${STORAGE} with name: ${NAME}"
|
||||
sudo qm create "${VMID}" \
|
||||
--agent 1 \
|
||||
--bios "ovmf" \
|
||||
--boot "order=scsi0;ide2" \
|
||||
--cores 2 \
|
||||
--cpu "host" \
|
||||
--efidisk0 "${STORAGE}:1,efitype=4m" \
|
||||
--ide2 "local:iso/nixos-yadunut.iso,media=cdrom" \
|
||||
--machine q35 \
|
||||
--memory 2048 \
|
||||
--name "${NAME}" \
|
||||
--net0 "virtio,bridge=vmbr0" \
|
||||
--ostype "l26" \
|
||||
--scsi0 "${STORAGE}:50,iothread=on" \
|
||||
--scsihw "virtio-scsi-single"
|
||||
|
||||
echo "Created VM, starting..."
|
||||
sudo qm start "${VMID}"
|
||||
echo "Started VM ${VMID}"
|
||||
|
||||
echo "Attempting to retrieve IP Address"
|
||||
start_time=$(date +%s)
|
||||
while true; do
|
||||
if IP_ADDRESS="$(sudo qm agent "${VMID}" network-get-interfaces 2>/dev/null | jq -r '.[] | select(.name != "lo") | .["ip-addresses"][] | select(.["ip-address-type"] == "ipv4")')"; then
|
||||
if [ -n "${IP_ADDRESS}" ]; then
|
||||
echo "Retrieved IP Address: ${IP_ADDRESS}"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "Failed to retrieve IP, trying again"
|
||||
|
||||
current_time=$(date +%s)
|
||||
elapsed_time=$((current_time - start_time))
|
||||
|
||||
if [ $elapsed_time -ge 60 ]; then
|
||||
echo "1 minute has elapsed. Failing"
|
||||
cleanup
|
||||
break
|
||||
fi
|
||||
|
||||
sleep 3
|
||||
done
|
||||
# try to retrieve IP address
|
||||
}
|
||||
|
||||
main "$@"
|
||||
23
nixos/proxmox/iso.nix
Normal file
23
nixos/proxmox/iso.nix
Normal file
@@ -0,0 +1,23 @@
|
||||
{ config, lib, pkgs, meta, ...}:
|
||||
{
|
||||
imports = [./users.nix];
|
||||
|
||||
nix = {
|
||||
settings.experimental-features = ["nix-command" "flakes"];
|
||||
};
|
||||
|
||||
isoImage = {
|
||||
isoName = "${config.system.nixos.distroId}-yadunut.iso";
|
||||
};
|
||||
services.openssh.enable = true;
|
||||
|
||||
services.qemuGuest.enable = true;
|
||||
|
||||
environment.systemPackages = with pkgs; [
|
||||
git
|
||||
neovim
|
||||
wget
|
||||
];
|
||||
|
||||
system.stateVersion = "24.05";
|
||||
}
|
||||
8
nixos/proxmox/users.nix
Normal file
8
nixos/proxmox/users.nix
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
users.users.yadunut = {
|
||||
isNormalUser = true;
|
||||
extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
|
||||
openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJXOpmWsAnl2RtOuJJMRUx+iJTwf2RWJ1iS3FqXJFzFG" ];
|
||||
};
|
||||
security.sudo.wheelNeedsPassword = false;
|
||||
}
|
||||
Reference in New Issue
Block a user