21 Commits

Author SHA1 Message Date
yo
9e057ed1c5 Add gocage destroy command, v0.35 2023-07-09 13:43:35 +02:00
yo
925c3dd96b Add gocage destroy command, v0.35 2023-07-09 13:42:24 +02:00
yo
e11fc96e05 Add gocage destroy command 2023-07-09 13:42:04 +02:00
yo
956e25c849 Display jail name instead of user given string 2023-07-09 13:41:19 +02:00
yo
f9f1d48023 Add zfsDestroy & zfsGetDatasetByMountpoint functions, rename createZfsDataset to zfsCreateDataset 2023-07-09 13:40:40 +02:00
yo
1b679bcd17 Include gocage update to README 2023-07-09 10:52:12 +02:00
yo
dc4213a8d5 v.0.34 : Jail names can be shortened 2023-07-09 10:38:00 +02:00
yo
5eed121f0b Protect devfs last ruleset acquiring with mutex 2023-06-25 23:32:15 +02:00
yo
812c77790a Add CHANGELOG 2023-06-25 23:09:03 +02:00
yo
7575da794e 0.33c : parallelize start/stop of jails up to gMaxThreads 2023-06-25 23:07:53 +02:00
yo
6f9bb504be Fix forever waiting on services not properly closing pipes at start 2023-06-10 14:12:53 +02:00
yo
7c3e14f0f1 version bump, README update 2023-06-03 11:18:42 +02:00
yo
a4ff9c1d51 datasets now should be specified with zpool 2023-06-03 11:18:40 +02:00
yo
00fd283987 resolver.conf bugfix, datasets now should be specified with zpool 2023-06-03 11:18:37 +02:00
yo
7cf4594f34 Update TODO list with some bugs 2022-11-20 20:20:44 +01:00
yo
37fea55e42 Add update command 2022-11-20 20:20:20 +01:00
yo
c15ee68d2e Add update command 2022-11-20 20:20:06 +01:00
yo
54fd1f8064 FIXME: Update Last_started 2022-11-20 20:19:37 +01:00
yo
89db166040 FIXME: Update release in config file when stopping jail 2022-11-20 20:17:59 +01:00
yo
9c18a83ee8 ExecuteCommandWithOutputToStdout 2022-11-20 20:16:23 +01:00
yo
561ae4386a FIXME 2022-11-20 20:13:47 +01:00
11 changed files with 640 additions and 92 deletions

4
CHANGELOG Normal file
View File

@ -0,0 +1,4 @@
v.0.33b : Support jailing datasets on differents pools : jail_zfs_dataset now have to include the pool name
v.0.33c : Parallelize start/stop of jails with same priority
v.0.34 : jail name can be shortened
v.0.35 : One can now "gocage destroy"

View File

@ -6,7 +6,13 @@ Support iocage jails, so they can coexist.
Gocage is meant to be a complete jail management tool with network, snapshots, jail cloning support and a web interface. This is the hypothetic future.
Gocage can handle multiple datastores, so you can have jails on HDD storage and jails on SSD storage.
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. :
<code>
Config.Jail_zfs = 1
Config.Jail_zfs_dataset = myzfspool/poudriere
Config.Jail_zfs_mountpoint = none
</code>
List jails
----------
@ -115,11 +121,22 @@ gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -
</pre></code>
Stop jails
Stop jails
----------
`gocage stop test`
Update jails
----------
To update jail patch version, use gocage update :
`gocage update test`
Delete jails
----------
`gocage destroy test`
Multi datastore
----------
A datastore is a ZFS dataset mounted. It should be declared in gocage.conf.yml, specifying its ZFS mountpoint :
@ -200,9 +217,7 @@ gocage fetch -r 12.3 -o iocage --from file:/iocage/download
TODO
----------
gocage update
gocage upgrade
gocage create
gocage destroy
gocage init
create default pool with defaults.json

View File

@ -1,4 +1,8 @@
Replicating jails between two servers (use zrepl)
DEBUG:
- cmd/list.go:275:
// FIXME ??? Shouldn't be ioc-$Name ?
j.InternalName = rj.Name
- WriteConfigToDisk don't write neither "release" in cmd stop neither "last_started" in cmd start

57
cmd/destroy.go Normal file
View File

