Merge branch 'develop'

This commit is contained in:
2025-07-23 20:56:07 +02:00

149
README.md
View File

@ -1,19 +1,54 @@
# kvm-cloudimage
Use cloud images on baremetal using libvirt/kvm
## Pre-requisites
- openssh
- mkpass (whois)
- arp
Actually, you can select these base OS for Guests
- Debian 12
- Ubuntu 20.04
- Ubuntu 22.04
- FreeBSD 14.3 with UFS filesystem
- FreeBSD 14.2 with ZFS filesystem
## Links
## Pre-requisites
* `openssh`
* `arp`
* `wget`
* `curl`
* `libvirt`
In order to create openbsd images you will also need
* `python3`
* `sudo`
* `signify` (Debian: `signify-openbsd` and `signify-openbsd-keys`)
* `qemu-system-x86_64`
## Links and credits
In order to get libvirt installed
- [Debian guide](https://wiki.debian.org/KVM)
- [Ubuntu guide](https://documentation.ubuntu.com/server/how-to/virtualisation/libvirt/)
- [Fedora guide](https://docs.fedoraproject.org/en-US/quick-docs/virtualization-getting-started/)
Inspirational sites for the project
- [https://blog.programster.org/create-debian-12-kvm-guest-from-cloud-image](https://blog.programster.org/create-debian-12-kvm-guest-from-cloud-image)
- [https://earlruby.org/2023/02/quickly-create-guest-vms-using-virsh-cloud-image-files-and-cloud-init/](https://earlruby.org/2023/02/quickly-create-guest-vms-using-virsh-cloud-image-files-and-cloud-init/)
- [https://sumit-ghosh.com/posts/create-vm-using-libvirt-cloud-images-cloud-init/)](https://sumit-ghosh.com/posts/create-vm-using-libvirt-cloud-images-cloud-init/)
For openbsd vm with cloud init, [hcartiaux's openbsd-cloud-image](https://github.com/hcartiaux/openbsd-cloud-image.git) project is used.
## Preparing host
### Create bridge network
- Define [variables](env_scripts/common.sh) file (env_scripts/common.sh). It's recommended to use a folder on your home directory to avoid libvirt permission problems.
- launch install.sh
### AppArmor exception
If needed
```shell
ln -s /etc/apparmor.d/usr.sbin.libvirtd /etc/apparmor.d/disable/
apparmor_parser -R /etc/apparmor.d/usr.sbin.libvirtd
```
<!-- ### Create bridge network
```shell
sudo virsh --connect qemu:///session net-define /dev/stdin << EOF
@ -23,61 +58,58 @@ sudo virsh --connect qemu:///session net-define /dev/stdin << EOF
<bridge name='brbackend' />
</network>
EOF
``` -->
## Examples
### Command usage
```shell
NAME
./vm_manage.sh
USAGE
Usage: ./vm_manage.sh create -n NAME [-b BRIDGE] [-r RAM] [-c VCPUS] [-s DISK] [-v]
./vm_manage.sh delete NAME
./vm_manage.sh info NAME
./vm_manage.sh connect NAME
./vm_manage.sh list
ACTIONS
create Create a new virtual machine
delete Delete a virtual machine
list List all defined virtual machines
info Show information about a virtual machine
connect Connect to the console of a virtual machine
OPTIONS
-h Show this help message
-n NAME Host name (required)
-b BRIDGE Bridge interface name
-r RAM RAM in MB (default: 1024)
-c VCPUS Number of VCPUs (default: 1)
-s DISK Disk size in GB (default: 10)
-v Verbose mode
AUTHOR
Victor Gracia Enguita <victor@burufalla.ovh>
COPYRIGHT
This is free software; see the source for copying conditions.
```
#### AppArmor exception
### Create VM
```shell
ln -s /etc/apparmor.d/usr.sbin.libvirtd /etc/apparmor.d/disable/
apparmor_parser -R /etc/apparmor.d/usr.sbin.libvirtd
./vm_manage.sh create -ntestMachine
```
## Creating VMs
### Usage
```shell
usage: ./vm_create.sh options
Quickly create guest VMs using cloud image files and cloud-init.
OPTIONS:
-h Show this message
-n Host name (required)
-b bridge interface name (bridge network is used)
-r RAM in MB (defaults to 2048)
-c Number of VCPUs (defaults to 2)
-s Amount of storage to allocate in GB (defaults to 20)
-v Verbose
```
The only required parameter is the hostname, but you can also set RAM size (in MB), number of VCPUs or storage size (in GB), if these parameters are not set, default values will used:
- RAM: 20248MB
- VCPUs: 2
- DISK: 20GB
Actually, you can select these base OS for Guests
- Debian 12
- Ubuntu 20.04
- Ubuntu 22.04
- FreeBSD 14.1
### Examples
```shell
./vm_create.sh -ntestMachine
```
A VM will ve created with default values
A VM will ve created with default values. you can set then on [env_scripts/common.sh](env_scripts/common.sh) file.
```shell
./vm_create.sh -ntestMachine -r4098 -c4 -s100
./vm_manage.sh create -ntestMachine -r4098 -c4 -s100
```
A VM will be created with 4098 MB of RAM, 4 vCPUs and 100Gb of storage
#### FreeBSD VMs
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
./vm_list.sh
./vm_manage.sh list
Id Nombre Estado
-------------------------------
7 debianTest ejecutando
@ -85,14 +117,13 @@ FreeBSD with cloud-init are now supported!! Just wait a little time to have the
```
## Connect to an VM
```shell
./vm_connect.sh debianTest
./vm_manage.sh connect debianTest
```
## Get ip of VM
Use as parameter machine name
```shell
./vm_get_ip.sh ubuntuTest
./vm_manage.sh info ubuntuTest
192.168.122.234
```
@ -100,7 +131,7 @@ Use as parameter machine name
Use as parameter machine name
```shell
./vm_delete.sh ubuntuTest
./vm_dmanage.sh delete ubuntuTest
Are you sure you want to remove the VM 'ubuntuTest' (y/N)? y
Domain 'ubuntuTest' destroyed
@ -110,16 +141,14 @@ VM 'ubuntuTest' removed successfully.
```
## TODO
- FreeBSD support is still in progress
- Check if used commands are available
./vm_create.sh: línea 52: mkpasswd: orden no encontrada
- Maybe add more BSD flavours guests support
- add non debian linux derivatives guests support
<!-- ./vm_create.sh: línea 52: mkpasswd: orden no encontrada
./vm_create.sh: línea 259: virt-install: orden no encontrada
./vm_create.sh: línea 261: virsh: orden no encontrada
qemu-img wget curl mkpass arp
qemu-img wget curl arp
sudo apt install --no-install-recommends qemu-system libvirt-clients libvirt-daemon-system whois virtinst net-tools
sudo chmod 750 /home/victor
sudo usermod -a -G libvirt $(whoami)
sudo usermod --append --groups earl libvirt-qemu
sudo usermod --append --groups earl libvirt-qemu -->
- Refactoring variables, functions and scripts calls for legibility and maintenance
- add script for create default files (network, variables etc)