Execute o script no host ao iniciar a máquina virtual com o virt-manager

2

Isso está relacionado à minha pergunta sobre o Ask Ubuntu: Adicionar partição física à máquina virtual QEMU / KVM no virt-manager (veja "Atualizações" perto do final, se necessário).

Existe uma boa maneira de executar qualquer script arbitrário no host toda vez que uma VM for iniciada a partir do virt-manager?

O motivo é que eu quero automagicamente desmontar a partição ESP /dev/sda1 (montada no host como /boot/efi ) e remontá-la quando a VM estiver desligada.

[update] O script que eu estava usando pode ser encontrado aqui .

    
por Marc.2377 01.07.2017 / 03:04

1 resposta

1

De libvirt: Ganchos para gerenciamento específico do sistema

At present, there are five hook scripts that can be called:

  • /etc/libvirt/hooks/daemon
    Executed when the libvirt daemon is started, stopped, or reloads its configuration

  • /etc/libvirt/hooks/qemu
    Executed when a QEMU guest is started, stopped, or migrated

(...)

Script arguments

The hook scripts are called with specific command line arguments, depending upon the script, and the operation being performed.

(...)

The command line arguments take this approach:

  1. The first argument is the name of the object involved in the operation, or '-' if there is none.
    For example, the name of a guest being started.

  2. The second argument is the name of the operation being performed.
    For example, "start" if a guest is being started.

(...)

/etc/libvirt/hooks/qemu

  • Before a QEMU guest is started, the qemu hook script is called in three locations; if any location fails, the guest is not started. The first location, since 0.9.0, is before libvirt performs any resource labeling, and the hook can allocate resources not managed by libvirt such as DRBD or missing bridges. This is called as:
    /etc/libvirt/hooks/qemu guest_name prepare begin -

    The second location, available Since 0.8.0, occurs after libvirt has finished labeling all resources, but has not yet started the guest, called as:
    /etc/libvirt/hooks/qemu guest_name start begin -

    The third location, 0.9.13, occurs after the QEMU process has successfully started up:
    /etc/libvirt/hooks/qemu guest_name started begin -

(...)

Então, na prática, isso:

/ etc / libvirt / hooks / qemu (sem extensão)

#!/bin/bash

if [[ $1 == "<domain_name>" ]] && [[ $2 == "start" ]] || [[ $2 == "stopped" ]]
then
  if [[ $2 == "start" ]]
  then
    # unmount logic here
  else
    # mount logic here
  fi
fi

Claro, edite <domain name> , implemente a lógica de montagem / desmontagem e reinicie libvirtd.service .

Estou disponibilizando minha solução completa no link .


Um adendo : para este caso de uso específico, você pode achar útil configurar o arquivo fstab com a opção noauto e talvez também x-systemd.automount e x-systemd.device-timeout=<seconds> . Veja link e link .

    
por 24.02.2018 / 02:09