~ubuntu-branches/ubuntu/karmic/vzctl/karmic

« back to all changes in this revision

Viewing changes to etc/vps-functions

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2007-04-10 18:08:16 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070410180816-0uuzj9fnna7gmzxv
Tags: 3.0.16-4
Etch has been released which means that this version can be uploaded
to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
#  Copyright (C) 2000-2006 SWsoft. All rights reserved.
3
 
#
4
 
#  This program is free software; you can redistribute it and/or modify
5
 
#  it under the terms of the GNU General Public License as published by
6
 
#  the Free Software Foundation; either version 2 of the License, or
7
 
#  (at your option) any later version.
8
 
#
9
 
#  This program is distributed in the hope that it will be useful,
10
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#  GNU General Public License for more details.
13
 
#
14
 
#  You should have received a copy of the GNU General Public License
15
 
#  along with this program; if not, write to the Free Software
16
 
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
#
18
 
#
19
 
# Common stuff for vzctl helper scripts
20
 
# get the name of the script
21
 
SELFNAME=`basename $0`
22
 
 
23
 
# Set the sane umask
24
 
umask 022
25
 
 
26
 
# Error codes
27
 
VZ_INVALID_PARAMETER_SYNTAX=20
28
 
VZ_FS_NO_DISK_SPACE=46
29
 
VZ_FS_BAD_TMPL=47
30
 
VZ_FS_NEW_VE_PRVT=48
31
 
VZ_CHANGEPASS=74
32
 
VZ_CANT_ADDIP=34
33
 
VZ_IP_INUSE=78
34
 
 
35
 
# iptables parameters
36
 
VE_STATE_DIR="/var/lib/vzctl/veip/"
37
 
CONF_DIR="/etc/vz/conf/"
38
 
 
39
 
ARPSEND_CMD="arpsend -c 1 -w 1"
40
 
IP_CMD=/sbin/ip
41
 
 
42
 
# Prints error message and exits
43
 
# Parameters:
44
 
#   $1 - error message
45
 
#   $2 - exit code
46
 
# Example of usage:
47
 
#   vzerror "Fatal error" 1
48
 
function vzerror()
49
 
{
50
 
        # print errors to stdout too
51
 
        ERR=$?
52
 
        echo "$SELFNAME ERROR: $1"
53
 
        exit $2
54
 
}
55
 
 
56
 
# Prints warning message
57
 
# Parameters:
58
 
#   $* - error message
59
 
# Example of usage:
60
 
#   vzwarning Invalid user
61
 
function vzwarning()
62
 
{
63
 
        echo "$SELFNAME WARNING: $*"
64
 
}
65
 
 
66
 
# Prints debug message
67
 
# Parameters:
68
 
#   $* - debug message
69
 
# Example of usage:
70
 
#   vzdebug Trying to start ls
71
 
function vzdebug()
72
 
{
73
 
        echo "$SELFNAME: $*"
74
 
}
75
 
 
76
 
# Checks if environment variable exists,
77
 
# and exits with exit code 1 if not
78
 
# Parameters:
79
 
#   $* - option names
80
 
# Example:
81
 
#   vzcheckvar VEID IP_ADDR
82
 
function vzcheckvar()
83
 
{
84
 
        for VAR in $*; do
85
 
                if eval test "\"x\$$VAR\"" = "x"; then
86
 
                        vzerror "Missing parameter: $VAR" $VZ_INVALID_PARAMETER_SYNTAX
87
 
                fi
88
 
        done
89
 
}
90
 
 
91
 
# This function fills $NETDEVICES with all network interfaces
92
 
# You should always call it before calling vzarp
93
 
function vzgetnetdev()
94
 
{
95
 
# Get a list of interfaces, excluding ones with LOOPBACK NOARP or SLAVE flags
96
 
        NETDEVICES=`${IP_CMD} link list | egrep -v -E "LOOPBACK|NOARP|SLAVE" | \
97
 
                awk "/^[0-9].*:/&&/UP/ {print \\$2}" | sed -e "s/:\$//"`
98
 
}
99
 
 
100
 
