#!/usr/bin/perl

# 1. Install OVMF rpm, then two files should exist as below.
#    - /usr/share/OVMF/OVMF_CODE.secboot.fd
#    - /usr/share/OVMF/OVMF_VARS.fd
# 2. Edit guest XML to include below:
# <domain>
#   <os>
#     <type arch='x86_64' machine='q35'>hvm</type>
#     <loader readonly='yes' secure='yes' type='pflash'>/usr/share/OVMF/OVMF_CODE.secboot.fd</loader>
#     <nvram template='/usr/share/OVMF/OVMF_VARS.fd'>/var/lib/libvirt/qemu/nvram/test_VARS.fd</nvram>
#     <boot dev='hd'/>
#   </os>
#   <features>
#     <acpi/>
#     <apic/>
#     <smm state='on'/>
#   </features>
#   ...
# 3. Define a guest with above XML and start the guest and then destroy the guest
# 4. Check nvram file should exist: /var/lib/libvirt/qemu/nvram/test_VARS.fd
# 5. Run ./nvram.pl <guest_name>
#    The nvram file should be removed and the guest is removed, too.
# 6. Redefine the guest
# 7  Run ./nvram_keep.pl <guest_name>
#    The nvram file should still exist and the guest is removed.

use strict;
use warnings;
use Sys::Virt;

my $domname = shift @ARGV;
my $c = Sys::Virt->new();
my $dom = $c->get_domain_by_name($domname);
$dom->undefine(Sys::Virt::Domain::UNDEFINE_KEEP_NVRAM);

*********************************************************
Run 
# ./nvram_keep.pl vm1
#
# vm1 is undefined and nvram file still exist 


