Added isolated network available when install

This commit is contained in:
2025-08-27 15:32:28 +02:00
parent 41574c143a
commit aaa3fd5b32
7 changed files with 59 additions and 20 deletions

View File

@ -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)

View File

@ -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/"

View File

@ -293,9 +293,11 @@ 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
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
}

View File

@ -0,0 +1,5 @@
<network>
<name>YOURNAME</name>
<bridge name='YOURNAME' stp='on' delay='0'/>
<!-- <domain name='hostonly.local'/> -->
</network>

View File

@ -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

View File

@ -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,7 +71,11 @@ 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