source: src-sh/warden/bin/warden

Last change on this file was 5fb7b91, checked in by Kris Moore <kris@…>, 4 days ago

Update AppCafe? jails config page, allow setting / saving IPv4 address and
NIC to start jail on

  • Property mode set to 100755
File size: 32.2 KB
Line 
1#!/bin/sh
2# Command-line interaction script for the warden
3# Author: Kris Moore
4# License: BSD
5# "The Warden" Copyright 2013 PC-BSD Software (iXsystems)
6######################################################################
7
8# Source external functions
9. /usr/local/share/pcbsd/scripts/functions.sh
10
11require_root() 
12{
13  UTEST="`whoami`"
14  if [ "$UTEST" != "root" ] ; then
15     exit_err "This command must be run as root!"
16  fi
17}
18
19# Source our functions
20PROGDIR="/usr/local/share/warden"
21
22# Source our variables
23. ${PROGDIR}/scripts/backend/functions.sh
24
25title()
26{
27  echo "Warden version ${WARDENVER}
28---------------------------------"
29};
30
31
32# Function to display help information
33help_main()
34{
35  title
36  echo "Available commands
37
38Type in help <command> for information and usage about that command
39
40         help - This help file"
41
42if [ -e "${PROGDIR}/bin/warden-gui" ] ; then
43   echo "          gui - Launch the GUI menu"
44fi
45
46cat<<__EOF__
47         auto - Toggles the autostart flag for a jail
48      bspkgng - BootStrap pkgng and setup TrueOS repo
49      checkup - Check for updates to a jail
50       chroot - Launches chroot into a jail
51       create - Creates a new jail
52      details - Display usage details about a jail
53       delete - Deletes a jail
54       export - Exports a jail to a .wdn file
55        fstab - Start users \$EDITOR on jails custom fstab
56          get - Gets options list for a jail
57       import - Imports a jail from a .wdn file
58         list - Lists the installed jails
59         pkgs - Lists the installed packages in a jail
60         pbis - Lists the installed pbi's in a jail
61          set - Sets options for a jail
62        start - Start a jail
63         stop - Stops a jail
64         type - Set the jail type (pluginjail|portjail|standard)
65     template - Manage jail templates
66         snap - Jail snapshot management
67        clone - Clone an existing jail to a new jail
68     cronsnap - Schedule snapshot creation via cron
69__EOF__
70};
71
72help_get()
73{
74 title
75 echo "Help get
76
77Lets you get options for your jail
78
79Available options:
80
81             iface: Lets you see the network interface for this jail
82              ipv4: Lets you see the IPv4 address for this jail
83              ipv6: Lets you see the IPv6 address for this jail
84        alias-ipv4: Lets you see additional IPv4 addresses for this jail
85        alias-ipv6: Lets you see additional IPv6 addresses for this jail
86       bridge-ipv4: Lets you see the vnet bridge IPv4 address for this jail
87       bridge-ipv6: Lets you see the vnet bridge IPv6 address for this jail
88 alias-bridge-ipv4: Lets you see additional vnet bridge IPv4 addresses for this jail
89 alias-bridge-ipv6: Lets you see additional vnet bridge IPv6 addresses for this jail
90defaultrouter-ipv4: Lets you see the default IPv4 router for this jail
91defaultrouter-ipv6: Lets you see the default IPv6 router for this jail
92             flags: Lets you see additional flags to pass to the 'jail' command at startup
93
94Usage:
95
96  warden get ipv4 <Jail>
97
98  warden get flags <Jail>
99
100Example:
101
102  warden get ipv4 myjail
103
104  warden get flags myjail
105"
106};
107
108help_set()
109{
110 title
111 echo "Help set
112
113Lets you set options for your jail
114
115Available options:
116
117             iface: Set the network interface for this jail
118              ipv4: Set the IPv4 address for this jail
119              ipv6: Set the IPv6 address for this jail
120        alias-ipv4: Set additional IPv4 addresses for this jail
121        alias-ipv6: Set additional IPv6 addresses for this jail
122       bridge-ipv4: Set the vnet bridge IPv4 address for this jail
123       bridge-ipv6: Set the vnet bridge IPv6 address for this jail
124 alias-bridge-ipv4: Set additional vnet bridge IPv4 for this jail
125 alias-bridge-ipv6: Set additional vnet bridge IPv6 for this jail
126defaultrouter-ipv4: Set the default vnet IPv4 router for this jail
127defaultrouter-ipv6: Set the default vnet IPv6 router for this jail
128             flags: Set additional flags to pass to the 'jail' command
129       vnet-enable: Enables vnet support for the jail
130      vnet-disable: Disables vnet support for this jail
131
132Usage:
133
134  warden set ipv4 <Jail> <IP1>,<IP2>,<IP3>
135
136  warden set flags <Jail> <Flag1>,<Flag2>,<Flag3>
137
138Example:
139
140  warden set ipv4 myjail 192.168.1.50,192.168.2.10
141
142  warden set ipv6 myjail fe80::8e89:a5ff:fe52:ad19,fe80::8e89:a5ff:fe52:ad21
143
144  warden set flags myjail allow.raw_sockets=true,allow.chflags=true
145
146  warden set vnet-enable myjail
147"
148};
149
150help_checkup()
151{
152 title
153 echo "Help checkup
154
155Check for updates to a jail(s)
156
157Usage:
158
159  warden checkup <Jail>
160   or
161  warden checkup all
162
163Example:
164
165  warden checkup myjail
166"
167};
168
169help_snap()
170{
171 title
172 echo "Help snap
173
174Perform Snapshot operations on a jail
175
176Available Sub-Commands: list make remove revert
177
178list:
179
180  warden snap list <jail>
181
182make:
183
184  warden snap make <jail> <Optional Comment>
185
186remove:
187
188  warden snap remove <jail> <snapshot>
189
190revert:
191
192  warden snap revert <jail> <snapshot>
193
194"
195};
196
197help_clone()
198{
199 title
200 echo "Help clone
201
202Clone an existing jail into a new jail with (optional) new IP address
203
204Usage:
205
206  warden clone <Jail> <NewJail>
207
208Example:
209
210  warden clone myjail newjail --ipv4=127.0.0.4/24
211"
212};
213
214help_cronsnap()
215{
216 title
217 echo "Help cronsnap
218
219Schedule a jail snapshot
220
221Usage:
222
223  warden cronsnap <Jail> <action> <frequency> <daysToKeep>
224 
225  action = start / stop
226  frequency = daily / hourly
227  daysToKeep = Number of days to keep snapshots
228
229Example:
230
231  warden cronsnap myjail start daily 10
232
233  or
234
235  warden cronsnap myjail stop
236"
237};
238
239help_details()
240{
241 title
242 echo "Help details
243
244View usage details about a jail
245
246Usage:
247
248  warden details <Jail>
249
250Example:
251
252  warden details myjail
253"
254};
255
256help_template()
257{
258 title
259 echo "Help template
260
261Manage the jail templates used to create new FreeBSD jails
262
263Required subcommands:
264
265  create - Create a new jail template
266  delete - Delete a jail template
267    list - List jail templates on system
268
269Flags:
270
271  create:
272     -fbsd <version>   - FreeBSD Version, 9.1-RELEASE, 9.0-RELEASE, etc
273     -trueos <version> - TrueOS Version, 9.1-RELEASE, 9-STABLE, etc
274     -arch <arch type> - System architecture, i386/amd64
275     -tar <file>       - Use tar archive as template contents (Ignores -arch / -fbsd)
276     -nick <nickname>  - Template nickname for creating jails / deleting templates
277
278Usage:
279
280  warden template create <flags>
281   or
282  warden template delete <template nickname>
283   or
284  warden template list
285
286Example:
287
288  warden template create -fbsd 9.1-RELEASE -arch amd64 -nick 91amd64
289"
290};
291
292
293help_type()
294{
295 title
296 echo "Help type
297
298Lets you set the type of jail to these options:
299
300portjail: Jail with access to your home directories and Xorg
301pluginjail: Jail that can be used to install FreeNAS plugins
302standard: Normal FreeBSD secure jail environment
303
304The flag status can be checked with 'warden list'.
305
306Usage:
307
308  warden type <Jail> <type>
309
310Example:
311
312  warden type myjail portjail
313"
314};
315
316
317help_bspkgng()
318{
319 title
320 echo "Help bspkgng
321
322BootStraps the pkgng package system, and installs the TrueOS repository
323and utilities into a jail.
324
325Usage:
326
327  warden bspkgng <Jail>
328
329Example:
330
331  warden bspkgng myjail
332"
333};
334
335
336
337help_auto()
338{
339 title
340 echo "Help auto
341
342Toggles the autostart flag for a specified jail. If the flag is
343enabled, then the jail will be started at system bootup automatically.
344The flag status can be checked with 'warden list'.
345
346Usage:
347
348  warden auto <Jail>
349
350Example:
351
352  warden auto myjail
353"
354};
355
356help_pkgs()
357{
358 title
359 echo "Help pkgs
360
361Generates a listing of the installed ports in a jail.
362
363Usage:
364
365  warden pkgs <Jail>
366
367Example:
368
369  warden pkgs myjail
370"
371};
372
373help_pbis()
374{
375 title
376 echo "Help pbis
377
378Generates a listing of the installed pbi's in a jail.
379
380Usage:
381
382  warden pbis <Jail>
383
384Example:
385
386  warden pbis myjail
387"
388};
389
390help_list()
391{
392 title
393 echo "Help list
394
395Generates a quick listing of the installed jails, including the running
396status.
397
398Available Flags:
399  -v           (Verbose listing)
400
401Usage:
402
403  warden list [flags] [hosts]
404"
405};
406
407help_export()
408{
409 title
410 echo "Help export
411
412Export will backup a full copy of your jail, compressing it and
413saving it to a .wdn file. This file can then be taken and
414imported on another system to migrate the jail to new hardware.
415Also a .wdn file may be imported on the same system, in order to
416'clone' an existing jail with a new IP address.
417
418Available Flags:
419  --dir=<DIR>  (Specify the directory you want to place the finished .wdn file)
420
421Usage:
422
423  warden export <Jail> --dir=<path>
424
425Example:
426
427  warden export myjail --dir=/tmp
428"
429};
430
431help_fstab()
432{
433 title
434 echo "Help fstab
435
436Runs the users \$EDITOR on the jails custom fstab file. Can be used to setup
437nullfs mounts and others for a specific jail
438
439Usage:
440
441  warden fstab [hosts]
442"
443};
444
445help_import()
446{
447 title
448 echo "Help import
449
450Imports a jail from a .wdn file. Can assign a new IP / Hostname to
451the imported jail on the fly using optional flags. 
452
453Available Flags:
454  --ipv4=<IP/NETMASK> (Specify a new IPv4/NETMASK address to assign to this jail)
455  --ipv6=<IP/NETMASK> (Specify a new IPv6/NETMASK address to assign to this jail)
456  --host=<HOST>       (Specify a new host name to assign to this jail)
457
458Usage:
459
460  warden import <jailname> <.wdn file>
461
462Example:
463
464  warden import myjail /home/kris/myjail.wdn
465"
466};
467
468
469
470help_chroot()
471{
472 title
473 echo "Help chroot
474
475Logs into a jail using chroot, or runs the supplied command.
476
477Usage:
478
479  warden chroot <Jail> <optional cmd>
480
481Example:
482
483  warden chroot myjail
484"
485};
486
487
488help_start()
489{
490 title
491 echo "Help start
492
493Starts a stopped jail
494
495Usage:
496
497  warden start <Jail>
498
499Example:
500
501  warden start myjail
502"
503};
504
505help_stop()
506{
507 title
508 echo "Help stop
509
510Stops a running jail
511
512Usage:
513  warden stop <Jail>
514
515Example:
516
517  warden stop myjail
518"
519};
520
521
522help_delete()
523{
524 title
525 echo "Help delete
526
527Stops and deletes a jail
528
529Available Flags:
530  --confirm       (Does not prompt to confirm deletion. DANGEROUS)
531
532Usage:
533
534  warden delete <Jail>
535
536Example:
537
538  warden delete myjail
539"
540};
541
542
543
544help_create()
545{
546 title
547 echo "Help create
548
549Creates a new jail, with options for system source, ports and autostarting.
550
551Available Flags:
552  -32                          Create 32bit jail on 64bit system
553  --autoipv4                   Use the next available IPv4 address from the pool
554  --ipv4=<ip/mask>             Set primary IPv4 address for jail
555  --ipv6=<ip/mask>             Set primary IPv6 address for jail
556  --archive <tar>              Use specified tar file for BSD jail creation
557  --bulk <number>              Create <number> of new jails, using default IP4 pool
558                               or address pool specified with --ip4pool
559  --ip4pool <address>          Starting IPv4 address to use when creating jails in bulk
560  --linuxjail <script>         Make this a linux jail and use supplied script for installation
561  --linuxarchive <tar>         Use specified tar file for Linux jail creation
562  --pluginjail                 Make this a pluginjail
563  --ports                      Includes the ports tree
564  --portjail                   Make this a portjail
565  --src                        Includes /usr/src system source
566  --startauto                  Start this jail at system boot
567  --template <string>          Specify a jail template to build with
568  --vanilla                    Don't install PC-BSD pkgng repo and utilities
569  --version <string>           Use this instead of /etc/version
570
571Usage:
572
573  warden create <JAILNAME> <flags>
574
575Example:
576
577  warden create jailbird --ipv4=192.168.0.25/24 --src --ports --startauto
578"
579};
580
581# Fix old style meta-data, can be removed after 9.1
582fix_old_meta
583
584# This checks if we have JDIR on a valid ZFS dataset
585zfs_prog_check
586
587# Check what the user wants to do
588case "$1" in
589
590   help) case "$2" in
591             auto) help_auto ;;
592          bspkgng) help_bspkgng ;;
593           create) help_create ;;
594           start) help_start ;;
595           stop) help_stop ;;
596           delete) help_delete ;;
597           details) help_details ;;
598           checkup) help_checkup ;;
599           chroot) help_chroot ;;
600           list) help_list ;;
601           export) help_export ;;
602           fstab) help_fstab ;;
603           import) help_import ;;
604           pkgs) help_pkgs ;;
605           pbis) help_pbis ;;
606           template) help_template ;;
607           type) help_type ;;
608           get) help_get ;;
609           set) help_set ;;
610           snap) help_snap ;;
611           cronsnap) help_cronsnap ;;
612           clone) help_clone ;;
613           *) help_main ;;
614         esac  ;;
615
616
617    pkgs) JAILNAME="${2}"
618         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
619         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
620         set_warden_metadir
621
622         ${PROGDIR}/scripts/backend/listpkgs.sh "${JAILNAME}" 
623         ;;
624details) JAILNAME="${2}"
625         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
626         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
627         set_warden_metadir
628
629         ${PROGDIR}/scripts/backend/details.sh "${JAILNAME}" 
630         ;;
631    pbis) JAILNAME="${2}"
632         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
633         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
634         set_warden_metadir
635
636         ${PROGDIR}/scripts/backend/listpbis.sh "${JAILNAME}" 
637         ;;
638    fstab) JAILNAME="${2}"
639         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
640         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
641         set_warden_metadir
642         if [ ! -e "${JMETADIR}/fstab" ] ; then
643            echo "# Device      Mountpoint      FStype          Options Dump Pass" > ${JMETADIR}/fstab
644         fi
645         if [ -z "$EDITOR" ]; then EDITOR="vi"; fi
646         $EDITOR ${JMETADIR}/fstab
647         exit $?
648         ;;
649template) require_root
650         OPT="$2"
651         case $OPT in
652            create) shift ; shift;
653                    # Read our flags
654                    while [ $# -gt 0 ]; do
655                    case $1 in
656                          -fbsd) shift
657                                 if [ -z "$1" ] ; then exit_err "No FreeBSD version specified"; fi
658                                 FBSDVER="${1}"
659                                 ;;
660                        -trueos) shift
661                                 if [ -z "$1" ] ; then exit_err "No TrueOS version specified"; fi
662                                 TRUEOSVER="${1}"
663                                 ;;
664                          -arch) shift
665                                 if [ -z "$1" ] ; then exit_err "No FreeBSD architecture specified"; fi
666                                 FBSDARCH="${1}"
667                                 ;;
668                           -tar) shift
669                                 if [ -z "$1" ] ; then exit_err "No tar file specified"; fi
670                                 if [ ! -e "$1" ] ; then exit_err "Could not find tar file: $1"; fi
671                                 FBSDTAR="${1}"
672                                 ;;
673                          -nick) shift
674                                 if [ -z "$1" ] ; then exit_err "No nickname specified"; fi
675                                 TNICK="`echo ${1} | sed 's| |_|g'`"
676                                 ;;
677                    -pluginjail) shift
678                                 TPLUGJAIL="YES"
679                                 ;;
680                              *) exit_err "Invalid option: $1" ;;
681                    esac
682                    shift
683                    done
684                    export TPLUGJAIL TNICK FBSDTAR FBSDARCH TRUEOSVER FBSDVER
685                    ${PROGDIR}/scripts/backend/createtemplate.sh ; exit $? ;;
686            delete) delete_template "$3" ;;
687              list) list_templates ;;
688                 *) help_template ; exit 1 ;;
689         esac
690         ;;
691    get) require_root
692         OPT="${2}"
693         JAILNAME="${3}"
694         if [ -z "${JAILNAME}" ] ; then help_get; exit 1; fi
695         if [ ! -e "${JDIR}/${JAILNAME}" ] ; then exit_err "No such jail!"; fi
696         set_warden_metadir
697
698         case $OPT in
699             flags) cat "${JMETADIR}/jail-flags" 2>/dev/null | sed 's| |,|g' ;;
700             iface) cat "${JMETADIR}/iface"  2>/dev/null ;;
701              ipv4) cat "${JMETADIR}/ipv4"  2>/dev/null | sed 's| |,|g' ;;
702              ipv6) cat "${JMETADIR}/ipv6"  2>/dev/null | sed 's| |,|g' ;;
703        alias-ipv4) cat "${JMETADIR}/alias-ipv4"  2>/dev/null | sed 's| |,|g' ;;
704        alias-ipv6) cat "${JMETADIR}/alias-ipv6"  2>/dev/null | sed 's| |,|g' ;;
705       bridge-ipv4) cat "${JMETADIR}/bridge-ipv4" 2>/dev/null | sed 's| |,|g' ;;
706       bridge-ipv6) cat "${JMETADIR}/bridge-ipv6" 2>/dev/null | sed 's| |,|g' ;;
707 alias-bridge-ipv4) cat "${JMETADIR}/alias-bridge-ipv4" 2>/dev/null | sed 's| |,|g' ;;
708 alias-bridge-ipv6) cat "${JMETADIR}/alias-bridge-ipv6" 2>/dev/null | sed 's| |,|g' ;;
709defaultrouter-ipv4) cat "${JMETADIR}/defaultrouter-ipv4" 2>/dev/null | sed 's| |,|g' ;;
710defaultrouter-ipv6) cat "${JMETADIR}/defaultrouter-ipv6" 2>/dev/null | sed 's| |,|g' ;;
711              *) exit_err "Invalid option!" ;;
712         esac
713         ;;
714
715    set) require_root
716         OPT="${2}" 
717         JAILNAME="${3}"
718         if [ -z "${JAILNAME}" ] ; then exit_err "No jail specified!"; fi
719         if [ ! -e "${JDIR}/${JAILNAME}" ] ; then exit_err "No such jail!"; fi
720         set_warden_metadir
721
722         case $OPT in
723          iface) IFACE="${4}"
724                 if [ -z "$IFACE" ] ; then
725                    rm "${JMETADIR}/iface"
726                    exit 0
727                 fi
728                 ifconfig $IFACE 2>/dev/null >/dev/null
729                 if [ $? -eq 0 ] ; then
730                    echo "WARNING: The interface ($IFACE) does not exist"
731                 fi
732                 echo $IFACE > "${JMETADIR}/iface"
733                 ;;
734             ipv4) IP4="${4}"
735                 get_ip_and_netmask "${IP4}"
736                 IP4="${JIP}"
737                 MASK4="${JMASK}"
738                 if [ -z "$MASK4" ] ; then MASK4="24"; fi
739                 echo "$IP4/$MASK4" > "${JMETADIR}/ipv4"
740                 ;;
741             ipv6) IP6="${4}"
742                 get_ip_and_netmask "${IP6}"
743                 IP6="${JIP}"
744                 MASK6="${JMASK}"
745                 if [ -z "$MASK6" ] ; then MASK4="64"; fi
746                 echo "$IP6/$MASK6" > "${JMETADIR}/ipv6"
747                 ;;
748       alias-ipv4) EXTRAIPS="${4}"
749                 IPS=`echo $EXTRAIPS | sed 's|,| |g'`
750                 rm "${JMETADIR}/alias-ipv4" >/dev/null 2>/dev/null
751                 for _ip in $IPS
752                 do
753                    echo "$_ip" >> "${JMETADIR}/alias-ipv4"
754                 done
755                ;;
756       alias-ipv6) EXTRAIPS="${4}"
757                 IPS=`echo $EXTRAIPS | sed 's|,| |g'`
758                 rm "${JMETADIR}/alias-ipv6" >/dev/null 2>/dev/null
759                 for _ip in $IPS
760                 do
761                    echo "$_ip" >> "${JMETADIR}/alias-ipv6"
762                 done
763                ;;
764       bridge-ipv4) BIP4="${4}"
765                 echo "$BIP4" > "${JMETADIR}/bridge-ipv4"
766                 ;;
767       bridge-ipv6) BIP6="${4}"
768                 echo "$BIP6" > "${JMETADIR}/bridge-ipv6"
769                 ;;
770 alias-bridge-ipv4) EXTRAIPS="${4}"
771                 IPS=`echo $EXTRAIPS | sed 's|,| |g'`
772                 rm "${JMETADIR}/alias-bridge-ipv4" >/dev/null 2>/dev/null
773                 for _ip in $IPS
774                 do
775                    echo "$_ip" >> "${JMETADIR}/alias-bridge-ipv4"
776                 done
777                ;;
778 alias-bridge-ipv6) EXTRAIPS="${4}"
779                 IPS=`echo $EXTRAIPS | sed 's|,| |g'`
780                 rm "${JMETADIR}/alias-bridge-ipv6" >/dev/null 2>/dev/null
781                 for _ip in $IPS
782                 do
783                    echo "$_ip" >> "${JMETADIR}/alias-bridge-ipv6"
784                 done
785                ;;
786defaultrouter-ipv4) DEFAULTROUTER="${4}"
787                 echo "$DEFAULTROUTER" > "${JMETADIR}/defaultrouter-ipv4"
788                 ;;
789defaultrouter-ipv6) DEFAULTROUTER="${4}"
790                 echo "$DEFAULTROUTER" > "${JMETADIR}/defaultrouter-ipv6"
791                 ;;
792          flags) EXTRAFLAGS="${4}"
793                 echo $EXTRAFLAGS | sed 's|,| |g' > "${JMETADIR}/jail-flags"
794                 ;;
795    vnet-enable) touch "${JMETADIR}/vnet" ;;
796    vnet-disable) rm "${JMETADIR}/vnet" 2>/dev/null ;;
797              *) exit_err "Invalid option!" ;;
798           esac
799         ;;
800
801   type) require_root
802         JAILNAME="${2}"
803         TYPE="${3}"
804         JAILDIR="${JDIR}/${JAILNAME}"
805
806         if [ -z "${JAILNAME}" ] ; then exit_err "No jail specified!"; fi
807         if [ ! -e "${JAILDIR}" ] ; then exit_err "No such jail!"; fi
808
809         set_warden_metadir
810         if [ -e "${JMETADIR}/jail-linux" ] ; then exit_err "Cannot change type on Linux jail!"; fi
811
812         case $TYPE in
813           portjail)
814              rm ${JMETADIR}/jail-pluginjail > /dev/null 2>/dev/null
815              mkportjail "${JAILDIR}"
816              ;;
817           standard)
818              rm ${JMETADIR}/jail-pluginjail > /dev/null 2>/dev/null
819              rm ${JMETADIR}/jail-portjail >/dev/null 2>/dev/null
820              ;;
821           pluginjail)
822              mkpluginjail "${JAILDIR}"
823              ;;
824           *) exit_err "Invalid TYPE" ;;
825         esac
826         ;;
827
828 bspkgng) require_root
829    JAILNAME="${2}"
830         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
831         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
832         bootstrap_pkgng "${JDIR}/${JAILNAME}"
833         ;;
834
835   auto) require_root
836         JAILNAME="${2}"
837
838         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
839
840         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
841         set_warden_metadir
842
843         if [ -e "${JMETADIR}/autostart" ]
844         then
845            rm "${JMETADIR}/autostart"
846            echo "Disabled autostart for ${JAILNAME}"
847         else
848            touch "${JMETADIR}/autostart"
849            echo "Enabled autostart for ${JAILNAME}"
850         fi 
851
852         # Update flags so syscache knows to re-scan
853         pc-systemflag WARDENUPDATE SUCCESS
854       
855         sleep 2
856
857         ;;
858
859    gui) require_root
860          ${PROGDIR}/bin/warden-gui ;;
861
862    list) require_root
863        shift
864        ${PROGDIR}/scripts/backend/listjails.sh $* ;;
865   
866   start) require_root
867          # Time to startup the jail
868          JAILNAME="${2}"
869
870          if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
871          if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
872          set_warden_metadir
873         
874          # Check if this jail is already running
875          ${PROGDIR}/scripts/backend/checkstatus.sh "${JAILNAME}"
876          if [ "$?" = "0" ]; then exit_err "This jail is already running!"; fi
877 
878          ${PROGDIR}/scripts/backend/startjail.sh "${JAILNAME}" 
879          ;;
880   stop)  require_root
881          # Time to stop the jail
882          JAILNAME="${2}"
883
884          if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
885          if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
886          set_warden_metadir
887         
888          ${PROGDIR}/scripts/backend/stopjail.sh "${JAILNAME}" 
889          ;;
890
891 checkup) # Log a user into chroot session
892          JAILNAME="${2}"
893
894          if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
895          if [ ! -e "${JDIR}/${JAILNAME}" -a "${JAILNAME}" != "all" ]; then exit_err "No such jail!"; fi
896
897          ${PROGDIR}/scripts/backend/checkupdates.sh "${JAILNAME}"
898          ;;
899
900
901 chroot) # Log a user into chroot session
902          JAILNAME="${2}"
903
904          if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
905          if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
906          set_warden_metadir
907
908          ${PROGDIR}/scripts/backend/chrootjail.sh "${JAILNAME}" "${3}"
909          ;;
910
911 import) require_root
912         # The user wants to import a jail, lets do it!
913         NAME="$2"
914         IFILE="$3"
915         if [ ! -e "$IFILE" -o -z "$IFILE" ]; then exit_err "Invalid filename: ${IFILE}"; fi
916
917         # Parse the IP / Host flags
918         HOST="OFF"
919         IP4="OFF"
920         IP6="OFF"
921         get_ip_host_flags "$@"
922         if [ "${IP4}" != "OFF" ] ; then
923            IP4="${IP4}/${MASK4}"
924         fi
925         if [ "${IP6}" != "OFF" ] ; then
926            IP6="${IP6}/${MASK6}"
927         fi
928
929         # Import the jail now!
930         ${PROGDIR}/scripts/backend/importjail.sh "$NAME" "$IFILE" "$HOST" "$IP4" "$IP6"
931        ;;
932
933 export) require_root
934         # The user wants to export a jail, lets do it!
935         JAILNAME="$2"
936         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
937         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
938         set_warden_metadir
939
940         # Check if the user wants to place the file in their own DIR
941         DIR=""
942         for i in "$@"
943         do
944           echo ${i} | grep "\-\-dir=" >/dev/null 2>/dev/null
945           if [ "$?" = "0" ]
946           then
947              DIR="`echo ${i} | cut -d '=' -f 2`"
948              # Check if the directory exists
949              if [ ! -d "${DIR}" ]; then exit_err "No such directory ${DIR}!"; fi
950           fi
951
952         done
953
954         # Export the jail now
955         ${PROGDIR}/scripts/backend/exportjail.sh "${JAILNAME}" "${DIR}"
956
957         ;;
958
959 snap) require_root
960         JAILNAME="$3"
961         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
962         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
963         set_warden_metadir
964
965         # Get the sub-command here
966         case $2 in
967            list) ${PROGDIR}/scripts/backend/zfslistsnap.sh "${JAILNAME}" ;;
968            make) ${PROGDIR}/scripts/backend/zfsmksnap.sh "${JAILNAME}" "$4" ;;
969          remove) ${PROGDIR}/scripts/backend/zfsrmsnap.sh "${JAILNAME}" "$4" ;;
970          revert) ${PROGDIR}/scripts/backend/zfsrevertsnap.sh "${JAILNAME}" "$4" ;;
971                *) help_snap ; exit 1 ;;
972         esac
973         ;;
974
975 clone) require_root
976         JAILNAME="$2"
977         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
978         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
979         set_warden_metadir
980
981         get_ip_host_flags "$@"
982
983         if [ "${IP4}" != "OFF" ] ; then
984            IP4="${IP4}/${MASK4}"
985         fi
986         if [ "${IP6}" != "OFF" ] ; then
987            IP6="${IP6}/${MASK6}"
988         fi
989
990         ${PROGDIR}/scripts/backend/zfsclone.sh "${JAILNAME}" "$3" "$IP4" "$IP6"
991         ;;
992
993 cronsnap) require_root
994         JAILNAME="$2"
995         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
996         if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
997         set_warden_metadir
998         ${PROGDIR}/scripts/backend/zfscronsnap.sh "${JAILNAME}" "$3" "$4" "$5"
999         ;;
1000
1001
1002 create) require_root
1003         # The user wants to create a new jail, do error checking
1004         JAILNAME="$2"
1005         if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
1006         echo "$2" | grep -q '^-'
1007         if [ $? -eq 0 ] ; then
1008            exit_err "Jailname must be specified first before -- flags!"
1009         fi
1010
1011
1012         # Parse the IP flags
1013         IP4="OFF"
1014         IP6="OFF"
1015         get_ip_host_flags "$@"
1016         if [ "${IP4}" != "OFF" ] ; then
1017            IP4="${IP4}/${MASK4}"
1018         fi
1019         if [ "${IP6}" != "OFF" ] ; then
1020            IP6="${IP6}/${MASK6}"
1021         fi
1022
1023         # Set the hostname
1024         HOST="$2"
1025         export HOST
1026
1027         #Now check for the presence of the optional flags
1028         SRC="NO"
1029         SOURCE="NO"
1030         PORTS="NO"
1031         AUTOSTART="NO"
1032         VANILLA="NO"
1033         CREATEVERSION=""
1034         JAILTYPE="standard"
1035         ARCHIVE_FILE= 
1036         while [ $# -gt 0 ]; do
1037           case $1 in
1038             --src) SRC="YES" ; SOURCE="YES" ;;
1039             --ports) PORTS="YES" ;;
1040             --startauto) AUTOSTART="YES" ;;
1041             --autoipv4) AUTOIPV4="YES" ;;
1042             --vanilla) VANILLA="YES" ;;
1043             --portjail) JAILTYPE="portjail" ;;
1044             --pluginjail) JAILTYPE="pluginjail" ; VANILLA="YES" ;;
1045             --linuxjail) JAILTYPE="linuxjail" ; shift
1046                          if [ -z "$1" ] ; then exit_err "No linux setup script specified!"; fi
1047                          LINUX_JAIL_SCRIPT="$1" ; export LINUX_JAIL_SCRIPT
1048                          if [ -n "$LINUXARCHIVE_FILE" ] ; then
1049                             exit_err "--linuxjail and --linuxarchive are mutually exclusive!"
1050                          fi
1051                          ;;
1052                     -32) if [ "$REALARCH" != "amd64" ] ; then
1053                                exit_err "-32 can only be used on amd64 host"
1054                          fi
1055                          ARCH=i386
1056                          export ARCH
1057                          ;;
1058             --archive) shift
1059                          if [ -z "$1" ] ; then exit_err "No archive file specified!"; fi
1060                          ARCHIVE_FILE="$1" ; export ARCHIVE_FILE
1061                          if [ ! -e "$ARCHIVE_FILE" ] ; then
1062                             exit_err "No such file: $ARCHIVE_FILE"
1063                          fi
1064                          ;;
1065             --linuxarchive) JAILTYPE="linuxjail" ; shift
1066                          if [ -z "$1" ] ; then exit_err "No archive file specified!"; fi
1067                          LINUXARCHIVE_FILE="$1" ; export LINUXARCHIVE_FILE
1068                          if [ ! -e "$LINUXARCHIVE_FILE" ] ; then
1069                             exit_err "No such file: $LINUXARCHIVE_FILE"
1070                          fi
1071                          if [ -n "$LINUX_JAIL_SCRIPT" ] ; then
1072                             exit_err "--linuxjail and --linuxarchive are mutually exclusive!"
1073                          fi
1074                          ;;
1075             --version) shift
1076                          if [ -z "$1" ] ; then exit_err "No version string specified!"; fi
1077                          CREATEVERSION="${1}"
1078                          ;;
1079             --bulk) shift
1080                          if [ -z "$1" ] ; then exit_err "No bulk number specified!"; fi
1081                          BULKCOUNT="${1}"
1082                          ;;
1083             --ip4pool) shift
1084                          if [ -z "$1" ] ; then exit_err "No IPv4 pool specified!"; fi
1085                          IP4POOL="${1}"
1086                          ;;
1087             --template) shift
1088                          if [ -z "$1" ] ; then exit_err "No template string specified!"; fi
1089                          isDirZFS "${JDIR}"
1090                          if [ $? -eq 0 ] ; then
1091                             TDIR="${JDIR}/.warden-template-$1"
1092                          else
1093                             TDIR="${JDIR}/.warden-template-$1.tbz"
1094                          fi
1095                          if [ ! -e "$TDIR" ] ; then
1096                             exit_err "Invalid template specified: $1"
1097                          fi
1098                          TEMPLATE="${1}"
1099                          ;;
1100           esac
1101           shift
1102         done
1103
1104         #
1105         # Redonkulous number of parameters that exceeds 9,
1106         # export into environment
1107         #
1108         export SRC SOURCE PORTS AUTOSTART JAILTYPE ARCHIVE_FILE CREATEVERSION VANILLA TEMPLATE
1109
1110         # Are we doing bulk creation?
1111         if [ -n "$BULKCOUNT" ] ; then
1112           if [ ! $(is_num "$BULKCOUNT") ] ; then exit_err "Invalid bulk number"; fi
1113
1114           # Set the IP pool to use
1115           if [ -z "$IP4POOL" ] ; then IP4POOL="$DEFAULT_IP4POOL"; fi
1116           if [ -z "$IP4POOL" ] ; then
1117              exit_err "No IPv4 pool specified! Use --ipv4pool or setup IP4POOL: in warden.conf"
1118           fi
1119           curNum="`echo $IP4POOL | cut -d '.' -f 4`"
1120           baseIP="`echo $IP4POOL | cut -d '.' -f 1-3`"
1121           if [ ! $(is_num "$curNum") ] ; then exit_err "Invalid IPv4 pool number"; fi
1122
1123           num=0
1124           while :
1125           do
1126             # Is this host / jail directory available?
1127             if [ -e "${JDIR}/${JAILNAME}${curNum}" ] ; then
1128                curNum=`expr $curNum + 1`
1129                continue
1130             fi
1131
1132             # Now check if this IP address is available
1133             ipConflict=0
1134             for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
1135             do
1136                if [ ! -e "${i}/ipv4" ] ; then continue ; fi
1137                if [ "`cat ${i}/ipv4`" = "${baseIP}.${curNum}/24" ] ; then
1138                   ipConflict=1 ; break
1139                fi
1140             done
1141             if [ $ipConflict -eq 1 ] ; then
1142                curNum=`expr $curNum + 1`
1143                continue
1144             fi
1145
1146             IP="${baseIP}.${curNum}/24" ; export IP
1147
1148             # Passed all tests, create the jail now
1149             echo "Creating BULK jail: ${JAILNAME}${curNum} - ${IP}"
1150             ${PROGDIR}/scripts/backend/createjail.sh "${JAILNAME}${curNum}"
1151             if [ $? -ne 0 ] ; then
1152                exit 1
1153             fi
1154             echo ""
1155
1156             num=`expr $num + 1`
1157             if [ $num -ge $BULKCOUNT ] ; then break ; fi
1158           done
1159
1160         else
1161
1162           # Check to ensure this jail does not already exist
1163           if [ -e "${JDIR}/${JAILNAME}" ]; then exit_err "A jail with this name already exists!"; fi
1164
1165           if [ -n "$AUTOIPV4" ] ; then
1166             # Get the pool addresses loaded
1167             if [ -z "$IP4POOL" ] ; then IP4POOL="$DEFAULT_IP4POOL"; fi
1168             curNum="`echo $IP4POOL | cut -d '.' -f 4`"
1169             baseIP="`echo $IP4POOL | cut -d '.' -f 1-3`"
1170             if [ ! $(is_num "$curNum") ] ; then exit_err "Invalid IPv4 pool number"; fi
1171
1172             # Get the next available IPv4 Address in this pool
1173             while :
1174             do
1175               ipConflict=0
1176               for i in `ls -d ${JDIR}/.*.meta 2>/dev/null`
1177               do
1178                if [ ! -e "${i}/ipv4" ] ; then continue ; fi
1179                if [ "`cat ${i}/ipv4`" = "${baseIP}.${curNum}/24" ] ; then
1180                   ipConflict=1 ; break
1181                fi
1182               done
1183               if [ $ipConflict -eq 0 ] ; then break; fi
1184               curNum=`expr $curNum + 1`
1185             done
1186
1187             # Set the next IP address
1188             IP4="${baseIP}.${curNum}/24"
1189           fi
1190
1191           export IP4 IP6
1192
1193           # Passed all tests, create the jail now
1194           ${PROGDIR}/scripts/backend/createjail.sh "${JAILNAME}"
1195           exit $?
1196         fi
1197
1198        ;;
1199  delete|destroy) require_root
1200          # Time to delete a jail
1201          JAILNAME="${2}"
1202
1203          if [ -z "${JAILNAME}" ]; then exit_err "No jail specified!"; fi
1204          if [ ! -e "${JDIR}/${JAILNAME}" ]; then exit_err "No such jail!"; fi
1205          set_warden_metadir
1206
1207          if [ "$3" != "--confirm" ]
1208          then
1209            echo "Are you sure you want to delete ${JAILNAME}?"
1210            echo -e "Choice: (y/n)\c"
1211            read confirm
1212            if [ "$confirm" != "y" ]; then exit_err "Canceled!"; fi
1213          fi
1214
1215          # Check if this jail is running
1216          ${PROGDIR}/scripts/backend/checkstatus.sh "${JAILNAME}"
1217          if [ "$?" = "0" ] ; then
1218            ${PROGDIR}/scripts/backend/stopjail.sh "${JAILNAME}"
1219          fi
1220
1221          # Check if this jail is STILL running
1222          ${PROGDIR}/scripts/backend/checkstatus.sh "${JAILNAME}"
1223          if [ "$?" = "0" ] ; then exit_err "Jail still is running or has leftovers.."; fi
1224
1225          # Delete it now
1226          ${PROGDIR}/scripts/backend/deletejail.sh "${JAILNAME}"
1227        ;;
1228   *) help_main ;;
1229esac
1230
1231
1232exit 0
Note: See TracBrowser for help on using the repository browser.