Virtualization with libvirt

De Wiki de Sistemas Operativos
Saltar a: navegación, buscar

For the realization of this practice you will need an Ubuntu Cloud image that we will download from https://cloud-images.ubuntu.com/ and we will need a host system with Ubuntu 18.04.

In this practice we will use the image downloaded from the Ubuntu Cloud website and we will use the cloud-init tool to configure it.

Next, we will use the Ubuntu image already configured to be imported to the virtualization software libvirt'.

Step 1: Downloading the Ubuntu Cloud image

We enter https://cloud-images.ubuntu.com/ and download the Ubuntu 18.04 LTS (Long Time Support) image.

You can download it directly with the command wget:

wget https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img

if it started downloading you will see output similar to this:

-2019-08-06 12:26:26-- https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img
Resolving cloud-images.ubuntu.com (cloud-images.ubuntu.com).... 91.189.88.89, 2001:67c:1560:8001::8001
Connecting to cloud-images.ubuntu.com (cloud-images.ubuntu.com)[91.189.88.89]:443... connected.
HTTP request sent, waiting for response... 200 OK
Length: 343474176 (328M) [application/octet-stream] [application/octet-stream] [application/octet-stream]...
Saving to: "bionic-server-cloudimg-amd64.img"

bionic-server-cloudimg-amd64.img. 0%[ ] 2.21M 472KB/s eta 14m 6s

Wait until the image is 100% downloaded.

The Ubuntu cloud image comes with 2 GBytes of space by default, you can expand the size with the following command before using it in the virtual machine:

qemu-img resize bionic-server-cloudimg-amd64.img +8G

This increases the image by 8 Gbytes, so you are left with a 10 GByte Ubuntu cloud image.

Step 3: Installing the libguestfs package

Next, you have to install the packages that provide the virt-customize tool to configure the Ubuntu image you downloaded.

We can install these packages with the apt' tool from the shell.

For Ubuntu 18.04:

sudo apt install libguestfs

For Ubuntu 20.04:

sudo apt install libguestfs-tools

On the practice classroom computers these packages will already be pre-installed, so you can skip this step.

Step 3: Configuring the Ubuntu image with virt-customize

The virt-customize command allows you to set the password of the administrator user of the image we have downloaded.

virt-customize -a bionic-server-cloudimg-amd64.img --root-password password:coolpass

The above example shows how to configure the downloaded image to have the user root, and in this case, the password is coolpass.


Translated with www.DeepL.com/Translator (free version)For the realization of this practice you will need an Ubuntu Cloud image that we will download from https://cloud-images.ubuntu.com/ and we will need a host system with Ubuntu 18.04.

In this practice we will use the image downloaded from the Ubuntu Cloud website and we will use the cloud-init tool to configure it.

Next, we will use the Ubuntu image already configured to be imported to the virtualization software libvirt'.

= Step 1: Downloading the Ubuntu Cloud image

We enter https://cloud-images.ubuntu.com/ and download the Ubuntu 18.04 LTS (Long Time Support) image.

You can download it directly with the command wget:

wget https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img

if it started downloading you will see output similar to this:

-2019-08-06 12:26:26-- https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img
Resolving cloud-images.ubuntu.com (cloud-images.ubuntu.com).... 91.189.88.89, 2001:67c:1560:8001::8001
Connecting to cloud-images.ubuntu.com (cloud-images.ubuntu.com)[91.189.88.89]:443... connected.
HTTP request sent, waiting for response... 200 OK
Length: 343474176 (328M) [application/octet-stream] [application/octet-stream] [application/octet-stream]...
Saving to: "bionic-server-cloudimg-amd64.img"

bionic-server-cloudimg-amd64.img. 0%[ ] 2.21M 472KB/s eta 14m 6s

Wait until the image is 100% downloaded.

The Ubuntu cloud image comes with 2 GBytes of space by default, you can expand the size with the following command before using it in the virtual machine:

