new vm location: /lmn/vm
This commit is contained in:
parent
bdbd4f8ce5
commit
8eb9748f93
4 changed files with 43 additions and 41 deletions
|
@ -3,7 +3,7 @@
|
|||
|
||||
set -eu
|
||||
# if less than two arguments supplied, display usage
|
||||
if [[ $# -ne 2 ]]; then
|
||||
if [[ $# -ne 2 ]]; then
|
||||
echo "This script takes as input the name of the VM to clone" >&2
|
||||
echo "Usage: $0 vm_name_orig vm_name_clone" >&2
|
||||
exit 1
|
||||
|
@ -11,16 +11,16 @@ fi
|
|||
|
||||
VM_NAME=$1
|
||||
VM_CLONE=$2
|
||||
VM_DIR="/tmp/${UID}/vmimages"
|
||||
VM_DIR="/tmp/${UID}/vm"
|
||||
|
||||
# Create User-VM-Dir and link system VM-Images
|
||||
[[ -d "${VM_DIR}/xml" ]] || mkdir -p "${VM_DIR}/xml"
|
||||
[[ -d "${VM_DIR}" ]] || mkdir -p "${VM_DIR}"
|
||||
sudo /usr/local/bin/link-images.sh
|
||||
|
||||
# change to image-directory
|
||||
cd "${VM_DIR}"
|
||||
|
||||
if { [[ ! -f "xml/${VM_NAME}.xml" ]] && [[ ! -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" ]]; } || [[ ! -f "${VM_NAME}.qcow2" ]]; then
|
||||
if { [[ ! -f "${VM_NAME}.xml" ]] && [[ ! -f "/lmn/vm/${VM_NAME}.xml" ]]; } || [[ ! -f "${VM_NAME}.qcow2" ]]; then
|
||||
echo "xml or qcow2 File does not exists." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
@ -30,10 +30,10 @@ chmod a-w "${VM_NAME}-${VM_CLONE}.qcow2"
|
|||
|
||||
# virsh --connect=qemu:///system dumpxml "${VM_NAME}" > "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||
# copy machine-definition-file
|
||||
if [[ -f "xml/${VM_NAME}.xml" ]]; then
|
||||
cp "xml/${VM_NAME}.xml" "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||
elif [[ -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" ]]; then
|
||||
cp "/var/lib/libvirt/images/xml/${VM_NAME}.xml" "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||
if [[ -f "${VM_NAME}.xml" ]]; then
|
||||
cp "${VM_NAME}.xml" "${VM_NAME}-${VM_CLONE}.xml"
|
||||
elif [[ -f "/lmn/vm/${VM_NAME}.xml" ]]; then
|
||||
cp "/lmn/vm/${VM_NAME}.xml" "${VM_NAME}-${VM_CLONE}.xml"
|
||||
else
|
||||
echo "no machine definition file found" >&2
|
||||
exit 1
|
||||
|
@ -41,8 +41,8 @@ fi
|
|||
|
||||
# hardware addresses need to be removed, libvirt will assign
|
||||
# new addresses automatically
|
||||
sed -i /uuid/d "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||
sed -i '/mac address/d' "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||
sed -i /uuid/d "${VM_NAME}-${VM_CLONE}.xml"
|
||||
sed -i '/mac address/d' "${VM_NAME}-${VM_CLONE}.xml"
|
||||
|
||||
# and actually rename the vm: (this also updates the storage path)
|
||||
sed -i "s/${VM_NAME}/${VM_NAME}-${VM_CLONE}/" "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||
sed -i "s/${VM_NAME}/${VM_NAME}-${VM_CLONE}/" "${VM_NAME}-${VM_CLONE}.xml"
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/bash
|
||||
set -eu
|
||||
|
||||
share="/srv/samba/schools/default-school/share/"
|
||||
home="$(getent passwd "$SUDO_UID" | cut -d : -f 6 | sed 's|/srv/samba/schools/default-school/||')"
|
||||
|
||||
exit_script() {
|
||||
|
|
|
@ -31,7 +31,7 @@ if [[ $# -ne 1 ]]; then
|
|||
fi
|
||||
|
||||
# change to Images directory
|
||||
VM_DIR="/tmp/${UID}/vmimages"
|
||||
VM_DIR="/tmp/${UID}/vm"
|
||||
cd "${VM_DIR}"
|
||||
|
||||
VM_NAME="$1"
|
||||
|
@ -68,15 +68,15 @@ qemu-img rebase -f qcow2 -b "${NEWBASE}" -F qcow2 "${VM_NAME}.qcow2"
|
|||
if [[ -v NEWNAME ]]; then
|
||||
# copy and adapt machine definition file
|
||||
CURRENTNAME="${CURRENTBASE/.qcow2/}"
|
||||
if [[ -f "xml/${CURRENTNAME}.xml" ]]; then
|
||||
cp "xml/${CURRENTNAME}.xml" "xml/${NEWNAME}.xml"
|
||||
elif [[ -f "/var/lib/libvirt/images/xml/${CURRENTNAME}.xml" ]]; then
|
||||
cp "/var/lib/libvirt/images/xml/${CURRENTNAME}.xml" "xml/${NEWNAME}.xml"
|
||||
if [[ -f "${CURRENTNAME}.xml" ]]; then
|
||||
cp "${CURRENTNAME}.xml" "${NEWNAME}.xml"
|
||||
elif [[ -f "/lmn/vm/${CURRENTNAME}.xml" ]]; then
|
||||
cp "/lmn/vm/${CURRENTNAME}.xml" "${NEWNAME}.xml"
|
||||
else
|
||||
echo "no machine definition file found" >&2
|
||||
exit 1
|
||||
fi
|
||||
sed -i "s/${CURRENTNAME}/${NEWNAME}/" "xml/${NEWNAME}.xml"
|
||||
sed -i "s/${CURRENTNAME}/${NEWNAME}/" "${NEWNAME}.xml"
|
||||
NEWNAME="${NEWNAME}.qcow2"
|
||||
else
|
||||
rm "${CURRENTBASE}"
|
||||
|
|
|
@ -9,43 +9,46 @@ Create torrent and upload disk, torrent and xml-VM-Definiton on server.
|
|||
EOF
|
||||
}
|
||||
|
||||
VM_DIR="/tmp/${SUDO_UID}/vmimages"
|
||||
VM_DIR="/tmp/${SUDO_UID}/vm"
|
||||
|
||||
upload_image() {
|
||||
# check if VM-Diskimage exists
|
||||
if [[ ! (-f "/var/lib/libvirt/images/${VM_NAME}.qcow2" || -f "${VM_DIR}/${VM_NAME}.qcow2") ]]; then
|
||||
if [[ ! (-f "/lmn/vm/${VM_NAME}.qcow2" || -f "${VM_DIR}/${VM_NAME}.qcow2") ]]; then
|
||||
echo "File not found ${VM_NAME}.qcow2" >&2
|
||||
exit 1
|
||||
fi
|
||||
# link private VM-Diskimage to system-Dir
|
||||
if [[ -f "${VM_DIR}/${VM_NAME}.qcow2" \
|
||||
&& ( -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" && ("${VM_DIR}/${VM_NAME}.qcow2" -nt "/var/lib/libvirt/images/${VM_NAME}.qcow2") \
|
||||
|| ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2") ]]; then
|
||||
echo "copy private VM-Diskimage to system-dir"
|
||||
ln -f "${VM_DIR}/${VM_NAME}.qcow2" "/var/lib/libvirt/images/${VM_NAME}.qcow2"
|
||||
fi
|
||||
# check if VM-Machine-Definition XML exists
|
||||
if [[ ! (-f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" || -f "${VM_DIR}/xml/${VM_NAME}.xml") ]]; then
|
||||
if [[ ! (-f "/lmn/vm/${VM_NAME}.xml" || -f "${VM_DIR}/${VM_NAME}.xml") ]]; then
|
||||
echo "File not found ${VM_NAME}.xml" >&2
|
||||
exit 1
|
||||
fi
|
||||
# copy private VM-Maschine-Definition XML to system-Dir
|
||||
if [[ -f "${VM_DIR}/xml/${VM_NAME}.xml" \
|
||||
&& ( -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" && $(cmp -s "${VM_DIR}/xml/${VM_NAME}.xml" "/var/lib/libvirt/images/xml/${VM_NAME}.xml") \
|
||||
|| ! -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml") ]]; then
|
||||
echo "copy private VM-Maschine-Definition XML to system-dir"
|
||||
cp "${VM_DIR}/xml/${VM_NAME}.xml" "/var/lib/libvirt/images/xml/"
|
||||
sudo -u lmnsynci /usr/local/bin/vmimage-torrent stop "${VM_NAME}.qcow2"
|
||||
# link private VM-Diskimage to system-Dir
|
||||
if [[ -f "${VM_DIR}/${VM_NAME}.qcow2" \
|
||||
&& ( -f "/lmn/vm/${VM_NAME}.qcow2" && ("${VM_DIR}/${VM_NAME}.qcow2" -nt "/lmn/vm/${VM_NAME}.qcow2") \
|
||||
|| ! -f "/lmn/vm/${VM_NAME}.qcow2") ]]; then
|
||||
echo "copy private VM-Diskimage to system-dir"
|
||||
chown lmnsynci:lmnsynci "${VM_DIR}/${VM_NAME}.qcow2"
|
||||
ln -f "${VM_DIR}/${VM_NAME}.qcow2" "/lmn/vm/${VM_NAME}.qcow2"
|
||||
fi
|
||||
cd /var/lib/libvirt/images
|
||||
# copy private VM-Maschine-Definition XML to system-Dir
|
||||
if [[ -f "${VM_DIR}/${VM_NAME}.xml" \
|
||||
&& ( -f "/lmn/vm/${VM_NAME}.xml" && $(cmp -s "${VM_DIR}/${VM_NAME}.xml" "/lmn/vm/${VM_NAME}.xml") \
|
||||
|| ! -f "/lmn/vm/${VM_NAME}.xml") ]]; then
|
||||
echo "copy private VM-Maschine-Definition XML to system-dir"
|
||||
chown lmnsynci:lmnsynci "${VM_DIR}/${VM_NAME}.xml"
|
||||
cp -a "${VM_DIR}/${VM_NAME}.xml" "/lmn/vm/"
|
||||
fi
|
||||
cd /lmn/vm
|
||||
# (re-) create torrent file
|
||||
/usr/local/bin/vmimage-torrent create "${VM_NAME}.qcow2"
|
||||
sudo -u lmnsynci /usr/local/bin/vmimage-torrent create "${VM_NAME}.qcow2"
|
||||
# Upload Torrent, qcow2 and machine-definition-XML
|
||||
[[ -f "/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" ]] && rsync -av --password-file=/etc/rsync.secret \
|
||||
"/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" rsync://vmuser@server:/vmimages-upload/
|
||||
rsync -av --password-file=/etc/rsync.secret "/var/lib/libvirt/images/${VM_NAME}.qcow2" \
|
||||
[[ -f "/lmn/vm/${VM_NAME}.qcow2.torrent" ]] && rsync -av --password-file=/etc/rsync.secret \
|
||||
"/lmn/vm/${VM_NAME}.qcow2.torrent" rsync://vmuser@server:/vmimages-upload/
|
||||
rsync -av --password-file=/etc/rsync.secret "/lmn/vm/${VM_NAME}.qcow2" \
|
||||
rsync://vmuser@server:/vmimages-upload/
|
||||
rsync -av --password-file=/etc/rsync.secret "/lmn/vm/${VM_NAME}.xml" \
|
||||
rsync://vmuser@server:/vmimages-upload/
|
||||
rsync -av --password-file=/etc/rsync.secret "/var/lib/libvirt/images/xml/${VM_NAME}.xml" \
|
||||
rsync://vmuser@server:/vmimages-upload/xml/
|
||||
}
|
||||
|
||||
# if less than one arguments supplied, display usage
|
||||
|
|
Loading…
Add table
Reference in a new issue