195 shaares
4 results
tagged
qemu
export LIBVIRT_DEFAULT_URI=qemu:///system # for virsh autoconnection
pth=/vms/virt
domain=cuckoo-1804
newdomain=cuckoo-1804-clone
pthdisk=$pth/${domain}.qcow2
pthnewdisk=$pth/${domain}-clone.qcow2
pthtmp=/vms/share/${domain}-${date +%s}
lssnapshot=$(virsh snapshot-list $domain --name)
[ -d ${pthtmp} ] || mkdir -p ${pthtmp}
# dump snapshots
for snapshot in ${lssnapshot}; do
virsh snapshot-dumpxml --domain ${domain} ${snapshot} > ${pthtmp}/${domain}_${snapshot}.xml
done
# create a volatily image
sudo qemu-img create -f qcow2 ${pthnewdisk} 100M
# clone domain
virt-clone --original ${domain} --name ${newdomain} --file ${pthnewdisk} --preserve-data
# restore snapshots to empty disk
for snapshot in ${lssnapshot}; do
virsh snapshot-create ${newdomain} --xmlfile ${pthtmp}/${domain}_${snapshot}.xml --atomic
done
# verify snapshots list
echo "- domain snapshots:"
virsh snapshot-list ${newdomain}
# full copy disk / qemu-img convert loses snapshots
sudo cp -a ${pthdisk} ${pthnewdisk}
# verify image snapshots list
echo "- disk snapshots:"
qemu-img snapshot -l ${pthnewdisk}
echo "Launch domain ${newdomain} in virt-manager (restart virt-manager)MASTER
echo -e -n "Give the domain name: " && read domain
echo -e -n "Give the path of pool (/vms/virt) : " && read pth_pool
pth_pool=${pth_pool:-/vms/virt}
echo -e -n "Give the path of disk ($pth_pool/${domain}.qcow2) : " && read pth_disk
pth_disk=${pth_disk:-$pth_pool/${domain}.qcow2}
pth_pool=/vms/virt
domain=cuckoo-1804
pth_disk=$pth_pool/${domain}.qcow2
sdate=$(date +%s)
pth_xml=/vms/share/${domain}-${sdate}
export LIBVIRT_DEFAULT_URI=qemu:///system # for virsh autoconnection
[ -d ${pth_xml} ] || mkdir -p ${pth_xml}
# dumps
echo "> ${domain}"
virsh dumpxml ${domain} > ${pth_xml}/${domain}.xml
for snapshot in $(virsh snapshot-list $domain --name); do
echo " ${snapshot}"
virsh snapshot-dumpxml --domain ${domain} ${snapshot} > ${pth_xml}/${domain}_${snapshot}.xml
done
pth=/tmp/${domain}::${sdate}.tar.gz
cd ${pth_xml}
tar czf ${pth} *
echo -e "\nPlease copy this files to slave:"
echo ${pth}
echo ${pth_disk}
SECOND
echo -e -n "Give the path of snapshots compressed files: " && read pth_tgz
echo -e -n "Give the path of copied disk: " && read pth_disk
echo -e -n "Give the path of pool (/vms/virt) : " && read pth_pool
pth_pool=${pth_pool:-/vms/virt}
export LIBVIRT_DEFAULT_URI=qemu:///system # for virsh autoconnection
domain=${pth_tgz##*/}; domain=${name%::*}
sdate=${pth_tgz##*::}; sdate=${sdate%%.*}
pth_disk=${pth_pool}/${domain}.qcow2
pth_xml=/vms/share/${domain}-${sdate}
[ -d "${pth_xml}" ] || mkdir -p "${pth_xml}"
tar xzf ${pth_tgz} -C "${pth_xml}"
# create a volatily image
sudo qemu-img create -f qcow2 ${pth_disk} 100M
# define domain
virsh define ${pth_xml}/${domain}.xml
virt-clone --original ${domain} --name ${domain} --file ${pth_disk} --preserve-data
# restore snapshots to empty disk
for snapshot in ${lssnapshot}; do
virsh snapshot-create ${domain} --xmlfile ${pth_tmp}/${domain}_${snapshot}.xml --atomic
done
# verify snapshots list
echo "- domain snapshots:"
virsh snapshot-list ${domain}
# full copy disk / qemu-img convert loses snapshots
if ! [ "${pth_disk%%/*}" = /vms/virt ]; then
echo "copy disk from '${pth_disk}' to '/vms/virt/${pth_disk##*/}'"
sudo cp -a ${pth_disk} ${pth_disk}
fi
# verify image snapshots list
echo "- disk snapshots:"
qemu-img snapshot -l ${pth_disk}
echo "Launch domain ${domain} in virt-manager (restart virt-manager)/etc/libvirt/qemu/: path of xml domain files
connect
export LIBVIRT_DEFAULT_URI=qemu:///system # to use sumply virsh <command>
virsh -c qemu:///system # enter in virsh console
virsh -c qemu:///system <command> # execute only a virsh command (needed for dumping to file)
help
help # list all commands with a one line description
help <command> # show complete description & options
domains
monitor
domblkerror # Show errors on block devices
domblkinfo # domain block device size information
domblklist # list all domain blocks
--inactive # get inactive rather than running configuration
--details # additionally display the type and device value
domblkstat # get device block stats for a domain
domcontrol # domain control interface state
domif-getlink # get link state of a virtual interface
domifaddr # Get network interfaces' addresses for a running domain
domiflist # list all domain virtual interfaces
domifstat # get network interface stats for a domain
dominfo # domain information
dommemstat # get memory statistics for a domain
domstate # domain state
domstats # get statistics about one or multiple domains
domtime # domain time
list # list domains
--state-running
--inactive # list only all inactive domains
--state-shutoff
--all # list all domains
--with-snapshot # list domains with existing snapshot
--without-snapshot # list domains without a snapshot
--with-checkpoint # list domains with existing checkpoint
--without-checkpoint # list domains without a checkpoint
others
attach-disk <domain> <source> <target> # attach-disk <domain> <source> <target>
--targetbus <string # target bus of disk device
--driver <string> # driver of disk device
--subdriver <string> # subdriver of disk device
--iothread <string> # IOThread to be used by supported device
--cache <string> # cache mode of disk device
--io <string> # io policy of disk device
--type <string> # target device type
--mode <string> # mode of device reading and writing
--sourcetype <string> # type of source (block|file|network)
--serial <string> # serial of disk device
--wwn <string> # wwn of disk device
--alias <string> # custom alias name of disk device
--rawio # needs rawio capability
--address <string> # address of disk device
--multifunction # use multifunction pci under specified address
--print-xml # print XML document rather than attach the disk
--source-protocol # <string> protocol used by disk device source
--source-host-name # <string> host name for source of disk device
--source-host-transport # <string> host transport for source of disk device
--source-host-socket <string> # host socket for source of disk device
--persistent # make live change persistent
--config # affect next boot
--live # affect running domain
--current # affect current domain
create <file> # create a domain from an XML file
--console # attach to console after creation
--paused # leave the guest paused after creation
--autodestroy # automatically destroy the guest when virsh disconnects
--pass-fds <string> # pass file descriptors N,M,... to the guest
--validate # validate the XML against the schema
--reset-nvram # re-initialize NVRAM from its pristine template
define <file> # define (but don't start) a domain from an XML file
--validate # validate the XML against the schema
detach-disk <domain> <target> # detach disk device (target is the device name in domain)
--persistent # make live change persistent (use to detach when the domain is inactive)
--config # affect next boot
--live # affect running domain
--current # affect current domain
--print-xml # print XML document rather than detach the disk
dumpxml <domain> # Output the domain information as an XML dump to stdout
--inactive # show inactive defined XML
--security-info # include security sensitive information in XML dump
--update-cpu # update guest CPU according to host CPU
--migratable # provide XML suitable for migrations
pool
# list
pool-list # list pools
--inactive # list inactive pools
--all # list inactive & active pools
--autostart # list pools with autostart enabled
--no-autostart # list pools with autostart disabled
--type <string> # only list pool of specified type(s) (if supported)
--details # display extended details for pools
--uuid # list UUID of active pools only
--name # list name of active pools only
# info
pool-info <pool> # storage pool information
# refresh
pool-refresh # refresh a pool
volume
# list
vol-list <pool> # list volumes from a pool
--details # display extended details for volumes
# info
vol-info <vol> <pool> # show storage volume informations
vol-info --pool <pool> <vol>
# clone
vol-clone --pool <pool> <vol> <clonevol> # Clone an existing volume within the parent pool
--prealloc-metadata # preallocate metadata (for qcow2 instead of full allocation)
--reflink # use btrfs COW lightweight copy
# delete
vol-delete --pool <pool> <vol> # delete a vol
--delete-snapshots # delete snapshots associated with volume (must be supported by storage driver)
# upload
vol-upload --pool <pool> <vol> <file> # upload (import) file contents to a volume
# dumpxml
vol-dumpxml --pool <pool> <vol> # output the vol information as an XML dump to stdout
virsh vol-dumpxml <vol> <pool> > <file> # dump into file
# wipeexport LIBVIRT_DEFAULT_URI=qemu:///system
vol-wipe --pool <pool> <vol> # wipe a vol. Ensure data previously on a volume is not accessible to future reads
--algorithm <string> # perform selected wiping algorithm (ex: zero, nnsa, dod, pfitzner7, pfitzner33, random)
# key
vol-key --pool <pool> <vol> # returns the volume key for a given volume name or path
# path
vol-path --pool <pool> <vol> # returns the volume path for a given volume name or key
--pool <string> # pool name or uuid
--offset <number> # volume offset to upload to
--length <number> # amount of data to upload
--sparse # preserve sparseness of volume
backup
# dumpxml
backup-dumpxml <domain> # Dump XML for an ongoing domain block backup job
# backup
backup-begin <domain> # Start a disk backup of a live domain. Use XML to start a full or incremental disk backup of a live domain, optionally creating a checkpoint
--backupxml <string> # domain backup XML
--checkpointxml <string> # domain checkpoint XML
--reuse-external # reuse files provided by caller
snapshot
# list
snapshot-list <domain> # list all snapshots from a domain (domain name, id or uuid)
--tree # list snapshots in a tree
--current # limit list to children of current snapshot
--name # list snapshot names only
--parent # add a column showing parent snapshot
--roots # list only snapshots without parents
--leaves # list only snapshots without children
--no-leaves # list only snapshots that are not leaves (with children)
--metadata # list only snapshots that have metadata that would prevent undefine
--no-metadata # list only snapshots that have no metadata managed by libvirt
--internal # filter by internal snapshots
--external # filter by external snapshots
--from <snapshot> # limit list to children of given snapshot
--descendants --from <snapshot> # list all descendants from one snapshot
--descendants --current
# info
snapshot-info <domain> <snapshot> # show snapshot informations (domain name, id or uuid)
# revert
snapshot-revert <domain> [<snapshot>] # Revert a domain to a snapshot
--snapshotname <snapshot> snapshot name
--current # revert to current snapshot
--running # after reverting, change state to running
--paused # after reverting, change state to paused
--force # try harder on risky reverts
--reset-nvram # re-initialize NVRAM from its pristine template
# dumpxml
snapshot-dumpxml --domain <domain> <snapshot> # dump snapshot definition to stdout
--domain <domain> # domain name, id or uuid
--security-info include security sensitive information in XML dump
virsh snapshot-dumpxml --domain <domain> <snapshot> > <file> # dump into file
tricks
dumpxml all domains
pthxml=/vms/virt/xml-$(date "+%Y%m%d")
# xml
! [ -d ${pthxml} ] && sudo mkdir -p ${pthxml} && sudo chown 1000:1000 ${pthxml}
echo "--> ${pthxml}"
for domain in $(virsh list --all --name); do
echo "> ${domain}"
virsh dumpxml ${domain} > ${pthxml}/${domain}.xml
for snapshot in $(virsh snapshot-list $domain --name); do
echo " ${snapshot}"
virsh snapshot-dumpxml --domain ${domain} ${snapshot} > ${pthxml}/${domain}:${snapshot}.xml
done
done
export all domains & insternal snapshots to appliances
pthpool=/vms/virt
pthapl=${pthpool}/appliances
pthxml=${pthpool}/xml-$(date "+%Y%m%d")
which pigz || { echo "Install pigz"; exit; }
cd ${pthpool} || { echo "Unable to enter in ${pthpool}"; exit; }
for pth in ${pthxml} ${pthxml}; do sudo mkdir -p ${pth} && sudo chown 1000:1000 ${pth}; done
for domain in $(virsh list --all --name); do
echo "> ${domain}"
virsh dumpxml ${domain} > ${pthxml}/${domain}.xml
for snapshot in $(virsh snapshot-list $domain --name); do
echo " ${snapshot}"
virsh snapshot-dumpxml --domain ${domain} ${snapshot} > ${pthxml}/${domain}:${snapshot}.xml
done
file=${pthapl##*/}/${domain}-$(date "+%Y%m%d").tar.gz
echo ">>> ${file}"
tar -cI pigz -f ${file} ${domain}.qcow2 ${pthxml##*/}/${domain}.xml ${pthxml##*/}/${domain}:*.xml
done
sudo chown 1000:1000 -R ${pthapl}
clone domain with internal snapshots
See: virsh - migrate
migrates domain to another host with internal snapshots
See: virsh - clone
https://wiki.manjaro.org/index.php?title=Virt-manager
install
https://computingforgeeks.com/install-kvm-qemu-virt-manager-arch-manjar/
sudo pacman -S qemu virt-manager virt-viewer libguestfs # dnsmasq vde2 bridge-utils openbsd-netcat
# see virt-sandbox
sudo systemctl enable libvirtd.service
sudo systemctl start libvirtd.service
systemctl status libvirtd.service
conf
file=/etc/libvirt/libvirtd.conf
sudo cp -a $file $file$(date +%s)
sudo sed -i 's|^#\?\(unix_sock_group =\).*$|\1 "libvirt"|' $file
sudo sed -i 's|^#\?\(unix_sock_rw_perms =\).*$|\1 "0770"|' $file
sudo usermod -a -G libvirt $USER
sudo systemctl restart libvirtd.service
mod=$(lsmod|awk '/^kvm_/ {print $1}')
sudo modprobe -r $mod
sudo modprobe $mod nested=1
echo "options ${mod/_/-} nested=1" | sudo tee /etc/modprobe.d/${mod/_/-}.conf
systool -m ${mod} -v | grep nested
share
host
hostpath=/vms/share
sudo mkdir $hostpath
#sudo usermod -G libvirtd -a $USER
sudo usermod -G libvirt-qemu -a $USER
hostpath=/vms/share
#sudo chown -R libvirt-qemu:libvirt-qemu $hostpath
sudo setfacl -d -Rm g:libvirt-qemu:rwx $hostpath
virt-manager
hostshare
<filesystem type="mount" accessmode="mapped" fmode="0660" dmode="0770">
<source dir="/vms/share"/>
<target dir="/hostshare"/>
<address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/>
</filesystem>
guest
guestpath=/share
#sudo chmod -R 777 $guestpath
sudo chown -R $USER:$USER $guestpath
mount
sudo mount -t 9p -o trans=virtio,rw,version=9p2000.L /hostshare $guestpath
sudo sh -c "echo '
# qemu share
/hostshare /share 9p trans=virtio,version=9p2000.L,rw,noauto 0 0' >> /etc/fstab"
import / export
domain
virsh --connect qemu:///system dumpxml $guest > $guest.xml
virsh --connect qemu:///system define $guest.xml
domain
virsh --connect qemu:///system dumpxml $guest > $guest.xml
virsh --connect qemu:///system define $guest.xml
disk
shrink
# linux
sudo dd if=/dev/zero of=/zero bs=4M
sudo rm /zero
# windows
sdelete -z C
qemu-img convert -O qcow2 $file $file2
reduce
qemu-img info $file
qemu-img resize -f qcow2 --shrink $file -10G
expand
qemu-img info $file
qemu-img resize $file +2G
ova
vm=tsurugi_linux_2022.1_vmdk
mkdir tmp
tar xf ${vm}.ova -C tmp/
qemu-img convert -f vmdk tmp/${vm}.vmdk tmp/${vm}.qcow2
qemu-img convert -O qcow2 tmp/${vm}-disk001.qcow2 ${vm}.qcow2