10 Commits

Author SHA1 Message Date
yo
f33b3cfec4 v0.42h: finish previous commit 2024-11-21 20:36:19 +01:00
yo
1006207fc2 v0.42h: Add static-macs option for bridged vnet setup with mac flapping 2024-11-21 18:47:55 +01:00
yo
6acea0d25b better handling of version/patch especiallly for snapshots, use updateWorkDir 2024-10-19 09:57:36 +02:00
yo
fb4010378f ZFS Snapshot release before updating 2024-09-22 21:35:54 +02:00
yo
d0646c51b3 Update README 2024-09-22 19:47:37 +02:00
yo
c6e0a93ed8 Rename "gocage list properties" to "gocage properties" 2024-09-22 19:46:22 +02:00
yo
1e2f937cb5 Update README 2024-09-22 19:39:42 +02:00
yo
b72c432a47 Update README 2024-09-22 19:37:31 +02:00
yo
ca1c7bea5f Update README 2024-09-22 19:35:46 +02:00
yo
fb5c4690bf Update README 2024-09-22 19:32:07 +02:00
7 changed files with 135 additions and 95 deletions

View File

@ -8,8 +8,7 @@ Gocage can handle multiple datastores, so you can have jails on HDD storage and
From v0.33b, due to multi ZFS pool support, gocage is no longer 100% compatible with iocage.
Zfs datasets now should be specified with the ZFS pool. e.g. :
<pre><code>
Config.Jail_zfs = 1
<pre><code>Config.Jail_zfs = 1
Config.Jail_zfs_dataset = myzfspool/poudriere
Config.Jail_zfs_mountpoint = none
</code></pre>
@ -17,25 +16,22 @@ Config.Jail_zfs_mountpoint = none
Create jails
------------
You need to specify release, and optional configuration:
<pre><code>
gocage create jail1 -r 13.2-RELEASE -p "Config.Ip4_addr='vnet0|192.168.1.91/24',Config.Ip6=none,Config.Boot=1"
<pre><code>gocage create jail1 -r 13.2-RELEASE -p "Config.Ip4_addr='vnet0|192.168.1.91/24',Config.Ip6=none,Config.Boot=1"
</code></pre>
Create basejail. A basejail is a jail based on a release: system will be nullfs read-only mounted from the release directory. Main advantage is that release updates will immediately apply to jails based on this release. Another advantage is that jail system is mounted read-only, a plus from a security perspective:
<pre><code>
gocage create -b -r 14.0-RELEASE basejail1
<pre><code>gocage create -b -r 14.0-RELEASE basejail1
</code></pre>
List jails
----------
`gocage list`
<pre><code>gocage list</code></pre>
### Specify fields to display
Use -o to specify which fields you want to display:
<pre><code>
gocage list -o JID,Name,Running,Config.Boot,Config.Comment
<pre><code>gocage list -o JID,Name,Running,Config.Boot,Config.Comment
+=====+==========+=========+=============+================+
| JID | Name | Running | Config.Boot | Config.Comment |
+=====+==========+=========+=============+================+
@ -49,7 +45,7 @@ gocage list -o JID,Name,Running,Config.Boot,Config.Comment
+-----+----------+---------+-------------+----------------+
</code></pre>
See [cmd/struct.go](https://git.nosd.in/yo/gocage/src/branch/master/cmd/struct.go) for field names.
Use `gocage properties`to list available fields.
Filter jails
@ -57,8 +53,7 @@ Filter jails
### By name
Just add name on gocage list command :
<pre><code>
gocage list srv-bdd srv-web
<pre><code>gocage list srv-bdd srv-web
+=====+=========+=================+=======================+=========+
| JID | Name | Config.Release | Config.Ip4_addr | Running |
+=====+=========+=================+=======================+=========+
@ -66,12 +61,11 @@ gocage list srv-bdd srv-web
+-----+---------+-----------------+-----------------------+---------+
| 41 | srv-web | 13.0-RELEASE-p4 | vnet0|192.168.1.26/24 | true |
+-----+---------+-----------------+-----------------------+---------+
</pre></code>
</code></pre>
### By field value
You can filter jails with -f option, followed by key=value. Suppose you want to see only active at boot jails:
<pre><code>
gocage list -f Config.Boot=1 -o JID,Name,Running,Config.Boot,Config.Comment
<pre><code>gocage list -f Config.Boot=1 -o JID,Name,Running,Config.Boot,Config.Comment
+=====+==========+=========+=============+================+
| JID | Name | Running | Config.Boot | Config.Comment |
+=====+==========+=========+=============+================+
@ -83,11 +77,10 @@ gocage list -f Config.Boot=1 -o JID,Name,Running,Config.Boot,Config.Comment
+-----+----------+---------+-------------+----------------+
| 22 | srv-dns1 | true | 1 | |
+-----+----------+---------+-------------+----------------+
</pre></code>
</code></pre>
Now, only active at boot and running :
<pre><code>
gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot
<pre><code>gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot
+=====+==========+=========+=============+
| JID | Name | Running | Config.Boot |
+=====+==========+=========+=============+
@ -97,13 +90,12 @@ gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot
+-----+----------+---------+-------------+
| 22 | srv-dns1 | true | 1 |
+-----+----------+---------+-------------+
</pre></code>
</code></pre>
Sort jails
----------
Use -s switch followed by sort criteria. Criteria is a field name, prefixed with + or - for sort order (increase/decrease):
<pre><code>
gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JID
<pre><code>gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JID
+=====+==========+=========+=============+
| JID | Name | Running | Config.Boot |
+=====+==========+=========+=============+
@ -113,12 +105,11 @@ gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JI
+-----+----------+---------+-------------+
| 183 | test | true | 1 |
+-----+----------+---------+-------------+
</pre></code>
</code></pre>
You can use up to 3 criteria, delimited with comma.
As an example, you want to list boot priorities of automatically starting jails:
<pre><code>
gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -Config.Priority,-Config.Boot -f Running=true
<pre><code>gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -Config.Priority,-Config.Boot -f Running=true
+=====+==============+=======================+=================+=============+=========+
| JID | Name | Config.Ip4_addr | Config.Priority | Config.Boot | Running |
+=====+==============+=======================+=================+=============+=========+
@ -130,27 +121,27 @@ gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -
+-----+--------------+-----------------------+-----------------+-------------+---------+
| 4 | coincoin | vnet0|192.168.1.9/24 | 20 | 0 | true |
+-----+--------------+-----------------------+-----------------+-------------+---------+
</pre></code>
</code></pre>
Stop jails
----------
`gocage stop test`
<pre><code>gocage stop test</code></pre>
Update jails
----------
To update jail patch version, use gocage update :
`gocage update test`
<pre><code>gocage update test</code></pre>
Update basejails/releases
----------
To update basejails, you need to update the release they are base on. Specify release with -r, and the datastore storing concerned release with -d :
`gocage update -d fastgocage -r 14.1-RELEASE`
To update basejails, you need to update the release they are based on. Specify release with -r, and the datastore storing concerned release with -d :
<pre><code>gocage update -d fastgocage -r 14.1-RELEASE</code></pre>
Upgrade jails
----------
To upgrade jail to newer release, use gocage upgrade :
`gocage upgrade -r 13.2-RELEASE test`
<pre><code>gocage upgrade -r 13.2-RELEASE test</code></pre>
A pre-upgrade snapshot wil be made so you can rollback if needed.
@ -160,33 +151,32 @@ Upgrading basejails currently needs to be done manually, for each jail.
The idea is to stop the jail, change the content of its fstab file to point to the new release, then start jail.
If one change the fstab while the jail is running, its system directories won't be unmounted at stop time and this will provoke stop errors.
To minimize downtime, the change could be scripted:
`gocage stop jail1
<pre><code>gocage stop jail1
sed -i .bak 's/14.0-RELEASE/14.1-RELEASE/' /iocage/jails/jail1/fstab
# Avoid race-condition by waiting for the update in fstab
until grep -q 14.1-RELEASE /iocage/jails/jail1/fstab; do sleep 0.2; done
gocage start jail1`
gocage start jail1
</code></pre>
You can now update ports.
Delete jails
----------
`gocage destroy test`
<pre><code>gocage destroy test</code></pre>
Multi datastore
----------
A datastore is a ZFS dataset mounted. It should be declared in gocage.conf.yml, specifying its ZFS mountpoint :
<pre><code>
datastore:
<pre><code>datastore:
- /iocage
- /fastiocage
</pre></code>
</code></pre>
In gocage commands, datastore name is the mountpoint without its "/" prefix.
### List datastores
<pre><code>
gocage datastore list
<pre><code>gocage datastore list
+============+=============+============+===========+==========+============+
| Name | Mountpoint | ZFSDataset | Available | Used | Referenced |
+============+=============+============+===========+==========+============+
@ -194,23 +184,21 @@ gocage datastore list
+------------+-------------+------------+-----------+----------+------------+
| fastiocage | /fastiocage | ssd/iocage | 1.5 TB | 65.3 KB | 34.6 KB |
+------------+-------------+------------+-----------+----------+------------+
</pre></code>
</code></pre>
### Filter datastores
As with jails and snapshots, you can filter by name:
<pre><code>
gocage datastore list iocage
<pre><code>gocage datastore list iocage
+============+=============+============+===========+==========+============+
| Name | Mountpoint | ZFSDataset | Available | Used | Referenced |
+============+=============+============+===========+==========+============+
| iocage | /iocage | hdd/iocage | 1.6 TB | 414.9 GB | 27.5 KB |
+------------+-------------+------------+-----------+----------+------------+
</pre></code>
</code></pre>
### Sort datastores
You can sort datastores:
<pre><code>
gocage datastore list -s -Available
<pre><code>gocage datastore list -s -Available
+============+=============+============+===========+==========+============+
| Name | Mountpoint | ZFSDataset | Available | Used | Referenced |
+============+=============+============+===========+==========+============+
@ -218,9 +206,9 @@ gocage datastore list -s -Available
+------------+-------------+------------+-----------+----------+------------+
| fastiocage | /fastiocage | ssd/iocage | 1.5 TB | 65.3 KB | 34.6 KB |
+------------+-------------+------------+-----------+----------+------------+
</pre></code>
</code></pre>
See [cmd/struct.go](https://git.nosd.in/yo/gocage/src/branch/master/cmd/struct.go) for field names.
Use `gocage properties`to list available fields.
Migrating jails
----------
@ -233,26 +221,23 @@ Be aware the moment you migrate a jail to another datastore than /iocage default
Then you need to disable iocage service, and enable gocage so the jails will start automatically at boot.
Also make sure, if you don't destroy source jail, that it won't have the "boot" property set or you will have the 2 jails up at boot.
<pre><code>
gocage migrate -d fastiocage srv-random
<pre><code>gocage migrate -d fastiocage srv-random
Snapshot data/iocage/jails/srv-random: Done
Snapshot data/iocage/jails/srv-random/root: Done
Migrate jail config dataset to fastdata/iocage/jails/srv-random: Done
Migrate jail filesystem dataset to fastdata/iocage/jails/srv-random/root: Done
</pre></code>
</code></pre>
Fetch
----------
Files can be fetched from custom repository, or from local directory with "from" option.
For example if you destroyed releases/12.3-RELEASE and still have the downloaded files in /iocage/download/12.3-RELEASE:
<pre><code>
gocage fetch -r 12.3 -d iocage -f file:/iocage/download
</pre></code>
<pre><code>gocage fetch -r 12.3 -d iocage -f file:/iocage/download
</code></pre>
TODO
----------
gocage create from templates
gocage init
create default pool with defaults.json

View File

@ -4,7 +4,6 @@ import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
"io/ioutil"
"golang.org/x/net/route"
@ -190,34 +189,17 @@ func getArch() (string, error) {
func getFreeBSDVersion() (FreeBSDVersion, error) {
var version FreeBSDVersion
regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?`
re := regexp.MustCompile(regex)
out, err := executeCommand("/bin/freebsd-version")
if err != nil {
return version, fmt.Errorf("Error executing \"/bin/freebsd-version\": %v", err)
}
if re.MatchString(out) {
version.major, err = strconv.Atoi(re.FindStringSubmatch(out)[1])
if err != nil {
return version, err
}
version.minor, err = strconv.Atoi(re.FindStringSubmatch(out)[3])
if err != nil {
return version, err
}
version.flavor = strings.Trim(re.FindStringSubmatch(out)[4], "\n")
// Skip the 'p' starting patch level
if len(re.FindStringSubmatch(out)[6]) > 0 {
version.patchLevel, err = strconv.Atoi(re.FindStringSubmatch(out)[6][1:])
if err != nil {
return version, err
}
}
version, err = freebsdVersionToStruct(out)
if err != nil {
return version, err
}
return version, nil
}

View File

@ -14,7 +14,7 @@ import (
)
const (
gVersion = "0.42c"
gVersion = "0.42h"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
@ -52,6 +52,7 @@ var (
gNoJailLineSep bool
gNoSnapLineSep bool
gNoDSLineSep bool
gBridgeStaticMac bool
gHostVersion float64
@ -99,7 +100,11 @@ It support iocage jails and can coexist with iocage.`,
Long: `Let this show you how much fail I had to get this *cough* perfect`,
Run: func(cmd *cobra.Command, args []string) {
fv, _ := getFreeBSDVersion()
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
if fv.patchLevel > 0 {
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s-p%d\n", gVersion, fv.major, fv.minor, fv.flavor, fv.patchLevel)
} else {
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
}
},
}
@ -437,7 +442,7 @@ func init() {
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(listCmd)
listCmd.AddCommand(listPropsCmd)
rootCmd.AddCommand(listPropsCmd)
rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(restartCmd)
@ -515,6 +520,8 @@ func initConfig() {
os.Exit(1)
}
gBridgeStaticMac = viper.GetBool("static-macs")
if gDebug {
log.SetLevel(log.DebugLevel)
log.Debugf("Debug mode enabled\n")

View File

@ -1007,6 +1007,19 @@ func setupVnetInterfaceJailSide(jail *Jail, hostepairs []string) error {
if err != nil {
return fmt.Errorf("Error adding member %s to %s: %v: %s\n", nic, bridge, err, out)
}
// Fix mac flapping and instabilities with bridged vnet network
if gBridgeStaticMac {
printablemac := hex.EncodeToString(jsmac)
for i := 2 ; i < len(printablemac) ; i += 3 {
printablemac = printablemac[:i] + ":" + printablemac[i:]
}
log.Debugf("Set %s.%d as static to %s in %s address cache\n", nic, jail.JID, printablemac, bridge)
cmd = fmt.Sprintf("/sbin/ifconfig %s static %s.%d %s", bridge, nic, jail.JID, printablemac)
out, err := executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error setting %s.%d static with %s on %s: %v: %s\n", nic, jail.JID, printablemac, bridge, err, out)
}
}
}
// Check we have an IP for the nic, and set it into jail

View File

@ -66,7 +66,7 @@ func UpdateJail(args []string) {
// get datastore mountpoing from datastore name
ds, err := getDatastoreFromArray(gUpdateReleaseDS, gDatastores)
if err != nil {
fmt.Printf("Error gettting datastore %s: %v\n", gUpdateReleaseDS, err)
fmt.Printf("Error getting datastore %s: %v\n", gUpdateReleaseDS, err)
return
}
rp := fmt.Sprintf("%s/releases/%s/root", ds.Mountpoint, gUpdateRelease)
@ -77,6 +77,30 @@ func UpdateJail(args []string) {
return
}
fakeJail.Config.Release = v
// Remove patch level from Release
fv, err := freebsdVersionToStruct(fakeJail.Config.Release)
if err != nil {
fmt.Printf("Error converting release %s: %v\n", fakeJail.Config.Release, err)
return
}
release := fmt.Sprintf("%d.%d-%s", fv.major, fv.minor, fv.flavor)
// Snapshot before updating
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05"))
snapshotName := fmt.Sprintf("gocage_update_%s_%s", v, curDate)
err = zfsSnapshot(fmt.Sprintf("%s/releases/%s", ds.ZFSDataset, release), snapshotName)
if err != nil {
fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err)
return
}
err = zfsSnapshot(fmt.Sprintf("%s/releases/%s/root", ds.ZFSDataset, release), snapshotName)
if err != nil {
fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err)
} else {
fmt.Printf("Release %s was snapshoted with success: %s\n", gUpdateRelease, snapshotName)
}
if err = updateJail(&fakeJail, false); err != nil {
fmt.Printf("Error updating release %s: %v\n", gUpdateRelease, err)
}
@ -101,7 +125,7 @@ func UpdateJail(args []string) {
// Set snapshot name
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05"))
gSnapshotName = fmt.Sprintf("goc_update_%s_%s", cj.Config.Release, curDate)
gSnapshotName = fmt.Sprintf("gocage_update_%s_%s", cj.Config.Release, curDate)
err := createJailSnapshot(*cj)
if err != nil {
fmt.Printf(" > Snapshot jail %s: ERROR: %s\n", cj.Name, err.Error())

View File

@ -8,6 +8,7 @@ import (
//"log"
"time"
"strings"
"github.com/spf13/viper"
)
// Internal usage only
@ -18,19 +19,20 @@ func upgradeJail(jail *Jail, version string) error {
return err
}
cfgFile.Write([]byte(fbsdUpdateConfig))
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Folder containing update/uipgrade temporary files. Common so we save bandwith when upgrading multiple jails
// TODO: Variabilize /iocage/freebsd-update
_, err = os.Stat("/iocage/freebsd-update")
// Folder containing update/upgrade temporary files. Mutualized so we save bandwith when upgrading multiple jails
uwd := viper.GetString("updateWorkDir")
if len(uwd) == 0 {
return fmt.Errorf("updateWorkDir not set in configuration")
}
_, err = os.Stat(uwd)
if os.IsNotExist(err) {
if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil {
if err := os.Mkdir(uwd, 0755); err != nil {
return err
}
}
cfgFile.Write([]byte(strings.Replace(fbsdUpdateConfig, "TO-BE-REPLACED-WITH-UPDATEWORKDIR", uwd, 1)))
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Get current version. Won't work on stopped jail.
fbsdvers, err := executeCommandInJail(jail, "/bin/freebsd-version")
@ -39,7 +41,6 @@ func upgradeJail(jail *Jail, version string) error {
return err
}
fbsdvers = strings.TrimRight(fbsdvers, "\n")
//fbsdvers := jail.Config.Release
cmd := fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s upgrade",
cfgFile.Name(), jail.RootPath, fbsdvers, version)

View File

@ -8,6 +8,7 @@ import (
"sort"
"bufio"
"errors"
"regexp"
"os/exec"
"reflect"
"strconv"
@ -1110,13 +1111,10 @@ func getDevfsRuleset(ruleset int) []string {
if err != nil {
return []string{}
}
// Get rid of the last "\n"
return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))-1]
return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))]
}
func copyDevfsRuleset(ruleset int, srcrs int) error {
// Resulting ruleset as an array of line
//var result []string
out := getDevfsRuleset(srcrs)
for _, line := range out {
//fields := strings.Fields(line)
@ -1287,6 +1285,36 @@ func setJailConfigUpdated(jail *Jail) error {
return nil
}
func freebsdVersionToStruct(rawVersion string) (FreeBSDVersion, error) {
var version FreeBSDVersion
var err error
regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?`
re := regexp.MustCompile(regex)
if re.MatchString(rawVersion) {
version.major, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[1])
if err != nil {
return version, err
}
version.minor, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[3])
if err != nil {
return version, err
}
version.flavor = strings.Trim(re.FindStringSubmatch(rawVersion)[4], "\n")
// Skip the 'p' starting patch level
if len(re.FindStringSubmatch(rawVersion)[6]) > 0 {
version.patchLevel, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[6][1:])
if err != nil {
return version, err
}
}
}
return version, nil
}
func getVersion(jail *Jail) (string, error) {
cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath))
if err != nil {