diff --git a/multi-node-aio/README.rst b/multi-node-aio/README.rst index 43022c8d..6df7d328 100644 --- a/multi-node-aio/README.rst +++ b/multi-node-aio/README.rst @@ -258,3 +258,22 @@ backing store) for the VM's, then set the following option before executing export MNAIO_ANSIBLE_PARAMETERS="-e default_vm_disk_mode=file" ./build.sh + +If you wish to save the current file-based images in order to implement a +thin-provisioned set of VM's which can be saved and re-used, then use the +``save-vms.yml`` playbook. This will stop the VM's and save the files to +``/var/lib/libvirt/images/*-base.img``. Re-executing the ``deploy-vms.yml`` +playbook afterwards will rebuild the VMs from those images. + +.. code-block:: bash + + ansible-playbook -i playbooks/inventory playbooks/save-vms.yml + ansible-playbook -i playbooks/inventory -e default_vm_disk_mode=file playbooks/deploy-vms.yml + +To disable this default functionality when re-running ``build.sh`` set the +build not to use the snapshots as follows. + +.. code-block:: bash + + export MNAIO_ANSIBLE_PARAMETERS="-e default_vm_disk_mode=file -e vm_use_snapshot=no" + ./build.sh diff --git a/multi-node-aio/playbooks/deploy-vms.yml b/multi-node-aio/playbooks/deploy-vms.yml index 77ec3437..08e0d6fd 100644 --- a/multi-node-aio/playbooks/deploy-vms.yml +++ b/multi-node-aio/playbooks/deploy-vms.yml @@ -67,13 +67,34 @@ - default_vm_disk_mode | default('lvm') == "lvm" with_items: "{{ groups['pxe_servers'] }}" - - name: Create VM Disk Image - command: | - qemu-img create -f qcow2 /var/lib/libvirt/images/{{ hostvars[item]['server_hostname'] }}.img {{ default_vm_storage }}m + - name: Setup file-based disk images when: - - hostvars[item]['server_vm'] | default(false) | bool - default_vm_disk_mode | default('lvm') == "file" - with_items: "{{ groups['pxe_servers'] }}" + block: + - name: Find existing base image files + find: + paths: /var/lib/libvirt/images + patterns: '*-base.img' + register: _base_images + + - name: Set vm_use_snapshot if it's not defined + set_fact: + vm_use_snapshot: "{{ _base_images['matched'] > 0 }}" + when: + - vm_use_snapshot is not defined + + - name: Create VM Disk Image + command: >- + qemu-img create + -f qcow2 + {% if vm_use_snapshot | bool %} + -b /var/lib/libvirt/images/{{ hostvars[item]['server_hostname'] }}-base.img + {% endif %} + /var/lib/libvirt/images/{{ hostvars[item]['server_hostname'] }}.img + {{ default_vm_storage }}m + when: + - hostvars[item]['server_vm'] | default(false) | bool + with_items: "{{ groups['pxe_servers'] }}" - name: Create the VM template template: diff --git a/multi-node-aio/playbooks/save-vms.yml b/multi-node-aio/playbooks/save-vms.yml new file mode 100644 index 00000000..61f063f1 --- /dev/null +++ b/multi-node-aio/playbooks/save-vms.yml @@ -0,0 +1,48 @@ +--- +# Copyright 2018, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in witing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Save VM disk images for re-use + hosts: vm_hosts + gather_facts: "{{ gather_facts | default(true) }}" + environment: "{{ deployment_environment_variables | default({}) }}" + tags: + - save-vms + tasks: + - name: Gather variables for each operating system + include_vars: "{{ item }}" + with_first_found: + - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_distribution | lower }}.yml" + - "{{ playbook_dir }}/vars/{{ ansible_os_family | lower }}.yml" + tags: + - always + + - name: Stop running VMs + command: "virsh destroy {{ hostvars[item]['server_hostname'] }}" + failed_when: false + when: + - hostvars[item]['server_vm'] | default(false) | bool + with_items: "{{ groups['pxe_servers'] }}" + + - name: Save VM Disk Image + command: >- + mv + /var/lib/libvirt/images/{{ hostvars[item]['server_hostname'] }}.img + /var/lib/libvirt/images/{{ hostvars[item]['server_hostname'] }}-base.img + when: + - hostvars[item]['server_vm'] | default(false) | bool + with_items: "{{ groups['pxe_servers'] }}"