ready for tagging
[util-vserver.git] / scripts / chbind
1 #! /bin/bash
2 # $Id: chbind 2599 2007-08-26 21:30:50Z 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              [--lback <loopback>] [--bcast <broadcast>]
33              [--] <commands> <args>*
34
35 --silent
36     Do not print the addresses assigned.
37 --nid <nid>
38     Network context id to use.
39 --ip <ip_num>[/<mask>]
40     IP address to bind to.
41 --lback <loopback>
42     Loopback address for the network context.
43 --bcast <broadcast>
44     Broadcast address for the network context.
45
46 Report bugs to <$PACKAGE_BUGREPORT>."
47     exit $2
48 }
49
50 function showVersion()
51 {
52     echo \
53 $"chbind $PACKAGE_VERSION -- bind to IP addresses and execute a program
54 This program is part of $PACKAGE_STRING
55
56 Copyright (C) 2004 Enrico Scholz
57 Copyright (C) 2006 Daniel Hokka Zakrisson
58 This program is free software; you may redistribute it under the terms of
59 the GNU General Public License.  This program has absolutely no warranty."
60     exit $1
61 }
62
63 tmp=$(getopt -o + --long ncap:,nid:,ip:,bcast:,lback:,disconnect,flag:,secure,silent,help,version -n "$0" -- "$@") || exit 1
64 eval set -- "$tmp"
65
66 OPT_CAPS=()
67 OPT_NID=
68 OPT_DISCONNECT=
69 OPT_FLAGS=()
70 OPT_SECURE=
71 OPT_SILENT=
72 OPT_BCAST=
73 OPT_LBACK=
74 OPT_IPS=()
75
76 while true; do
77     case "$1" in
78         --help)         showHelp $0 0;;
79         --version)      showVersion 0;;
80         --ncap)         OPT_CAPS=( "${OPT_CAPS[@]}" "$2" ); shift;;
81         --nid)          OPT_NID=$2; shift;;
82         --disconnect)   OPT_DISCONNECT=1;;
83         --flag)         OPT_FLAGS=( "${OPT_FLAGS[@]}" "$2" ); shift;;
84         --secure)       OPT_SECURE=1;;
85         --silent)       OPT_SILENT=1;;
86         --ip)           OPT_IPS=( "${OPT_IPS[@]}" --ip "$2" ); shift;;
87         --bcast)        OPT_BCAST=$2; shift;;
88         --lback)        OPT_LBACK=$2; shift;;
89         --)             shift; break;;
90         *)              echo $"chbind: internal error; arg=='$1'" >&2; exit 1;;
91     esac
92     shift
93 done
94
95 $_VSERVER_INFO - FEATURE vnet || exec $_CHBIND_COMPAT \
96         ${OPT_NID:+--nid "$OPT_NID"} \
97         ${OPT_SILENT:+--silent} \
98         ${OPT_BCAST:+--bcast "$OPT_BCAST"} \
99         "${OPT_IPS[@]}" \
100         -- "$@"
101
102 create_cmd=( $_NCONTEXT --create --silentexist
103              ${OPT_SILENT:+--silent}
104              ${OPT_NID:+--nid "$OPT_NID"} )
105
106 chain_cmd=()
107
108 old_IFS=$IFS
109 IFS=,$IFS
110
111 chain_cmd=( "${chain_cmd[@]}"
112                 --
113                 $_NATTRIBUTE --set
114                 ${OPT_SECURE:+--secure}
115                 ${OPT_CAPS:+--ncap "${OPT_CAPS[*]}"}
116                 ${OPT_FLAGS:+--flag "${OPT_FLAGS[*]}"}
117                 --
118                 $_NADDRESS --add
119                 ${OPT_SILENT:+--silent}
120                 ${OPT_BCAST:+--bcast "$OPT_BCAST"}
121                 ${OPT_LBACK:+--lback "$OPT_LBACK"}
122                 "${OPT_IPS[@]}" )
123
124 migrate_cmd=( $_NCONTEXT
125               ${OPT_SILENT:+--silent}
126               ${OPT_DISCONNECT:+--disconnect} )
127
128 IFS=$old_IFS
129
130 if test -z "$OPT_NID" || $_VSERVER_INFO -q "$OPT_NID" XIDTYPE static; then
131     "${create_cmd[@]}" "${chain_cmd[@]}" -- "$@"
132     rc=$?
133 else
134     rc=254
135 fi
136
137 test "$rc" -ne 254 || exec "${migrate_cmd[@]}" --nid "$OPT_NID" --migrate -- "$@"
138 exit $rc