source: src-sh/warden/bin/warden @ 1620346

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 21 months ago

Initial import of PC-BSD /current/ SVN repo

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