qemu-img resize bionic-server-cloudimg-amd64.img +8G

This increases the image by 8 Gbytes, so you are left with a 10 GByte Ubuntu cloud image.

Step 3: Installing the libguestfs package

Next, you have to install the packages that provide the virt-customize tool to configure the Ubuntu image you downloaded.

We can install these packages with the apt' tool from the shell.

For Ubuntu 18.04:

sudo apt install libguestfs

For Ubuntu 20.04:

sudo apt install libguestfs-tools

On the practice classroom computers these packages will already be pre-installed, so you can skip this step.

Step 3: Configuring the Ubuntu image with virt-customize

The virt-customize command allows you to set the password of the administrator user of the image we have downloaded.

virt-customize -a bionic-server-cloudimg-amd64.img --root-password password:coolpass

The above example shows how to configure the downloaded image to have the user root, and in this case, the password is coolpass.

Step 4: Installing libvirt and importing images

First, install libvirt on the system:

sudo apt install libvirt-clients libvirt-daemon

On the practice classroom computers this package will already be pre-installed, so you can skip the above command..

To create a virtual machine with libvirt, you have to make use of virt-install:

virt-install --name mi-mv --memory 2048 --disk path=/home/user/bionic-server-cloudimg-amd64.img --network network=default --os-variant ubuntu18.04 --import

In the above command:

  • A virtual machine named my-mv is created.
  • The memory of the virtual machine is 2 GBytes (2048 Mbytes).
  • The --disk path indicates the path to the Ubuntu Cloud image.
  • The default network configuration is used.
  • The operating system variant of Ubuntu 18.04 is specified.
  • Indicate that this is an import of an image with an operating system already installed using --import.

''Remember to modify the virtual machine name my-mv and update the path to the Ubuntu Cloud image you downloaded in the previous step'.

After invoking this which will bring up the virtual machine screen in a window, you can exit the screen by clicking on File -> Quit.

To access the virtual machine screen again at any time, you can do:

virt-viewer mi-mv

To stop the virtual machine:

virsh shutdown my-mv

To list the existing virtual machines and check their status:

virsh list --all

You can use grep to locate your virtual machine and show the state it is in.

virsh list --all | grep my-mv

To launch a shutdown virtual machine, you can use:

virsh shutdown mi-mv

If you need to wipe a virtual machine completely (previously shutdown), you can do it with:

virsh undefine mi-mv

after which it will no longer appear in the virsh list --all listing.

Alternatively, libvirt provides the graphical interface 'virt-manager', which can be installed from the shell:

sudo apt install virt-manager
Virt-manager interface
]

Once installed we can launch virt-manager from the shell:

virt-manager

From the virt-manager GUI, we create a new virtual machine using the Ubuntu image we downloaded in step 1 and the image generated by cloud-localds in step 3.

  1. From the virt-manager interface we create a new Virtual Machine (File --> New virtual machine)'.
  2. In the new window that opens, select Local installation media and click Go.
  3. In Choose ISO or CDROM install media we select the media.img image created in step 3; and uncheck the box Automatic detection to select our Operating System, in this case, Ubuntu 18.04 LTS. We see that everything is as in the image and click Go ahead. You can use the Browse locally button to locate the file.
    Archivo:Virt-manager-step4-3.png
    Choose configuration image and OS
    ]
  4. We select the amount of memory and CPU we want, by default 2048 Memory and 1 CPU is fine.
  5. In the storage, let's select a custom storage, and this is where we select the downloaded ubuntu image, in our case bionic-server-cloudimg-amd64.img and click Go. You can use the Browse locally button to locate the file.
  6. In the last step, we can select the network, for now we will leave NAT by default and click Finish' (If it asks us if we want to create a network NAT because it is not created, we say yes).

We already have our virtual machine created with our configuration. Now we are going to test that everything works correctly. The machine should be booting and the login message should appear. [File:virt-manager-login.png|thumb|Login for ubuntu.