Compare commits
5 Commits
7c3e14f0f1
...
v0.34
Author | SHA1 | Date | |
---|---|---|---|
dc4213a8d5 | |||
5eed121f0b | |||
812c77790a | |||
7575da794e | |||
6f9bb504be |
3
CHANGELOG
Normal file
3
CHANGELOG
Normal file
@ -0,0 +1,3 @@
|
||||
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
|
10
cmd/root.go
10
cmd/root.go
@ -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.33b"
|
||||
gVersion = "0.34"
|
||||
|
||||
// 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",
|
||||
|
65
cmd/start.go
65
cmd/start.go
@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
"errors"
|
||||
"regexp"
|
||||
@ -475,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")
|
||||
@ -508,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
|
||||
}
|
||||
@ -1055,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 {
|
||||
@ -1072,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()
|
||||
}
|
||||
|
||||
|
||||
@ -1299,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
|
||||
@ -1423,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")
|
||||
}
|
||||
|
66
cmd/stop.go
66
cmd/stop.go
@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"fmt"
|
||||
//"log"
|
||||
"sync"
|
||||
"errors"
|
||||
"regexp"
|
||||
"os/exec"
|
||||
@ -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()
|
||||
}
|
||||
|
||||
/*
|
||||
@ -243,8 +290,16 @@ func StopJail(args []string) {
|
||||
return
|
||||
}
|
||||
cvers = strings.TrimRight(cvers, "\n")
|
||||
fmt.Sprintf(cj.Config.Release, cvers)
|
||||
cj.ConfigUpdated = true
|
||||
|
||||
//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 {
|
||||
@ -410,6 +465,7 @@ func StopJail(args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("DEBUG: release = %s\n", cj.Config.Release)
|
||||
WriteConfigToDisk(cj.Name, false, true)
|
||||
|
||||
}
|
||||
|
77
cmd/utils.go
77
cmd/utils.go
@ -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,72 @@ 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
|
||||
@ -616,7 +684,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) {
|
||||
@ -704,7 +772,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
|
||||
@ -718,7 +787,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
|
||||
}
|
||||
|
Reference in New Issue
Block a user