• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/ISSUE_TEMPLATE/H25-Dec-2021-4833

docs/H03-May-2022-1,6501,199

usr/local/H25-Dec-2021-6,0914,043

.gitignoreH A D25-Dec-202112 21

.readthedocs.yamlH A D25-Dec-2021127 107

AUTHORS.mdH A D25-Dec-2021769 4539

CODE-OF-CONDUCT.mdH A D25-Dec-20213.2 KiB7455

LICENSEH A D25-Dec-20211.5 KiB3023

MakefileH A D25-Dec-2021770 2927

README.mdH A D25-Dec-202138.6 KiB1,185923

ROADMAP.mdH A D25-Dec-20212.3 KiB5644

VagrantfileH A D25-Dec-2021587 2617

README.md

1Bastille
2========
3[Bastille](https://bastillebsd.org/) is an open-source system for automating
4deployment and management of containerized applications on FreeBSD.
5
6Looking for [Bastille Templates](https://gitlab.com/BastilleBSD-Templates/)?
7
8Installation
9============
10Bastille is available in the official FreeBSD ports tree.
11
12**pkg**
13```shell
14pkg install bastille
15```
16
17**ports**
18```shell
19portsnap fetch auto
20make -C /usr/ports/sysutils/bastille install clean
21```
22
23**Git** (bleeding edge / unstable -- primarily for developers)
24```shell
25git clone https://github.com/BastilleBSD/bastille.git
26cd bastille
27make install
28```
29
30**enable at boot**
31```shell
32sysrc bastille_enable=YES
33```
34
35Basic Usage
36-----------
37```shell
38Bastille is an open-source system for automating deployment and management of
39containerized applications on FreeBSD.
40
41Usage:
42  bastille command TARGET args
43
44Available Commands:
45  bootstrap   Bootstrap a FreeBSD release for container base.
46  clone       Clone an existing container.
47  cmd         Execute arbitrary command on targeted container(s).
48  config      Get or set a config value for the targeted container(s).
49  console     Console into a running container.
50  convert     Convert a thin container into a thick container.
51  cp          cp(1) files from host to targeted container(s).
52  create      Create a new thin or thick container.
53  destroy     Destroy a stopped container or a bootstrapped release.
54  edit        Edit container configuration files (advanced).
55  export      Exports a container archive or image.
56  help        Help about any command
57  htop        Interactive process viewer (requires htop).
58  import      Import a container archive or image.
59  limits      Apply resources limits to targeted container(s). See rctl(8).
60  list        List containers, releases, templates, logs, limits or backups.
61  mount       Mount a volume inside the targeted container(s).
62  pkg         Manipulate binary packages within targeted container(s). See pkg(8).
63  rdr         Redirect host port to container port.
64  restart     Restart a running container.
65  service     Manage services within targeted container(s).
66  start       Start a stopped container.
67  stop        Stop a running container.
68  sysrc       Safely edit rc files within targeted container(s).
69  template    Apply automation templates to targeted container(s).
70  top         Display and update information about the top(1) cpu processes.
71  umount      Unmount a volume from within the targeted container(s).
72  update      Update container base -pX release.
73  upgrade     Upgrade container release to X.Y-RELEASE.
74  verify      Verify bootstrapped release or automation template.
75  zfs         Manage (get|set) ZFS attributes on targeted container(s).
76
77Use "bastille -v|--version" for version information.
78Use "bastille command -h|--help" for more information about a command.
79
80```
81
82## 0.9-beta
83This document outlines the basic usage of the Bastille container management
84framework. This release is still considered beta.
85
86Network Requirements
87====================
88Several networking options can be performed regarding the user needs.  Basic
89containers can support IP alias networking, where the IP address is assigned to
90the host interface and used by the container, generally known as "shared IP"
91based containers.
92
93If you administer your own network and can assign and remove unallocated IP
94addresses, then "shared IP" is a simple method to get started. If this is the
95case, skip ahead to ZFS Support.
96
97If you are not the administator of the network, or perhaps you're in "the
98cloud" someplace and are only provided a single IP4 address. In this situation
99Bastille can create and attach containers to a private loopback interface. The
100host system then acts as the firewall, permitting and denying traffic as
101needed. (This method has been my primary method for years.)
102
103**bastille0**
104
105First, create the loopback interface:
106
107```shell
108ishmael ~ # sysrc cloned_interfaces+=lo1
109ishmael ~ # sysrc ifconfig_lo1_name="bastille0"
110ishmael ~ # service netif cloneup
111```
112
113Create the firewall config, or merge as necessary.
114
115/etc/pf.conf
116------------
117```
118ext_if="vtnet0"
119
120set block-policy return
121scrub in on $ext_if all fragment reassemble
122set skip on lo
123
124table <jails> persist
125nat on $ext_if from <jails> to any -> ($ext_if:0)
126
127## static rdr example
128# rdr pass inet proto tcp from any to any port {80, 443} -> 10.17.89.45
129
130## Enable dynamic rdr (see below)
131rdr-anchor "rdr/*"
132
133block in all
134pass out quick keep state
135antispoof for $ext_if inet
136pass in inet proto tcp from any to any port ssh flags S/SA keep state
137
138## make sure you also open up ports that you are going to use for dynamic rdr
139# pass in inet proto tcp from any to any port <rdr-start>:<rdr-end> flags S/SA keep state
140# pass in inet proto udp from any to any port <rdr-start>:<rdr-end> flags S/SA keep state
141## for IPv6 networks please uncomment the following rule
142# pass inet6 proto icmp6 icmp6-type { echoreq, routersol, routeradv, neighbradv, neighbrsol }
143
144```
145
146* Make sure to change the `ext_if` variable to match your host system interface.
147* Note that if multiple interface aliases are in place, the index `($ext_if:0)`
148can be changed accordingly; so if you want to send traffic out the second IP alias
149of the interface, change the value to `($ext_if:1)` and so on.
150* Make sure to include the last line (`port ssh`) or you'll end up locked
151out of a remote system.
152
153Note: if you have an existing firewall, the key lines for in/out traffic to
154containers are:
155
156```
157table <jails> persist
158nat on $ext_if from <jails> to any -> ($ext_if:0)
159
160## rdr example
161## rdr pass inet proto tcp from any to any port {80, 443} -> 10.17.89.45
162```
163
164The `nat` routes traffic from the loopback interface to the external interface
165for outbound access.
166
167The `rdr pass ...` will redirect traffic from the host firewall on port X to
168the ip of container Y. The example shown redirects web traffic (80 & 443) to the
169container at `10.17.89.45`.
170
171Finally, enable and (re)start the firewall:
172
173## dynamic rdr
174
175The `rdr-anchor "rdr/*"` enables dynamic rdr rules to be setup using the
176`bastille rdr` command at runtime - eg.
177
178```
179  bastille rdr <jail> tcp 2001 22 # Redirects tcp port 2001 on host to 22 on jail
180  bastille rdr <jail> udp 2053 53 # Same for udp
181  bastille rdr <jail> list        # List dynamic rdr rules
182  bastille rdr <jail> clear       # Clear dynamic rdr rules
183```
184
185  Note that if you are rediirecting ports where the host is also listening
186  (eg. ssh) you should make sure that the host service is not listening on
187  the cloned interface - eg. for ssh set sshd_flags in rc.conf
188
189## Enable pf rules
190
191```shell
192ishmael ~ # sysrc pf_enable="YES"
193ishmael ~ # service pf restart
194```
195
196At this point you'll likely be disconnected from the host. Reconnect the ssh
197session and continue.
198
199This step only needs to be done once in order to prepare the host.
200
201
202ZFS support
203===========
204
205![BastilleBSD Twitter Poll](/docs/images/bastillebsd-twitter-poll.png)
206
207Bastille 0.4 added initial support for ZFS. `bastille bootstrap` and `bastille
208create` will generate ZFS volumes based on settings found in the
209`bastille.conf`. This section outlines how to enable and configure Bastille for
210ZFS.
211
212Two values are required for Bastille to use ZFS. The default values in the
213`bastille.conf` are empty. Populate these two to enable ZFS.
214
215```shell
216## ZFS options
217bastille_zfs_enable=""                                  ## default: ""
218bastille_zfs_zpool=""                                   ## default: ""
219bastille_zfs_prefix="bastille"                          ## default: "${bastille_zfs_zpool}/bastille"
220bastille_prefix="/bastille"                             ## default: "/usr/local/bastille". ${bastille_zfs_prefix} gets mounted here
221bastille_zfs_options="-o compress=lz4 -o atime=off"     ## default: "-o compress=lz4 -o atime=off"
222```
223
224**Example**
225
226```shell
227ishmael ~ # sysrc -f /usr/local/etc/bastille/bastille.conf bastille_zfs_enable=YES
228ishmael ~ # sysrc -f /usr/local/etc/bastille/bastille.conf bastille_zfs_zpool=ZPOOL_NAME
229```
230
231Replace `ZPOOL_NAME` with the zpool you want Bastille to use. Tip: `zpool list`
232and `zpool status` will help. If you get 'no pools available' you are likely
233not using ZFS and can safely ignore these settings.
234
235
236bastille bootstrap
237------------------
238Before you can begin creating containers, Bastille needs to "bootstrap" a
239release.  Current supported releases are 11.4-RELEASE, 12.2-RELEASE and
24013.0-RELEASE.
241
242**Important: If you need ZFS support see the above section BEFORE
243bootstrapping.**
244
245To `bootstrap` a release, run the bootstrap sub-command with the
246release version as the argument.
247
248**FreeBSD 11.4-RELEASE**
249```shell
250ishmael ~ # bastille bootstrap 11.4-RELEASE
251```
252
253**FreeBSD 12.2-RELEASE**
254```shell
255ishmael ~ # bastille bootstrap 12.2-RELEASE
256```
257
258**HardenedBSD 11-STABLE-BUILD-XX**
259```shell
260ishmael ~ # bastille bootstrap 11-STABLE-BUILD-XX
261```
262
263**HardenedBSD 12-STABLE-BUILD-XX**
264```shell
265ishmael ~ # bastille bootstrap 12-STABLE-BUILD-XX
266```
267
268> `bastille bootstrap RELEASE update` to apply updates automatically at bootstrap.
269
270This command will ensure the required directory structures are in place and
271download the requested release. For each requested release, `bootstrap` will
272download the base.txz. If you need more than base (eg; ports, lib32, src) you
273can configure the `bastille_bootstrap_archives` in the configuration file. By
274default this value is set to "base". Additional components are added, space
275separated, without file extension.
276
277Bastille will attempt to fetch the required archives if they are not found in
278the `cache/$RELEASE` directory.
279
280Downloaded artifacts are stored in the `cache/RELEASE` directory. "bootstrapped"
281releases are stored in `releases/RELEASE`.
282
283Advanced: If you want to create your own custom base.txz, or use an unsupported
284variant of FreeBSD, drop your own base.txz in `cache/RELEASE/base.txz` and
285`bastille bootstrap` will attempt to extract and use it.
286
287The bootstrap subcommand is generally only used once to prepare the system. The
288other use cases for the bootstrap command are when a new FreeBSD version is
289released and you want to start building containers on that version, or
290bootstrapping templates from GitHub or GitLab.
291
292See `bastille update` to ensure your bootstrapped releases include the latest
293patches.
294
295** Ubuntu Linux [new since 0.9] **
296
297The bootstrap process for Linux containers is very different from the BSD process.
298You will need the package debootstrap and some kernel modules for that.
299But don't worry, Bastille will do that for you.
300
301```shell
302ishmael ~ # bastille bootstrap focal
303sysrc: unknown variable 'linprocfs_load'
304sysrc: unknown variable 'linsysfs_load'
305sysrc: unknown variable 'tmpfs_load'
306linprocfs_load, linsysfs_load, tmpfs_load not enabled in /boot/loader.conf or linux_enable not active. Should I do that for you?  (N|y)
307#y
308Loading modules
309Persisting modules
310linux_enable:  -> YES
311linprocfs_load:  -> YES
312linsysfs_load:  -> YES
313tmpfs_load:  -> YES
314Debootstrap not found. Should it be installed? (N|y)
315#y
316FreeBSD repository is up to date.
317All repositories are up to date.
318Checking integrity... done (0 conflicting)
319The following 1 package(s) will be affected (of 0 checked):
320
321New packages to be INSTALLED:
322        debootstrap: 1.0.123_4
323[...]
324```
325As of 0.9.20210714 Bastille supports Ubuntu 18.04 (bionic) and Ubuntu 20.04 (focal).
326
327bastille create
328---------------
329`bastille create` uses a bootstrapped release to create a lightweight container
330system. To create a container simply provide a name, release and a private
331(rfc1918) IP address. Optionally provide a network interface name to attach the
332IP at container creation.
333
334- name
335- release (bootstrapped)
336- ip (ip4 or ip6)
337- interface (optional)
338
339
340**ip4**
341```shell
342ishmael ~ # bastille create folsom 12.2-RELEASE 10.17.89.10
343Valid: (10.17.89.10).
344
345NAME: folsom.
346IP: 10.17.89.10.
347RELEASE: 12.2-RELEASE.
348
349syslogd_flags: -s -> -ss
350sendmail_enable: NO -> NONE
351cron_flags:  -> -J 60
352```
353
354This command will create a 12.2-RELEASE container assigning the 10.17.89.10 ip
355address to the new system.
356
357**ip6**
358```shell
359ishmael ~ # bastille create folsom 12.2-RELEASE fd35:f1fd:2cb6:6c5c::13
360Valid: (fd35:f1fd:2cb6:6c5c::13).
361
362NAME: folsom.
363IP: fd35:f1fd:2cb6:6c5c::13
364RELEASE: 12.1-RELEASE.
365
366syslogd_flags: -s -> -ss
367sendmail_enable: NO -> NONE
368cron_flags:  -> -J 60
369```
370
371This command will create a 12.2-RELEASE container assigning the
372fd35:f1fd:2cb6:6c5c::13  ip address to the new system.
373
374**VNET**
375```shell
376ishmael ~ # bastille create -V vnetjail 12.2-RELEASE 192.168.87.55/24 em0
377Valid: (192.168.87.55/24).
378Valid: (em0).
379
380NAME: vnettest0.
381IP: 192.168.87.55/24.
382INTERFACE: em0.
383RELEASE: 12.1-RELEASE.
384
385syslogd_flags: -s -> -ss
386sendmail_enable: NO -> NONE
387cron_flags:  -> -J 60
388ifconfig_e0b_bastille0_name:  -> vnet0
389ifconfig_vnet0:  -> inet 192.168.87.55/24
390```
391
392This command will create a 12.2-RELEASE container assigning the
393192.168.87.55/24 ip address to the new system.
394
395VNET-enabled containers are attached to a virtual bridge interface for
396connectivity. This bridge interface is defined by the interface argument in the
397create command (in this case, em0).
398
399VNET also requires a custom `devfs` ruleset. Create the file as needed on the host system:
400
401**/etc/devfs.rules**
402```
403[bastille_vnet=13]
404add path 'bpf*' unhide
405```
406
407Optionally `bastille create [ -T | --thick ]` will create a container with a
408private base. This is sometimes referred to as a "thick" container (whereas the
409shared base container is a "thin").
410
411```shell
412ishmael ~ # bastille create -T folsom 12.2-RELEASE 10.17.89.10
413```
414
415**Linux**
416```shell
417ishmael ~ # bastille create folsom focal 10.17.89.10
418```
419
420Systemd is not supported due to the missing boot process.
421
422
423
424I recommend using private (rfc1918) ip address ranges for your containers.
425These ranges include:
426
427- 10.0.0.0/8
428- 172.16.0.0/12
429- 192.168.0.0/16
430
431If your Bastille host also uses private (rfc1918) addresses, use a different
432range for your containers. ie; Host uses 192.168.0.0/16, containers use 10.0.0.0/8.
433
434Bastille does its best to validate the submitted ip is valid. I generally use
435the 10.0.0.0/8 range for containers.
436
437
438bastille start
439--------------
440To start a containers you can use the `bastille start` command.
441
442```shell
443ishmael ~ # bastille start folsom
444[folsom]:
445folsom: created
446
447```
448
449
450bastille stop
451-------------
452To stop a containers you can use the `bastille stop` command.
453
454```shell
455ishmael ~ # bastille stop folsom
456[folsom]:
457folsom: removed
458
459```
460
461
462bastille restart
463----------------
464To restart a container you can use the `bastille restart` command.
465
466```shell
467ishmael ~ # bastille restart folsom
468[folsom]:
469folsom: removed
470
471[folsom]:
472folsom: created
473
474```
475
476bastille list
477-------------
478This sub-command will show you the running containers on your system.
479
480```shell
481ishmael ~ # bastille list
482 JID             IP Address      Hostname                      Path
483 bastion         10.17.89.65      bastion                       /usr/local/bastille/jails/bastion/root
484 unbound0        10.17.89.60      unbound0                      /usr/local/bastille/jails/unbound0/root
485 unbound1        10.17.89.61      unbound1                      /usr/local/bastille/jails/unbound1/root
486 squid           10.17.89.30      squid                         /usr/local/bastille/jails/squid/root
487 nginx           10.17.89.45      nginx                         /usr/local/bastille/jails/nginx/root
488 folsom          10.17.89.10      folsom                        /usr/local/bastille/jails/folsom/root
489```
490
491You can also list non-running containers with `bastille list containers`.  In
492the same manner you can list archived `logs`, downloaded `templates`, and
493`releases` and `backups`.  Providing the `-j` flag to list alone will result in
494JSON output.
495
496
497bastille service
498----------------
499To restart services inside a containers you can use the `bastille service`
500command.
501
502```shell
503ishmael ~ # bastille service folsom postfix restart
504[folsom]
505postfix/postfix-script: stopping the Postfix mail system
506postfix/postfix-script: starting the Postfix mail system
507
508```
509
510
511bastille cmd
512------------
513To execute commands within the container you can use `bastille cmd`.
514
515```shell
516ishmael ~ # bastille cmd folsom ps -auxw
517[folsom]:
518USER   PID %CPU %MEM   VSZ  RSS TT  STAT STARTED    TIME COMMAND
519root 71464  0.0  0.0 14536 2000  -  IsJ   4:52PM 0:00.00 /usr/sbin/syslogd -ss
520root 77447  0.0  0.0 16632 2140  -  SsJ   4:52PM 0:00.00 /usr/sbin/cron -s
521root 80591  0.0  0.0 18784 2340  1  R+J   4:53PM 0:00.00 ps -auxw
522
523```
524
525
526bastille pkg
527------------
528To manage binary packages within the container use `bastille pkg`.
529
530```shell
531ishmael ~ # bastille pkg folsom install vim-console git-lite zsh
532[folsom]:
533Updating FreeBSD repository catalogue...
534[folsom] Fetching meta.txz: 100%    944 B   0.9kB/s    00:01
535[folsom] Fetching packagesite.txz: 100%    6 MiB   6.6MB/s    00:01
536Processing entries: 100%
537FreeBSD repository update completed. 32617 packages processed.
538All repositories are up to date.
539Updating database digests format: 100%
540The following 10 package(s) will be affected (of 0 checked):
541
542New packages to be INSTALLED:
543	vim-console: 8.1.1954
544	git-lite: 2.23.0
545	zsh: 5.7.1_1
546	expat: 2.2.8
547	curl: 7.66.0
548	libnghttp2: 1.39.2
549	ca_root_nss: 3.47.1
550	pcre: 8.43_2
551	gettext-runtime: 0.20.1
552	indexinfo: 0.3.1
553
554Number of packages to be installed: 10
555
556The process will require 87 MiB more space.
55718 MiB to be downloaded.
558
559Proceed with this action? [y/N]:
560...[snip]...
561```
562
563The PKG sub-command can, of course, do more than just `install`. The
564expectation is that you can fully leverage the pkg manager. This means,
565`install`, `update`, `upgrade`, `audit`, `clean`, `autoremove`, etc.
566
567```shell
568ishmael ~ # bastille pkg ALL upgrade
569[bastion]:
570Updating pkg.bastillebsd.org repository catalogue...
571[bastion] Fetching meta.txz: 100%    560 B   0.6kB/s    00:01
572[bastion] Fetching packagesite.txz: 100%  118 KiB 121.3kB/s    00:01
573Processing entries: 100%
574pkg.bastillebsd.org repository update completed. 493 packages processed.
575All repositories are up to date.
576Checking for upgrades (1 candidates): 100%
577Processing candidates (1 candidates): 100%
578Checking integrity... done (0 conflicting)
579Your packages are up to date.
580
581[unbound0]:
582Updating pkg.bastillebsd.org repository catalogue...
583[unbound0] Fetching meta.txz: 100%    560 B   0.6kB/s    00:01
584[unbound0] Fetching packagesite.txz: 100%  118 KiB 121.3kB/s    00:01
585Processing entries: 100%
586pkg.bastillebsd.org repository update completed. 493 packages processed.
587All repositories are up to date.
588Checking for upgrades (0 candidates): 100%
589Processing candidates (0 candidates): 100%
590Checking integrity... done (0 conflicting)
591Your packages are up to date.
592
593[unbound1]:
594Updating pkg.bastillebsd.org repository catalogue...
595[unbound1] Fetching meta.txz: 100%    560 B   0.6kB/s    00:01
596[unbound1] Fetching packagesite.txz: 100%  118 KiB 121.3kB/s    00:01
597Processing entries: 100%
598pkg.bastillebsd.org repository update completed. 493 packages processed.
599All repositories are up to date.
600Checking for upgrades (0 candidates): 100%
601Processing candidates (0 candidates): 100%
602Checking integrity... done (0 conflicting)
603Your packages are up to date.
604
605[squid]:
606Updating pkg.bastillebsd.org repository catalogue...
607[squid] Fetching meta.txz: 100%    560 B   0.6kB/s    00:01
608[squid] Fetching packagesite.txz: 100%  118 KiB 121.3kB/s    00:01
609Processing entries: 100%
610pkg.bastillebsd.org repository update completed. 493 packages processed.
611All repositories are up to date.
612Checking for upgrades (0 candidates): 100%
613Processing candidates (0 candidates): 100%
614Checking integrity... done (0 conflicting)
615Your packages are up to date.
616
617[nginx]:
618Updating pkg.bastillebsd.org repository catalogue...
619[nginx] Fetching meta.txz: 100%    560 B   0.6kB/s    00:01
620[nginx] Fetching packagesite.txz: 100%  118 KiB 121.3kB/s    00:01
621Processing entries: 100%
622pkg.bastillebsd.org repository update completed. 493 packages processed.
623All repositories are up to date.
624Checking for upgrades (1 candidates): 100%
625Processing candidates (1 candidates): 100%
626The following 1 package(s) will be affected (of 0 checked):
627
628Installed packages to be UPGRADED:
629	nginx-lite: 1.14.0_14,2 -> 1.14.1,2
630
631Number of packages to be upgraded: 1
632
633315 KiB to be downloaded.
634
635Proceed with this action? [y/N]: y
636[nginx] [1/1] Fetching nginx-lite-1.14.1,2.txz: 100%  315 KiB 322.8kB/s    00:01
637Checking integrity... done (0 conflicting)
638[nginx] [1/1] Upgrading nginx-lite from 1.14.0_14,2 to 1.14.1,2...
639===> Creating groups.
640Using existing group 'www'.
641===> Creating users
642Using existing user 'www'.
643[nginx] [1/1] Extracting nginx-lite-1.14.1,2: 100%
644You may need to manually remove /usr/local/etc/nginx/nginx.conf if it is no longer needed.
645```
646
647
648bastille destroy
649----------------
650Containers can be destroyed and thrown away just as easily as they were
651created.  Note: containers must be stopped before destroyed.
652
653```shell
654ishmael ~ # bastille stop folsom
655[folsom]:
656folsom: removed
657
658ishmael ~ # bastille destroy folsom
659Deleting Container: folsom.
660Note: container console logs not destroyed.
661/usr/local/bastille/logs/folsom_console.log
662
663```
664
665bastille template
666-----------------
667Looking for ready made CI/CD validated [Bastille
668Templates](https://gitlab.com/BastilleBSD-Templates)?
669
670Bastille supports a templating system allowing you to apply files, pkgs and
671execute commands inside the container automatically.
672
673Currently supported template hooks are: `ARG`, `LIMITS`, `INCLUDE`,
674 `MOUNT`, `PKG`, `CP`, `SYSRC`, `SERVICE`, `RDR`, `CMD`, `RENDER`.
675
676Templates are created in `${bastille_prefix}/templates` and can leverage any of
677the template hooks. Simply create a new directory in the format project/repo,
678ie; `username/base-template`
679
680```shell
681mkdir -p /usr/local/bastille/templates/username/base-template
682```
683
684To leverage a template hook, create an UPPERCASE file in the root of the
685template directory named after the hook you want to execute. eg;
686
687```shell
688echo "PKG zsh vim-console git-lite htop" >> /usr/local/bastille/templates/username/base-template/Bastillefile
689echo "CMD /usr/bin/chsh -s /usr/local/bin/zsh" >> /usr/local/bastille/templates/username/base-template/Bastillefile
690echo "CP usr" > /usr/local/bastille/templates/username/base-template/Bastillefile
691```
692
693Template hooks are executed in specific order and require specific syntax to
694work as expected. This table outlines that order and those requirements:
695
696| SUPPORTED | format                | example                                        |
697|-----------|-----------------------|------------------------------------------------|
698| ARG       | name=value (one/line) | domain=example.com (omit value for no default) |
699| LIMITS    | resource value        | memoryuse 1G                                   |
700| INCLUDE   | template path/URL     | http?://TEMPLATE_URL or username/base-template |
701| PRE       | /bin/sh command       | mkdir -p /usr/local/path                       |
702| FSTAB     | fstab syntax          | /host/path container/path nullfs ro 0 0        |
703| PKG       | port/pkg name(s)      | vim-console zsh git-lite tree htop             |
704| OVERLAY   | paths (one/line)      | etc usr                                        |
705| SYSRC     | sysrc command(s)      | nginx_enable=YES                               |
706| SERVICE   | service command(s)    | nginx restart                                  |
707| CMD       | /bin/sh command       | /usr/bin/chsh -s /usr/local/bin/zsh            |
708| RENDER    | paths (one/line)      | /usr/local/etc/nginx                           |
709| RDR       | protocol port port    | tcp 2200 22                                    |
710
711Note: SYSRC requires NO quotes or that quotes (`"`) be escaped. ie; `\"`)
712
713Any name provided in the ARG file can be used as a variable in the other hooks.
714For example, `name=value` in the ARG file will cause instances of `${name}`
715to be replaced with `value`. The `RENDER` hook can be used to specify existing files or
716directories inside the jail whose contents should have the variables replaced. Values can be
717specified either through the command line when applying the template or as a default in the ARG
718file.
719
720In addition to supporting template hooks, Bastille supports overlaying files
721into the container. This is done by placing the files in their full path, using the
722template directory as "/".
723
724An example here may help. Think of
725`/usr/local/bastille/templates/username/base`, our example template, as the
726root of our filesystem overlay. If you create an `etc/hosts` or
727`etc/resolv.conf` inside the base template directory, these can be overlayed
728into your container.
729
730Note: due to the way FreeBSD segregates user-space, the majority of your
731overlayed template files will be in `usr/local`. The few general
732exceptions are the `etc/hosts`, `etc/resolv.conf`, and `etc/rc.conf.local`.
733
734After populating `usr/local/` with custom config files that your container will
735use, be sure to include `usr` in the template OVERLAY definition. eg;
736
737```shell
738echo "CP etc" >> /usr/local/bastille/templates/username/base/Bastillefile
739echo "CP usr" >> /usr/local/bastille/templates/username/base/Bastillefile
740```
741
742The above example will include anything under "etc" and "usr" inside
743the template. You do not need to list individual files. Just include the
744top-level directory name.
745
746For more control over the order of operations when applying a template,
747create a `Bastillefile` inside the base template directory. Each line in
748the file should begin with an uppercase reference to a Bastille command
749followed by its arguments (omitting the target, which is deduced from the
750`template` arguments). Lines beginning with `#` are treated as comments.
751Variables can also be defined using `ARG` with one `name=value` pair per
752line. Subsequent references to `${name}` would be replaced by `value`.
753Note that argument values are not available for use until after the point
754at which they are defined in the file. Both `${JAIL_NAME}` and `${JAIL_IP}`
755are made available in templates without having to define them as args.
756
757Bastillefile example:
758
759```shell
760LIMITS memoryuse 1G
761
762# This value can be overridden when the template is applied.
763ARG domain=example.com
764
765# Replace all argument variables inside the nginx config.
766RENDER /usr/local/etc/nginx
767
768# Install and start nginx.
769PKG nginx
770SYSRC nginx_enable=YES
771SERVICE nginx restart
772
773# Copy files to nginx.
774CP www/ usr/local/www/nginx-dist/
775
776# Use the "domain" arg to create a file on the server containing the domain.
777CMD echo "${domain}" > /usr/local/www/nginx-dist/domain.txt
778
779# Create a file on the server containing the jail's hostname.
780CMD hostname > /usr/local/www/nginx-dist/hostname.txt
781
782# Forward TCP port 80 on the host to port 80 in the container.
783RDR tcp 80 80
784```
785
786Use the following command to convert a hook-based template into the Bastillefile format:
787```shell
788bastille template --convert my-template
789```
790
791Applying Templates
792------------------
793
794Containers must be running to apply templates.
795
796Bastille includes a `template` sub-command. This sub-command requires a target
797and a template name. As covered in the previous section, template names
798correspond to directory names in the `bastille/templates` directory.
799
800To provide values for arguments defined by `ARG` in the template, pass the
801optional `--arg` parameter as many times as needed. Alternatively, use
802`--arg-file <fileName>` with one `name=value` pair per line.
803
804```shell
805ishmael ~ # bastille template folsom username/base --arg domain=example.com
806[folsom]:
807Copying files...
808Copy complete.
809Installing packages.
810...[snip]...
811Executing final command(s).
812chsh: user information updated
813Template Complete.
814
815```
816
817
818bastille top
819------------
820This one simply runs `top` in that container. This command is interactive, as
821`top` is interactive.
822
823
824bastille htop
825-------------
826This one simply runs `htop` inside the container. This one is a quick and dirty
827addition. note: won't work if you don't have htop installed in the container.
828
829
830bastille sysrc
831--------------
832The `sysrc` sub-command allows for safely editing system configuration files.
833In container terms, this allows us to toggle on/off services and options at
834startup.
835
836```shell
837ishmael ~ # bastille sysrc nginx nginx_enable=YES
838[nginx]:
839nginx_enable: NO -> YES
840```
841
842See `man sysrc(8)` for more info.
843
844
845bastille console
846----------------
847This sub-command launches a login shell into the container. Default is
848password-less root login. If you provide an additional argument of a username
849you will be logged in as that user. (user must be created first)
850
851```shell
852ishmael ~ # bastille console folsom
853[folsom]:
854FreeBSD 11.3-RELEASE-p4 (GENERIC) #0: Thu Sep 27 08:16:24 UTC 2018
855
856Welcome to FreeBSD!
857
858Release Notes, Errata: https://www.FreeBSD.org/releases/
859Security Advisories:   https://www.FreeBSD.org/security/
860FreeBSD Handbook:      https://www.FreeBSD.org/handbook/
861FreeBSD FAQ:           https://www.FreeBSD.org/faq/
862Questions List: https://lists.FreeBSD.org/mailman/listinfo/freebsd-questions/
863FreeBSD Forums:        https://forums.FreeBSD.org/
864
865Documents installed with the system are in the /usr/local/share/doc/freebsd/
866directory, or can be installed later with:  pkg install en-freebsd-doc
867For other languages, replace "en" with a language code like de or fr.
868
869Show the version of FreeBSD installed:  freebsd-version ; uname -a
870Please include that output and any error messages when posting questions.
871Introduction to manual pages:  man man
872FreeBSD directory layout:      man hier
873
874Edit /etc/motd to change this login announcement.
875root@folsom:~ #
876```
877
878At this point you are logged in to the container and have full shell access.
879The system is yours to use and/or abuse as you like. Any changes made inside
880the container are limited to the container.
881
882
883bastille cp
884-----------
885This sub-command allows efficiently copying files from host to container(s).
886
887```shell
888ishmael ~ # bastille cp ALL /tmp/resolv.conf-cf etc/resolv.conf
889[folsom]:
890/tmp/resolv.conf-cf -> /usr/local/bastille/jails/folsom/root/etc/resolv.conf
891
892[nginx]:
893/tmp/resolv.conf-cf -> /usr/local/bastille/jails/nginx/root/etc/resolv.conf
894
895[squid]:
896/tmp/resolv.conf-cf -> /usr/local/bastille/jails/squid/root/etc/resolv.conf
897
898[unbound0]:
899/tmp/resolv.conf-cf -> /usr/local/bastille/jails/unbound0/root/etc/resolv.conf
900```
901
902bastille rdr
903------------
904
905`bastille rdr` allows you to configure dynamic rdr rules for your containers
906without modifying pf.conf (assuming you are using the `bastille0` interface
907for a private network and have enabled `rdr-anchor 'rdr/*'` in /etc/pf.conf
908as described in the Networking section).
909
910```shell
911    # bastille rdr help
912    Usage: bastille rdr TARGET [clear] | [list] | [tcp <host_port> <jail_port>] | [udp <host_port> <jail_port>]
913    # bastille rdr dev1 tcp 2001 22
914    # bastille rdr dev1 list
915    rdr on em0 inet proto tcp from any to any port = 2001 -> 10.17.89.1 port 22
916    # bastille rdr dev1 udp 2053 53
917    # bastille rdr dev1 list
918    rdr on em0 inet proto tcp from any to any port = 2001 -> 10.17.89.1 port 22
919    rdr on em0 inet proto udp from any to any port = 2053 -> 10.17.89.1 port 53
920    # bastille rdr dev1 clear
921    nat cleared
922```
923
924bastille update
925---------------
926The `update` command targets a release instead of a container. Because every
927container is based on a release, when the release is updated all the containers
928are automatically updated as well.
929
930To update all containers based on the 11.4-RELEASE `release`:
931
932Up to date 11.4-RELEASE:
933```shell
934ishmael ~ # bastille update 11.4-RELEASE
935Targeting specified release.
93611.4-RELEASE
937
938Looking up update.FreeBSD.org mirrors... 2 mirrors found.
939Fetching metadata signature for 11.4-RELEASE from update4.freebsd.org... done.
940Fetching metadata index... done.
941Inspecting system... done.
942Preparing to download files... done.
943
944No updates needed to update system to 11.4-RELEASE-p4.
945No updates are available to install.
946```
947
948To be safe, you may want to restart any containers that have been updated live.
949
950
951bastille upgrade
952----------------
953This sub-command lets you upgrade a release to a new release. Depending on the
954workflow this can be similar to a `bootstrap`.
955
956For standard containers you need to upgrade the shared base jail:
957```shell
958ishmael ~ # bastille upgrade 12.1-RELEASE 12.2-RELEASE
959...
960```
961
962For thick jails you need to upgrade every single container (according the freebsd-update procedure):
963```shell
964ishmael ~ # bastille upgrade folsom 12.2-RELEASE
965ishmael ~ # bastille upgrade folsom install
966...
967ishmael ~ # bastille restart folsom
968ishmael ~ # bastille upgrade folsom install
969```
970
971
972bastille verify
973---------------
974This sub-command scans a bootstrapped release and validates that everything
975looks in order. This is not a 100% comprehensive check, but it compares the
976release against a "known good" index.
977
978If you see errors or issues here, consider deleting and re-bootstrapping the
979release.
980
981It should be noted that releases bootstrapped through Bastille are validated
982using `sha256` checksum against the release manifest. Archives that fail
983validation are not used.
984
985
986bastille zfs
987------------
988This sub-command allows managing ZFS attributes for the targeted container(s).
989Common usage includes setting container quotas.
990
991**set quota**
992```shell
993ishmael ~ # bastille zfs folsom set quota=1G
994```
995
996**built-in: df**
997```shell
998ishmael ~ # bastille zfs ALL df
999```
1000
1001**built-in: df**
1002```shell
1003ishmael ~ # bastille zfs folsom df
1004```
1005
1006bastille export
1007----------------
1008Containers can be exported for archiving purposes easily.
1009Note: On UFS systems containers must be stopped before export.
1010
1011```shell
1012ishmael ~ # bastille export folsom
1013Exporting 'folsom' to a compressed .xz archive.
1014Sending ZFS data stream...
1015  100 %     1057.2 KiB / 9231.5 KiB = 0.115                   0:01
1016Exported '/usr/local/bastille/jails/backups/folsom_2020-01-26-19:23:04.xz' successfully.
1017
1018```
1019
1020bastille import
1021----------------
1022Containers can be imported from supported archives easily.
1023
1024```shell
1025ishmael ~ # bastille import folsom_2020-01-26-19:22:23.xz
1026Validating file: folsom_2020-01-26-19:22:23.xz...
1027File validation successful!
1028Importing 'folsom' from compressed .xz archive.
1029Receiving ZFS data stream...
1030/usr/local/bastille/jails/backups/folsom_2020-01-26-19:22:23.xz (1/1)
1031  100 %      626.4 KiB / 9231.5 KiB = 0.068                   0:02
1032Container 'folsom' imported successfully.
1033```
1034
1035bastille clone
1036---------------
1037`bastille clone` will duplicate an existing container.
1038Please be aware that no host specific keys or hashes will be regenerated.
1039E. g. remove OpenSSH host keys to avoid duplicate host keys `rm /etc/ssh/ssh_host_*`
1040
1041Usage: `bastille clone [TARGET] [NEWJAIL] [NEW_IPADRRESS]`
1042
1043```shell
1044ishmael ~ # bastille clone sourcejail targetjail 10.17.89.11
1045```
1046
1047bastille mount
1048---------------
1049`bastille mount` will nullfs mount a path from the host inside the container.
1050Uses the same format as an fstab entry.
1051Filesystem type, options, dump, and pass number are optional and default to: nullfs ro 0 0
1052
1053Usage: `bastille mount [TARGET] [HOST_PATH] [CONTAINER_PATH] [FILESYSTEM_TYPE] [OPTIONS] [DUMP] [PASS_NUMBER]`
1054
1055```shell
1056ishmael ~ # bastille mount targetjail /host/path container/path
1057[targetjail]:
1058Added: /host/path container/path nullfs ro 0 0
1059```
1060
1061bastille umount
1062---------------
1063`bastille umount` will unmount a volume from inside the container.
1064
1065Usage: `bastille umount [TARGET] [CONTAINER_PATH]`
1066
1067```shell
1068ishmael ~ # bastille umount targetjail container/path
1069[targetjail]:
1070Unmounted: container/path
1071```
1072
1073Example (create, start, console)
1074================================
1075This example creates, starts and consoles into the container.
1076
1077```shell
1078ishmael ~ # bastille create alcatraz 11.4-RELEASE 10.17.89.7
1079```
1080
1081```shell
1082ishmael ~ # bastille start alcatraz
1083[alcatraz]:
1084alcatraz: created
1085```
1086
1087```shell
1088ishmael ~ # bastille console alcatraz
1089[alcatraz]:
1090FreeBSD 11.4-RELEASE-p4 (GENERIC) #0: Thu Sep 27 08:16:24 UTC 2018
1091
1092Welcome to FreeBSD!
1093
1094Release Notes, Errata: https://www.FreeBSD.org/releases/
1095Security Advisories:   https://www.FreeBSD.org/security/
1096FreeBSD Handbook:      https://www.FreeBSD.org/handbook/
1097FreeBSD FAQ:           https://www.FreeBSD.org/faq/
1098Questions List: https://lists.FreeBSD.org/mailman/listinfo/freebsd-questions/
1099FreeBSD Forums:        https://forums.FreeBSD.org/
1100
1101Documents installed with the system are in the /usr/local/share/doc/freebsd/
1102directory, or can be installed later with:  pkg install en-freebsd-doc
1103For other languages, replace "en" with a language code like de or fr.
1104
1105Show the version of FreeBSD installed:  freebsd-version ; uname -a
1106Please include that output and any error messages when posting questions.
1107Introduction to manual pages:  man man
1108FreeBSD directory layout:      man hier
1109
1110Edit /etc/motd to change this login announcement.
1111root@alcatraz:~ #
1112```
1113
1114```shell
1115root@alcatraz:~ # ps -auxw
1116USER   PID %CPU %MEM  VSZ  RSS TT  STAT STARTED    TIME COMMAND
1117root 83222  0.0  0.0 6412 2492  -  IsJ  02:21   0:00.00 /usr/sbin/syslogd -ss
1118root 88531  0.0  0.0 6464 2508  -  SsJ  02:21   0:00.01 /usr/sbin/cron -s
1119root  6587  0.0  0.0 6912 2788  3  R+J  02:42   0:00.00 ps -auxw
1120root 92441  0.0  0.0 6952 3024  3  IJ   02:21   0:00.00 login [pam] (login)
1121root 92565  0.0  0.0 7412 3756  3  SJ   02:21   0:00.01 -csh (csh)
1122root@alcatraz:~ #
1123```
1124
1125
1126Project Goals
1127=============
1128These tools are created initially with the mindset of function over form. I
1129want to simply prove the concept is sound for real work. The real work is a
1130sort of meta-container-port system. Instead of installing the MySQL port
1131directly on a system, you would use Bastille to install the MySQL port within a
1132container template built for MySQL. The same goes for DNS servers, and
1133everything else in the ports tree.
1134
1135Eventually I would like to have Bastille templates created for popular
1136FreeBSD-based services. From Plex Media Servers to ad-blocking DNS resolvers.
1137From tiny SSH containers to dynamic web servers. [COMPLETE]
1138
1139I don't want to tell you what you can and can't run within this framework.
1140There are no arbitrary limitations based on what I think may or may not be the
1141best way to design systems. This is not my goal.
1142
1143My goal is to provide a secure framework where processes and services can run
1144isolated. I want to limit the scope and reach of bad actors. I want to severely
1145limit the target areas available to anyone that has (or has gained) access.
1146
1147Networking Tips
1148===============
1149
1150Tip #1:
1151-------
1152Ports and destinations can be defined as lists. eg;
1153```
1154rdr pass inet proto tcp from any to any port {80, 443} -> {10.17.89.45, 10.17.89.46, 10.17.89.47, 10.17.89.48}
1155```
1156
1157This rule would redirect any traffic to the host on ports 80 or 443 and
1158round-robin between containers with ips 45, 46, 47, and 48 (on ports 80 or
1159443).
1160
1161
1162Tip #2:
1163-------
1164Ports can redirect to other ports. eg;
1165```
1166rdr pass inet proto tcp from any to any port 8080 -> 10.17.89.5 port 80
1167rdr pass inet proto tcp from any to any port 8081 -> 10.17.89.5 port 8080
1168rdr pass inet proto tcp from any to any port 8181 -> 10.17.89.5 port 443
1169```
1170
1171Tip #3:
1172-------
1173Don't worry too much about IP assignments.
1174
1175Initially I spent time worrying about what IP addresses to assign. In the end
1176I've come to the conclusion that it _really_ doesn't matter. Pick *any* private
1177address and be done with it.  These are all isolated networks. In the end, what
1178matters is you can map host:port to container:port reliably, and we can.
1179
1180
1181Community Support
1182=================
1183If you've found a bug in Bastille, please submit it to the [Bastille Issue
1184Tracker](https://github.com/bastillebsd/bastille/issues/new).
1185