Compare commits

...

3 Commits

Author SHA1 Message Date
d7a5596068 Merge branch 'feature/freebsd_guest' into develop 2025-07-22 19:45:53 +02:00
c6c3bb3fe9 FreeBSD geust added Qemu test folder deleted. 2025-07-22 19:45:31 +02:00
f658307bf4 FreeBSD guest added 2025-07-22 19:42:01 +02:00
7 changed files with 118 additions and 30 deletions

View File

@ -73,9 +73,7 @@ A VM will be created with 4098 MB of RAM, 4 vCPUs and 100Gb of storage
#### FreeBSD VMs
__Please note that FreeBSD-based VMs do not currently support cloud-init, so once they are created, you need to connect to the machine through the console (using virt-manager or virs-console) and use the root user (without a password). Once inside, you add a password to root and I recommend that you add a non-root user and that the ssh connection is through that user.
Also note that since there is no cloud-init support, you will have to manually copy the generated ssh keys to the machine. I apologize for the inconvenience but I have not yet been able to make it work any other way.__
FreeBSD with cloud-init are now supported!! Just wait a little time to have the VM fully initialized (check it with virt-manager or serial connection manually)
## List VMs
```shell

View File

@ -12,6 +12,7 @@ check_host_os()
fi
}
show_vm_menu()
{
# Show dinamic menu
@ -42,6 +43,7 @@ show_vm_menu()
# Asignar variables
VM_OS_VARIANT=$(echo "$selected" | jq -r .variant)
VM_OS_TYPE=$(echo "$selected" | jq -r .os_type)
VM_BASE_IMAGE_URL=$(echo "$selected" | jq -r .url)
VM_BASE_IMAGE=$(echo "$selected" | jq -r .origin_image_name)
VM_BOOT_TYPE=$(echo "$selected" | jq -r .boot_type)
@ -52,10 +54,14 @@ compare_checksum()
{
CHECKSUM_TMP_FOLDER=$(mktemp)
curl -s -o "${CHECKSUM_TMP_FOLDER}" "${VM_CHECKSUMS_URL}"
VM_BASE_IMAGE_CHECKSUM=$(grep "$VM_BASE_IMAGE_NAME.${VM_BASE_IMAGE_EXTENSION}" "${CHECKSUM_TMP_FOLDER}" | awk '{print $1}')
if [[ "${VM_CHECKSUMS_URL}" == *"SHA256SUMS"* ]]; then
if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
VM_BASE_IMAGE_CHECKSUM=$(grep "FreeBSD-14.3-STABLE-amd64-BASIC-CLOUDINIT" "${CHECKSUM_TMP_FOLDER}" | grep "ufs.qcow2.xz" | awk '{print $4}')
else
VM_BASE_IMAGE_CHECKSUM=$(grep "$VM_BASE_IMAGE_NAME.${VM_BASE_IMAGE_EXTENSION}" "${CHECKSUM_TMP_FOLDER}" | awk '{print $1}')
fi
if [[ "${VM_CHECKSUMS_URL}" == *"SHA256"* ]]; then
HASH_CMD="sha256sum"
elif [[ "${VM_CHECKSUMS_URL}" == *"SHA512SUMS"* ]]; then
elif [[ "${VM_CHECKSUMS_URL}" == *"SHA512"* ]]; then
HASH_CMD="sha512sum"
else
echo "ERROR: Unknown checksum type in URL: $CHECKSUM_URL"
@ -185,29 +191,42 @@ vm_delete ()
}
vm_download_base_image()
{
VM_BASE_IMAGE_NAME=${VM_BASE_IMAGE%%.*}
VM_BASE_IMAGE_EXTENSION=${VM_BASE_IMAGE#*.}
if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
VM_BASE_IMAGE_NAME="${VM_OS_VARIANT}"
VM_BASE_IMAGE_EXTENSION="qcow2.xz"
else
VM_BASE_IMAGE_NAME=${VM_BASE_IMAGE%%.*}
VM_BASE_IMAGE_EXTENSION=${VM_BASE_IMAGE#*.}
fi
VM_BASE_IMAGE_LOCATION="${VM_BASE_DIR}/${VM_BASE_IMAGES}/${VM_BASE_IMAGE_NAME}.${VM_BASE_IMAGE_EXTENSION}"
if ! test -f "${VM_BASE_IMAGE_LOCATION}"; then
wget -O "${VM_BASE_IMAGE_LOCATION}" ${VM_BASE_IMAGE_URL}
fi
}
vm_create_guest_image()
{
echo "Creating a qcow2 image file ${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} that uses the cloud image file ${VM_BASE_IMAGE_LOCATION} as its base"
if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
if ! test -f "${VM_BASE_DIR}/images/${VM_HOSTNAME}.qcow"; then
xz -d ${VM_BASE_IMAGE_LOCATION}
fi
VM_BASE_IMAGE_EXTENSION="qcow2"
VM_BASE_IMAGE_LOCATION="${VM_BASE_DIR}/${VM_BASE_IMAGES}/${VM_BASE_IMAGE_NAME}.${VM_BASE_IMAGE_EXTENSION}"
fi
if ! test -f "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}"; then
qemu-img convert \
-O qcow2 \
"${VM_BASE_IMAGE_LOCATION}" \
"${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}"
qemu-img resize \
"${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}" \
"${VM_DISK_SIZE}G"
sudo chown -R $USER:libvirt-qemu "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}"
qemu-img convert \
-O qcow2 \
"${VM_BASE_IMAGE_LOCATION}" \
"${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}"
qemu-img resize \
"${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}" \
"${VM_DISK_SIZE}G"
sudo chown -R $USER:libvirt-qemu "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}"
else
echo "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} already exists. Delete VM with "delete" option"
exit 1
echo "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} already exists. Delete VM with "delete" option"
exit 1
fi
}
@ -221,10 +240,37 @@ vm_generate_ssh_hey()
#rm "${VM_BASE_DIR}/ssh/${VM_HOSTNAME}".pub.txt
}
vm_gen_linux_user_data()
vm_gen_user_data()
{
VM_USER_PASS=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8; echo)
VM_USER_PASS_HASH=$(mkpasswd --method=SHA-512 --rounds=4096 ${VM_USER_PASS})
#FREEBSD GUEST
if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
VM_ROOT_PASS=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8; echo)
VM_ROOT_PASS_HASH=$(mkpasswd --method=SHA-512 --rounds=4096 ${VM_ROOT_PASS})
cat <<EOF > "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
#cloud-config
hostname: ${VM_HOSTNAME}
users:
- name: root
lock_passwd: false
hashed_passwd: ${VM_ROOT_PASS}
ssh_pwauth: false
- name: ${VM_USERNAME}
ssh_authorized_keys:
- ${SSH_PUB_KEY}.
hashed_passwd: ${VM_USER_PASS}
groups: wheel
ssh_pwauth: true
write_files:
- path: /usr/local/etc/sudoers
content: |
%wheel ALL=(ALL) NOPASSWD: ALL
append: true
EOF
#LINUX GUEST
else
cat <<EOF > "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
#cloud-config
hostname: ${VM_HOSTNAME}
@ -240,16 +286,29 @@ users:
ssh_authorized_keys:
- ${SSH_PUB_KEY}
EOF
fi
}
vm_gen_user_data()
{
VM_USER_PASS=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8; echo)
VM_USER_PASS_HASH=$(mkpasswd --method=SHA-512 --rounds=4096 ${VM_USER_PASS})
cp files/user-data "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
sed -i "s|__SSH_KEY__|${SSH_PUB_KEY}|g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
sed -i "s| __USER_PASSWORD__|${VM_USER_PASS_HASH}|g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
}
# vm_gen_user_data()
# {
# VM_USER_DATA_FILE=files/user-data
# VM_USER_PASS=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8; echo)
# VM_USER_PASS_HASH=$(mkpasswd --method=SHA-512 --rounds=4096 ${VM_USER_PASS})
# #FREEBSD GUEST
# if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
# VM_ROOT_PASS=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8; echo)
# VM_ROOT_PASS_HASH=$(mkpasswd --method=SHA-512 --rounds=4096 ${VM_ROOT_PASS})
# VM_USER_DATA_FILE="files/freebsd-user-data"
# fi
# cp ${VM_USER_DATA_FILE} "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
# sed -i "s|__SSH_KEY__|${SSH_PUB_KEY}|g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
# sed -i "s| __USER_PASSWORD__|${VM_USER_PASS_HASH}|g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
# sed -i "s| __USER_NAME__|${VM_USERNAME}|g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
# if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
# sed -i "s| __ROOT_PASSWORD__|${VM_ROOT_PASS_HASH} |g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-user-data"
# fi
# }
vm_gen_meta_data()
{
@ -257,6 +316,15 @@ vm_gen_meta_data()
sed -i "s|__VMname__|${VM_HOSTNAME}|g" "$VM_BASE_DIR/init/${VM_HOSTNAME}-meta-data"
}
vm_set_guest_type()
{
if [[ "$VM_OS_TYPE" == "freebsd" ]]; then
VM_OS_VARIANT=${GUEST_OS_TYPE_FREEBSD}
elif [[ "${VM_OS_VARIANT}" == *"debian13"* ]]; then
VM_OS_VARIANT=${GUEST_OS_TYPE_DEBIAN}
fi
}
vm_guest_install()
{
VM_INSTALL_OPTS=""

View File

@ -0,0 +1,3 @@
#!/bin/env bash
GUEST_OS_TYPE_DEBIAN="debian13"
GUEST_OS_TYPE_FREEBSD="freebsd14.2"

View File

@ -1,2 +1,3 @@
#!/bin/env bash
GUEST_OS_TYPE_DEBIAN="debian11"
GUEST_OS_TYPE_DEBIAN="debian11"
GUEST_OS_TYPE_FREEBSD="freebsd13.1"

View File

@ -3,7 +3,8 @@
{
"id": 1,
"name": "Debian 12",
"variant": "$GUEST_OS_TYPE_DEBIAN",
"variant": "debian13",
"os_type": "GNULinux",
"url": "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2",
"origin_image_name": "debian-12-generic-amd64.qcow2",
"md5sum": "https://cdimage.debian.org/images/cloud/bookworm/latest/SHA512SUMS",
@ -13,6 +14,7 @@
"id": 2,
"name": "Ubuntu 20.04 server",
"variant": "ubuntu20.04",
"os_type": "GNULinux",
"url": "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img",
"origin_image_name": "focal-server-cloudimg-amd64.img",
"md5sum": "https://cloud-images.ubuntu.com/focal/current/SHA256SUMS",
@ -23,6 +25,7 @@
"id": 3,
"name": "Ubuntu 20.04 minimal",
"variant": "ubuntu20.04",
"os_type": "GNULinux",
"url": "https://cloud-images.ubuntu.com/minimal/releases/focal/release/ubuntu-20.04-minimal-cloudimg-amd64.img",
"origin_image_name": "ubuntu-20.04-minimal-cloudimg-amd64.img",
"md5sum": "https://cloud-images.ubuntu.com/minimal/releases/focal/release/SHA256SUMS",
@ -32,6 +35,7 @@
"id": 4,
"name": "Ubuntu 22.04 server",
"variant": "ubuntu22.04",
"os_type": "GNULinux",
"url": "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img",
"origin_image_name": "jammy-server-cloudimg-amd64.img",
"md5sum": "https://cloud-images.ubuntu.com/jammy/current/SHA256SUMS",
@ -41,6 +45,7 @@
"id": 5,
"name": "Ubuntu 22.04 minimal",
"variant": "ubuntu22.04",
"os_type": "GNULinux",
"url": "https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img",
"origin_image_name": "ubuntu-22.04-minimal-cloudimg-amd64.img",
"md5sum": "https://cloud-images.ubuntu.com/minimal/releases/jammy/release/SHA256SUMS",
@ -50,6 +55,7 @@
"id": 6,
"name": "Ubuntu 24.04 server",
"variant": "ubuntu22.04",
"os_type": "GNULinux",
"url": "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img",
"origin_image_name": "noble-server-cloudimg-amd64.img",
"md5sum": "https://cloud-images.ubuntu.com/noble/current/SHA256SUMS",
@ -58,11 +64,21 @@
{
"id": 7,
"name": "Ubuntu 24.04 minimal",
"os_type": "GNULinux",
"variant": "ubuntu22.04",
"url": "https://cloud-images.ubuntu.com/minimal/releases/noble/release/ubuntu-24.04-minimal-cloudimg-amd64.img",
"origin_image_name": "ubuntu-24.04-minimal-cloudimg-amd64.img",
"md5sum": "https://cloud-images.ubuntu.com/minimal/releases/noble/release/SHA256SUMS",
"boot_type": "uefi"
} ,
{
"id": 8,
"name": "FreeBSD 14.3",
"os_type": "freebsd",
"variant": "freebsd14.2",
"url": "https://download.freebsd.org/ftp/snapshots/VM-IMAGES/14.3-STABLE/amd64/Latest/FreeBSD-14.3-STABLE-amd64-BASIC-CLOUDINIT-ufs.qcow2.xz",
"origin_image_name": "FreeBSD-14.3-STABLE-amd64-BASIC-CLOUDINIT-ufs.qcow2.xz",
"md5sum": "https://download.freebsd.org/ftp/snapshots/VM-IMAGES/14.3-STABLE/amd64/Latest/CHECKSUM.SHA512"
}
]
}

View File

@ -95,6 +95,8 @@ case "${ACTION}" in
#Read os_options.json and generate guests menu
#Select guest
show_vm_menu
#Set guest type based on check_host_os
vm_set_guest_type
#Download cloud image
vm_download_base_image
#Compare hashes
@ -106,7 +108,7 @@ case "${ACTION}" in
#Generate meta-data file for VM
vm_gen_meta_data
#Generate user-data file for VM
vm_gen_linux_user_data
vm_gen_user_data
#Install VM
vm_guest_install
;;

0
vm_set_ip.sh Normal file
View File