@ -0,0 +1,57 @@
package cmd
import (
"fmt"
//"log"
//"errors"
"strings"
)
func DestroyJails(args []string) {
for _, a := range args {
cj, err := getJailFromArray(a, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
return
}
if cj.Running == true {
fmt.Printf("Jail %s is running\n", cj.Name)
if gForce == false {
var answer string
fmt.Printf("Stop jail and delete? (y/n) ")
fmt.Scanln(&answer)
if false == strings.EqualFold(answer, "y") {
return
}
}
fmt.Printf("Stopping jail %s\n", cj.Name)
StopJail([]string{fmt.Sprintf("%s/%s", cj.Datastore, cj.Name)})
}
// Get root and config datasets, then destroy
dsRootName, err := zfsGetDatasetByMountpoint(cj.RootPath)
if err != nil {
fmt.Printf("Error getting root dataset: %s\n", err)
return
}
fmt.Printf("DEBUG: Prepare to zfs destroy %s\n", dsRootName)
if err = zfsDestroy(dsRootName); err != nil {
fmt.Printf("Error deleting root dataset: %s\n", err)
return
}
dsConfName, err := zfsGetDatasetByMountpoint(cj.ConfigPath)
if err != nil {
fmt.Printf("Error getting config dataset: %s\n", err)
return
}
fmt.Printf("DEBUG: Prepare to zfs destroy %s\n", dsConfName)
if err = zfsDestroy(dsConfName); err != nil {
fmt.Printf("Error deleting config dataset: %s\n", err)
return
}
//TODO: Delete jail named directory
}
}

View File

