LFCS - Virtual Machine Management

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
360
Reaction score
403
Credits
13,016
Now that we can create Virtual Machines (VM) on a Linux system, we need to manage those VMs.

We will cover controlling VM installation, controlling a VM, resource monitoring, snapshots and cloning a VM.

Controlling a VM Install

Sometimes we need to control the setup of Virtual Machines to control hardware aspects such as MAC Address, IP Address and Host Name during the installation.

Here, you'll need to have PXE installation ability enabled on your server and enabled on the client. The server will need the Kickstart file for the clients. The name of the Kickstart file will be the hostname with an extension of '.ks'.

For the MAC Address, the VMs will start with '52:54:00' and you can finish the address as you need. In my examples, I will use '00:00:00:01' for the first system with a hostname of 'sys1'. The second system will be 'sys2' with a MAC Address ending in '00:00:00:02' and counting up. Since the VM NAT has an IP Address of '192.168.122.x' for the machines, I will increment the addresses from '192.168.122.51'.

Another point to keep in mind is that the 'default' menus in '/etc/lib/tftpboot/pxelinux.cfg/' needs to be copied to match the MAC Address of the VMs. So, we can use the command 'cp default 01-52-54-00-00-00-01' for 'sys1'. We need to place a '01' in front of the filename for TFTP and change the colons :)) to dashes (-).

Be sure to place the appropriate ISO file in the folder on the server to allow use during the PXE boot.

So, let's first change the DHCP range assigned by the virtual network card. Use the command:

Code:
sudo virsh net-edit default

Find the following line, as shown in Figure 1:

<range start='192.168.122.2' end='192.168.122.254'/>

Figure 1.jpg

FIGURE 1

Change the line to:

<range start='192.168.122.100' end='192.168.122.254'/>

For the settings to take effect, you need to 'sudo virsh net-destroy default' to stop the virtual network. Restart the virtual network with the command 'sudo virsh net-start default'. The new settings should be active.

Now, we can add a setting to allow the first Virtual Machine to load by adding a setting in the virtual network:

Code:
sudo virsh net-update default add ip-dhcp-host "<host mac='52:54:00:00:00:01' name='sys1' ip='192.168.122.51'"/> --live --config

The '--live' adds the entry and makes the update live. By using '--config', the new line appears to the configuration file.

You can verify the addition with the command:

Code:
sudo virsh net-edit default

The new line should exist, and everything will start.

Keep in mind that you need to add a line for each VM you want to do with PXE. Be sure that the hostnames, MAC Addresses, IP Addresses are all unique.

To start the installation of the VM using these settings, use the command:

Code:
sudo virt-install --hvm --network=bridge:virbr0,mac='52:54:00:00:00:01' --pxe --name=sys1 --ram:1024 --vcpus=1 --os-type=linux --os-variant=rhel7 --disk path=/var/lib/libvirt/images/sys1.qcow2,size=10 -v

Of course, you will want to change any settings that you need to. This is only an example.

Once the command starts, you can close the CLI and see that the VM is still running when you look in 'virt-manager'. A window should also open to allow you to perform the installation steps as normal. It should start and show something similar to Figure 2.

Figure 2.jpg

FIGURE 2

VM Status


During installation, the VM is 'active' since it is running. In our instance, while installing 'Sys1', it is active. We can perform a few things to the VM, hopefully once the install is compete.

The things we can do to a VM are:
  • Run
  • Pause/Resume
  • Shutdown
  • Clone
  • Migrate
  • Delete
  • Open
If a VM is running, then you have the following options available:
  • Pause/Resume
  • Shutdown
  • Migrate
  • Delete
  • Open (GUI/CLI)
If the VM is not running, then you have the following options:
  • Run
  • Clone
  • Delete
  • Open (Settings)
The Shutdown option has a few choices as well:
  • Reboot
  • Shutdown
  • Force Reset
  • Force Off
  • Save
Now, we need to look over these options to explain what they do. For anyone that uses VirtualBox, these will be familiar.

The choice 'Run' is used to start a stopped VM. This will let you have a VM running and allow you to interact with it.

'Pause/Resume' allows you to 'pause' a running VM, or 'resume' a paused VM. When a VM is 'Paused', the resources are still in use. You can almost think of a paused VM as being in 'hibernation', but can 'resume' immediately.

