From 2834d86e927d33c07c90a489aadd933228189c66 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Mon, 4 Oct 2021 20:41:57 -0400 Subject: [PATCH] add cleanup playbook for libvirt --- README.md | 1 + libvirt/README.md | 13 +++++++++--- libvirt/cleanup.yml | 49 +++++++++++++++++++++++++++++++++++++++++++++ libvirt/main.yml | 1 + 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 libvirt/cleanup.yml diff --git a/README.md b/README.md index ee6599f..8f7d465 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Follow instructions in the [readme](qemu/README.md) of the qemu directory. Follow the instructions in [readme](libvirt/README.md) of the libirt directory. ## Issues/Remarks +- vm is set to autostart when installing using libvirt playbook - cron config `/etc/cron.d/csc-mirror` is commented out for now - merlin.service is NOT started - outsider/insider diff --git a/libvirt/README.md b/libvirt/README.md index f63d517..31ac7d5 100644 --- a/libvirt/README.md +++ b/libvirt/README.md @@ -26,9 +26,8 @@ $ pacman -S qemu libvirt virt-install virt-viewer ansible ``` ## Running the Playbook -Run the playbook from one directory down ``` -ansible-playbook -K libvirt/main.yml +ansible-playbook -K main.yml ``` Then connect to the created vm using virt-viewer ``` @@ -67,6 +66,14 @@ $ apt install git ansible $ git clone https://git.csclub.uwaterloo.ca/public/mirror-env.git ``` Then enter `mirror-env/mirror` and follow the instructions there. + +## Deleting the VM + +The VM and everything related can be removed by running the `cleanup.yml` playbook +``` +ansible-playbook -K cleanup.yml +``` + ## Troubleshooting ``` @@ -75,7 +82,7 @@ net.ipv4.ip_forward = 1 ``` modprobe kvm_intel ``` -If running in `/root` may need to edit config +If running as `root` may need to edit config ``` # edit /etc/libvirt/qemu.conf user = "root" diff --git a/libvirt/cleanup.yml b/libvirt/cleanup.yml new file mode 100644 index 0000000..c900028 --- /dev/null +++ b/libvirt/cleanup.yml @@ -0,0 +1,49 @@ +--- +- name: delete the mirror vm + hosts: 127.0.0.1 + gather_facts: no + become: yes + tasks: + - name: mirbr0 network exists + command: "virsh net-dumpxml mirbr0" + changed_when: false + ignore_errors: true + register: net_exists + + - name: storage pool exists + command: "virsh pool-dumpxml mirror" + changed_when: false + ignore_errors: true + register: pool_exists + + - name: mirror vm exists + command: "virsh dumpxml mirror" + changed_when: false + ignore_errors: true + register: vm_exists + + - name: delete vm + command: "virsh {{ item }}" + loop: + - destroy mirror + - undefine mirror + when: vm_exists.rc == 0 + + - name: delete storage pool + command: "virsh {{ item }}" + loop: + - pool-destroy mirror + - pool-undefine mirror + when: pool_exists.rc == 0 + + - name: delete mirbr0 bridge network + command: "virsh {{ item }}" + loop: + - net-destroy mirbr0 + - net-undefine mirbr0 + when: net_exists.rc == 0 + + - name: delete directory for vm + file: + path: "{{ playbook_dir }}/vm" + state: absent \ No newline at end of file diff --git a/libvirt/main.yml b/libvirt/main.yml index 55a4180..2e67cdd 100644 --- a/libvirt/main.yml +++ b/libvirt/main.yml @@ -2,6 +2,7 @@ - name: setup mirror vm hosts: 127.0.0.1 gather_facts: no + become: yes tasks: - name: mirbr0 network exists command: "virsh net-dumpxml mirbr0"