#!/bin/bash # Copyright 2015 Jelastic, Inc. # 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. # this library requires bash version 4; [ -n "${VIRTUOZZOLIB_VERSION:-}" ] && return 0; VIRTUOZZOLIB_VERSION="0.2"; $PROGRAM 'vzctl'; $PROGRAM 'vzlist'; $PROGRAM 'grep'; # create a user container on HN # params: # $1 - CTID # $2 - array with params # $3 - Template name (default centos-5) function vzCreateContainer() { : ${1:?"missing CTID param"}; local template="${3:-"centos-5"}" params=${2} ctid=${1}; ${VZCTL} create ${ctid} --ostemplate "${template}-x86_64"; [ -z "${params}" ] && vzSetParams $ctid $params; return ${?}; } function vzIsContainerExists() { : ${1:?"Missing CTID param"}; local ctid=$1 vzlist -H1 $ctid &>/dev/null return $? } function isVMexist(){ : ${1:?"Missing ENVID param"}; local envid=$1 prlctl list -H $envid &>/dev/null && return 0 || return 1 } function isVEexist(){ : ${1:?"Missing VEID param"}; local veid=$1 if isUUID $veid; then isVMexist $veid && return 0 || return 1 else vzIsContainerExists $veid && return 0 || return 1 fi } # # readFileFromCtontainer # # ARGS: # CTID:integer - container CTID # File:string - full path to file to read # RESULT: # 0 - if file readed successfully # 1 - file read error # 2 - CTID not found # function return file content to stdout function readFileFromCtontainer() { : ${1:?"Missing CTID param"}; : ${2:?"Missing FILE param"}; local CTID=$1 fileName=$2; vzIsContainerExists ${CTID} || { return 2; }; cat "/vz/root/${CTID}/${filename}"; return $?; } # # isContainerRunning # # ARGS: # CTID:integer - container CTID # RESULT: # 0 - container status is running # 1 - container is stopped,mounted,suspended function isContainerRunning() { : ${1:?"Missing CTID param"}; local CTID=$1 out=$($VZLIST -Ho status $CTID) if [[ "x$out" != "xrunning" ]] ; then return 1 fi return 0 } function isVERunning() { : ${1:?"Missing CTID or UUID param"}; if isUUID $1; then out=$($PRLCTL list -Ho status $1) else out=$($VZLIST -Ho status $1) fi if [[ "x$out" != "xrunning" ]] ; then return 1 fi return 0 } function getCtPrivate(){ : ${1:?"Missing CTID param"}; local CTID=$1 vzlist $CTID -Ho private 2>/dev/null return $?; } function getCtRoot(){ : ${1:?"Missing CTID param"}; local CTID=$1 vzlist $CTID -Ho root 2>/dev/null return $?; } function getCtVeConf(){ : ${1:?"Missing CTID param"}; local CTID=$1 local private=$(getCtPrivate $CTID) if [ ! -z $private -a -d $private ] then echo "${private}/ve.conf" else echo /vz/private/${CTID}/ve.conf fi } # # isContainerMounted # # ARGS: # CTID:integer - container CTID # RESULT: # 0 - container status is mounted (either running or stopped/suspended but explicitly mounted) # 1 - container is not mounted function isContainerMounted() { : ${1:?"Missing CTID param"}; local CTID=$1 out=$($VZLIST -Ho status $CTID) [ "x$out" == "xmounted" -o "x$out" == "xrunning" ] } function isContainerMountedStopped() { : ${1:?"Missing CTID param"}; local CTID=$1 out=$($VZLIST -Ho status $CTID) [ "x$out" == "xmounted" ] } function isContainerLocked() { : ${1:?"Missing CTID param"} [ -e "/vz/lock/$1.lck" ] } function mountVM(){ : ${1:?"Missing UUID param"} local UUID=$1 isVERunning ${UUID} && return 255 local MOUNTPOINT=${2:-/vz/tmp/${UUID}} local PRIVATE=$(prlctl list ${UUID} -Ho private) local HDDS=( $(find "$PRIVATE" -name 'harddisk*.hdd' 2>/dev/null) ) (( ${#HDDS[@]} == 1 )) || return 255 local HDD=${HDDS[0]} mkdir -p "$MOUNTPOINT" mountpoint -q "$MOUNTPOINT" && umount "$MOUNTPOINT" guestmount -a $HDD -i --rw "$MOUNTPOINT" >> ${JEM_CALLS_LOG} 2>&1; return $? } mountVE(){ : ${1:?"Missing CTID or UUID param"}; if isUUID $1; then mountVM $@ return $? else $VZCTL mount $CTID > /dev/null 2>&1 return $? fi } umountVE(){ : ${1:?"Missing CTID or UUID param"}; if isUUID $1; then local mountpoint=${2:-/vz/tmp/${UUID}} umount $mountpoint 2>&1 && rm -r $mountpoint return $? else $VZCTL umount $1 > /dev/null 2>&1 return $? fi } function populateContainerVarsVEID() { : ${1:?"Missing CTID param"} vzIsContainerExists $1 || return 3 local _root=$(getCtRoot $1) local _private=$(getCtPrivate $1) local need_umount=0 if ! isContainerMounted $1 ; then $VZCTL mount $1 &>>/dev/null need_umount=1 fi local issue_file="$(vzctPath $_root /etc/issue )" local release_files="$(vzctPath $_root /etc/)/*-release" local issue_string=$(set +f; cat $release_files $issue_file 2>/dev/null) local _os=$(detectOS "$issue_string") local _os_ver=$(getOSVersion "$issue_string") local _v="VE$1" local _cmd="${_v}_ROOT=$_root;${_v}_PRIVATE=$_private;${_v}_OS=$_os;${_v}_OS_VER=${_os_ver%%.*}" eval $_cmd [[ $need_umount -eq 1 ]] && $VZCTL umount $1 &>>/dev/null return 0; } function populateVMVarsVEID() { : ${1:?"Missing ENVID param"} isVMexist $1 || return 3 local _root="/vz/tmp/$1" local _private="prlctl list ${ENVID} -Ho private" if ! isVERunning $1 ; then $VZCTL mount $1 &>>/dev/null need_umount=1 local issue_file="$(vzctPath $VE_ROOT /etc/issue )" local release_files="$(vzctPath $VE_ROOT /etc/)/*-release" issue_string=$(set +f; cat $release_files $issue_file 2>/dev/null); else VEExecRun "set +f; cat /etc/*-release /etc/issue 2>/dev/null" issue_string="$(VEexecGetLastStdOut)" fi local _os=$(detectOS "$issue_string") local _os_ver=$(getOSVersion "$issue_string") if [[ "$OS" == "ubuntu" ]]; then SED_COPY_OPTION='' fi local _v="VE$1" local _cmd="${_v}_ROOT=$_root;${_v}_PRIVATE=$_private;${_v}_OS=$_os;${_v}_OS_VER=${_os_ver%%.*}" eval $_cmd [[ $need_umount -eq 1 ]] && $umountVE $1 &>>/dev/null return 0; } function populateVEVarsVZ() { : ${1:?"Missing VEID param"} if isUUID $1; then populateVMVarsVEID $1 || return $? else populateContainerVarsVEID $1 || return $? fi local _cmd="VZ_VEID_ROOT=\$VE$1_ROOT;VZ_VEID_PRIVATE=\$VE$1_PRIVATE;VZ_VEID_OS=\$VE$1_OS;VZ_VEID_OS_VER=\$VE$1_OS_VER" eval $_cmd return 0 }