@ -61,7 +61,7 @@ func fetchRelease(release string, proto string, arch string, datastore string, f
}
if false == exist {
// Then create dataset
if err := createZfsDataset(downloadDsName, downloadDsMountPoint, "lz4"); err != nil {
if err := zfsCreateDataset(downloadDsName, downloadDsMountPoint, "lz4"); err != nil {
return fmt.Errorf("Error creating dataset %s: %v\n", downloadDsName, err)
}
}
@ -75,7 +75,7 @@ func fetchRelease(release string, proto string, arch string, datastore string, f
}
if false == exist {
// Then create dataset
if err := createZfsDataset(thisDownloadDsName, thisDownloadDsMountPoint, "lz4"); err != nil {
if err := zfsCreateDataset(thisDownloadDsName, thisDownloadDsMountPoint, "lz4"); err != nil {
return fmt.Errorf("Error creating dataset %s: %v\n", thisDownloadDsName, err)
}
}
@ -147,7 +147,7 @@ func extractRelease(release string, datastore string) {
}
if false == exist {
// Then create dataset
if err := createZfsDataset(releaseDsName, releaseDsMountPoint, "lz4"); err != nil {
if err := zfsCreateDataset(releaseDsName, releaseDsMountPoint, "lz4"); err != nil {
fmt.Printf("Error creating dataset %s: %v\n", releaseDsName, err)
return
}
@ -163,7 +163,7 @@ func extractRelease(release string, datastore string) {
}
if false == exist {
// Then create dataset
if err := createZfsDataset(thisReleaseDsName, thisReleaseDsMountPoint, "lz4"); err != nil {
if err := zfsCreateDataset(thisReleaseDsName, thisReleaseDsMountPoint, "lz4"); err != nil {
fmt.Printf("Error creating dataset %s: %v\n", thisReleaseDsName, err)
return
}
@ -179,7 +179,7 @@ func extractRelease(release string, datastore string) {
}
if false == exist {
// Then create dataset
if err := createZfsDataset(thisReleaseRootDsName, thisReleaseRootDsMountPoint, "lz4"); err != nil {
if err := zfsCreateDataset(thisReleaseRootDsName, thisReleaseRootDsMountPoint, "lz4"); err != nil {
fmt.Printf("Error creating dataset %s: %v\n", thisReleaseRootDsName, err)
return
}

View File

@ -272,6 +272,7 @@ func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
if rj.Path == j.RootPath {
j.JID = rj.Jid
j.Running = true
// FIXME ??? Shouldn't be ioc-$Name ?
j.InternalName = rj.Name
j.Devfs_ruleset = rj.Devfs_ruleset
break

View File

@ -1,11 +1,12 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"fmt"
"sync"
"strings"
"io/ioutil"
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -14,7 +15,7 @@ import (
)
const (
gVersion = "0.32a"
gVersion = "0.35"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
@ -55,6 +56,7 @@ var (
gFetchIntoDS string
gFetchFrom string
gMdevfs sync.Mutex
rootCmd = &cobra.Command{
Use: "gocage",
@ -97,8 +99,8 @@ ex: gocage list srv-db srv-web`,
ListJailsProps(args)
},
}
/* destroyCmd = &cobra.Command{
destroyCmd = &cobra.Command{
Use: "destroy",
Short: "destroy jails",
Long: `Destroy jail filesystem, snapshots and configuration file.`,
@ -107,7 +109,7 @@ ex: gocage list srv-db srv-web`,
DestroyJails(args)
},
}
*/
stopCmd = &cobra.Command{
Use: "stop",
Short: "stop jail",
@ -134,7 +136,7 @@ ex: gocage list srv-db srv-web`,
} else {
StartJail(args)
}
WriteConfigToDisk(false)
WriteConfigToDisk("", false, false)
},
}
@ -148,7 +150,7 @@ ex: gocage list srv-db srv-web`,
ListJails(args, false)
StopJail(args)
StartJail(args)
WriteConfigToDisk(false)
WriteConfigToDisk("", false, false)
},
}
@ -171,7 +173,7 @@ Multiples properties can be specified, separated with space (Ex: gocage set allo
// Load inventory
ListJails(args, false)
SetJailProperties(args)
WriteConfigToDisk(true)
WriteConfigToDisk("", true, false)
},
}
@ -253,7 +255,7 @@ You can specify multiple jails.`,
// Load inventory
ListJails(args, false)
MigrateJail(args)
WriteConfigToDisk(false)
WriteConfigToDisk("", false, false)
},
}
@ -303,6 +305,15 @@ You can specify multiple datastores.`,
},
}
UpdateCmd = &cobra.Command{
Use: "update",
Short: "Update FreeBSD release",
Run: func(cmd *cobra.Command, args []string) {
ListJails(args, false)
UpdateJail(args)
},
}
testCmd = &cobra.Command{
Use: "test",
Short: "temporary command to test some code snippet",
@ -332,7 +343,7 @@ func init() {
listCmd.Flags().StringVarP(&gFilterJails, "filter", "f", "none", "Only display jails with these values. Ex: \"gocage list -f Config.Boot=1\" will only list started on boot jails")
listCmd.Flags().StringVarP(&gSortJailFields, "sort", "s", "none", "Display jails sorted by field values. Ex: \"gocage list -s +Name,-Config.Priority\" will sort jails by their decreasing name, then increasing start priority. 3 critera max supported.")
// destroyCmd.Flags().BoolVarP(&gForce, "force", "f", false, "Force stop jail if running")
destroyCmd.Flags().BoolVarP(&gForce, "force", "f", false, "Force stop jail if running")
snapshotListCmd.Flags().StringVarP(&gDisplaySColumns, "outcol", "o", "Jailname,Name,Creation,Referenced,Used", "Show these columns in output")
snapshotListCmd.Flags().BoolVarP(&gNoSnapLineSep, "nolinesep", "l", false, "Do not display line separator between snapshots")
@ -370,7 +381,7 @@ func init() {
rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(restartCmd)
// rootCmd.AddCommand(destroyCmd)
rootCmd.AddCommand(destroyCmd)
rootCmd.AddCommand(shellCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(setCmd)
@ -378,6 +389,7 @@ func init() {
rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(datastoreCmd)
rootCmd.AddCommand(fetchCmd)
rootCmd.AddCommand(UpdateCmd)
rootCmd.AddCommand(testCmd)
@ -461,59 +473,66 @@ func initConfig() {
}
/********************************************************************************
* Write jails config which been updated to disk.
* Write jail(s) config which been updated to disk.
* If name is specified, work on the jail. If name is empty string, work on all.
* If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current
* default route, so if route change on jailhost this will reflect on jail next
* start)
*******************************************************************************/
func WriteConfigToDisk(changeauto bool) {
func WriteConfigToDisk(jailName string, changeauto bool, forceWrite bool) {
for _, j := range gJails {
if j.ConfigUpdated {
//log.Debug("%s config has changed, write changes to disk\n", j.Name)
if len(jailName) > 0 && j.Name == jailName || len(jailName) == 0 {
if j.ConfigUpdated || forceWrite {
log.Debug("%s config has changed, write changes to disk\n", j.Name)
// we will manipulate properties so get a copy
jc := j.Config
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
//fmt.Printf("DEBUG: Will write config to disk, with content:\n")
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
}
}
}
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)

View File

@ -4,6 +4,8 @@ import (
"os"
"fmt"
"net"
"sync"
"time"
"errors"
"regexp"
"reflect"
@ -189,24 +191,26 @@ func prepareJailedZfsDatasets(jail *Jail) error {
}
for _, d := range strings.Split(jail.Config.Jail_zfs_dataset, " ") {
// Check if dataset exist, create if necessary
cmd := fmt.Sprintf("zfs get -H creation %s/%s", jail.Zpool, d)
// Support jailing datasets on differents pools : dataset should be specified with pool name
cmd := fmt.Sprintf("zfs get -H creation %s", d)
out, err := executeCommand(cmd)
if err != nil {
if strings.HasSuffix(out, "dataset does not exist") {
cmd = fmt.Sprintf("zfs create -o compression=lz4 -o mountpoint=none %s/%s", jail.Zpool, d)
// Support jailing datasets on differents pools : dataset should be specified with pool name
cmd = fmt.Sprintf("zfs create -o compression=lz4 -o mountpoint=none %s", d)
_, err = executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error creating dataset %s/%s: %s", jail.Zpool, d, err.Error()))
return errors.New(fmt.Sprintf("Error creating dataset %s: %s", d, err.Error()))
}
} else {
return errors.New(fmt.Sprintf("Error getting zfs dataset %s: %s", d, err.Error()))
}
}
cmd = fmt.Sprintf("zfs set jailed=on %s/%s", jail.Zpool, d)
cmd = fmt.Sprintf("zfs set jailed=on %s", d)
out, err = executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error executing \"zfs set jailed=on %s/%s\": %s", jail.Zpool, d, err.Error()))
return errors.New(fmt.Sprintf("Error executing \"zfs set jailed=on %s\": %s", d, err.Error()))
}
}
}
@ -217,27 +221,27 @@ func jailZfsDatasets(jail *Jail) error {
if jail.Config.Jail_zfs > 0 {
for _, d := range strings.Split(jail.Config.Jail_zfs_dataset, " ") {
// Jail dataset
cmd := fmt.Sprintf("zfs jail %d %s/%s", jail.JID, jail.Zpool, d)
// Support jailing datasets on differents pools : dataset should be specified with pool name
cmd := fmt.Sprintf("zfs jail %d %s", jail.JID, d)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error jailling zfs dataset %s: %v: out", d, err, out))
}
// Mount from inside jail if mountpoint is set
cmd = fmt.Sprintf("zfs get -H -o value mountpoint %s/%s", jail.Zpool, d)
cmd = fmt.Sprintf("zfs get -H -o value mountpoint %s", d)
out, err = executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error getting zfs dataset %s/%s mountpoint: %v: %s", jail.Zpool, d, err, out))
return errors.New(fmt.Sprintf("Error getting zfs dataset %s mountpoint: %v: %s", d, err, out))
}
if len(out) > 0 && out != "-" && (false == strings.EqualFold(out, "none")) {
//cmd = fmt.Sprintf("zfs mount %s/%s", jail.Zpool, d)
cmd = fmt.Sprintf("zfs mount -a")
// Should we "mount -a" ? cmd = fmt.Sprintf("zfs mount -a")
cmd = fmt.Sprintf("zfs mount %s", d)
out, err = executeCommandInJail(jail, cmd)
if err != nil {
// If already mounted, continue processing
if ! strings.HasSuffix(out, "filesystem already mounted\n") {
//return errors.New(fmt.Sprintf("Error mounting zfs dataset %s/%s: %v: %s", jail.Zpool, d, err, out))
return errors.New(fmt.Sprintf("Error executing \"zfs mount -a\" from inside jail: %v: %s", err, out))
return errors.New(fmt.Sprintf("Error mounting zfs dataset %s from inside jail: %v: %s", d, err, out))
}
}
}
@ -472,12 +476,17 @@ func genNatIpv4(jail *Jail) ([]string, error) {
return ippair, nil
}
func buildDevfsRuleSet(jail *Jail) (error, int) {
// FIXME : Must lock this function so parallel start do not
func buildDevfsRuleSet(jail *Jail, m *sync.Mutex) (error, int) {
rulesets := []int{}
m.Lock()
//defer m.Unlock()
// Get known rulesets
out, err := executeCommand("devfs rule showsets")
if err != nil {
m.Unlock()
return errors.New(fmt.Sprintf("Error executing command \"devfs rule showsets\": %v; command returned: %s\n", err, out)), 0
}
srs := strings.Split(out, "\n")
@ -505,19 +514,23 @@ func buildDevfsRuleSet(jail *Jail) (error, int) {
// UPDATE: We don't need this as every jail have a default Devfs_ruleset value
/*ds, err := getDatastoreFromArray(jail.Datastore, gDatastores)
if err != nil {
m.Unlock()
return errors.New(fmt.Sprintf("Error getting datastore %s for jail %s", jail.Datastore, jail.Name)), 0
}
defaultrs, err := strconv.ParseInt(ds.DefaultJailConfig.Devfs_ruleset, 10, 64)
if err != nil {
m.Unlock()
return errors.New(fmt.Sprintf("Error parsing default devfs_ruleset for datastore %s", jail.Datastore)), 0
}*/
// Clone configured devfs_ruleset to a dynamic ruleset
if false == isStringInArray(srs, jail.Config.Devfs_ruleset) {
m.Unlock()
return errors.New(fmt.Sprintf("Unknown ruleset: %s", jail.Config.Devfs_ruleset)), 0
}
rs, _ := strconv.Atoi(jail.Config.Devfs_ruleset)
err = copyDevfsRuleset(ruleset, rs)
m.Unlock()
if err != nil {
return err, 0
}
@ -945,7 +958,7 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
// Get bridge MTU
mtu, err := gJailHost.GetBridgeMTU(bridge)
if err != nil {
return fmt.Errorf("Error getting bridge mtu: %v\n", err)
return fmt.Errorf("Error getting bridge %s mtu: %v\n", bridge, err)
}
cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jsepair, mtu)
@ -1010,7 +1023,7 @@ func generateResolvConf(jail *Jail) error {
for _, l := range strings.Split(jail.Config.Resolver, ";") {
f.WriteString(fmt.Sprintf("%s\n", l))
}
} else if jail.Config.Resolver == "none" {
} else if jail.Config.Resolver == "none" || jail.Config.Resolver == "/etc/resolv.conf" {
read, err := ioutil.ReadFile("/etc/resolv.conf")
if err != nil {
return fmt.Errorf("Error opening /etc/resolv.conf: %v", err)
@ -1052,6 +1065,9 @@ func cleanAfterStartCrash() {
// Start all jails with boot=true, in priority order
func StartJailsAtBoot() {
var startList []Jail
var wg *sync.WaitGroup
var curThNb int
var curPri int
// Get boot enabled jails
for _, j := range gJails {
@ -1069,11 +1085,51 @@ func StartJailsAtBoot() {
}
JailsOrderedBy(fct.Interface().(jailLessFunc)).Sort(startList)
for _, j := range startList {
wg = new(sync.WaitGroup)
curThNb = 0
for i, j := range startList {
jFullName := fmt.Sprintf("%s/%s", j.Datastore, j.Name)
log.Debugf("Starting %s with priority %s\n", jFullName, j.Config.Priority)
StartJail([]string{jFullName})
jailPri, err := strconv.Atoi(j.Config.Priority)
if err != nil {
panic(fmt.Sprintf("Invalid format for Priority (Jail %s)\n", jFullName))
}
if (curThNb >= gMaxThreads || i == 0) {
// FIXME : Use a pool instead of waiting for all threads to run a new one
wg.Wait()
curThNb = 0
wg.Add(1)
curThNb++
curPri = jailPri
go func(jailFullName string) {
defer wg.Done()
StartJail([]string{jailFullName})
}(jFullName)
} else {
if (curPri == jailPri) {
wg.Add(1)
curThNb++
go func(jailFullName string) {
defer wg.Done()
StartJail([]string{jailFullName})
}(jFullName)
} else {
wg.Wait()
curThNb = 0
wg.Add(1)
curThNb++
curPri = jailPri
go func(jailFullName string) {
defer wg.Done()
StartJail([]string{jailFullName})
}(jFullName)
}
}
}
wg.Wait()
}
@ -1120,7 +1176,7 @@ func StartJail(args []string) {
continue
}
fmt.Printf("> Starting jail %s\n", a)
fmt.Printf("> Starting jail %s\n", cj.Name)
// Set InternalName as it is used by some of these
cj.InternalName = fmt.Sprintf("ioc-%s", cj.Name)
@ -1296,7 +1352,7 @@ func StartJail(args []string) {
net = append(net, strings.Split(cj.Config.Vnet_interfaces, " ")...)
}
err, dynrs := buildDevfsRuleSet(cj)
err, dynrs := buildDevfsRuleSet(cj, &gMdevfs)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
@ -1309,7 +1365,7 @@ func StartJail(args []string) {
}
// Synchronize jail config to disk
WriteConfigToDisk(false)
WriteConfigToDisk(cj.Name, false, false)
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
@ -1420,9 +1476,9 @@ func StartJail(args []string) {
if len(cj.Config.Exec_start) > 0 {
fmt.Printf(" > Start services:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_start)
out, err := executeCommand(cmd)
err := executeCommandNonBlocking(cmd)
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf(" > Start services: OK\n")
}
@ -1455,6 +1511,11 @@ func StartJail(args []string) {
// TODO: Handle dhcp
// TODO: Apply rctl
// Update last_started
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
fmt.Sprintf(cj.Config.Last_started, curDate)
WriteConfigToDisk(cj.Name, false, true)
/*

View File

@ -4,6 +4,7 @@ import (
"os"
"fmt"
//"log"
"sync"
"errors"
"regexp"
"os/exec"
@ -50,10 +51,10 @@ func umountAndUnjailZFS(jail *Jail) error {
for _, zd := range ds {
// 1. Get dataset and childs
cmd := fmt.Sprintf("zfs list -H -r -o name -S name %s/%s", jail.Zpool, zd)
cmd := fmt.Sprintf("zfs list -H -r -o name -S name %s", zd)
out, err := executeCommand(cmd)
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR listing dataset %s/%s\n", jail.Zpool, zd))
fmt.Printf(fmt.Sprintf("ERROR listing dataset %s\n", zd))
os.Exit(1)
}
for _, c := range strings.Split(out, "\n") {
@ -71,10 +72,10 @@ func umountAndUnjailZFS(jail *Jail) error {
}
// 2. Unjail dataset from the host
cmd := fmt.Sprintf("zfs unjail %s %s/%s", jail.InternalName, jail.Zpool, ds[len(ds)-1])
cmd := fmt.Sprintf("zfs unjail %s %s", jail.InternalName, ds[len(ds)-1])
_, err := executeCommand(cmd)
if err != nil {
fmt.Printf("ERROR unjailing %s/%s: %s\n", jail.Zpool, ds[len(ds)-1], err.Error())
fmt.Printf("ERROR unjailing %s: %s\n", ds[len(ds)-1], err.Error())
return err
}
@ -169,8 +170,13 @@ func stopJail(jail *Jail) error {
}
// Stop all running jails by reverse priority
// Parallelize up to gMaxThreads
// Only parallelize same priority level jails
func StopAllRunningJails() {
var stopList []Jail
var wg *sync.WaitGroup
var curThNb int
var curPri int
// Get boot enabled jails
for _, j := range gJails {
@ -187,12 +193,53 @@ func StopAllRunningJails() {
return
}
JailsOrderedBy(fct.Interface().(jailLessFunc)).Sort(stopList)
for _, j := range stopList {
wg = new(sync.WaitGroup)
curThNb = 0
for i, j := range stopList {
jFullName := fmt.Sprintf("%s/%s", j.Datastore, j.Name)
log.Debugf("Stopping %s with priority %s\n", jFullName, j.Config.Priority)
StopJail([]string{jFullName})
jailPri, err := strconv.Atoi(j.Config.Priority)
if err != nil {
panic(fmt.Sprintf("Invalid format for Priority (Jail %s)\n", jFullName))
}
if (curThNb >= gMaxThreads || i == 0) {
// FIXME : Use a pool instead of waiting for all threads to run a new one
wg.Wait()
curThNb = 0
wg.Add(1)
curThNb++
curPri = jailPri
go func(jailFullName string) {
defer wg.Done()
StopJail([]string{jailFullName})
}(jFullName)
} else {
if (curPri == jailPri) {
wg.Add(1)
curThNb++
go func(jailFullName string) {
defer wg.Done()
StopJail([]string{jailFullName})
}(jFullName)
} else {
wg.Wait()
curThNb = 0
wg.Add(1)
curThNb++
curPri = jailPri
go func(jailFullName string) {
defer wg.Done()
StopJail([]string{jailFullName})
}(jFullName)
}
}
}
wg.Wait()
}
/*
@ -228,14 +275,32 @@ func StopJail(args []string) {
fmt.Printf("Error getting jail: %s\n", err)
continue
}
if cj.Running == false {
fmt.Printf("Jail %s is not running!\n", cj.Name)
continue
}
fmt.Printf("> Stopping jail %s\n", a)
// Get current version to update config.json
cvers, err := executeCommandInJail(cj, "/bin/freebsd-version")
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return
}
cvers = strings.TrimRight(cvers, "\n")
//fmt.Sprintf(cj.Config.Release, cvers)
//cj.Config.Release = cvers
//cj.ConfigUpdated = true
// This is working in this context, but value is not available in WriteConfigToDisk context :/
setStructFieldValue(cj, "Config.Release", cvers)
fmt.Printf("DEBUG: release was set, now is : %s\n", cj.Config.Release)
// We need to get the real Config object, not a copy of it
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
if err == nil && len(out) > 0 {
fmt.Printf(" > Remove RCTL rules:\n")
@ -399,5 +464,9 @@ func StopJail(args []string) {
}
}
}
fmt.Printf("DEBUG: release = %s\n", cj.Config.Release)
WriteConfigToDisk(cj.Name, false, true)
}
}

157
cmd/update.go Normal file
View File

@ -0,0 +1,157 @@
package cmd
import (
"os"
"fmt"
//"log"
"time"
)
const (
fbsdUpdateConfig = `
# $FreeBSD: releng/12.2/usr.sbin/freebsd-update/freebsd-update.conf 337338 2018-08-04 22:25:41Z brd $
# Trusted keyprint. Changing this is a Bad Idea unless you've received
# a PGP-signed email from <security-officer@FreeBSD.org> telling you to
# change it and explaining why.
KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5
# Server or server pool from which to fetch updates. You can change
# this to point at a specific server if you want, but in most cases
# using a "nearby" server won't provide a measurable improvement in
# performance.
ServerName update.FreeBSD.org
# Components of the base system which should be kept updated.
Components world
# Example for updating the userland and the kernel source code only:
# Components src/base src/sys world
# Paths which start with anything matching an entry in an IgnorePaths
# statement will be ignored.
IgnorePaths
# Paths which start with anything matching an entry in an IDSIgnorePaths
# statement will be ignored by "freebsd-update IDS".
IDSIgnorePaths /usr/share/man/cat
IDSIgnorePaths /usr/share/man/whatis
IDSIgnorePaths /var/db/locate.database
IDSIgnorePaths /var/log
# Paths which start with anything matching an entry in an UpdateIfUnmodified
# statement will only be updated if the contents of the file have not been
# modified by the user (unless changes are merged; see below).
UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile
# When upgrading to a new FreeBSD release, files which match MergeChanges
# will have any local changes merged into the version from the new release.
MergeChanges /etc/
### Default configuration options:
# Directory in which to store downloaded updates and temporary
# files used by FreeBSD Update.
# WorkDir /var/db/freebsd-update
# Destination to send output of "freebsd-update cron" if an error
# occurs or updates have been downloaded.
# MailTo root
# Is FreeBSD Update allowed to create new files?
# AllowAdd yes
# Is FreeBSD Update allowed to delete files?
# AllowDelete yes
# If the user has modified file ownership, permissions, or flags, should
# FreeBSD Update retain this modified metadata when installing a new version
# of that file?
# KeepModifiedMetadata yes
# When upgrading between releases, should the list of Components be
# read strictly (StrictComponents yes) or merely as a list of components
# which *might* be installed of which FreeBSD Update should figure out
# which actually are installed and upgrade those (StrictComponents no)?
# StrictComponents no
# When installing a new kernel perform a backup of the old one first
# so it is possible to boot the old kernel in case of problems.
# BackupKernel yes
# If BackupKernel is enabled, the backup kernel is saved to this
# directory.
# BackupKernelDir /boot/kernel.old
# When backing up a kernel also back up debug symbol files?
# BackupKernelSymbolFiles no
# Create a new boot environment when installing patches
# CreateBootEnv yes
`
)
// Internal usage only
func updateJail(jail *Jail) error {
// Create default config as temporary file
cfgFile, err := os.CreateTemp("", "gocage-jail-update-")
if err != nil {
return err
}
cfgFile.Write([]byte(fbsdUpdateConfig))
defer cfgFile.Close()
//defer os.Remove(cfgFile.Name())
cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch install",
cfgFile.Name(), jail.RootPath, jail.Config.Release)
fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd)
err = executeCommandWithOutputToStdout(cmd)
if err != nil {
return err
}
// Get and write new release into config.json
return nil
}
func UpdateJail(args []string) {
// Current jail were stopping
var cj *Jail
var err error
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
}
fmt.Printf(" > Snapshot jail %s\n", cj.Name)
// 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)
err := createJailSnapshot(*cj)
if err != nil {
fmt.Printf(" > Snapshot jail %s: ERROR: %s\n", cj.Name, err.Error())
return
}
fmt.Printf(" > Snapshot jail %s: OK\n", cj.Name)
fmt.Printf(" > Update jail %s\n", cj.Name)
err = updateJail(cj)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
} else {
fmt.Printf(" > Update jail %s: OK\n", cj.Name)
}
}
}

View File

@ -21,6 +21,8 @@ import (
const (
ipv4re = `[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}`
ifconfigipv4re = `inet[[:space:]](` + ipv4re + `)`
// Maximum thread qty for start/stop
gMaxThreads = 4
)
/*****************************************************************************
@ -215,7 +217,7 @@ func executeCommand(cmdline string) (string, error) {
// else
word = word + string(c)
}
if len(cmd) > 1 {
out, err = exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
} else {
@ -225,6 +227,144 @@ func executeCommand(cmdline string) (string, error) {
return string(out), err
}
/* From iocage:
* # Courtesy of @william-gr
* # service(8) and some rc.d scripts have the bad h*abit of
* # exec'ing and never closing stdout/stderr. This makes
* # sure we read only enough until the command exits and do
* # not wait on the pipe to close on the other end.
* So this function executes process without waiting after completion
*/
func executeCommandNonBlocking(cmdline string) (error) {
var cmd []string
var oCmd *exec.Cmd
var err error
if gUseSudo {
cmd = append(cmd, "sudo")
}
var word string
var in_escaped bool
// Split by words, or " enclosed words
for i, c := range (cmdline) {
if string(c) == "\"" {
if in_escaped {
// This is the closing "
cmd = append(cmd, word)
in_escaped = false
} else {
in_escaped = true
}
continue
}
if string(c) == " " {
if in_escaped {
word = word + string(c)
continue
} else {
cmd = append(cmd, word)
word = ""
continue
}
}
if i == (len(cmdline) - 1) {
word = word + string(c)
cmd = append(cmd, word)
break
}
// else
word = word + string(c)
}
if len(cmd) > 1 {
oCmd = exec.Command(cmd[0], cmd[1:]...)
} else {
oCmd = exec.Command(cmd[0])
}
if err = oCmd.Start(); err != nil {
return err
}
err = oCmd.Wait()
return err
}
// Executed command outputs to stdout in realtime
func executeCommandWithOutputToStdout(cmdline string) (error) {
var cmd []string
var err error
if gUseSudo {
cmd = append(cmd, "sudo")
}
var word string
var in_escaped bool
// Split by words, or " enclosed words
for i, c := range (cmdline) {
if string(c) == "\"" {
if in_escaped {
// This is the closing "
cmd = append(cmd, word)
in_escaped = false
} else {
in_escaped = true
}
continue
}
if string(c) == " " {
if in_escaped {
word = word + string(c)
continue
} else {
cmd = append(cmd, word)
word = ""
continue
}
}
if i == (len(cmdline) - 1) {
word = word + string(c)
cmd = append(cmd, word)
break
}
// else
word = word + string(c)
}
var execHandle *exec.Cmd
if len(cmd) > 1 {
execHandle = exec.Command(cmd[0], cmd[1:]...)
} else {
execHandle = exec.Command(cmd[0])
}
stdout, err := execHandle.StdoutPipe()
if err != nil {
return err
}
execHandle.Start()
buf := bufio.NewReader(stdout)
for {
line, _, err := buf.ReadLine()
if err != nil {
if err.Error() == "EOF" {
return nil
} else {
return err
}
}
fmt.Println(string(line))
}
return fmt.Errorf("Unknown error: you shouldn't be here!\n")
}
func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
var cmd []string
@ -428,7 +568,7 @@ func doZfsDatasetExist(dataset string) (bool, error) {
}
// Create ZFS dataset. mountpoint can be "none", then the dataset won't be mounted
func createZfsDataset(dataset, mountpoint, compression string) error {
func zfsCreateDataset(dataset, mountpoint, compression string) error {
cmd := fmt.Sprintf("zfs create -o mountpoint=%s -o compression=%s %s", mountpoint, compression, dataset)
out, err := executeCommand(cmd)
if err != nil {
@ -436,6 +576,26 @@ func createZfsDataset(dataset, mountpoint, compression string) error {
}
return nil
}
// Return dataset name for a given mountpoint
func zfsGetDatasetByMountpoint(mountpoint string) (string, error) {
cmd := fmt.Sprintf("zfs list -p -r -H -o name %s", mountpoint)
out, err := executeCommand(cmd)
if err != nil {
return "", errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
}
return strings.TrimSuffix(out, "\n"), nil
}
// Delete a ZFS Dataset by name
func zfsDestroy(dataset string) error {
cmd := fmt.Sprintf("zfs destroy -r %s", dataset)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
}
return nil
}
/*****************************************************************************
*
@ -544,7 +704,7 @@ func copyDevfsRuleset(ruleset int, srcrs int) error {
/********************************************************************************
* Returns value of parameter as read in /var/run/jail.$InternalName.conf
* Directoves without value will return "true" if found
* Directives without value will return "true" if found
* Returns an error if parameter not found in file
*******************************************************************************/
func getValueFromRunningConfig(jname string, param string) (string, error) {
@ -632,7 +792,8 @@ func getJailFromArray(name string, jarray []Jail) (*Jail, error) {
}
for i, j := range jarray {
if jail == j.Name {
//if jail == j.Name {
if strings.HasPrefix(j.Name, jail) {
if len(ds) > 0 {
if strings.EqualFold(ds, j.Datastore) {
return &jarray[i], nil
@ -646,7 +807,7 @@ func getJailFromArray(name string, jarray []Jail) (*Jail, error) {
}
if len(jails) > 0 {
if len(jails) > 1 {
return &Jail{}, errors.New("More than one jail found with this name, please use datastore/jail format")
return &Jail{}, errors.New("More than one jail matching, please use datastore/jail format or full name")
} else {
return &jails[0], nil
}
@ -677,7 +838,7 @@ func setJailConfigUpdated(jail *Jail) error {
return err
}
j.ConfigUpdated = true
return nil
}