Add start and stop all jails for boot/shutdown, add debug mode

This commit is contained in:
yo 2022-10-15 14:53:43 +02:00
parent 38266af66e
commit c97f5317dd
3 changed files with 80 additions and 5 deletions

View File

@ -10,11 +10,11 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
// TODO : Use log
//log "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
const (
gVersion = "0.29g"
gVersion = "0.30"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
@ -27,6 +27,7 @@ var (
gUseSudo bool
gForce bool
gDebug bool
gConfigFile string
gDisplayJColumns string
@ -109,7 +110,11 @@ ex: gocage list srv-db srv-web`,
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
StopJail(args)
if len(args) == 0 {
StopAllRunningJails()
} else {
StopJail(args)
}
},
}
@ -119,11 +124,17 @@ ex: gocage list srv-db srv-web`,
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
StartJail(args)
if len(args) == 0 {
StartJailsAtBoot()
} else {
StartJail(args)
}
WriteConfigToDisk(false)
},
}
restartCmd = &cobra.Command{
Use: "restart",
Short: "restart jail",
@ -293,6 +304,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/usr/local/etc/gocage.conf.yml", "GoCage configuration file")
rootCmd.PersistentFlags().BoolVarP(&gUseSudo, "sudo", "u", false, "Use sudo to run commands")
rootCmd.PersistentFlags().StringVarP(&gTimeZone, "timezone", "t", "", "Specify timezone. Will get from /var/db/zoneinfo if not set.")
rootCmd.PersistentFlags().BoolVarP(&gDebug, "debug", "d", false, "Debug mode")
// Command dependant switches
@ -414,6 +426,12 @@ func initConfig() {
fmt.Printf("More than 3 sort criteria is not supported!\n")
os.Exit(1)
}
if gDebug {
log.SetLevel(log.DebugLevel)
log.Debugf("Debug mode enabled\n")
}
}
/********************************************************************************

View File

@ -1049,6 +1049,34 @@ func cleanAfterStartCrash() {
}
// Start all jails with boot=true, in priority order
func StartJailsAtBoot() {
var startList []Jail
// Get boot enabled jails
for _, j := range gJails {
if j.Config.Boot > 0 {
startList = append(startList, j)
}
}
// Order by priority
js := initJailSortStruct()
fct, _, err := getStructFieldValue(js, "Config.PriorityInc")
if err != nil {
log.Errorf("ERROR getting JailSort struct field \"Config.PriorityInc\"\n")
return
}
JailsOrderedBy(fct.Interface().(jailLessFunc)).Sort(startList)
for _, 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})
}
}
/*
Start jail:
Check jail fstab?
@ -1088,7 +1116,7 @@ func StartJail(args []string) {
}
if cj.Running == true {
fmt.Printf("Jail %s is already running!\n", cj.Name)
fmt.Printf("Jail %s/%s is already running!\n", cj.Datastore, cj.Name)
continue
}

View File

@ -10,6 +10,8 @@ import (
//"reflect"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
// TODO : Use SYS_RCTL_GET_RACCT syscall
@ -166,6 +168,33 @@ func stopJail(jail *Jail) error {
return nil
}
// Stop all running jails by reverse priority
func StopAllRunningJails() {
var stopList []Jail
// Get boot enabled jails
for _, j := range gJails {
if j.Running == true {
stopList = append(stopList, j)
}
}
// Order by priority
js := initJailSortStruct()
fct, _, err := getStructFieldValue(js, "Config.PriorityDec")
if err != nil {
log.Errorf("ERROR getting JailSort struct field \"Config.PriorityDec\"\n")
return
}
JailsOrderedBy(fct.Interface().(jailLessFunc)).Sort(stopList)
for _, 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})
}
}
/*
Stop jail:
Remove rctl rules