Break up the old chbind into ncontext, nattribute, and naddress.
[util-vserver.git] / scripts / chbind
1 #! /bin/bash
2 # $Id$
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 $_VSERVER_INFO - FEATURE vnet || exec $_CHBIND_COMPAT "$@"
61
62 tmp=$(getopt -o + --long ncap:,nid:,ip:,bcast:,disconnect,flag:,secure,silent,help,version -n "$0" -- "$@") || exit 1
63 eval set -- "$tmp"
64
65 OPT_CAPS=()
66 OPT_NID=
67 OPT_DISCONNECT=
68 OPT_FLAGS=()
69 OPT_SECURE=
70 OPT_SILENT=
71 OPT_BCAST=
72 OPT_IPS=()
73
74 while true; do
75     case "$1" in
76         --help)         showHelp $0 0;;
77         --version)      showVersion 0;;
78         --ncap)         OPT_CAPS=( "${OPT_CAPS[@]}" "$2" ); shift;;
79         --nid)          OPT_NID=$2; shift;;
80         --disconnect)   OPT_DISCONNECT=1;;
81         --flag)         OPT_FLAGS=( "${OPT_FLAGS[@]}" "$2" ); shift;;
82         --secure)       OPT_SECURE=1;;
83         --silent)       OPT_SILENT=1;;
84         --ip)           OPT_IPS=( "${OPT_IPS[@]}" "$2" ); shift;;
85         --bcast)        OPT_BCAST=$2; shift;;
86         --)             shift; break;;
87         *)              echo $"chbind: internal error; arg=='$1'" >&2; exit 1;;
88     esac
89     shift
90 done
91
92 create_cmd=( $_NCONTEXT --create --silentexist
93              ${OPT_SILENT:+--silent}
94              ${OPT_NID:+--nid "$OPT_NID"} )
95
96 chain_cmd=()
97
98 old_IFS=$IFS
99 IFS=,$IFS
100
101 chain_cmd=( "${chain_cmd[@]}"
102                 --
103                 $_NATTRIBUTE --set
104                 ${OPT_SECURE:+--secure}
105                 ${OPT_CAPS:+--ncap "${OPT_CAPS[*]}"}
106                 ${OPT_FLAGS:+--flag "${OPT_FLAGS[*]}"}
107                 --
108                 $_NADDRESS --add
109                 ${OPT_SILENT:+--silent}
110                 ${OPT_BCAST:+--bcast "$OPT_BCAST"} )
111
112 for ip in "${OPT_IPS[@]}"; do
113     chain_cmd=( "${chain_cmd[@]}" --ip "$ip" )
114 done
115                 
116 migrate_cmd=( $_NCONTEXT
117               ${OPT_SILENT:+--silent}
118               ${OPT_DISCONNECT:+--disconnect} )
119
120 IFS=$old_IFS
121
122 if test -z "$OPT_NID" || $_VSERVER_INFO -q "$OPT_NID" XIDTYPE static; then
123     "${create_cmd[@]}" "${chain_cmd[@]}" -- "$@"
124     rc=$?
125 else
126     rc=254
127 fi
128
129 test "$rc" -ne 254 || exec "${migrate_cmd[@]}" --nid "$OPT_NID" --migrate -- "$@"
130 exit $rc