diff --git a/README.md b/README.md index 3f54261..040cb7b 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,24 @@ For OpenBSD images with cloud-init support, this project uses: [hcartiaux's open 1. Configure the [variables](env_scripts/common.sh) file (`env_scripts/common.sh`). It is recommended to place this directory in your home folder to avoid libvirt permission issues. -2. Run the installation script: install.sh +2. Run the installation script: `install.sh` + +## Networking + +Two networks are installed when you run `install.sh`: + +| Name | Type |DCHP Range |Default route |Host device | +| ----- | -------- |-------------|----------------|--------------| +| vmnetwork | NAT |192.168.100.100 - 254| 192.168.100.1| virb1| +| host-only | Isolated Network |-|-| -| + +**Table 1:** Default Available Networks + +You can network names changing on [env_scripts/common.sh](env_scripts/common.sh) +``` +VM_NETWORK_HOSTONLY="host-only" +VM_USERNAME="user" +``` ### AppArmor exception (if needed) diff --git a/env_scripts/common.sh b/env_scripts/common.sh index 28d57b1..51a701d 100644 --- a/env_scripts/common.sh +++ b/env_scripts/common.sh @@ -1,19 +1,22 @@ #!/bin/env bash DISTRO="" -LIBVIRT_NET_MODEL="virtio" -LIBVIRT_NET_OPTION="network=$VM_NETWORK,model=$LIBVIRT_NET_MODEL" -OS_JSON_FILE="files/os_options.json" -OS_JSON_FILE_INSTALL="files/software.json" + #VM_BASE_DIR=${VM_BASE_DIR:-"${HOME}/.local/share/libvirt"} #VM_BASE_DIR=${VM_BASE_DIR:-"${HOME}/var/lib/libvirt"} VM_BASE_DIR="${HOME}/vms" VM_BASE_IMAGES="base" VM_DISK_EXTENSION="img" +VM_NETWORK_NAT="vmnetwork" +VM_NETWORK_HOSTONLY="host-only" +VM_NETWORK_TYPE="nat" VM_USERNAME="user" VM_SOFT=""; - VM_IMAGE_PATH="${VM_BASE_DIR}/images/$1.img" CI_IMAGE_PATH="${VM_BASE_DIR}/images/$1-cidata.iso" -VM_NETWORK="vmnetwork" -REPO_BRANCH="main" -REPO_SOURCE="https://raw.githubusercontent.com/vgenguita/kvm-cloudimage/refs/heads/${REPO_BRANCH}/env_scripts/" +LIBVIRT_NET_MODEL="virtio" +LIBVIRT_NET_OPTION="network=${VM_NETWORK_NAT},model=${LIBVIRT_NET_MODEL}" +OS_JSON_FILE="files/os_options.json" +OS_JSON_FILE_INSTALL="files/software.json" + +# REPO_BRANCH="main" +# REPO_SOURCE="https://raw.githubusercontent.com/vgenguita/kvm-cloudimage/refs/heads/${REPO_BRANCH}/env_scripts/" diff --git a/env_scripts/functions.sh b/env_scripts/functions.sh index 236e933..9a6fc85 100644 --- a/env_scripts/functions.sh +++ b/env_scripts/functions.sh @@ -293,10 +293,12 @@ vm_net_bridge_set_ip() ssh -i ${VM_BASE_DIR}/ssh/${VM} -l${VM_USERNAME} ${CURRENT_IP} "bash -s" -- < ../vm_example_scripts/apply_netplan.sh } -vm_net_set_bridge_mode() +vm_net_set_network_type() { - if [[ -n $VM_BRIDGE_INT ]]; then - LIBVIRT_NET_OPTION="model=virtio,bridge=${VM_BRIDGE_INT}" + if [[ "${VM_NETWORK_TYPE}" == "isolated" ]]; then + LIBVIRT_NET_OPTION="network=${VM_NETWORK_HOST_ONLY},model=${LIBVIRT_NET_MODEL}" + elif [[ "${VM_NETWORK_TYPE}" == "bridge" ]]; then + LIBVIRT_NET_OPTION="model=virtio,bridge=${VM_BRIDGE_INT}" fi } ## Connect to an existent VM using ssh diff --git a/files/network-host-only.xml b/files/network-host-only.xml new file mode 100644 index 0000000..8c47861 --- /dev/null +++ b/files/network-host-only.xml @@ -0,0 +1,5 @@ + + YOURNAME + + + diff --git a/files/network.xml b/files/network-nat.xml similarity index 100% rename from files/network.xml rename to files/network-nat.xml diff --git a/install.sh b/install.sh index c6b5dba..2f3671c 100755 --- a/install.sh +++ b/install.sh @@ -29,9 +29,16 @@ sudo systemctl enable libvirtd sudo systemctl start libvirtd mkdir -p "${VM_BASE_DIR}"/{images,xml,init,base,ssh} -cp files/network.xml ${VM_BASE_DIR}/xml/network.xml -sed -i "s/YOURNETWORK/${VM_NETWORK}/g" ${VM_BASE_DIR}/xml/network.xml -virsh net-define ${VM_BASE_DIR}/xml/network.xml -virsh net-autostart ${VM_NETWORK} -virsh net-start ${VM_NETWORK} +#Isolated network +cp files/network-host-only.xml ${VM_BASE_DIR}/xml/network-host-only.xml +sed -i "s/YOURNETWORK/${VM_NETWORK_NAT}/g" ${VM_BASE_DIR}/xml/network-host-only.xml +virsh net-define ${VM_BASE_DIR}/xml/network-host-only.xml +virsh net-autostart ${VM_NETWORK_HOSTONLY} +virsh net-start ${VM_NETWORK_HOSTONLY} +#NAT +cp files/network-nat.xml ${VM_BASE_DIR}/xml/network-nat.xml +sed -i "s/YOURNETWORK/${VM_NETWORK_NAT}/g" ${VM_BASE_DIR}/xml/network-nat.xml +virsh net-define ${VM_BASE_DIR}/xml/network-nat.xml +virsh net-autostart ${VM_NETWORK_NAT} +virsh net-start ${VM_NETWORK_NAT} newgrp libvirt diff --git a/vm_manage.sh b/vm_manage.sh index 9a3bf91..208e53b 100755 --- a/vm_manage.sh +++ b/vm_manage.sh @@ -33,6 +33,7 @@ OPTIONS -h Show this help message -n NAME Host name (required) -b BRIDGE Bridge interface name + -H Host Only Network -r RAM RAM in MB (default: ${VM_MEM_SIZE}) -c VCPUS Number of VCPUs (default: ${VM_VCPUS}) -s DISK Disk size in GB (default: ${VM_DISK_SIZE}) @@ -60,7 +61,7 @@ case "${ACTION}" in VERBOSE=false NAME_SET=false - while getopts ":hn:b:r:c:s:v" opt; do + while getopts ":hn:b:H:r:c:s:v" opt; do case "${opt}" in h) usage @@ -70,8 +71,12 @@ case "${ACTION}" in NAME_SET=true ;; b) - BRIDGE_INTERFACE="${OPTARG}" + VM_BRIDGE_INT="${OPTARG}" + VM_NETWORK_TYPE_DEFAULT="bridge" ;; + H) + VM_NETWORK_TYPE_DEFAULT="isolated" + ;; r) VM_MEM_SIZE="${OPTARG}" ;; @@ -103,7 +108,7 @@ case "${ACTION}" in source env_scripts/common.sh detect_distro #Check network type - vm_net_set_bridge_mode + vm_net_set_network_type #Check host os for guest debian type check_host_os #Read os_options.json and generate guests menu