Hi All,
I have this script that installs bhyve and alpine linux in Freebsd host. I use this link as reference.
However I get this error when running it:
Also is it possible to run GUI apps using a server iso using the FreeBSD's GUI please
Thanks & Best Regards
AMJS
I have this script that installs bhyve and alpine linux in Freebsd host. I use this link as reference.
Code:
michael@cx:~ $ sudo vi bhyve.sh
#!/bin/sh
# Variables
ISO_URL="https://dl-cdn.alpinelinux.org/alpine/v3.21/releases/x86_64/alpine-virt-3.21.2-x86_64.iso"
ISO_PATH="/home/michael/Downloads/alpine-virt-3.21.2-x86_64.iso"
VM_NAME="alpine-vm2"
VM_CPU="1"
VM_RAM="2G"
VM_DISK_SIZE="5G"
VM_DISK_PATH="/vms/$VM_NAME/disk0.img"
VM_NETWORK_SWITCH="public"
VM_GRAPHICS_PORT="5999"
# Extract screen resolution
SCREEN_RES=$(xrandr | grep '*' | awk '{print $1}')
if [ -z "$SCREEN_RES" ]; then
echo "Unable to detect screen resolution. Using default 1600x900."
SCREEN_RES="1600x900"
fi
# Extract network interface
NETWORK_INTERFACE=$(netstat -rn | grep default | awk '{print $4}')
if [ -z "$NETWORK_INTERFACE" ]; then
echo "Unable to detect network interface. Using default em0."
NETWORK_INTERFACE="em0"
fi
# Check if the Downloads directory exists
if [ ! -d "/home/michael/Downloads" ]; then
echo "Directory /home/michael/Downloads does not exist. Please create it or update the ISO_PATH variable."
exit 1
fi
# Install vm-bhyve and required kernel modules
echo "Installing vm-bhyve and required kernel modules..."
sudo pkg install -y vm-bhyve
sudo kldload if_bridge if_tap nmdm wmm
# Enable vm-bhyve and set the VM directory
echo "Enabling vm-bhyve and setting VM directory..."
sudo sysrc vm_enable="YES"
sudo sysrc vm_dir="zfs:zroot/vms"
# Create the ZFS dataset for VMs
echo "Creating ZFS dataset for VMs..."
sudo zfs create -o mountpoint=/vms zroot/vms
# Initialize vm-bhyve
echo "Initializing vm-bhyve..."
sudo vm init
# Copy default templates
echo "Copying default templates..."
cp /usr/local/share/examples/vm-bhyve/* /vms/.templates/
# Create a network switch
echo "Creating network switch..."
sudo vm switch create $VM_NETWORK_SWITCH
sudo vm switch add $VM_NETWORK_SWITCH $NETWORK_INTERFACE
# Download the Alpine Linux ISO if it doesn't exist
if [ ! -f "$ISO_PATH" ]; then
echo "Downloading Alpine Linux ISO..."
sudo vm iso "$ISO_URL" -o "$ISO_PATH"
else
echo "ISO already exists at $ISO_PATH. Skipping download."
fi
# Create the VM
echo "Creating the VM..."
sudo vm create "$VM_NAME"
# Configure the VM
echo "Configuring the VM..."
sudo vm configure "$VM_NAME" <<EOF
loader="uefi"
cpu="$VM_CPU"
memory="$VM_RAM"
network0_type="virtio-net"
network0_switch="$VM_NETWORK_SWITCH"
disk0_type="virtio-blk"
disk0_name="disk0.img"
disk0_size="$VM_DISK_SIZE"
graphics="yes"
graphics_port="$VM_GRAPHICS_PORT"
graphics_listen="0.0.0.0"
graphics_res="$SCREEN_RES"
EOF
# Install Alpine Linux from the ISO
echo "Installing Alpine Linux..."
sudo vm install -f "$VM_NAME" "$ISO_PATH"
# Start the VM
echo "Starting the VM..."
sudo vm start "$VM_NAME"
# Set the VM to autostart at boot
echo "Setting VM to autostart at boot..."
sudo sysrc vm_list="$VM_NAME"
# Connect to the VM via VNC
echo "You can now connect to the VM using VNC at 127.0.0.1:$VM_GRAPHICS_PORT"
echo "To stop the VM, use: sudo vm stop $VM_NAME"
echo "To restart the VM, use: sudo vm restart $VM_NAME"
Code:
michael@cx:~ $ sudo sh bhyve.sh
Installing vm-bhyve and required kernel modules...
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
Checking integrity... done (0 conflicting)
The most recent versions of packages are already installed
kldload: can't load if_bridge: module already loaded or in kernel
kldload: can't load if_tap: module already loaded or in kernel
kldload: can't load nmdm: module already loaded or in kernel
kldload: can't load wmm: No such file or directory
Enabling vm-bhyve and setting VM directory...
vm_enable: YES -> YES
vm_dir: zfs:zroot/vms -> zfs:zroot/vms
Creating ZFS dataset for VMs...
cannot create 'zroot/vms': dataset already exists
Initializing vm-bhyve...
Copying default templates...
Creating network switch...
egrep: warning: egrep is obsolescent; using /usr/local/bin/ggrep -E
/usr/local/sbin/vm: ERROR: switch public already exists
/usr/local/sbin/vm: ERROR: failed to add member wlan0 to the virtual switch public
ISO already exists at /home/michael/Downloads/alpine-virt-3.21.2-x86_64.iso. Skipping download.
Creating the VM...
egrep: warning: egrep is obsolescent; using /usr/local/bin/ggrep -E
Configuring the VM...
ex/vi: Vi's standard input and output must be a terminal
Installing Alpine Linux...
Starting alpine-vm2
* found guest in /vms/alpine-vm2
* booting...
Consoles: userboot
FreeBSD/amd64 User boot lua, Revision 3.0
ERROR: cannot open /boot/lua/loader.lua: no such file or directory.
Type '?' for a list of commands, 'help' for more detailed help.
OK
Thanks & Best Regards
AMJS