# Adds/deletes public ARP records for given IP for all interfaces
101
 
# Parameters:
102
 
#   $1          - should be either "add" or "del"
103
 
#   $2          - IP address
104
 
#   $NETDEVICES - Network devices used to take MAC addresses from
105
 
function vzarp()
106
 
{
107
 
        local DEV
108
 
 
109
 
        [ -z "${NETDEVICES}" ] && vzwarning "Device list is empty"
110
 
        for DEV in $NETDEVICES; do
111
 
                if [ $(cat /proc/sys/net/ipv4/conf/$DEV/proxy_arp) == 0 ] ; then
112
 
                    vzwarning "Function proxy_arp for $DEV is set to 0. Enable with 'sysctl -w net.ipv4.conf.$DEV.proxy_arp=1'. See /usr/share/doc/vzctl/README.Debian."
113
 
                fi
114
 
                ${IP_CMD} neigh $1 proxy $2 dev $DEV > /dev/null 2>&1
115
 
        done
116
 
}
117
 
 
118
 
# Send ARP request to detect that somebody already have this IP 
119
 
function vzarpipdetect()
120
 
{
121
 
        local DEV
122
 
        local ip
123
 
        local cmd
124
 
 
125
 
        [ -z "${1}" ] && return
126
 
        [ "${SKIP_ARPDETECT}" = "yes" ] && return
127
 
 
128
 
        for ip in ${1}; do
129
 
                cmd="$cmd -e $ip"
130
 
        done
131
 
 
132
 
        for DEV in $NETDEVICES; do
133
 
                ${ARPSEND_CMD} -D ${cmd} $DEV || vzwarning "${ARPSEND_CMD} -D ${cmd} $DEV FAILED"
134
 
        done    
135
 
}
136
 
 
137
 
# Send ARP request to update neighbour ARP caches
138
 
function vzarpipset()
139
 
{
140
 
        local DEV
141
 
        local ip
142
 
        local dev
143
 
 
144
 
        [ -z "${1}" ] && return
145
 
 
146
 
        for dev in $NETDEVICES; do
147
 
                for ip in ${1}; do
148
 
                        opt="-i ${ip} -e ${ip}"
149
 
                        ${ARPSEND_CMD} -U ${opt} ${dev} || vzwarning "${ARPSEND_CMD} -U ${opt} ${dev} FAILED"
150
 
                done
151
 
        done    
152
 
}
153
 
 
154
 
# Sets VE0 source routing for given IP
155
 
# Parameters:
156
 
#   $1 - IP address
157
 
function vzaddrouting()
158
 
{
159
 
        local src_addr
160
 
        local device=
161
 
        if ! ${IP_CMD} route list $1 | grep "$1 dev venet0" > /dev/null 2>&1;
162
 
        then
163
 
                if [ -n "${VE_ROUTE_SRC_DEV}" ]; then
164
 
                        device="dev ${VE_ROUTE_SRC_DEV}"
165
 
                fi
166
 
                src_addr=`ip route list table local ${device} | grep '^local'|\
167
 
                        cut -d' ' -f2 | grep -v '^127\.' | head -n 1`
168
 
                if [ -z "${src_addr}" ]; then
169
 
                        vzerror "Unable to get source ip [${device}]" $VZ_CANT_ADDIP
170
 
                fi
171
 
                ${IP_CMD} route add $1 dev venet0 src ${src_addr} || \
172
 
                        vzerror "Unable to add route ${IP_CMD} route add $1 dev venet0 src ${src_addr}" $VZ_CANT_ADDIP
173
 
        fi
174
 
}
175
 
 
176
 
# Deletes VE0 source routing for given IP
177
 
# Parameters:
178
 
#   $1 - IP address
179
 
function vzdelrouting()
180
 
{
181
 
if ${IP_CMD} route list $1 | grep  "$1 dev venet0" >/dev/null 2>&1; then
182
 
        ${IP_CMD} route del $1 dev venet0 || \
183
 
                vzwarning "Unable to del route ${IP_CMD} route del $1 dev venet0"
184
 
fi
185
 
}