Skip to content

Instantly share code, notes, and snippets.

@oliviertoupin
Created December 7, 2016 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliviertoupin/69657df6c35c467669cffa77639e9cdc to your computer and use it in GitHub Desktop.
Save oliviertoupin/69657df6c35c467669cffa77639e9cdc to your computer and use it in GitHub Desktop.
fs_lvm fixes
#!/bin/bash
### Patched version: Keep qcow2 format when image is qcow2.
# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
# clone fe:SOURCE host:remote_system_ds/disk.i size
# - fe is the front-end hostname
# - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk
# - host is the target host to deploy the VM
# - remote_system_ds is the path for the system datastore in the host
SRC=$1
DST=$2
VM_ID=$3
DS_ID=$4
if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi
DRIVER_PATH=$(dirname $0)
source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/libfs.sh
#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
DST_DIR=`dirname $DST_PATH`
SRC_ARG_PATH=`arg_path $SRC`
DST_DS_PATH="$(dirname $(dirname $(dirname $DST_PATH)))"
SRC_DS_PATH="$(dirname $(dirname $SRC_ARG_PATH))"
SRC_PATH="${DST_DS_PATH}${SRC_ARG_PATH##$SRC_DS_PATH}"
DS_SYS_ID=$(echo $DST_DIR | $AWK -F '/' '{print $(NF-1)}')
#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------
DISK_ID=$(basename ${DST_PATH} | cut -d. -f2)
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
unset i j XPATH_ELEMENTS
while IFS= read -r -d '' element; do
XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VM_ID| $XPATH \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SIZE \
/VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/ORIGINAL_SIZE \
/VM/HISTORY_RECORDS/HISTORY[last\(\)]/TM_MAD)
SIZE="${XPATH_ELEMENTS[j++]}"
ORIGINAL_SIZE="${XPATH_ELEMENTS[j++]}"
TM_MAD="${XPATH_ELEMENTS[j++]}"
FILE_SIZE=`fs_size $SRC_PATH`
if [ $FILE_SIZE -gt $SIZE ]; then
SIZE="$FILE_SIZE"
fi
#-------------------------------------------------------------------------------
# Create the snapshot and link it
#-------------------------------------------------------------------------------
LV_NAME="lv-one-$VM_ID-$DISK_ID"
VG_NAME="vg-one-$DS_SYS_ID"
DEV="/dev/${VG_NAME}/${LV_NAME}"
CLONE_CMD=$(cat <<EOF
set -e -o pipefail
mkdir -p $DST_DIR
$SUDO $LVCREATE --wipesignatures n -L${SIZE}M -n $LV_NAME $VG_NAME
dd if="$SRC_PATH" of="$DEV" bs=64k
qemu-img resize $DEV ${SIZE}M || true
rm -f "$DST_PATH"
ln -s "$DEV" "$DST_PATH"
EOF
)
ssh_exec_and_log "$DST_HOST" "$CLONE_CMD" \
"Error cloning $SRC_PATH to $LV_NAME"
exit 0
#!/bin/python
#
# Fix the inactive lv for a specific host and VM
#
import glob
import os
import os.path
import base64
import sys
import xml.etree.ElementTree as ET
datastores_path = "/var/lib/one/datastores/"
vm_template_64 = sys.argv[2]
vm_template = base64.b64decode(vm_template_64)
vm_template_xml = ET.fromstring(vm_template)
vm_id = vm_template_xml.findall("ID")[0].text
# Scan the datastores for disks matching this VM
disks = glob.glob('{0}*/{1}/disk.*'.format(datastores_path, vm_id))
# Find broken symlink
possible_lvs = filter(lambda p: os.path.islink(p) and not os.path.exists(p), disks)
possible_lvs_with_path = map(lambda p: (p, os.path.realpath(p)), possible_lvs)
real_lvs = filter(lambda t: 'vg-one' in t[1], possible_lvs_with_path)
# Activate the one that matched an lvm lv
for lv_volume in real_lvs:
print 'Activating LVM {0} for VM {1}'.format(lv_volume[0], vm_id)
return_code = os.system('sudo lvchange -a y {0}'.format(lv_volume[1]))
if return_code != 0:
sys.exit(return_code)
#!/bin/bash
### Patched version: Fix image migration
# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
# MV <hostA:system_ds/disk.i|hostB:system_ds/disk.i> vmid dsid
# <hostA:system_ds/|hostB:system_ds/>
# - hostX is the target host to deploy the VM
# - system_ds is the path for the system datastore in the host
# - vmid is the id of the VM
# - dsid is the target datastore (0 is the system datastore)
SRC=$1
DST=$2
VMID=$3
DSID=$4
if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi
. $TMCOMMON
SRC=`fix_dir_slashes $SRC`
DST=`fix_dir_slashes $DST`
SRC_PATH=`arg_path $SRC`
DST_PATH=`arg_path $DST`
SRC_HOST=`arg_host $SRC`
DST_HOST=`arg_host $DST`
DST_DIR=`dirname $DST_PATH`
SRC_DS_DIR=`dirname $SRC_PATH`
SRC_VM_DIR=`basename $SRC_PATH`
ssh_make_path "$DST_HOST" "$DST_DIR" "ssh"
CMD=$(cat <<EOF
set -ex -o pipefail
# If it's a directory make sure to add "/" to copy folder content and not the folder itself
if [ -d "$SRC_PATH" ]
then
rsync -av $SRC_PATH/ $DST/
else
rsync -av $SRC_PATH $DST
fi
EOF
)
ssh_exec_and_log "$SRC_HOST" "$CMD" \
"Error moving VM files to another System DS: $SRC_PATH to $DST_PATH in $SRC_HOST"
CMD=$(cat <<EOF
set -ex -o pipefail
if [ -L "$DST_PATH" ]; then
DEVICE=\$(readlink "$DST_PATH")
$SUDO $SYNC
$SUDO $LVSCAN
$SUDO $LVCHANGE -ay \$DEVICE
fi
EOF
)
ssh_exec_and_log "$DST_HOST" "$CMD" \
"Error activating disk $DST_PATH"
VM_HOOK = [
name = "fs_lvm_fixes",
on = "CUSTOM",
state = "ACTIVE",
lcm_state = "POWEROFF",
command = "gvs/fs_lvm_fixes.py",
remote = "yes",
arguments = "$ID $TEMPLATE" ]
VM_HOOK = [
name = "fs_lvm_fixes",
on = "CUSTOM",
state = "ACTIVE",
lcm_state = "BOOT_POWEROFF",
command = "gvs/fs_lvm_fixes.py",
remote = "yes",
arguments = "$ID $TEMPLATE" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment