| #!/bin/bash |
| # Copyright (C) 2000-2006 SWsoft. All rights reserved. |
| # |
| # This program is free software; you can redistribute it and/or modify |
| # it under the terms of the GNU General Public License as published by |
| # the Free Software Foundation; either version 2 of the License, or |
| # (at your option) any later version. |
| # |
| # This program is distributed in the hope that it will be useful, |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| # GNU General Public License for more details. |
| # |
| # You should have received a copy of the GNU General Public License |
| # along with this program; if not, write to the Free Software |
| # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| # |
| # |
| |
| # Set the sane umask |
| umask 022 |
| |
| # Error codes |
| VZ_INVALID_PARAMETER_SYNTAX=20 |
| VZ_FS_NO_DISK_SPACE=46 |
| VZ_FS_BAD_TMPL=47 |
| VZ_FS_NEW_VE_PRVT=48 |
| VZ_CHANGEPASS=74 |
| VZ_CANT_ADDIP=34 |
| VZ_IP_INUSE=78 |
| |
| # Prints error message and exits |
| # Parameters: |
| # $1 - error message |
| # $2 - exit code |
| # Example of usage: |
| # error "Fatal error" 1 |
| function error() |
| { |
| # print errors to stdout too |
| ERR=$? |
| echo "$SELFNAME ERROR: $1" |
| exit $2 |
| } |
| |
| # Puts line |
| # NAME="value" |
| # to config file. If NAME is found, line gets replaced, |
| # otherwise it is added to the end of file. |
| # Parameters: |
| # $1 - config file |
| # $2 - NAME |
| # $3 - value |
| function put_param() |
| { |
| local file="$1" |
| local name="$2" |
| local value="$3" |
| local path |
| |
| path=${file%/*} |
| if [ ! -d "${path}" ]; then |
| mkdir -p ${path} || error "Unable to create dir ${path}" $VZ_FS_NO_DISK_SPACE |
| fi |
| if grep -E "^$name=.*" $file>/dev/null 2>&1; then |
| /bin/cp -fp ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE |
| /bin/sed -e "s|^$name=.*|$name=\"$value\"|" < ${file} > ${file}.$$ |
| if [ $? -ne 0 ]; then |
| rm -f ${file}.$$ 2>/dev/null |
| error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| mv -f ${file}.$$ ${file} |
| else |
| echo "$name=\"$value\"" >> $file || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| } |
| |
| # Puts line |
| # NAME value |
| # to config file. If NAME is found, line gets replaced, |
| # otherwise it is added to the end of file. |
| # Parameters: |
| # $1 - config file |
| # $2 - NAME |
| # $3 - value |
| function put_param2() |
| { |
| local file="$1" |
| local name="$2" |
| local value="$3" |
| local path; |
| |
| path=${file%/*} |
| if [ ! -d "${path}" ]; then |
| mkdir -p ${path} || error "Unable to create dir ${path}" $VZ_FS_NO_DISK_SPACE |
| fi |
| if grep -E "^\<$name\>" $file>/dev/null 2>&1; then |
| /bin/cp -fp ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE |
| /bin/sed -e "s|^\<$name\>.*|$name $value|" < ${file} > ${file}.$$ |
| if [ $? -ne 0 ]; then |
| rm -f ${file}.$$ 2>/dev/null |
| error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| mv -f ${file}.$$ ${file} |
| else |
| echo "$name $value" >> $file || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| } |
| |
| # Puts line |
| # NAME=( value ) |
| # to config file. If NAME is found, line gets replaced, |
| # otherwise it is added to the end of file. |
| # Parameters: |
| # $1 - config file |
| # $2 - NAME |
| # $3 - value |
| function put_param3() { |
| local file=$1 |
| local name=$2 |
| local value=$3 |
| local path |
| |
| path=${file%/*} |
| if [ ! -d "${path}" ]; then |
| mkdir -p ${path} || error "Unable to create dir ${path}" $VZ_FS_NO_DISK_SPACE |
| fi |
| if grep -E "^$name=\(.*\)" $file>/dev/null 2>&1; then |
| /bin/cp -fp ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE |
| if [ -z "${value}" ]; then |
| /bin/sed -e "s|^$name=\(.*\)|$name=\( \)|" < ${file} > ${file}.$$ |
| else |
| /bin/sed -e "s|^$name=\(.*\)|$name=\( \"$value\" \)|" < ${file} > ${file}.$$ |
| fi |
| if [ $? -ne 0 ]; then |
| rm -f ${file}.$$ 2>/dev/null |
| error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| mv -f ${file}.$$ ${file} |
| else |
| if [ -z "${value}" ]; then |
| echo "$name=( )" >> $file || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| else |
| echo "$name=( \"$value\" )" >> $file || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| fi |
| } |
| |
| # Adds value to array NAME |
| # in config file. If NAME is found, value gets added, |
| # otherwise it is added to the end of file. |
| # Parameters: |
| # $1 - config file |
| # $2 - NAME |
| # $3 - value |
| function add_param3() { |
| local file=$1 |
| local name=$2 |
| local value=$3 |
| local path |
| |
| path=${file%/*} |
| if [ ! -d "${path}" ]; then |
| mkdir -p ${path} || error "Unable to create dir ${path}" $VZ_FS_NO_DISK_SPACE |
| fi |
| if grep -E "^$name=\(.*\)" $file>/dev/null 2>&1; then |
| /bin/cp -fp ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE |
| /bin/sed -r "s|^$name=\((.*)\)|$name=\( \1 \"$value\" \)|" < ${file} > ${file}.$$ |
| if [ $? -ne 0 ]; then |
| rm -f ${file}.$$ 2>/dev/null |
| error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| mv -f ${file}.$$ ${file} |
| else |
| echo "$name=( \"$value\" )" >> $file || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| } |
| |
| # Removes value from array NAME |
| # in config file. If NAME is found, value gets removed, |
| # otherwise this is a noop function. |
| # Parameters: |
| # $1 - config file |
| # $2 - NAME |
| # $3 - value |
| function del_param3() { |
| local file=$1 |
| local name=$2 |
| local value=$3 |
| |
| [ ! -f $file ] && return |
| |
| if grep -E "^$name=\(.*\)" $file>/dev/null 2>&1; then |
| /bin/cp -fp ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE |
| /bin/sed -r "s|^($name=\( .*)\"$value\"(.* \))|\1\2|" < ${file} > ${file}.$$ |
| if [ $? -ne 0 ]; then |
| rm -f ${file}.$$ 2>/dev/null |
| error "Can't change file $file" $VZ_FS_NO_DISK_SPACE |
| fi |
| mv -f ${file}.$$ ${file} |
| else |
| return |
| fi |
| } |
| |