• No se han encontrado resultados

INTEGRACIÓN EN CÓDIGO LOCAL

5. DISEÑO E IMPLEMENTACIÓN

5.3. INTEGRACIÓN EN CÓDIGO LOCAL

In the previous chapters, we have covered some of the main operations on a virtual machine. You have learned the steps to create a virtual machine. In this chapter, we are going to cover the major tasks associated with a system administrator to manage a virtual machine, including offline and live migration of a virtual machine. You might see some commands revisited or repeated from the previous chapters. You need a Fedora 22 virtual machine ready to follow the examples in this chapter. You can download fedora from the following link:

https://getfedora.org/en/workstation/download/

We will start this chapter by describing the state of a virtual machine during its lifecycle:

• Undefined: This is a state where the virtual machine is neither created nor defined in libvirt.

• Defined/Shutoff: In this state, libvirt is aware of the virtual machine.

The configuration file to define and start a virtual machine is available in /etc/libvirt/qemu. We can also call this state as stopped or shut down.

• Running: This state is self explanatory. The virtual machine is started by libvirt.

• Shutdown: The virtual machine's OS has been notified about the shutdown and it is stopping its processes for a graceful shutdown.

• Paused: The virtual machine has been moved from a running state to a suspended state. The memory image has been stored temporarily. The virtual machine can be resumed without the guest OS being aware.

• Saved: In this state, the virtual machine is in a permanent suspend mode.

The memory state has been dumped to a file stored in a persistent storage.

The virtual machine can be resumed to the original running state from this saved state file.

• Idle: This state means that a virtual machine is waiting on I/O, or has gone to sleep as it has no job to perform.

• Crashed: The virtual machine has crashed. It could be a QEMU process killed or core dumped.

• Dying: The virtual machine has neither shut down nor crashed. It could be due to a failure in the shutdown process also.

• Pmsuspended: The virtual machine has been suspended by the guest OS's power management.

The current status of a virtual machine is displayed on the opening screen of the virt-manager. When you right-click on a virtual machine, virt-manager will present options to change the status of a virtual machine:

Let us now check the options available in virsh.

Chapter 6 To check the status of all virtual machines defined and running on the hypervisor, execute the following command:

# virsh list --all

Domain F22-01 is being shutdown

Id Name State

12 F22-02 paused

13 fedora22 running 14 F22-01 in shutdown

The virsh list command has a couple of options to filter the output displayed, based on the status of the virtual machines. These filter options are very useful when you have to automate the actions based on a virtual machine's status using custom scripts:

• inactive: This lists the inactive domains

• all: This lists the inactive & active domains

• transient: This lists the transient domains

• -persistent: This lists the persistent domains

• with-snapshot: This lists the domains with an existing snapshot

• without-snapshot: This lists the domains without a snapshot

• state-running: This lists the domains in running state

• state-paused: This lists the domains in paused state

• state-shutoff: This lists the domains in shutoff state

• state-other: This lists the domains in other states

• autostart: This lists the domains with autostart enabled

• no-autostart: This lists the domains with autostart disabled

• with-managed-save: This lists the domains with managed save state

• without-managed-save: This lists the domains without managed save

• uuid: This lists the uuid's only

• name: This lists the domain names only

• table: This lists the table (default)

• managed-save: This marks the inactive domains with managed save state

• title: shows domain title

To get help on virsh use virshhelp. An example of the command is virsh help list.

Let's now play with some virsh commands that are used to change the status of a virtual machine. In most cases, the command itself answers its purpose.

start: Start a (previously defined) inactive domain. Previously defined means you should be able to list the domain using virsh list --inactive:

• shutdown: Gracefully shuts down a domain

• reboot: Reboots a domain

When you issue virsh shutdown vm_name or virsh reboot vm_name and the VM is not responding to the commands, then you need to check if the ACPI service is active in VM OS:

• reset: This resets a domain. Imagine this command to be a power cycle operation.

• destroy: This destroys (stops) a domain. This is like you pulling a power cable from the server. libvirt will just kill the associated QEMU process for the VM in the hypervisor. You need to use destroy if the VM is not responding to any of the virsh commands, and if you cannot access its console, the VM has either crashed or the status shown is incorrect.

Before going to the next option, try out the preceding commands yourself and understand the results.

Let's take a look at a set of virsh commands, which will help you to create/define a virtual machine:

• create: This creates a domain from an XML file. Using this option, you can start a virtual machine using its XML file. This virtual machine is not defined in libvirt. Once stopped, it disappears from libvirt till you start using it again virsh create /location/vm_name.xml.

• define: This defines (but doesn't start) a domain from an XML file. Here you add the virtual machine to libvirt.

• undefine: This undefines a domain. undefine will remove a virtual machine from libvirt.

Chapter 6 Let's try those commands with a real world example. The following step is also one of the backup strategies, which we are going to describe in the next chapter.

1. First dump a defined VM's configuration file. In this example, the name of the VM is F22-03:

# virsh dumpxml F22-03 > /root/F22-03.xml

2. We have now saved the configuration file of F22-03 as an XML file. Just open the file and try to understand the tags.

3. Remove the virtual machine from libvirt. Executing undefine alone will not remove the underlying storage:

Domain F22-03 created from F22-03.xml

# virsh list

---# virsh define F22-03.xml --validate Domain F22-03 defined from F22-03.xml

# virsh list --all

Id Name State

- F22-03 shut off

You can now start the VM as usual. Once it starts try the following commands yourself and observe the state changes:

• suspend: Suspend a domain

• resume: Resume a domain

An advanced level of suspend and resume is save and restore. Here you are saving the machine state to a file and then restoring it later. This feature comes in handy for system administrators when they want to make an unscheduled restart of the virtual environment and one of the domains has an application that needs a complex starting process:

# virsh save F22-03 /root/F22-03_before_host_reboot libvirt will stop the virtual machine after saving the state to a file.

# virsh restore /root/F22-03_before_host_reboot

There are some interesting add-on commands once the image is saved:

• save-image-define: Redefines the XML for a domain's saved state file

• save-image-dumpxml: Saves state domain information in XML

• save-image-edit: Edits XML for a domain's saved state file

Once the VM is saved, it is not mandatory to restore it from the saved image file.

You can always start the VM as usual.

There is one more option, called managedsave. This will save the state file automatically in /var/lib/libvirt/qemu/save. When the VM starts next time, libvirt will try to restore it from the state file saved there. If the VM fails to start, do not panic, just delete the file using managedsave-remove and start the VM again.

Our recommendation is to always use save instead of managedsave. Try to run virshmanagedsavevm_name and virsh start vm_name.

Chapter 6

Documento similar