14 changed files with 193 additions and 7 deletions
@ -0,0 +1,8 @@ |
|||
FROM edge-registry.itix.fr/demo-edge-retail/base:latest |
|||
|
|||
ADD --chown=root:root root / |
|||
|
|||
RUN <<EOF |
|||
set -Eeuo pipefail |
|||
systemctl enable migrate-vm@printserver.service |
|||
EOF |
|||
@ -0,0 +1,10 @@ |
|||
DOMAIN_VCPUS=4 |
|||
DOMAIN_RAM=8192 |
|||
DOMAIN_OS_VARIANT=win11 |
|||
DOMAIN_MAC_ADDRESS=04:00:00:00:00:02 |
|||
RCLONE_CONFIG_HYPERV_TYPE=smb |
|||
RCLONE_CONFIG_HYPERV_HOST=192.168.2.72 |
|||
RCLONE_CONFIG_HYPERV_USER=Red Hat |
|||
RCLONE_CONFIG_HYPERV_PASS=3EIvjq3rHcHYcfktOS6HpZyvxefr1A |
|||
DOMAIN_HYPERV_DISK_LOCATION=hyperv:c$/ProgramData/Microsoft/Windows/Virtual Hard Disks/Print Server.vhdx |
|||
DOMAIN_HYPERV_DISK_NAME=Print Server.vhdx |
|||
@ -0,0 +1,18 @@ |
|||
#!/bin/bash |
|||
|
|||
set -Eeuo pipefail |
|||
MAX_ATTEMPTS=60 |
|||
|
|||
for (( attempt=1; attempt<=MAX_ATTEMPTS; attempt++ )); do |
|||
echo "Checking VM ($attempt/$MAX_ATTEMPTS)..." |
|||
|
|||
if virsh domstate printserver | grep -q 'running'; then |
|||
echo "The printserver VM is running." |
|||
exit 0 |
|||
fi |
|||
|
|||
sleep 5 |
|||
done |
|||
|
|||
echo "printserver VM is not running correctly after $MAX_ATTEMPTS attempts!" |
|||
exit 1 |
|||
@ -0,0 +1 @@ |
|||
../default.xml |
|||
@ -0,0 +1,19 @@ |
|||
<network> |
|||
<name>default</name> |
|||
<bridge name="virbr0" stp="on" delay="5" /> |
|||
<forward mode='nat' /> |
|||
<domain name="libvirt.test" /> |
|||
<dns> |
|||
<host ip='192.168.122.1'> |
|||
<hostname>host</hostname> |
|||
</host> |
|||
</dns> |
|||
<ip address="192.168.122.1" netmask="255.255.255.0" localPtr="yes"> |
|||
<dhcp> |
|||
<range start="192.168.122.100" end="192.168.122.200"> |
|||
<lease expiry='24' unit='hours'/> |
|||
</range> |
|||
<host mac="04:00:00:00:00:01" name="nextcloud" ip="192.168.122.2" /> |
|||
</dhcp> |
|||
</ip> |
|||
</network> |
|||
@ -0,0 +1,19 @@ |
|||
[Unit] |
|||
Description=RHDE VM Migration Service |
|||
Documentation=man:systemd.service(5) |
|||
After=network-online.target |
|||
Wants=network-online.target |
|||
|
|||
# Only start if the VM root disk does not exist |
|||
ConditionPathExists=!/var/lib/libvirt/images/%i/root.qcow2 |
|||
|
|||
# Remain started to avoid race conditions |
|||
Persistent=true |
|||
|
|||
[Service] |
|||
Type=oneshot |
|||
ExecStart=/usr/local/bin/migrate-vm.sh %i |
|||
EnvironmentFile=/etc/default/migrate-vm-%i.env |
|||
|
|||
[Install] |
|||
WantedBy=multi-user.target |
|||
@ -0,0 +1,56 @@ |
|||
#!/bin/bash |
|||
|
|||
set -Eeuo pipefail |
|||
|
|||
if [[ $# -ne 1 ]]; then |
|||
echo "Usage: $0 <vm-name>" |
|||
exit 1 |
|||
fi |
|||
|
|||
VM="${1}" |
|||
if [ -d "/var/lib/libvirt/images/${VM}/" ]; then |
|||
echo "VM ${VM} already exists. Please remove it first." |
|||
exit 1 |
|||
fi |
|||
|
|||
temp_dir=$(mktemp -d) |
|||
cleanup() { |
|||
local exit_code=$? |
|||
rm -rf "$temp_dir" |
|||
if [ $exit_code -ne 0 ]; then |
|||
echo "An error occurred. Cleaning up..." |
|||
virsh destroy "${VM}" || true |
|||
virsh undefine "${VM}" --nvram || true |
|||
rm -rf "/var/lib/libvirt/images/${VM}/" |
|||
fi |
|||
} |
|||
trap cleanup EXIT |
|||
|
|||
# Create a temporary directory to hold the VM image and copy the base image there |
|||
install -m 0710 -o root -g qemu --context=system_u:object_r:virt_image_t:s0 -d "$temp_dir" |
|||
install -m 0710 -o root -g qemu -Z -d "/var/lib/libvirt/images/${VM}" |
|||
|
|||
# Migrate the VM disk from Hyper-V to KVM |
|||
echo "Copying the VM disk from Hyper-V..." |
|||
rclone copy "$DOMAIN_HYPERV_DISK_LOCATION" "$temp_dir" |
|||
echo "Converting the VM disk to qcow2 format..." |
|||
qemu-img convert -O qcow2 "$temp_dir/$DOMAIN_HYPERV_DISK_NAME" "/var/lib/libvirt/images/${VM}/root.qcow2" |
|||
restorecon -F "/var/lib/libvirt/images/${VM}/root.qcow2" |
|||
|
|||
# Create and start the VM using virt-install |
|||
echo "Creating and starting the VM ${VM}..." |
|||
virt-install --name "${VM}" \ |
|||
--autostart \ |
|||
--cpu=host-passthrough \ |
|||
--vcpus=${DOMAIN_VCPUS} \ |
|||
--ram=${DOMAIN_RAM} \ |
|||
--os-variant=${DOMAIN_OS_VARIANT} \ |
|||
--disk=path=/var/lib/libvirt/images/${VM}/root.qcow2,bus=sata,format=qcow2 \ |
|||
--disk=path=/usr/share/virtio-win/virtio-win.iso,device=cdrom,bus=sata \ |
|||
--boot uefi \ |
|||
--import \ |
|||
--network=network=default,mac=${DOMAIN_MAC_ADDRESS} \ |
|||
--noautoconsole |
|||
|
|||
echo "VM ${VM} has been created and started." |
|||
exit 0 |
|||
@ -1,2 +0,0 @@ |
|||
qcow2 |
|||
manifest-qcow2.json |
|||
Loading…
Reference in new issue