merge with 0.30.213
[util-vserver.git] / scripts / chbind
1 #! /bin/bash
2 # $Id: chbind 2394 2006-11-23 21:12:26Z dhozac $
3
4 # Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 # Copyright (C) 2006 Daniel Hokka Zakrisson
6 #  
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License.
10 #  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #  
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
21 test -e "$UTIL_VSERVER_VARS" || {
22     echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
23     exit 1
24 }
25 . "$UTIL_VSERVER_VARS"
26 . "$_LIB_FUNCTIONS"
27
28 function showHelp()
29 {
30     echo \
31 $"Usage: $1  [--silent] [--nid <nid>] [--ip <ip_num>[/<mask>]]
32              [--bcast <broadcast>] [--] <commands> <args>*
33
34 --silent
35     Do not print the addresses assigned.
36 --nid <nid>
37     Network context id to use.
38 --ip <ip_num>[/<mask>]
39     IP address to bind to.
40 --bcast <broadcast>
41     Broadcast address for the network context.
42
43 Report bugs to <$PACKAGE_BUGREPORT>."
44     exit $2
45 }
46
47 function showVersion()
48 {
49     echo \
50 $"chbind $PACKAGE_VERSION -- bind to IP addresses and execute a program
51 This program is part of $PACKAGE_STRING
52
53 Copyright (C) 2004 Enrico Scholz
54 Copyright (C) 2006 Daniel Hokka Zakrisson
55 This program is free software; you may redistribute it under the terms of
56 the GNU General Public License.  This program has absolutely no warranty."
57     exit $1
58 }
59
60 tmp=$(getopt -o + --long ncap:,nid:,ip:,bcast:,disconnect,flag:,secure,silent,help,version -n "$0" -- "$@") || exit 1
61 eval set -- "$tmp"
62
63 OPT_CAPS=()
64 OPT_NID=
65 OPT_DISCONNECT=
66 OPT_FLAGS=()
67 OPT_SECURE=
68 OPT_SILENT=
69 OPT_BCAST=
70 OPT_IPS=()
71
72 while true; do
73     case "$1" in
74         --help)         showHelp $0 0;;
75         --version)      showVersion 0;;
76         --ncap)         OPT_CAPS=( "${OPT_CAPS[@]}" "$2" ); shift;;
77         --nid)          OPT_NID=$2; shift;;
78         --disconnect)   OPT_DISCONNECT=1;;
79         --flag)         OPT_FLAGS=( "${OPT_FLAGS[@]}" "$2" ); shift;;
80         --secure)       OPT_SECURE=1;;
81         --silent)       OPT_SILENT=1;;
82         --ip)           OPT_IPS=( "${OPT_IPS[@]}" --ip "$2" ); shift;;
83         --bcast)        OPT_BCAST=$2; shift;;
84         --)             shift; break;;
85         *)              echo $"chbind: internal error; arg=='$1'" >&2; exit 1;;
86     esac
87     shift
88 done
89
90 $_VSERVER_INFO - FEATURE vnet || exec $_CHBIND_COMPAT \
91         ${OPT_NID:+--nid "$OPT_NID"} \
92         ${OPT_SILENT:+--silent} \
93         ${OPT_BCAST:+--bcast "$OPT_BCAST"} \
94         "${OPT_IPS[@]}" \
95         -- "$@"
96
97 create_cmd=( $_NCONTEXT --create --silentexist
98              ${OPT_SILENT:+--silent}
99              ${OPT_NID:+--nid "$OPT_NID"} )
100
101 chain_cmd=()
102
103 old_IFS=$IFS
104 IFS=,$IFS
105
106 chain_cmd=( "${chain_cmd[@]}"
107                 --
108                 $_NATTRIBUTE --set
109                 ${OPT_SECURE:+--secure}
110                 ${OPT_CAPS:+--ncap "${OPT_CAPS[*]}"}
111                 ${OPT_FLAGS:+--flag "${OPT_FLAGS[*]}"}
112                 --
113                 $_NADDRESS --add
114                 ${OPT_SILENT:+--silent}
115                 ${OPT_BCAST:+--bcast "$OPT_BCAST"}
116                 "${OPT_IPS[@]}" )
117
118 migrate_cmd=( $_NCONTEXT
119               ${OPT_SILENT:+--silent}
120               ${OPT_DISCONNECT:+--disconnect} )
121
122 IFS=$old_IFS
123
124 if test -z "$OPT_NID" || $_VSERVER_INFO -q "$OPT_NID" XIDTYPE static; then
125     "${create_cmd[@]}" "${chain_cmd[@]}" -- "$@"
126     rc=$?
127 else
128     rc=254
129 fi
130
131 test "$rc" -ne 254 || exec "${migrate_cmd[@]}" --nid "$OPT_NID" --migrate -- "$@"
132 exit $rc