-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathlibvirt_init
More file actions
executable file
·96 lines (83 loc) · 3.48 KB
/
libvirt_init
File metadata and controls
executable file
·96 lines (83 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Intialize libvirt config storage
# Invoked by emhttp after mounting libvirt loopback but before starting libvirt.
# run & log functions
. /etc/rc.d/rc.runlog
# Sync domain data if IMAGE_FILE and OLD_IMAGE_FILE differ
DOMAIN_CFG=/boot/config/domain.cfg
# Read values from domain.cfg
eval $(grep -E '^(IMAGE_FILE|OLD_IMAGE_FILE)=' "$DOMAIN_CFG")
# Remove quotes
IMAGE_FILE="${IMAGE_FILE%\"}"
IMAGE_FILE="${IMAGE_FILE#\"}"
OLD_IMAGE_FILE="${OLD_IMAGE_FILE%\"}"
OLD_IMAGE_FILE="${OLD_IMAGE_FILE#\"}"
# Proceed only if both variables are set and OLD_IMAGE_FILE exists
if [ -n "$IMAGE_FILE" ] && [ -n "$OLD_IMAGE_FILE" ] && [ "$IMAGE_FILE" != "$OLD_IMAGE_FILE" ]; then
if [ ! -e "$OLD_IMAGE_FILE" ]; then
log "OLD_IMAGE_FILE not found: $OLD_IMAGE_FILE — skipping sync"
else
log "IMAGE_FILE and OLD_IMAGE_FILE differ, syncing..."
TMP_MNT=/etc/libvirt-sync
IMG_FILE_NAME=$(basename "$IMAGE_FILE")
OLD_IMG_FILE_NAME=$(basename "$OLD_IMAGE_FILE")
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
if [[ "$OLD_IMAGE_FILE" == *.img ]]; then
# Backup image before mounting
BACKUP_PATH="${OLD_IMAGE_FILE%.img}.bak-${TIMESTAMP}.img"
log "Creating backup of OLD_IMAGE_FILE: $BACKUP_PATH"
cp -p "$OLD_IMAGE_FILE" "$BACKUP_PATH"
log "Mounting $OLD_IMAGE_FILE to $TMP_MNT"
mkdir -p "$TMP_MNT"
mount "$OLD_IMAGE_FILE" "$TMP_MNT"
log "Copying full contents from image to directory $IMAGE_FILE"
rsync -a --exclude="$OLD_IMG_FILE_NAME" "$TMP_MNT/" "$IMAGE_FILE/"
umount "$TMP_MNT"
elif [[ "$IMAGE_FILE" == *.img ]]; then
log "Mounting $IMAGE_FILE to $TMP_MNT"
mkdir -p "$TMP_MNT"
mount "$IMAGE_FILE" "$TMP_MNT"
log "Copying full contents from directory $OLD_IMAGE_FILE to image"
rsync -a --exclude="$IMG_FILE_NAME" --exclude='*.bak-*.img' "$OLD_IMAGE_FILE/" "$TMP_MNT/"
umount "$TMP_MNT"
else
log "Both IMAGE_FILE and OLD_IMAGE_FILE are directories, copying full contents"
rsync -a --exclude="$IMG_FILE_NAME" "$OLD_IMAGE_FILE/" "$IMAGE_FILE/"
fi
# Update OLD_IMAGE_FILE in domain.cfg
log "Updating OLD_IMAGE_FILE in $DOMAIN_CFG"
sed -i "s|^OLD_IMAGE_FILE=.*|OLD_IMAGE_FILE=\"$IMAGE_FILE\"|" "$DOMAIN_CFG"
fi
else
log "IMAGE_FILE and OLD_IMAGE_FILE match, or one is unset — skipping sync"
fi
# missing qemu directory would indicate new libvirt image file created
if [ ! -d /etc/libvirt/qemu ]; then
log "initializing /etc/libvirt"
# initialize with default settings
cp -rp /etc/libvirt-/* /etc/libvirt
# check if libvirt image file exists on USB flash
OLD_IMAGE=/boot/config/plugins/dynamix.kvm.manager/domain.img
if [ ! -f $OLD_IMAGE ]; then
OLD_IMAGE=/boot/config/plugins/virtMan/virtMan.img
if [ ! -f $OLD_IMAGE ]; then
OLD_IMAGE=
fi
fi
if [ "$OLD_IMAGE" != "" ]; then
# found existing image, use qemu config from there
rm -rf /etc/libvirt/qemu/*
mount $OLD_IMAGE /etc/libvirt-
cp -rp /etc/libvirt-/qemu/* /etc/libvirt/qemu
if [ -f /etc/libvirt-/hooks/qemu ]; then
cp -p /etc/libvirt-/hooks/qemu /etc/libvirt/hooks/qemu
fi
umount /etc/libvirt-
fi
fi
# if vfio-pci bind error, prevent autostart
if [ -s /var/log/vfio-pci-errors ]; then
mkdir -p /run/libvirt/qemu
echo "vfio-pci bind error" > /run/libvirt/qemu/autostarted
/usr/local/emhttp/webGui/scripts/notify -e "VM Autostart disabled" -s "vfio-pci-errors " -d "VM Autostart disabled due to vfio-bind error" -m "Please review /var/log/vfio-pci-errors" -i "alert" -l "/VMs"
fi