The 'Shutdown' option comprises five choices. The first is 'Reboot'. With this option, the system will perform a normal shutdown of the Operating System (OS) and shutdown, then restart the VM. The 'Shutdown' option will do a proper shutdown of the OS and then 'power off' the VM. A 'Force Reset' is when the VM is reset, similar to resetting the power on a PC. The 'Force Off' powers off the VM, such as cutting the power off to a PC and not shutting down the OS properly. 'Save' will save the state of the VM and power it off. You will then have a 'Restore' option to allow you to turn the VM back on at the point you saved it, which is closer to 'hibernate' than 'pause', since the resources are no longer in use.

Using 'Clone' lets you make a duplicate of a VM that is completely identical and you'll need to change the Hostname, IP Address, MAC Address, etc. The changes need to be made only if you plan on running both VMs at the same time or there can be a conflict if you use the same Virtual Network Interface.

The 'Migrate' option lets you move the VM to another system and control the VM as if it were local.

If a VM is no longer needed, you can use the 'Delete' option to remove the VM and free up the resources, mainly the storage space.

The 'Open' option works differently when you stop or start the VM. When it is not running, then you will get information about the VM itself. If the VM is in a 'running' state, then it will open a Graphical User Interface (GUI). Of course, the VM may have an actual GUI or be a Command-Line Interface (CLI).

What if we wanted to perform these functions through the CLI? We can use the command 'sudo virsh' to open a Command-Line Interface to 'virsh', which isn't left until we enter the command 'quit' or 'exit'. The command-prompt should be 'virsh #'.

Let's look at some of these:
  • Run - start <VM_name>
  • Pause - start <VM_name> --paused
  • Resume - resume <VM_name>
  • Shutdown
    • Reboot - reboot <VM_name>
    • Shutdown - shutdown <VM_name>
    • Force Reset - reset <VM_name>
    • Force Off - destroy <VM_name>
    • Save - save <VM_name>
VM Resources

By default, the 'virt-manager' only displays the CPU usage of the Virtual Machine, or Guest CPU. When you start a VM, it is being first installed, there should be more CPU usage, but once the installation is completed or the VM started, the CPU usage will near none.

It is possible to add the monitoring of Memory Usage, Disk I/O, and Network I/O as well as enable the showing of the Host CPU Usage.

To monitor the other types, in 'virt-manager' select 'Edit', 'Preferences' and then click on the Polling tab. You have a few options here. The first is the polling time, which is 3 seconds by default. Four options follow this to enable/disable polling of a resource:
  1. Poll CPU
  2. Poll Disk I/O
  3. Poll Network I/O
  4. Poll Memory stats
Once enabled, then you can view the graph on the line of 'virt-manager' on the line of the VM.

To enable the viewing of the monitor on the VM line in 'virt-manager', go to 'View', then 'Graph'. Here, are five different graphs to enable/disable on the VM line:
  1. Guest CPU
  2. Host CPU
  3. Memory Usage
  4. Disk I/O
  5. Network I/O
You can see resources from the CLI with a command like 'top'. You can install the command with: 'sudo yum install virt-top -y' or 'sudo apt install virt-top -y'. To run it, use the command 'sudo virt-top'.

Snapshot

We've covered cloning a VM, but a Snapshot is an 'image' of a VM at that moment. If we create a snapshot and change the VM, such as installing new software or making configuration changes, we can revert the system back to when the snapshot was first taken.

Any changes that were made are lost, but if those changes caused issues, then the system will be like it was before the issues.

In the 'virt-manager', we can see a listing of any VMs already created. If the VM is running, you can double-click on the VM line to open the Properties of the VM. Go to 'View' and select 'Snapshots'. There should now be a list of snapshots that have made for the VM. At the bottom of the listing is a plus sign (+) that allows you to add a snapshot. It asks you to give a name to the snapshot and an optional ‘Description’. Once the snapshot is on the list, you can then choose it to revert to it.

There is also a button to remove the snapshot from the list.

Conclusion

Managing the Virtual Machines from within Linux and not using a 'third-party' product like VirtualBox can make everything more contained within the OS.

Virtual Machines can be beneficial to test software with system upgrades before rolling them out company wide.

Try the KVM instead of VirtualBox or another third-party vendor.
 
Last edited:


Top