You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
264 lines
8.3 KiB
264 lines
8.3 KiB
|
9 years ago
|
## kvm-install-vm
|
||
|
|
|
||
|
|
A bash wrapper around virt-install to build virtual machines on a local KVM
|
||
|
9 years ago
|
hypervisor. You can run it as a normal user which will use `qemu:///session` to
|
||
|
9 years ago
|
connect locally to your KVM domains.
|
||
|
|
|
||
|
8 years ago
|
Tested on the latest Fedora.
|
||
|
9 years ago
|
|
||
|
|
### Prerequisites
|
||
|
|
|
||
|
6 years ago
|
You need to have the KVM hypervisor installed, along with a few other packages (naming of packages can differ on other distributions):
|
||
|
9 years ago
|
|
||
|
8 years ago
|
- genisoimage or mkisofs
|
||
|
9 years ago
|
- virt-install
|
||
|
|
- libguestfs-tools-c
|
||
|
|
- qemu-img
|
||
|
|
- libvirt-client
|
||
|
6 years ago
|
- libosinfo
|
||
|
9 years ago
|
|
||
|
|
To install the dependencies, run:
|
||
|
|
|
||
|
6 years ago
|
- Fedora example:
|
||
|
|
|
||
|
|
```
|
||
|
|
sudo dnf -y install genisoimage virt-install libguestfs-tools-c qemu-img libvirt-client wget libosinfo
|
||
|
|
```
|
||
|
|
|
||
|
|
- Ubuntu example:
|
||
|
|
|
||
|
9 years ago
|
```
|
||
|
6 years ago
|
sudo apt install -y genisoimage virtinst libguestfs-tools qemu-utils libvirt-clients wget libosinfo-bin
|
||
|
9 years ago
|
```
|
||
|
9 years ago
|
|
||
|
9 years ago
|
If you want to resolve guests by their hostnames, install the `libvirt-nss` package:
|
||
|
|
|
||
|
6 years ago
|
- Fedora example:
|
||
|
|
|
||
|
9 years ago
|
```
|
||
|
|
sudo dnf -y install libvirt-nss
|
||
|
|
```
|
||
|
|
|
||
|
6 years ago
|
- Ubuntu example:
|
||
|
|
|
||
|
|
```
|
||
|
6 years ago
|
sudo apt install -y libnss-libvirt
|
||
|
6 years ago
|
```
|
||
|
|
|
||
|
9 years ago
|
Then, add `libvirt` and `libvirt_guest` to list of **hosts** databases in
|
||
|
|
`/etc/nsswitch.conf`. See [here](https://libvirt.org/nss.html) for more
|
||
|
|
information.
|
||
|
|
|
||
|
9 years ago
|
### Usage
|
||
|
|
|
||
|
|
```
|
||
|
8 years ago
|
$ kvm-install-vm help
|
||
|
9 years ago
|
NAME
|
||
|
9 years ago
|
kvm-install-vm - Install virtual guests using cloud-init on a local KVM
|
||
|
|
hypervisor.
|
||
|
9 years ago
|
|
||
|
9 years ago
|
SYNOPSIS
|
||
|
8 years ago
|
kvm-install-vm COMMAND [OPTIONS]
|
||
|
9 years ago
|
|
||
|
9 years ago
|
DESCRIPTION
|
||
|
|
A bash wrapper around virt-install to build virtual machines on a local KVM
|
||
|
|
hypervisor. You can run it as a normal user which will use qemu:///session
|
||
|
|
to connect locally to your KVM domains.
|
||
|
|
|
||
|
8 years ago
|
COMMANDS
|
||
|
|
help - show this help or help for a subcommand
|
||
|
|
create - create a new guest domain
|
||
|
|
list - list all domains, running and stopped
|
||
|
|
remove - delete a guest domain
|
||
|
|
```
|
||
|
|
|
||
|
8 years ago
|
#### Creating Guest VMs
|
||
|
|
|
||
|
8 years ago
|
```
|
||
|
|
$ kvm-install-vm help create
|
||
|
|
NAME
|
||
|
6 years ago
|
kvm-install-vm create [COMMANDS] [OPTIONS] VMNAME
|
||
|
8 years ago
|
|
||
|
|
DESCRIPTION
|
||
|
|
Create a new guest domain.
|
||
|
9 years ago
|
|
||
|
6 years ago
|
COMMANDS
|
||
|
|
help - show this help
|
||
|
|
|
||
|
9 years ago
|
OPTIONS
|
||
|
6 years ago
|
-a Autostart (default: false)
|
||
|
|
-b Bridge (default: virbr0)
|
||
|
|
-c Number of vCPUs (default: 1)
|
||
|
|
-d Disk Size (GB) (default: 10)
|
||
|
|
-D DNS Domain (default: example.local)
|
||
|
|
-f CPU Model / Feature (default: host)
|
||
|
|
-g Graphics type (default: spice)
|
||
|
9 years ago
|
-h Display help
|
||
|
|
-i Custom QCOW2 Image
|
||
|
6 years ago
|
-k SSH Public Key (default: $HOME/.ssh/id_rsa.pub)
|
||
|
|
-l Location of Images (default: $HOME/virt/images)
|
||
|
|
-L Location of VMs (default: $HOME/virt/vms)
|
||
|
|
-m Memory Size (MB) (default: 1024)
|
||
|
|
-M Mac address (default: auto-assigned)
|
||
|
|
-p Console port (default: auto)
|
||
|
8 years ago
|
-s Custom shell script
|
||
|
6 years ago
|
-t Linux Distribution (default: centos8)
|
||
|
|
-T Timezone (default: US/Eastern)
|
||
|
|
-u Custom user (default: $USER)
|
||
|
|
-y Assume yes to prompts (default: false)
|
||
|
|
-n Assume no to prompts (default: false)
|
||
|
8 years ago
|
-v Be verbose
|
||
|
9 years ago
|
|
||
|
9 years ago
|
DISTRIBUTIONS
|
||
|
|
NAME DESCRIPTION LOGIN
|
||
|
8 years ago
|
amazon2 Amazon Linux 2 ec2-user
|
||
|
6 years ago
|
centos8 CentOS 8 centos
|
||
|
9 years ago
|
centos7 CentOS 7 centos
|
||
|
|
centos7-atomic CentOS 7 Atomic Host centos
|
||
|
|
centos6 CentOS 6 centos
|
||
|
|
debian9 Debian 9 (Stretch) debian
|
||
|
6 years ago
|
debian10 Debian 10 (Buster) debian
|
||
|
6 years ago
|
fedora29 Fedora 29 fedora
|
||
|
|
fedora29-atomic Fedora 29 Atomic Host fedora
|
||
|
|
fedora30 Fedora 30 fedora
|
||
|
6 years ago
|
fedora31 Fedora 31 fedora
|
||
|
6 years ago
|
fedora32 Fedora 32 fedora
|
||
|
6 years ago
|
opensuse15 OpenSUSE Leap 15.2 opensuse
|
||
|
9 years ago
|
ubuntu1604 Ubuntu 16.04 LTS (Xenial Xerus) ubuntu
|
||
|
8 years ago
|
ubuntu1804 Ubuntu 18.04 LTS (Bionic Beaver) ubuntu
|
||
|
6 years ago
|
ubuntu2004 Ubuntu 20.04 LTS (Focal Fossa) ubuntu
|
||
|
9 years ago
|
|
||
|
|
EXAMPLES
|
||
|
8 years ago
|
kvm-install-vm create foo
|
||
|
6 years ago
|
Create VM with the default parameters: CentOS 8, 1 vCPU, 1GB RAM, 10GB
|
||
|
9 years ago
|
disk capacity.
|
||
|
9 years ago
|
|
||
|
8 years ago
|
kvm-install-vm create -c 2 -m 2048 -d 20 foo
|
||
|
9 years ago
|
Create VM with custom parameters: 2 vCPUs, 2GB RAM, and 20GB disk
|
||
|
|
capacity.
|
||
|
9 years ago
|
|
||
|
8 years ago
|
kvm-install-vm create -t debian9 foo
|
||
|
9 years ago
|
Create a Debian 9 VM with the default parameters.
|
||
|
9 years ago
|
|
||
|
8 years ago
|
kvm-install-vm create -T UTC foo
|
||
|
|
Create a default VM with UTC timezone.
|
||
|
9 years ago
|
```
|
||
|
|
|
||
|
8 years ago
|
#### Deleting a Guest Domain
|
||
|
|
|
||
|
|
```
|
||
|
8 years ago
|
$ kvm-install-vm help remove
|
||
|
8 years ago
|
NAME
|
||
|
|
kvm-install-vm remove [COMMANDS] VMNAME
|
||
|
|
|
||
|
|
DESCRIPTION
|
||
|
|
Destroys (stops) and undefines a guest domain. This also remove the
|
||
|
|
associated storage pool.
|
||
|
|
|
||
|
|
COMMANDS
|
||
|
|
help - show this help
|
||
|
|
|
||
|
|
EXAMPLE
|
||
|
|
kvm-install-vm remove foo
|
||
|
|
Remove (destroy and undefine) a guest domain. WARNING: This will
|
||
|
|
delete the guest domain and any changes made inside it!
|
||
|
|
```
|
||
|
|
|
||
|
|
#### Attaching a new disk
|
||
|
|
|
||
|
|
```
|
||
|
8 years ago
|
$ kvm-install-vm help attach-disk
|
||
|
8 years ago
|
NAME
|
||
|
|
kvm-install-vm attach-disk [OPTIONS] [COMMANDS] VMNAME
|
||
|
|
|
||
|
|
DESCRIPTION
|
||
|
8 years ago
|
Attaches a new disk to a guest domain.
|
||
|
8 years ago
|
|
||
|
|
COMMANDS
|
||
|
|
help - show this help
|
||
|
|
|
||
|
|
OPTIONS
|
||
|
|
-d SIZE Disk size (GB)
|
||
|
|
-f FORMAT Disk image format (default: qcow2)
|
||
|
|
-s IMAGE Source of disk device
|
||
|
|
-t TARGET Disk device target
|
||
|
|
|
||
|
|
EXAMPLE
|
||
|
|
kvm-install-vm attach-disk -d 10 -s example-5g.qcow2 -t vdb foo
|
||
|
|
Attach a 10GB disk device named example-5g.qcow2 to the foo guest
|
||
|
|
domain.
|
||
|
8 years ago
|
```
|
||
|
8 years ago
|
|
||
|
8 years ago
|
### Setting Custom Defaults
|
||
|
|
|
||
|
|
Copy the `.kivrc` file to your $HOME directory to set custom defaults. This is
|
||
|
|
convenient if you find yourself repeatedly setting the same options on the
|
||
|
|
command line, like the distribution or the number of vCPUs.
|
||
|
|
|
||
|
|
Options are evaluated in the following order:
|
||
|
|
|
||
|
|
- Default options set in the script
|
||
|
|
- Custom options set in `.kivrc`
|
||
|
|
- Option flags set on the command line
|
||
|
|
|
||
|
9 years ago
|
### Notes
|
||
|
|
|
||
|
9 years ago
|
1. This script will download a qcow2 cloud image from the respective
|
||
|
|
distribution's download site. See script for URLs.
|
||
|
|
|
||
|
9 years ago
|
2. If using libvirt-nss, keep in mind that DHCP leases take some time to
|
||
|
9 years ago
|
expire, so if you create a VM, delete it, and recreate another VM with the
|
||
|
|
same name in a short period of time, there will be two DHCP leases for the
|
||
|
|
same host and its hostname will likely not resolve until the old lease
|
||
|
|
expires.
|
||
|
|
|
||
|
6 years ago
|
3. The Operating System information database (osinfo-db) provides Operating
|
||
|
|
System specific information needed to create guests for the various systems
|
||
|
|
supported by `kvm-install-vm`. The database files provided by your package
|
||
|
|
manager may be out of date and not provide definitions for recent Operating
|
||
|
|
System versions. If you encounter the following error message, you may need
|
||
|
|
to update the database files:
|
||
|
|
`ERR: Unknown OS variant '<name>'. Please update your osinfo-db.`
|
||
|
|
If you have already updated your system, and the osinfo-db is still to old,
|
||
|
|
then you can use the `osinfo-db-import` tool with the `--local` option, to
|
||
|
|
install an up-to-date database in your home directory which will not
|
||
|
|
conflict with your package manager files. The `osinfo-db-import` tool is
|
||
|
|
provided by the rpm/deb packages `osinfo-db-tools`.
|
||
|
|
See https://libosinfo.org/download for more information.
|
||
|
|
|
||
|
9 years ago
|
### Testing
|
||
|
|
|
||
|
|
Tests are written using [Bats](https://github.com/sstephenson/bats). To
|
||
|
|
execute the tests, run `./test.sh` in the root directory of the project.
|
||
|
9 years ago
|
|
||
|
|
### Use Cases
|
||
|
|
|
||
|
9 years ago
|
If you don't need to use Docker or Vagrant, don't want to make changes to a
|
||
|
9 years ago
|
production machine, or just want to spin up one or more VMs locally to test
|
||
|
9 years ago
|
things like:
|
||
|
9 years ago
|
|
||
|
|
- high availability
|
||
|
|
- clustering
|
||
|
|
- package installs
|
||
|
|
- preparing for exams
|
||
|
|
- checking for system defaults
|
||
|
|
- anything else you would do with a VM
|
||
|
|
|
||
|
9 years ago
|
...then this wrapper could be useful for you.
|
||
|
6 years ago
|
|
||
|
|
### Troubleshooting
|
||
|
|
|
||
|
|
If you will encounter something similar:
|
||
|
|
|
||
|
|
```
|
||
|
|
ERR: Unknown OS variant 'fedora31'. Please update your osinfo-db. See https://libosinfo.org/download for more information.
|
||
|
|
```
|
||
|
|
|
||
|
|
Then you need to update the DB in libosinfo.
|
||
|
|
Check the url and select the latest date ( https://releases.pagure.org/libosinfo/ )
|
||
|
|
|
||
|
|
```
|
||
|
|
wget -O "/tmp/osinfo-db.tar.xz" https://releases.pagure.org/libosinfo/osinfo-db-20200515.tar.xz
|
||
|
|
sudo osinfo-db-import --local "/tmp/osinfo-db.tar.xz"
|
||
|
|
```
|