Created
July 30, 2022 18:52
-
-
Save xddxdd/6930b722ba0f2b291d059c607f64c0af to your computer and use it in GitHub Desktop.
All startup scripts executed by OpenVZ 7 in guest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2001-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# Set the sane umask | |
umask 022 | |
# Error codes | |
VZ_SYSTEM_ERROR=3 | |
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 | |
VZ_SET_RATE=80 | |
VZ_SET_ACCOUNT=81 | |
CP='/bin/cp -fp' | |
WICKED=/etc/systemd/system/network.service | |
[ -f /etc/fedora-release ] && CP='/bin/cp -f --preserve=mode,ownership' | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# Adds value to variable 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_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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -qe "^$name=" $file.$$ >/dev/null 2>&1; then | |
/bin/sed -e "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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
function del_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 -qe "^$name=" $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if [ -z "${value}" ]; then | |
/bin/sed -e "/^${name}=.*/d" < ${file} > ${file}.$$ | |
else | |
sed -e "s|^${name}=\(.*\)${value}\(.*\)|${name}=\1\2|" <${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} || rm -f ${file}.$$ | |
fi | |
} | |
function del_param2() | |
{ | |
local file=$1 | |
local name=$2 | |
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 -qe "^$name " $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
/bin/sed -e "/^${name} .*/d" < ${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} || rm -f ${file}.$$ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^\<$name\>" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name $value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
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 | |
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 | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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_param4() | |
{ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=$value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=( \"$value\" )" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 | |
${CP} ${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 | |
} | |
function remove_debian_interface_by_proto() | |
{ | |
local dev="$1" | |
local proto=$2 | |
local cfg="$3" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}'$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}'$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}':[0-9]+$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}':[0-9]+$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
function remove_debian_interface() | |
{ | |
local dev="$1" | |
local cfg="$2" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
$1 == "auto" && $2 ~/'${dev}'$/ {next} | |
$1 == "iface" && $2 ~/'${dev}'$/ {skip = 1; next} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
restore_debian_venet_route() | |
{ | |
local proto=$1 | |
local cmd iproto | |
local cfg=/etc/network/interfaces | |
if [ "$proto" = "-6" ]; then | |
iproto=inet6 | |
cmd='up ip -6 r a default dev venet0' | |
else | |
iproto=inet | |
cmd='up route add default dev venet0' | |
fi | |
if grep -qe "$cmd" ${cfg}; then | |
return 0 | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
/^\t/ { print; next ;} | |
addgw { print "\t'"$cmd"'"; addgw=0; print; next } | |
$1 == "iface" && $2 == "venet0" && $3 == "'"$iproto"'" { addgw=1 } | |
{print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
ip $proto r r default dev venet0 2>/dev/null | |
rm -f ${cfg}.$$ | |
} | |
function add_debian_ip6() | |
{ | |
local ips=$1 | |
local cfg=/etc/network/interfaces | |
local i c ip | |
[ "${IPV6}" != "yes" ] && return | |
[ -z "$ips" ] && return | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
for ip in $ips; do | |
if [ -z "$i" ]; then | |
i="address $ip" | |
else | |
i="$i | |
up ip addr add $ip dev venet0" | |
fi | |
done | |
if ! grep -qe "auto ${VENET_DEV}" ${CFGFILE} 2>/dev/null; then | |
c="auto ${VENET_DEV}" | |
fi | |
echo "$c | |
iface ${VENET_DEV} inet6 static | |
${i} | |
up ip -6 r a default dev ${VENET_DEV}" >> ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ | |
} | |
function change_hostname() | |
{ | |
local cfg="$1" | |
local host="$2" | |
local ip="$3" | |
local comm='# Auto-generated hostname. Please do not remove this comment.' | |
[ -f "${cfg}" ] || touch ${cfg} | |
if [ "${host}" = "localhost" -o "${host}" = "localhost.localdomain" ]; | |
then | |
put_param2 ${cfg} "127.0.0.1" "localhost.localdomain localhost" | |
return | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk -v ip="${ip}" -v host="${host}" -v comm="${comm}" ' | |
BEGIN {found = 0; skip = 0} | |
$0 == comm {found = 1; next} | |
found { | |
if (ip == "") {ip = $1} | |
found = 0; | |
next; | |
} | |
$0 ~ "\\<" host "\\>" { | |
if (!skip) { | |
skip = 1; | |
} else { | |
next; | |
} | |
} | |
{print} | |
END { | |
if (skip) exit 0; | |
if (ip == "") { ip ="127.0.0.1" } | |
print comm; | |
alias="" | |
if ((i=index(host, ".")) > 1) { | |
alias=substr(host, 1, i - 1); | |
} | |
print ip " " host " " alias; | |
} | |
' < ${cfg} > ${cfg}.$$ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ 2>/dev/null | |
error "Can't change file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
} | |
function is_ipv6() | |
{ | |
if [ "${1#*:}" != "${1}" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function get_netmask() | |
{ | |
local dev=$1 | |
local ip=$2 | |
ip a l dev $dev 2>/dev/null | grep -e "inet[6]* ${ip}/" | sed -e 's/[^\/]*\/\([0-9]*\).*/\1/' | |
} | |
check_dhcp() | |
{ | |
for pkg in dhcpcd dhclient; do | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/$pkg ]; then | |
return | |
fi | |
done | |
done | |
echo "Warning: DHCP client daemon not found" | |
} | |
check_dhcp_ipv6() | |
{ | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/dhcp6c ]; then | |
return | |
fi | |
done | |
dhclient --help 2>&1 | grep -q -w -- -6 2>/dev/null | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
echo "Warning: DHCP IPv6 client daemon not found" | |
} | |
is_default_route_configured() | |
{ | |
local dev= | |
local proto=$2 | |
[ -n "$1" ] && dev="dev $1" | |
if ip $proto l r $dev 2>/dev/null | grep -qe "^default"; then | |
echo yes | |
return 0 | |
else | |
echo no | |
return 1 | |
fi | |
} | |
cleanup_vzquota() | |
{ | |
rm -f ${SCRIPTNAME} | |
rm -f /etc/mtab | |
ln -sf /proc/mounts /etc/mtab | |
} | |
is_quota_support_ext4() | |
{ | |
grep -q usrquota /proc/mounts && return 0 | |
quotacheck -V | awk '/ version /{ | |
i=split($4, a, "."); | |
if (i < 2) { | |
print("Unable to detect quota version: "$0); | |
exit 1; | |
} | |
if (a[1] >= 4) { | |
exit 0; | |
} | |
if (a[1] < 3 || (a[1] == 3 && a[2] < 17)) { | |
print "Old quota version detected " $0 " Quota should be > 3.16"; | |
exit 1; | |
} | |
}' | |
} | |
setup_quota() | |
{ | |
[ -f "$SCRIPTNAME" ] && cleanup_vzquota | |
if [ -z "$UGIDLIMIT" ]; then | |
quotaoff -a | |
rm -f /aquota.user /aquota.group 2>/dev/null | |
elif [ ! -f "/aquota.user" -o ! -f "/aquota.group" ]; then | |
is_quota_support_ext4 || exit 1 | |
fi | |
} | |
is_wicked() | |
{ | |
readlink ${WICKED} 2>/dev/null | grep wicked > /dev/null | |
return $? | |
} | |
#!/bin/bash | |
# Copyright (c) 1999-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# | |
# This script configure IP alias(es) inside VPS for Debian like distros. | |
# | |
# Parameters are passed in environment variables. | |
# Required parameters: | |
# IP_ADDR - IP address(es) to add | |
# (several addresses should be divided by space) | |
# Optional parameters: | |
# VE_STATE - state of VPS; could be one of: | |
# starting | stopping | running | stopped | |
# IPDELALL - delete all ip addresses | |
# | |
VENET_DEV=venet0 | |
CFGFILE=/etc/network/interfaces | |
HOSTFILE=/etc/hosts | |
USE_INET6=yes | |
function fix_networking_conf() | |
{ | |
local cfg=/etc/init/networking.conf | |
[ ! -f ${cfg} ] && return 0 | |
if grep -q "udevtrigger" ${cfg} 2>/dev/null; then | |
grep -v udevtrigger ${cfg} | \ | |
sed "s,(local-filesystems,local-filesystems,g" > ${cfg}.tmp && \ | |
mv -f ${cfg}.tmp ${cfg} | |
fi | |
} | |
function setup_network() | |
{ | |
echo -e "# This configuration file is auto-generated. | |
# WARNING: Do not edit this file, otherwise your changes will be lost. | |
# Please edit template $CFGFILE.template instead. | |
" > ${CFGFILE} | |
if [ -f ${CFGFILE}.template ]; then | |
cat ${CFGFILE}.template >> ${CFGFILE} | |
fi | |
if ! grep -qw lo ${CFGFILE}; then | |
echo "auto lo | |
iface lo inet loopback" >> ${CFGFILE} | |
fi | |
# Set up /etc/hosts | |
if [ ! -f $HOSTFILE ]; then | |
echo "127.0.0.1 localhost.localdomain localhost" > $HOSTFILE | |
fi | |
[ -z "${IP_ADDR}" ] && return | |
echo -e " | |
# Auto generated venet0 interfaces | |
auto ${VENET_DEV} | |
iface ${VENET_DEV} inet static | |
address 127.0.0.1 | |
netmask 255.255.255.255 | |
broadcast 0.0.0.0" >> ${CFGFILE} | |
if [ "x${VE_STATE}" = "xstarting" -o "$(is_default_route_configured)" = "no" ]; then | |
echo -e "\tup route add default dev ${VENET_DEV}" >> ${CFGFILE} | |
fi | |
fix_networking_conf | |
} | |
function add_ip() | |
{ | |
local dev=$1 | |
local ip=$2 | |
local mask=$3 | |
local cfg= | |
if ! grep -qe "auto $dev$" ${CFGFILE} 2>/dev/null; then | |
cfg="auto ${dev}" | |
fi | |
cfg="$cfg | |
iface ${dev} inet static | |
address ${ip}" | |
if [ -z "${mask}" ]; then | |
mask=255.255.255.255 | |
fi | |
cfg="${cfg} | |
netmask ${mask}" | |
echo -e "${cfg}\n" >> ${CFGFILE} | |
} | |
function add_ip6_alias() | |
{ | |
local ip=$1 | |
awk ' | |
BEGIN {found = 0} | |
NF == 0 {next} | |
!found && $1 == "iface" && $2 ~/'${VENET_DEV}'$/ && $3 == "inet6" { | |
found = 1; | |
print; | |
next; | |
} | |
found == 1 && !/^\t/{ | |
print "\tup ip addr add '$ip' dev venet0"; | |
found++; | |
} | |
{print} | |
END { | |
if (found == 1) { | |
print "\tup ip addr add '$ip' dev venet0"; | |
} | |
} | |
' < ${CFGFILE} > ${CFGFILE}.$$ && mv -f ${CFGFILE}.$$ ${CFGFILE} | |
rm -f ${CFGFILE}.$$ 2>/dev/null | |
} | |
function get_all_aliasid() | |
{ | |
IFNUM=-1 | |
IFNUMLIST=`grep -e "^auto ${VENET_DEV}:.*$" ${CFGFILE} 2>/dev/null | sed 's/^auto '${VENET_DEV}'://'` | |
} | |
function get_free_aliasid() | |
{ | |
# no main iface | |
grep -qe "^iface ${VENET_DEV} inet " ${CFGFILE} >/dev/null || return 0 | |
[ -z "${IFNUMLIST}" ] && get_all_aliasid | |
while true; do | |
let IFNUM=IFNUM+1 | |
echo "${IFNUMLIST}" | grep -q -E "^${IFNUM}$" 2>/dev/null || break | |
done | |
return 1 | |
} | |
function setup() | |
{ | |
local ipm ip mask | |
local found | |
local dev=$VENET_DEV | |
# IPv6 is not supported for ubuntu-8.04 | |
if grep -q "lenny" /etc/debian_version 2>/dev/null; then | |
USE_INET6=no | |
fi | |
if [ "${VE_STATE}" = "starting" ]; then | |
setup_network | |
elif ! grep -q "^auto ${VENET_DEV}\$" ${CFGFILE} 2>/dev/null; then | |
setup_network | |
fi | |
if [ "${IPDELALL}" = "yes" ]; then | |
ifdown ${VENET_DEV} >/dev/null 2>&1 | |
remove_debian_interface "${VENET_DEV}:[0-9]*" ${CFGFILE} | |
setup_network | |
fi | |
for ipm in ${IP_ADDR}; do | |
ip=${ipm%%/*} | |
[ -z "${ip}" ] && continue | |
mask= | |
if echo "${ipm}" | grep -q '/'; then | |
mask=${ipm##*/} | |
fi | |
if grep -qw -e "^[[:space:]]*address $ip" -e "^[[:space:]]*up ip addr add $ip" ${CFGFILE} 2>/dev/null; then | |
continue | |
fi | |
if is_ipv6 "${ip}"; then | |
[ -z "$mask" ] && mask=128 | |
if grep -q "iface ${dev} inet6" ${CFGFILE} 2>/dev/null; then | |
add_ip6_alias "${ip}/${mask}" | |
else | |
add_debian_ip6 "${ip}/${mask}" | |
fi | |
else | |
get_free_aliasid | |
if [ $? -ne 0 ]; then | |
add_ip "${dev}:${IFNUM}" "${ip}" "${mask}" | |
else | |
add_ip "${dev}" "${ip}" "${mask}" | |
fi | |
fi | |
done | |
if [ "x${VE_STATE}" = "xrunning" ]; then | |
/sbin/ifdown venet0 2>/dev/null | |
/sbin/ifup -a --force 2>/dev/null | |
fi | |
} | |
setup | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2001-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# Set the sane umask | |
umask 022 | |
# Error codes | |
VZ_SYSTEM_ERROR=3 | |
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 | |
VZ_SET_RATE=80 | |
VZ_SET_ACCOUNT=81 | |
CP='/bin/cp -fp' | |
WICKED=/etc/systemd/system/network.service | |
[ -f /etc/fedora-release ] && CP='/bin/cp -f --preserve=mode,ownership' | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# Adds value to variable 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_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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -qe "^$name=" $file.$$ >/dev/null 2>&1; then | |
/bin/sed -e "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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
function del_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 -qe "^$name=" $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if [ -z "${value}" ]; then | |
/bin/sed -e "/^${name}=.*/d" < ${file} > ${file}.$$ | |
else | |
sed -e "s|^${name}=\(.*\)${value}\(.*\)|${name}=\1\2|" <${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} || rm -f ${file}.$$ | |
fi | |
} | |
function del_param2() | |
{ | |
local file=$1 | |
local name=$2 | |
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 -qe "^$name " $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
/bin/sed -e "/^${name} .*/d" < ${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} || rm -f ${file}.$$ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^\<$name\>" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name $value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
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 | |
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 | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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_param4() | |
{ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=$value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=( \"$value\" )" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 | |
${CP} ${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 | |
} | |
function remove_debian_interface_by_proto() | |
{ | |
local dev="$1" | |
local proto=$2 | |
local cfg="$3" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}'$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}'$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}':[0-9]+$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}':[0-9]+$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
function remove_debian_interface() | |
{ | |
local dev="$1" | |
local cfg="$2" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
$1 == "auto" && $2 ~/'${dev}'$/ {next} | |
$1 == "iface" && $2 ~/'${dev}'$/ {skip = 1; next} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
restore_debian_venet_route() | |
{ | |
local proto=$1 | |
local cmd iproto | |
local cfg=/etc/network/interfaces | |
if [ "$proto" = "-6" ]; then | |
iproto=inet6 | |
cmd='up ip -6 r a default dev venet0' | |
else | |
iproto=inet | |
cmd='up route add default dev venet0' | |
fi | |
if grep -qe "$cmd" ${cfg}; then | |
return 0 | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
/^\t/ { print; next ;} | |
addgw { print "\t'"$cmd"'"; addgw=0; print; next } | |
$1 == "iface" && $2 == "venet0" && $3 == "'"$iproto"'" { addgw=1 } | |
{print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
ip $proto r r default dev venet0 2>/dev/null | |
rm -f ${cfg}.$$ | |
} | |
function add_debian_ip6() | |
{ | |
local ips=$1 | |
local cfg=/etc/network/interfaces | |
local i c ip | |
[ "${IPV6}" != "yes" ] && return | |
[ -z "$ips" ] && return | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
for ip in $ips; do | |
if [ -z "$i" ]; then | |
i="address $ip" | |
else | |
i="$i | |
up ip addr add $ip dev venet0" | |
fi | |
done | |
if ! grep -qe "auto ${VENET_DEV}" ${CFGFILE} 2>/dev/null; then | |
c="auto ${VENET_DEV}" | |
fi | |
echo "$c | |
iface ${VENET_DEV} inet6 static | |
${i} | |
up ip -6 r a default dev ${VENET_DEV}" >> ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ | |
} | |
function change_hostname() | |
{ | |
local cfg="$1" | |
local host="$2" | |
local ip="$3" | |
local comm='# Auto-generated hostname. Please do not remove this comment.' | |
[ -f "${cfg}" ] || touch ${cfg} | |
if [ "${host}" = "localhost" -o "${host}" = "localhost.localdomain" ]; | |
then | |
put_param2 ${cfg} "127.0.0.1" "localhost.localdomain localhost" | |
return | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk -v ip="${ip}" -v host="${host}" -v comm="${comm}" ' | |
BEGIN {found = 0; skip = 0} | |
$0 == comm {found = 1; next} | |
found { | |
if (ip == "") {ip = $1} | |
found = 0; | |
next; | |
} | |
$0 ~ "\\<" host "\\>" { | |
if (!skip) { | |
skip = 1; | |
} else { | |
next; | |
} | |
} | |
{print} | |
END { | |
if (skip) exit 0; | |
if (ip == "") { ip ="127.0.0.1" } | |
print comm; | |
alias="" | |
if ((i=index(host, ".")) > 1) { | |
alias=substr(host, 1, i - 1); | |
} | |
print ip " " host " " alias; | |
} | |
' < ${cfg} > ${cfg}.$$ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ 2>/dev/null | |
error "Can't change file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
} | |
function is_ipv6() | |
{ | |
if [ "${1#*:}" != "${1}" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function get_netmask() | |
{ | |
local dev=$1 | |
local ip=$2 | |
ip a l dev $dev 2>/dev/null | grep -e "inet[6]* ${ip}/" | sed -e 's/[^\/]*\/\([0-9]*\).*/\1/' | |
} | |
check_dhcp() | |
{ | |
for pkg in dhcpcd dhclient; do | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/$pkg ]; then | |
return | |
fi | |
done | |
done | |
echo "Warning: DHCP client daemon not found" | |
} | |
check_dhcp_ipv6() | |
{ | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/dhcp6c ]; then | |
return | |
fi | |
done | |
dhclient --help 2>&1 | grep -q -w -- -6 2>/dev/null | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
echo "Warning: DHCP IPv6 client daemon not found" | |
} | |
is_default_route_configured() | |
{ | |
local dev= | |
local proto=$2 | |
[ -n "$1" ] && dev="dev $1" | |
if ip $proto l r $dev 2>/dev/null | grep -qe "^default"; then | |
echo yes | |
return 0 | |
else | |
echo no | |
return 1 | |
fi | |
} | |
cleanup_vzquota() | |
{ | |
rm -f ${SCRIPTNAME} | |
rm -f /etc/mtab | |
ln -sf /proc/mounts /etc/mtab | |
} | |
is_quota_support_ext4() | |
{ | |
grep -q usrquota /proc/mounts && return 0 | |
quotacheck -V | awk '/ version /{ | |
i=split($4, a, "."); | |
if (i < 2) { | |
print("Unable to detect quota version: "$0); | |
exit 1; | |
} | |
if (a[1] >= 4) { | |
exit 0; | |
} | |
if (a[1] < 3 || (a[1] == 3 && a[2] < 17)) { | |
print "Old quota version detected " $0 " Quota should be > 3.16"; | |
exit 1; | |
} | |
}' | |
} | |
setup_quota() | |
{ | |
[ -f "$SCRIPTNAME" ] && cleanup_vzquota | |
if [ -z "$UGIDLIMIT" ]; then | |
quotaoff -a | |
rm -f /aquota.user /aquota.group 2>/dev/null | |
elif [ ! -f "/aquota.user" -o ! -f "/aquota.group" ]; then | |
is_quota_support_ext4 || exit 1 | |
fi | |
} | |
is_wicked() | |
{ | |
readlink ${WICKED} 2>/dev/null | grep wicked > /dev/null | |
return $? | |
} | |
#!/bin/bash | |
# Copyright (c) 1999-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# | |
# This script configures quota startup script inside VPS | |
# | |
# Parameters are passed in environment variables. | |
# Required parameters: | |
# MINOR - root device minor number | |
# MAJOR - root device major number | |
SCRIPTNAME='/etc/init.d/vzquota' | |
if grep -q '/dev/ploop' /proc/mounts; then | |
setup_quota | |
fi | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2001-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# Set the sane umask | |
umask 022 | |
# Error codes | |
VZ_SYSTEM_ERROR=3 | |
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 | |
VZ_SET_RATE=80 | |
VZ_SET_ACCOUNT=81 | |
CP='/bin/cp -fp' | |
WICKED=/etc/systemd/system/network.service | |
[ -f /etc/fedora-release ] && CP='/bin/cp -f --preserve=mode,ownership' | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# Adds value to variable 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_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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -qe "^$name=" $file.$$ >/dev/null 2>&1; then | |
/bin/sed -e "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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
function del_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 -qe "^$name=" $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if [ -z "${value}" ]; then | |
/bin/sed -e "/^${name}=.*/d" < ${file} > ${file}.$$ | |
else | |
sed -e "s|^${name}=\(.*\)${value}\(.*\)|${name}=\1\2|" <${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} || rm -f ${file}.$$ | |
fi | |
} | |
function del_param2() | |
{ | |
local file=$1 | |
local name=$2 | |
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 -qe "^$name " $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
/bin/sed -e "/^${name} .*/d" < ${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} || rm -f ${file}.$$ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^\<$name\>" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name $value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
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 | |
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 | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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_param4() | |
{ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=$value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=( \"$value\" )" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 | |
${CP} ${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 | |
} | |
function remove_debian_interface_by_proto() | |
{ | |
local dev="$1" | |
local proto=$2 | |
local cfg="$3" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}'$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}'$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}':[0-9]+$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}':[0-9]+$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
function remove_debian_interface() | |
{ | |
local dev="$1" | |
local cfg="$2" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
$1 == "auto" && $2 ~/'${dev}'$/ {next} | |
$1 == "iface" && $2 ~/'${dev}'$/ {skip = 1; next} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
restore_debian_venet_route() | |
{ | |
local proto=$1 | |
local cmd iproto | |
local cfg=/etc/network/interfaces | |
if [ "$proto" = "-6" ]; then | |
iproto=inet6 | |
cmd='up ip -6 r a default dev venet0' | |
else | |
iproto=inet | |
cmd='up route add default dev venet0' | |
fi | |
if grep -qe "$cmd" ${cfg}; then | |
return 0 | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
/^\t/ { print; next ;} | |
addgw { print "\t'"$cmd"'"; addgw=0; print; next } | |
$1 == "iface" && $2 == "venet0" && $3 == "'"$iproto"'" { addgw=1 } | |
{print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
ip $proto r r default dev venet0 2>/dev/null | |
rm -f ${cfg}.$$ | |
} | |
function add_debian_ip6() | |
{ | |
local ips=$1 | |
local cfg=/etc/network/interfaces | |
local i c ip | |
[ "${IPV6}" != "yes" ] && return | |
[ -z "$ips" ] && return | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
for ip in $ips; do | |
if [ -z "$i" ]; then | |
i="address $ip" | |
else | |
i="$i | |
up ip addr add $ip dev venet0" | |
fi | |
done | |
if ! grep -qe "auto ${VENET_DEV}" ${CFGFILE} 2>/dev/null; then | |
c="auto ${VENET_DEV}" | |
fi | |
echo "$c | |
iface ${VENET_DEV} inet6 static | |
${i} | |
up ip -6 r a default dev ${VENET_DEV}" >> ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ | |
} | |
function change_hostname() | |
{ | |
local cfg="$1" | |
local host="$2" | |
local ip="$3" | |
local comm='# Auto-generated hostname. Please do not remove this comment.' | |
[ -f "${cfg}" ] || touch ${cfg} | |
if [ "${host}" = "localhost" -o "${host}" = "localhost.localdomain" ]; | |
then | |
put_param2 ${cfg} "127.0.0.1" "localhost.localdomain localhost" | |
return | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk -v ip="${ip}" -v host="${host}" -v comm="${comm}" ' | |
BEGIN {found = 0; skip = 0} | |
$0 == comm {found = 1; next} | |
found { | |
if (ip == "") {ip = $1} | |
found = 0; | |
next; | |
} | |
$0 ~ "\\<" host "\\>" { | |
if (!skip) { | |
skip = 1; | |
} else { | |
next; | |
} | |
} | |
{print} | |
END { | |
if (skip) exit 0; | |
if (ip == "") { ip ="127.0.0.1" } | |
print comm; | |
alias="" | |
if ((i=index(host, ".")) > 1) { | |
alias=substr(host, 1, i - 1); | |
} | |
print ip " " host " " alias; | |
} | |
' < ${cfg} > ${cfg}.$$ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ 2>/dev/null | |
error "Can't change file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
} | |
function is_ipv6() | |
{ | |
if [ "${1#*:}" != "${1}" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function get_netmask() | |
{ | |
local dev=$1 | |
local ip=$2 | |
ip a l dev $dev 2>/dev/null | grep -e "inet[6]* ${ip}/" | sed -e 's/[^\/]*\/\([0-9]*\).*/\1/' | |
} | |
check_dhcp() | |
{ | |
for pkg in dhcpcd dhclient; do | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/$pkg ]; then | |
return | |
fi | |
done | |
done | |
echo "Warning: DHCP client daemon not found" | |
} | |
check_dhcp_ipv6() | |
{ | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/dhcp6c ]; then | |
return | |
fi | |
done | |
dhclient --help 2>&1 | grep -q -w -- -6 2>/dev/null | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
echo "Warning: DHCP IPv6 client daemon not found" | |
} | |
is_default_route_configured() | |
{ | |
local dev= | |
local proto=$2 | |
[ -n "$1" ] && dev="dev $1" | |
if ip $proto l r $dev 2>/dev/null | grep -qe "^default"; then | |
echo yes | |
return 0 | |
else | |
echo no | |
return 1 | |
fi | |
} | |
cleanup_vzquota() | |
{ | |
rm -f ${SCRIPTNAME} | |
rm -f /etc/mtab | |
ln -sf /proc/mounts /etc/mtab | |
} | |
is_quota_support_ext4() | |
{ | |
grep -q usrquota /proc/mounts && return 0 | |
quotacheck -V | awk '/ version /{ | |
i=split($4, a, "."); | |
if (i < 2) { | |
print("Unable to detect quota version: "$0); | |
exit 1; | |
} | |
if (a[1] >= 4) { | |
exit 0; | |
} | |
if (a[1] < 3 || (a[1] == 3 && a[2] < 17)) { | |
print "Old quota version detected " $0 " Quota should be > 3.16"; | |
exit 1; | |
} | |
}' | |
} | |
setup_quota() | |
{ | |
[ -f "$SCRIPTNAME" ] && cleanup_vzquota | |
if [ -z "$UGIDLIMIT" ]; then | |
quotaoff -a | |
rm -f /aquota.user /aquota.group 2>/dev/null | |
elif [ ! -f "/aquota.user" -o ! -f "/aquota.group" ]; then | |
is_quota_support_ext4 || exit 1 | |
fi | |
} | |
is_wicked() | |
{ | |
readlink ${WICKED} 2>/dev/null | grep wicked > /dev/null | |
return $? | |
} | |
#!/bin/bash | |
# Copyright (c) 1999-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# | |
# This script sets hostname inside VPS for Debian like distros | |
# For usage info see vz-veconfig(5) man page. | |
# | |
# Some parameters are passed in environment variables. | |
# Required parameters: | |
# Optional parameters: | |
# HOSTNM | |
# Sets host name for this VE. | |
function set_hostname() | |
{ | |
local cfgfile=$1 | |
local hostname=$2 | |
[ -z "${hostname}" ] && return 0 | |
echo "${hostname}" > /etc/hostname | |
hostname ${hostname} | |
} | |
[ -z "${HOSTNM}" ] && exit 0 | |
change_hostname /etc/hosts "${HOSTNM}" "${IP_ADDR}" | |
set_hostname /etc/hostname "${HOSTNM}" | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2001-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# Set the sane umask | |
umask 022 | |
# Error codes | |
VZ_SYSTEM_ERROR=3 | |
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 | |
VZ_SET_RATE=80 | |
VZ_SET_ACCOUNT=81 | |
CP='/bin/cp -fp' | |
WICKED=/etc/systemd/system/network.service | |
[ -f /etc/fedora-release ] && CP='/bin/cp -f --preserve=mode,ownership' | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# Adds value to variable 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_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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -qe "^$name=" $file.$$ >/dev/null 2>&1; then | |
/bin/sed -e "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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
function del_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 -qe "^$name=" $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if [ -z "${value}" ]; then | |
/bin/sed -e "/^${name}=.*/d" < ${file} > ${file}.$$ | |
else | |
sed -e "s|^${name}=\(.*\)${value}\(.*\)|${name}=\1\2|" <${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} || rm -f ${file}.$$ | |
fi | |
} | |
function del_param2() | |
{ | |
local file=$1 | |
local name=$2 | |
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 -qe "^$name " $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
/bin/sed -e "/^${name} .*/d" < ${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} || rm -f ${file}.$$ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^\<$name\>" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name $value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
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 | |
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 | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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_param4() | |
{ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=$value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=( \"$value\" )" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 | |
${CP} ${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 | |
} | |
function remove_debian_interface_by_proto() | |
{ | |
local dev="$1" | |
local proto=$2 | |
local cfg="$3" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}'$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}'$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}':[0-9]+$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}':[0-9]+$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
function remove_debian_interface() | |
{ | |
local dev="$1" | |
local cfg="$2" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
$1 == "auto" && $2 ~/'${dev}'$/ {next} | |
$1 == "iface" && $2 ~/'${dev}'$/ {skip = 1; next} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
restore_debian_venet_route() | |
{ | |
local proto=$1 | |
local cmd iproto | |
local cfg=/etc/network/interfaces | |
if [ "$proto" = "-6" ]; then | |
iproto=inet6 | |
cmd='up ip -6 r a default dev venet0' | |
else | |
iproto=inet | |
cmd='up route add default dev venet0' | |
fi | |
if grep -qe "$cmd" ${cfg}; then | |
return 0 | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
/^\t/ { print; next ;} | |
addgw { print "\t'"$cmd"'"; addgw=0; print; next } | |
$1 == "iface" && $2 == "venet0" && $3 == "'"$iproto"'" { addgw=1 } | |
{print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
ip $proto r r default dev venet0 2>/dev/null | |
rm -f ${cfg}.$$ | |
} | |
function add_debian_ip6() | |
{ | |
local ips=$1 | |
local cfg=/etc/network/interfaces | |
local i c ip | |
[ "${IPV6}" != "yes" ] && return | |
[ -z "$ips" ] && return | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
for ip in $ips; do | |
if [ -z "$i" ]; then | |
i="address $ip" | |
else | |
i="$i | |
up ip addr add $ip dev venet0" | |
fi | |
done | |
if ! grep -qe "auto ${VENET_DEV}" ${CFGFILE} 2>/dev/null; then | |
c="auto ${VENET_DEV}" | |
fi | |
echo "$c | |
iface ${VENET_DEV} inet6 static | |
${i} | |
up ip -6 r a default dev ${VENET_DEV}" >> ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ | |
} | |
function change_hostname() | |
{ | |
local cfg="$1" | |
local host="$2" | |
local ip="$3" | |
local comm='# Auto-generated hostname. Please do not remove this comment.' | |
[ -f "${cfg}" ] || touch ${cfg} | |
if [ "${host}" = "localhost" -o "${host}" = "localhost.localdomain" ]; | |
then | |
put_param2 ${cfg} "127.0.0.1" "localhost.localdomain localhost" | |
return | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk -v ip="${ip}" -v host="${host}" -v comm="${comm}" ' | |
BEGIN {found = 0; skip = 0} | |
$0 == comm {found = 1; next} | |
found { | |
if (ip == "") {ip = $1} | |
found = 0; | |
next; | |
} | |
$0 ~ "\\<" host "\\>" { | |
if (!skip) { | |
skip = 1; | |
} else { | |
next; | |
} | |
} | |
{print} | |
END { | |
if (skip) exit 0; | |
if (ip == "") { ip ="127.0.0.1" } | |
print comm; | |
alias="" | |
if ((i=index(host, ".")) > 1) { | |
alias=substr(host, 1, i - 1); | |
} | |
print ip " " host " " alias; | |
} | |
' < ${cfg} > ${cfg}.$$ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ 2>/dev/null | |
error "Can't change file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
} | |
function is_ipv6() | |
{ | |
if [ "${1#*:}" != "${1}" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function get_netmask() | |
{ | |
local dev=$1 | |
local ip=$2 | |
ip a l dev $dev 2>/dev/null | grep -e "inet[6]* ${ip}/" | sed -e 's/[^\/]*\/\([0-9]*\).*/\1/' | |
} | |
check_dhcp() | |
{ | |
for pkg in dhcpcd dhclient; do | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/$pkg ]; then | |
return | |
fi | |
done | |
done | |
echo "Warning: DHCP client daemon not found" | |
} | |
check_dhcp_ipv6() | |
{ | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/dhcp6c ]; then | |
return | |
fi | |
done | |
dhclient --help 2>&1 | grep -q -w -- -6 2>/dev/null | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
echo "Warning: DHCP IPv6 client daemon not found" | |
} | |
is_default_route_configured() | |
{ | |
local dev= | |
local proto=$2 | |
[ -n "$1" ] && dev="dev $1" | |
if ip $proto l r $dev 2>/dev/null | grep -qe "^default"; then | |
echo yes | |
return 0 | |
else | |
echo no | |
return 1 | |
fi | |
} | |
cleanup_vzquota() | |
{ | |
rm -f ${SCRIPTNAME} | |
rm -f /etc/mtab | |
ln -sf /proc/mounts /etc/mtab | |
} | |
is_quota_support_ext4() | |
{ | |
grep -q usrquota /proc/mounts && return 0 | |
quotacheck -V | awk '/ version /{ | |
i=split($4, a, "."); | |
if (i < 2) { | |
print("Unable to detect quota version: "$0); | |
exit 1; | |
} | |
if (a[1] >= 4) { | |
exit 0; | |
} | |
if (a[1] < 3 || (a[1] == 3 && a[2] < 17)) { | |
print "Old quota version detected " $0 " Quota should be > 3.16"; | |
exit 1; | |
} | |
}' | |
} | |
setup_quota() | |
{ | |
[ -f "$SCRIPTNAME" ] && cleanup_vzquota | |
if [ -z "$UGIDLIMIT" ]; then | |
quotaoff -a | |
rm -f /aquota.user /aquota.group 2>/dev/null | |
elif [ ! -f "/aquota.user" -o ! -f "/aquota.group" ]; then | |
is_quota_support_ext4 || exit 1 | |
fi | |
} | |
is_wicked() | |
{ | |
readlink ${WICKED} 2>/dev/null | grep wicked > /dev/null | |
return $? | |
} | |
#!/bin/bash | |
# Copyright (c) 1999-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# | |
# This script sets up resolver inside VPS | |
# For usage info see vz-veconfig(5) man page. | |
# | |
# Some parameters are passed in environment variables. | |
# Required parameters: | |
# SEARCHDOMAIN | |
# Sets search domain(s). Modifies /etc/resolv.conf | |
# NAMESERVER | |
# Sets name server(s). Modifies /etc/resolv.conf | |
RESOLVCONF=/etc/resolvconf/resolv.conf.d/base | |
set_resolvconf() | |
{ | |
local cfgfile=/etc/resolv.conf | |
local server="$1" | |
local search="$2" | |
local post_configure_cmd= | |
local srv fname | |
# Use resolvconf static storage | |
if which resolvconf >/dev/null 2>&1; then | |
if [ -e "$RESOLVCONF" ]; then | |
cfgfile=$RESOLVCONF | |
[ "${VE_STATE}" = "running" ] && post_configure_cmd='resolvconf -u' | |
fi | |
fi | |
if [ -n "${search}" ]; then | |
if [ "${search}" = '#' ]; then | |
sed "/search.*/d" < ${cfgfile} > ${cfgfile}.$$ && \ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfgfile}.$$ | |
error "Can't change file ${cfgfile}" ${VZ_FS_NO_DISK_SPACE} | |
fi | |
mv -f ${cfgfile}.$$ ${cfgfile} || rm -f ${cfgfile}.$$ | |
else | |
put_param2 "${cfgfile}" search "${search}" | |
fi | |
fi | |
if [ -n "${server}" ]; then | |
[ -f "${cfgfile}" ] || touch "${cfgfile}" | |
${CP} ${cfgfile} ${cfgfile}.$$ || error "Can't copy file $cfgfile" $VZ_FS_NO_DISK_SPACE | |
sed "/nameserver.*/d" ${cfgfile} > ${cfgfile}.$$ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfgfile}.$$ | |
error "Can't change file ${cfgfile}" ${VZ_FS_NO_DISK_SPACE} | |
fi | |
if [ "${server}" != '#' ]; then | |
for srv in ${server}; do | |
echo "nameserver ${srv}" >> ${cfgfile}.$$ || \ | |
error "Can't change file ${cfgfile}" ${VZ_FS_NO_DISK_SPACE} | |
done | |
fi | |
mv -f ${cfgfile}.$$ ${cfgfile} || rm -f ${cfgfile}.$$ | |
fi | |
[ -n "${post_configure_cmd}" ] && ${post_configure_cmd} | |
} | |
set_resolved() | |
{ | |
local cfg=/etc/systemd/resolved.conf | |
local server="$1" | |
local search="$2" | |
if [ "${server}" = '#' ]; then | |
sed "/DNS=.*/d" < ${cfg} > ${cfg}.$$ && \ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ | |
error "Can't change file ${cfg}" ${VZ_FS_NO_DISK_SPACE} | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
elif [ -n "${server}" ]; then | |
put_param4 "${cfg}" DNS "${server}" | |
fi | |
if [ "${search}" = '#' ]; then | |
sed "/Domains=.*/d" < ${cfg} > ${cfg}.$$ && \ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ | |
error "Can't change file ${cfg}" ${VZ_FS_NO_DISK_SPACE} | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
elif [ -n "${search}" ]; then | |
put_param4 "${cfg}" Domains "${search}" | |
fi | |
[ "${VE_STATE}" = "running" ] && service systemd-resolved restart | |
} | |
set_network_config() | |
{ | |
local cfg=/etc/sysconfig/network/config | |
if [ -n "$NAMESERVER" ]; then | |
[ "$NAMESERVER" = '#' ] && NAMESERVER="" | |
put_param "$cfg" NETCONFIG_DNS_STATIC_SERVERS "$NAMESERVER" | |
fi | |
if [ -n "$SEARCHDOMAIN" ]; then | |
[ "$SEARCHDOMAIN" = '#' ] && SEARCHDOMAIN="" | |
put_param "$cfg" NETCONFIG_DNS_STATIC_SEARCHLIST "$SEARCHDOMAIN" | |
fi | |
[ -n "$NAMESERVER" -o -n "$SEARCHDOMAIN" ] && netconfig -v update -m dns-resolver | |
} | |
if [ -e /etc/systemd/system/dbus-org.freedesktop.resolve1.service ]; then | |
set_resolved "${NAMESERVER}" "${SEARCHDOMAIN}" | |
elif [ -e /sbin/netconfig -a -e /etc/sysconfig/network/config ]; then | |
set_network_config | |
else | |
set_resolvconf "${NAMESERVER}" "${SEARCHDOMAIN}" | |
fi | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2001-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
# Set the sane umask | |
umask 022 | |
# Error codes | |
VZ_SYSTEM_ERROR=3 | |
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 | |
VZ_SET_RATE=80 | |
VZ_SET_ACCOUNT=81 | |
CP='/bin/cp -fp' | |
WICKED=/etc/systemd/system/network.service | |
[ -f /etc/fedora-release ] && CP='/bin/cp -f --preserve=mode,ownership' | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# Adds value to variable 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_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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -qe "^$name=" $file.$$ >/dev/null 2>&1; then | |
/bin/sed -e "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 | |
else | |
echo "$name=\"$value\"" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
function del_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 -qe "^$name=" $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if [ -z "${value}" ]; then | |
/bin/sed -e "/^${name}=.*/d" < ${file} > ${file}.$$ | |
else | |
sed -e "s|^${name}=\(.*\)${value}\(.*\)|${name}=\1\2|" <${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} || rm -f ${file}.$$ | |
fi | |
} | |
function del_param2() | |
{ | |
local file=$1 | |
local name=$2 | |
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 -qe "^$name " $file >/dev/null 2>&1; then | |
${CP} ${file} ${file}.$$ || \ | |
error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
/bin/sed -e "/^${name} .*/d" < ${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} || rm -f ${file}.$$ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^\<$name\>" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name $value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
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 | |
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 | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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_param4() | |
{ | |
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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=.*" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=$value" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 [ ! -e "${file}" ]; then | |
touch "${file}" || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
${CP} ${file} ${file}.$$ || error "Can't copy file $file" $VZ_FS_NO_DISK_SPACE | |
if grep -E "^$name=\(.*\)" $file.$$ >/dev/null 2>&1; then | |
/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 | |
else | |
echo "$name=( \"$value\" )" >> $file.$$ || error "Can't change file $file" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${file}.$$ ${file} || rm -f ${file}.$$ | |
} | |
# 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 | |
${CP} ${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 | |
} | |
function remove_debian_interface_by_proto() | |
{ | |
local dev="$1" | |
local proto=$2 | |
local cfg="$3" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}'$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}'$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
line="" | |
$1 == "auto" && $2 ~/'${dev}':[0-9]+$/ {line=$0; getline;} | |
$1 == "iface" && $3 ~/'$proto'$/ && $2 ~/'${dev}':[0-9]+$/ {skip = 1; next} | |
line != "" && !skip {print line} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
function remove_debian_interface() | |
{ | |
local dev="$1" | |
local cfg="$2" | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
$1 == "auto" && $2 ~/'${dev}'$/ {next} | |
$1 == "iface" && $2 ~/'${dev}'$/ {skip = 1; next} | |
/^\t/ && skip {next} | |
{skip = 0; print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ 2>/dev/null | |
} | |
restore_debian_venet_route() | |
{ | |
local proto=$1 | |
local cmd iproto | |
local cfg=/etc/network/interfaces | |
if [ "$proto" = "-6" ]; then | |
iproto=inet6 | |
cmd='up ip -6 r a default dev venet0' | |
else | |
iproto=inet | |
cmd='up route add default dev venet0' | |
fi | |
if grep -qe "$cmd" ${cfg}; then | |
return 0 | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk ' | |
NF == 0 {next} | |
/^\t/ { print; next ;} | |
addgw { print "\t'"$cmd"'"; addgw=0; print; next } | |
$1 == "iface" && $2 == "venet0" && $3 == "'"$iproto"'" { addgw=1 } | |
{print} | |
' < ${cfg} > ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
ip $proto r r default dev venet0 2>/dev/null | |
rm -f ${cfg}.$$ | |
} | |
function add_debian_ip6() | |
{ | |
local ips=$1 | |
local cfg=/etc/network/interfaces | |
local i c ip | |
[ "${IPV6}" != "yes" ] && return | |
[ -z "$ips" ] && return | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
for ip in $ips; do | |
if [ -z "$i" ]; then | |
i="address $ip" | |
else | |
i="$i | |
up ip addr add $ip dev venet0" | |
fi | |
done | |
if ! grep -qe "auto ${VENET_DEV}" ${CFGFILE} 2>/dev/null; then | |
c="auto ${VENET_DEV}" | |
fi | |
echo "$c | |
iface ${VENET_DEV} inet6 static | |
${i} | |
up ip -6 r a default dev ${VENET_DEV}" >> ${cfg}.$$ && mv -f ${cfg}.$$ ${cfg} | |
rm -f ${cfg}.$$ | |
} | |
function change_hostname() | |
{ | |
local cfg="$1" | |
local host="$2" | |
local ip="$3" | |
local comm='# Auto-generated hostname. Please do not remove this comment.' | |
[ -f "${cfg}" ] || touch ${cfg} | |
if [ "${host}" = "localhost" -o "${host}" = "localhost.localdomain" ]; | |
then | |
put_param2 ${cfg} "127.0.0.1" "localhost.localdomain localhost" | |
return | |
fi | |
${CP} ${cfg} ${cfg}.$$ || \ | |
error "Can't copy file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
awk -v ip="${ip}" -v host="${host}" -v comm="${comm}" ' | |
BEGIN {found = 0; skip = 0} | |
$0 == comm {found = 1; next} | |
found { | |
if (ip == "") {ip = $1} | |
found = 0; | |
next; | |
} | |
$0 ~ "\\<" host "\\>" { | |
if (!skip) { | |
skip = 1; | |
} else { | |
next; | |
} | |
} | |
{print} | |
END { | |
if (skip) exit 0; | |
if (ip == "") { ip ="127.0.0.1" } | |
print comm; | |
alias="" | |
if ((i=index(host, ".")) > 1) { | |
alias=substr(host, 1, i - 1); | |
} | |
print ip " " host " " alias; | |
} | |
' < ${cfg} > ${cfg}.$$ | |
if [ $? -ne 0 ]; then | |
rm -f ${cfg}.$$ 2>/dev/null | |
error "Can't change file ${cfg}" $VZ_FS_NO_DISK_SPACE | |
fi | |
mv -f ${cfg}.$$ ${cfg} || rm -f ${cfg}.$$ | |
} | |
function is_ipv6() | |
{ | |
if [ "${1#*:}" != "${1}" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function get_netmask() | |
{ | |
local dev=$1 | |
local ip=$2 | |
ip a l dev $dev 2>/dev/null | grep -e "inet[6]* ${ip}/" | sed -e 's/[^\/]*\/\([0-9]*\).*/\1/' | |
} | |
check_dhcp() | |
{ | |
for pkg in dhcpcd dhclient; do | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/$pkg ]; then | |
return | |
fi | |
done | |
done | |
echo "Warning: DHCP client daemon not found" | |
} | |
check_dhcp_ipv6() | |
{ | |
for p in /sbin /usr/sbin; do | |
if [ -x $p/dhcp6c ]; then | |
return | |
fi | |
done | |
dhclient --help 2>&1 | grep -q -w -- -6 2>/dev/null | |
if [ $? -eq 0 ]; then | |
return | |
fi | |
echo "Warning: DHCP IPv6 client daemon not found" | |
} | |
is_default_route_configured() | |
{ | |
local dev= | |
local proto=$2 | |
[ -n "$1" ] && dev="dev $1" | |
if ip $proto l r $dev 2>/dev/null | grep -qe "^default"; then | |
echo yes | |
return 0 | |
else | |
echo no | |
return 1 | |
fi | |
} | |
cleanup_vzquota() | |
{ | |
rm -f ${SCRIPTNAME} | |
rm -f /etc/mtab | |
ln -sf /proc/mounts /etc/mtab | |
} | |
is_quota_support_ext4() | |
{ | |
grep -q usrquota /proc/mounts && return 0 | |
quotacheck -V | awk '/ version /{ | |
i=split($4, a, "."); | |
if (i < 2) { | |
print("Unable to detect quota version: "$0); | |
exit 1; | |
} | |
if (a[1] >= 4) { | |
exit 0; | |
} | |
if (a[1] < 3 || (a[1] == 3 && a[2] < 17)) { | |
print "Old quota version detected " $0 " Quota should be > 3.16"; | |
exit 1; | |
} | |
}' | |
} | |
setup_quota() | |
{ | |
[ -f "$SCRIPTNAME" ] && cleanup_vzquota | |
if [ -z "$UGIDLIMIT" ]; then | |
quotaoff -a | |
rm -f /aquota.user /aquota.group 2>/dev/null | |
elif [ ! -f "/aquota.user" -o ! -f "/aquota.group" ]; then | |
is_quota_support_ext4 || exit 1 | |
fi | |
} | |
is_wicked() | |
{ | |
readlink ${WICKED} 2>/dev/null | grep wicked > /dev/null | |
return $? | |
} | |
#!/bin/bash | |
# Copyright (c) 1999-2017, Parallels International GmbH | |
# Copyright (c) 2017-2019 Virtuozzo International GmbH. All rights reserved. | |
# | |
# This file is part of OpenVZ libraries. OpenVZ is free software; you can | |
# redistribute it and/or modify it under the terms of the GNU Lesser 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 | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
# 02110-1301, USA. | |
# | |
# Our contact details: Virtuozzo International GmbH, Vordergasse 59, 8200 | |
# Schaffhausen, Switzerland. | |
if [ -x /lib/systemd//systemd ]; then | |
SYSTEMD_DIR=/lib/systemd/system | |
else | |
SYSTEMD_DIR=/usr/lib/systemd/system | |
fi | |
ETC_SYSTEMD_DIR="/etc/systemd/system" | |
SYSTEMD_GETTY_SERVICE=$SYSTEMD_DIR/[email protected] | |
create_dev() | |
{ | |
local dev=$1 | |
local major=$2 | |
local minor=$3 | |
if [ ! -c /dev/$dev -o -L /dev/$dev ]; then | |
rm -f /dev/$dev 2>/dev/null | |
mknod /dev/$dev c $major $minor | |
fi | |
} | |
fix_shell_console() | |
{ | |
[ -f $SYSTEMD_DIR/console-getty.service ] && | |
return | |
echo '# This file is part of systemd. | |
# | |
# systemd 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. | |
[Unit] | |
Description=Console Shell | |
After=systemd-user-sessions.service plymouth-quit-wait.service | |
Before=getty.target | |
[Service] | |
Environment=HOME=/root | |
WorkingDirectory=/root | |
ExecStart=-/sbin/agetty --noclear -s console 115200,38400,9600 | |
Restart=always | |
RestartSec=0 | |
UtmpIdentifier=cons | |
TTYPath=/dev/console | |
TTYReset=yes | |
TTYVHangup=yes | |
StandardInput=tty-force | |
StandardOutput=inherit | |
StandardError=inherit | |
KillMode=process | |
# Bash ignores SIGTERM, so we send SIGHUP instead, to ensure that bash | |
# terminates cleanly. | |
KillSignal=SIGHUP | |
[Install] | |
WantedBy=getty.target' > $SYSTEMD_DIR/console-getty.service | |
rm -f $SYSTEMD_DIR/console-shell.service | |
} | |
setup_systemd_console() | |
{ | |
if grep -q -e 'ConditionPathExists=!/run/openvz' \ | |
-e 'ConditionPathExists=!/proc/vz' $SYSTEMD_GETTY_SERVICE 2>/dev/null; then | |
sed -i -e '/ConditionPathExists=!\/run\/openvz/d' \ | |
-e '/ConditionPathExists=!\/proc\/vz/d' $SYSTEMD_GETTY_SERVICE | |
fi | |
[ -L $SYSTEMD_DIR/getty.target.wants/[email protected] ] && \ | |
rm -f $SYSTEMD_DIR/getty.target.wants/[email protected] | |
[ -L $SYSTEMD_DIR/getty.target.wants/getty-static.service ] && \ | |
rm -f $SYSTEMD_DIR/getty.target.wants/getty-static.service | |
[ -L $ETC_SYSTEMD_DIR/getty.target.wants/[email protected] ] && \ | |
rm -f $ETC_SYSTEMD_DIR/getty.target.wants/[email protected] | |
[ -L $SYSTEMD_DIR/getty.target.wants/[email protected] ] || \ | |
ln -sf $SYSTEMD_DIR/[email protected] $ETC_SYSTEMD_DIR/getty.target.wants/[email protected] | |
[ -f $SYSTEMD_DIR/console-shell.service ] && | |
fix_shell_console | |
} | |
setup_upstart_console() | |
{ | |
local file=/etc/init/$1.conf | |
local getty | |
if [ -x /sbin/mingetty ]; then | |
getty='exec /sbin/mingetty' | |
elif [ -x /sbin/getty ]; then | |
getty='exec /sbin/getty 38400' | |
else | |
echo "Unable to find suitable getty, console setup is skipped" | |
return | |
fi | |
echo "start on stopped rc RUNLEVEL=[2345] | |
stop on runlevel [!2345] | |
respawn | |
$getty $1" > $file | |
} | |
setup_upstart_event_console() | |
{ | |
local file=/etc/init.d/$1 | |
local getty | |
if [ -x /sbin/mingetty ]; then | |
getty='exec /sbin/mingetty' | |
elif [ -x /sbin/getty ]; then | |
getty='exec /sbin/getty 38400' | |
else | |
echo "Unable to find suitable getty console setup is skipped" | |
return | |
fi | |
echo "start on stopped rc2 | |
start on stopped rc3 | |
start on stopped rc4 | |
stop on runlevel 0 | |
stop on runlevel 1 | |
stop on runlevel 6 | |
$getty $1" > $file | |
} | |
setup_inittab() | |
{ | |
local line | |
local getty1 | |
local getty2 | |
if [ -x /sbin/mingetty ]; then | |
getty1='/sbin/mingetty console' | |
getty2='/sbin/mingetty tty2' | |
elif [ -x /sbin/getty ]; then | |
getty1='/sbin/getty 38400 console' | |
getty2='/sbin/getty 38400 tty2' | |
elif [ -x /sbin/agetty ]; then | |
getty1='/sbin/agetty console 38400' | |
getty2='/sbin/agetty tty2 38400' | |
else | |
echo "Unable to find suitable getty, console setup is skipped" | |
return | |
fi | |
line="1:2345:respawn:$getty1" | |
if ! grep -q "$line" /etc/inittab; then | |
echo $line >> /etc/inittab | |
fi | |
line="2:2345:respawn:$getty2" | |
if ! grep -q "$line" /etc/inittab; then | |
echo $line >> /etc/inittab | |
fi | |
} | |
setup_console() | |
{ | |
if [ -f "$SYSTEMD_GETTY_SERVICE" ]; then | |
setup_systemd_console | |
elif [ -d '/etc/init' ]; then | |
setup_upstart_console console | |
setup_upstart_console tty2 | |
elif [ -d "/etc/event.d" ]; then | |
setup_upstart_event_console console | |
setup_upstart_event_console tty2 | |
elif [ -f "/etc/inittab" ]; then | |
setup_inittab | |
fi | |
create_dev console 5 1 | |
create_dev tty2 4 2 | |
} | |
start_console() | |
{ | |
local cmd | |
local tty=tty$START_CONSOLE_ON_TTY | |
if [ -x /sbin/agetty ]; then | |
cmd="/sbin/agetty $tty 38400 $TERM" | |
elif [ -x /sbin/mingetty ]; then | |
cmd="/sbin/mingetty $tty" | |
elif [ -x /sbin/getty ]; then | |
cmd="/sbin/getty 38400 $tty" | |
else | |
echo "No getty found." | |
exit 1 | |
fi | |
create_dev $tty 4 $START_CONSOLE_ON_TTY | |
nohup setsid $cmd & | |
} | |
if [ -n "${START_CONSOLE_ON_TTY}" ]; then | |
start_console | |
else | |
setup_console | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment