1#!/bin/sh
2
3# See whether given command is readonly
4# and therefore doesn't need root credentials
5__readonly_cmd () {
6    case "$1" in
7        defaults|df|get|list|show|snaplist) echo "0"
8            ;;
9        *) echo "1"
10            ;;
11    esac
12}
13
14# Process command line options-------------------------
15__parse_cmd () {
16    while [ $# -gt 0 ] ; do
17        case "$1" in
18            activate)   __activate "$2"
19                        exit
20                ;;
21            cap)        __rctl_limits "$2"
22                        exit
23                ;;
24            clean)      __clean "$2" "$3"
25                        exit
26                ;;
27            clone)      __export_props "$@"
28                        __clone_jail "$2"
29                        exit
30                ;;
31            console)    export _force=0
32                        if [ "$2" = "-f" ] ; then
33                            export _force=1
34                            shift
35                        fi
36                        __console "$2"
37                        exit
38                ;;
39            create)     local _tag _hostname _ip4if _ip4addr _ip4subnet _ip4ip
40                        __export_props "$@"
41                        _hostname="${host_hostname}"
42                        _tag="${tag}"
43                        _ip4if="$(echo ${ip4_addr} | awk 'BEGIN { FS = "|" } \
44                            ; { print $1 }')"
45                        _ip4addr="$(echo ${ip4_addr} | \
46                            awk 'BEGIN { FS = "|" } \
47                            ; { gsub("\/[0-9]+","") ; print $2 }')"
48                        _ip4subnet="$(echo ${ip4_addr} | \
49                            awk 'BEGIN { FS = "/" } \
50                            ; { print $2 }')"
51
52                        for _i in $(seq ${count}) ; do
53                            unset uuid host_hostuuid host_hostname
54                            uuid="$(uuidgen)"
55                            host_hostuuid="${uuid}"
56
57                            if [ "${count}" -eq 1 ] ; then
58                                tag="${_tag}"
59                                if [ -n "${iocset_host_hostname}" ] ; then
60                                    host_hostname="${_hostname}"
61                                else
62                                    host_hostname="${uuid}"
63                                fi
64                            elif [ "${count}" -gt 1 ] ; then
65                                tag="${_tag}_${_i}"
66                                if [ -n "${iocset_host_hostname}" ] ; then
67                                    host_hostname="${_hostname}_${_i}"
68                                else
69                                    host_hostname="${uuid}"
70                                fi
71
72                                if [ "${ip4_addr}"  != "none" ] ; then
73                                    if [ "${_i}" = 1 ] ; then
74                                        _ip4ip="$(echo ${_ip4addr} | \
75                                            cut -d "." -f 4)"
76                                    else
77                                        _ip4ip="$(expr ${_ip4ip} + 1)"
78                                        ip4_addr="${_ip4if}|$(echo ${_ip4addr} | \
79                                            cut -d "." -f 1-3).${_ip4ip}/${_ip4subnet}"
80                                    fi
81                                fi
82                            fi
83
84                            __create_jail "$@"
85                        done
86                        exit
87                ;;
88            deactivate) __deactivate "$2"
89                        exit
90                ;;
91            defaults)   __print_defaults
92                        exit
93                ;;
94            destroy)    if [ "$2" = "-f" ] ; then
95                            export _force=1
96                            shift 2
97                            for _jdel in $@ ; do
98                                __destroy_jail "${_jdel}"
99                                unset jail_datasets uuid jail_zfs_dataset _fs \
100                                      _jail_type
101                                export jail_datasets="$(zfs list -d3 -rH -o \
102                                       name ${pool}/iocell | egrep -v \
103                                       "jails$|releases$|templates$|download|${pool}/iocell$|/root$")"
104                            done
105                        else
106                            export _force=0
107                            shift
108                            for _jdel in $@ ; do
109                                __destroy_jail "${_jdel}"
110                                unset jail_datasets uuid jail_zfs_dataset _fs \
111                                      _jail_type
112                                export jail_datasets="$(zfs list -d3 -rH -o \
113                                       name ${pool}/iocell | egrep -v \
114                                       "jails$|releases$|templates$|download|${pool}/iocell$|/root$")"
115                            done
116                        fi
117
118                        exit
119                ;;
120            df)         __print_disk
121                        exit
122                ;;
123            exec)       shift
124                        __exec "$@"
125                        exit
126                ;;
127            export)     __export "$2"
128                        exit
129                ;;
130            fetch)      __export_props "$@"
131                        local _gitcheck=$(echo $* | grep -v "ftp" | \
132                                        grep -q "/" ; echo $?)
133
134                        # Check if the user is specifying a git plugin and also
135                        # check to see if they want a thick plugin
136                        if [ "${_gitcheck}" = "0" ] ; then
137                            shift
138                            if [ "$1" = "-t" ] ; then
139                                shift
140                                __fetch_plugin_thick "$@"
141                                exit 0
142                            else
143                                __fetch_plugin "$@"
144                                exit 0
145                            fi
146                        fi
147
148                        __fetch_release "$@"
149                        exit
150                ;;
151            get)        __get_jail_prop "$2" "$3"
152                        exit
153                ;;
154            help)       __help
155                        exit
156                ;;
157            init-host)  __init_host "$2" "$3"
158                        exit
159                ;;
160            inuse)      __rctl_inuse "$2"
161                        exit
162                ;;
163            import)     shift
164                        __import "$@"
165                        exit
166                ;;
167            limits)     __rctl_list "$2"
168                        exit
169                ;;
170            list)       __list_jails "$2" "$3"
171                        exit
172                ;;
173            promote)    __promote "$2"
174                        exit
175                ;;
176            pull)       __pull "$2"
177                        exit
178                ;;
179            push)       __push "$2"
180                        exit
181                ;;
182            rcboot)     __rc_jails boot
183                        exit
184                ;;
185            rcshutdown) __rc_jails shutdown
186                        exit
187                ;;
188            reboot|restart)    shift
189                               if [ "$1" = "-s" ] ; then
190                                    shift
191                                    for _j in $@ ; do
192                                        __restart_jail "${_j}"
193                                    done
194                                    exit
195                               else
196                                    for _j in $@ ; do
197                                        __reboot_jail "${_j}"
198                                    done
199                                    exit
200                                fi
201                ;;
202            reset)      __reset_jail_props "$2"
203                        exit
204                ;;
205            rollback)   __rollback "$2"
206                        exit
207                ;;
208            runtime)    __runtime "$2"
209                        exit
210                ;;
211            send)       shift
212                        __send "$@"
213                        exit
214                ;;
215            set)        __export_props "$@"
216                        shift
217                        # Big thanks to john@ix for his huge help with this
218                        local _nprops _props _njails _jails _var _pair _i \
219                              _jail _fulluuid _j _prop _pname _pval _dataset
220
221                        _nprops=0
222                        _njails=0
223
224                        for _jset in "$@" ; do
225                            _var="$(echo ${_jset} | cut -f1 -d'=' -s)"
226
227                            if [ -n "${_var}" ] ; then
228                                _pair="_prop_${_nprops}='${_jset}'"
229                                eval "${_pair}"
230                                _nprops=$((_nprops + 1))
231                                continue
232                            fi
233
234                            eval "_jail_${_njails}=${_jset}"
235                            _njails=$((_njails + 1))
236                        done
237
238                        if [ "${_njails}" = "0" ] ; then
239                            __die "please specify a jail!"
240                        fi
241
242                        _i=0
243                        while [ "${_i}" -lt "${_njails}" ] ; do
244                            _var=\$$(printf "_jail_${_i}")
245                            _jail=$(eval "echo ${_var}")
246                            _dataset=$(__find_jail ${_jail}) || exit $?
247                            _fulluuid="$(__check_name ${_jail} ${_dataset})"
248                            _j=0
249
250                            while [ "${_j}" -lt "${_nprops}" ] ; do
251                                _var=\$$(printf "_prop_${_j}")
252                                _prop=$(eval "echo ${_var}")
253                                _pname=$(echo "${_prop}" | cut -f1 -d'=' -s)
254                                _pval=$(echo "${_prop}" | cut -f2 -d'=' -s)
255
256                                __set_jail_prop "${_pname}=${_pval}" \
257                                    "${_fulluuid}"
258                                _j=$((_j + 1))
259                            done
260
261                            _i=$((_i + 1))
262                        done
263                        exit
264                ;;
265            snaplist)   __snaplist "$2"
266                        exit
267                ;;
268            snapmount)  __mount_snapshot "$2" "$3"
269                        exit
270                ;;
271            snapremove) __snapremove "$2"
272                        exit
273                ;;
274            snapshot|snap)   if [ "$2" = "-f" ] ; then
275                            export _force=1
276                            shift
277                        fi
278
279                        __snapshot "$2" "$3"
280                        exit
281                ;;
282            snapumount) __umount_snapshot "$2"
283                        exit
284                ;;
285            start)      if [ "$2" = "-f" ] ; then
286                            export _force=1
287                            shift 2
288                        else
289                            export _force=0
290                            shift
291                        fi
292
293                        for _j in $@ ; do
294                            __start_jail "${_j}"
295                        done
296                        exit
297                ;;
298            stop)       shift
299                        for _j in $@ ; do
300                            __stop_jail "${_j}"
301                        done
302                        exit
303                ;;
304            uncap)      __rctl_uncap "$2"
305                        exit
306                ;;
307            update)     __update "$2" "$3"
308                        exit
309                ;;
310            upgrade)    __export_props "$@"
311                        shift
312                        __upgrade "$@"
313                        exit
314                ;;
315            pkg)        __pkg_wrapper "$@"
316                        exit
317                ;;
318            *)          __usage
319                        exit
320                ;;
321        esac
322        shift
323    done
324}
325