gocage console jailname now working

This commit is contained in:
yo 2022-06-18 11:08:03 +02:00
parent 0bf825ee5a
commit a446a19a08
2 changed files with 40 additions and 32 deletions

View File

@ -1,39 +1,39 @@
package cmd
import (
"os"
"fmt"
"log"
"strconv"
"strings"
"syscall"
)
func ShellJail(args []string) error {
var jailnames []string
// We cant shell more than one jail bc we replace gocage execution with jexec, so there wont be no return to gocage
if len(args) > 0 {
for _, a := range args {
jailnames = append(jailnames, a)
}
}
for _, cj := range gJails {
for _, jn := range jailnames {
if strings.EqualFold(cj.Name, jn) {
err := shellJail(cj)
if err != nil {
return err
}
if strings.EqualFold(cj.Name, args[0]) {
shellJail(cj)
}
}
}
fmt.Printf("Jail not found: %s\n", args[0])
return nil
}
func shellJail(jail Jail) error {
cmd := fmt.Sprintf("jexec %s /bin/csh", jail.JID)
_, err := executeCommand(cmd)
func shellJail(jail Jail) {
jid := strconv.Itoa(jail.JID)
err := syscall.Exec("/usr/sbin/jexec", []string{"jexec", jid, "/bin/csh"}, os.Environ())
// We should never get here, as syscall.Exec replace the gocage binary execution with jexec
// This means the moment syscall.Exec fires, gocage execution halt.
if err != nil {
fmt.Errorf("Error executing %s: %s", cmd, err.Error())
return err
log.Printf("Exec returned %v\n", err)
}
return nil
return
}

View File

@ -15,7 +15,12 @@ import (
)
const (
gVersion = "0.27c"
gVersion = "0.27d"
DEVFS_DEFAULT_RULESET = 4
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
)
var (
@ -35,6 +40,7 @@ var (
gSnapshotName string
gMigrateDestPool string
gYesToAll bool
rootCmd = &cobra.Command{
Use: "gocage",
@ -98,7 +104,7 @@ ex: gocage list srv-db srv-web`,
},
}
/* shellCmd = &cobra.Command {
shellCmd = &cobra.Command {
Use: "console",
Short: "Execute shell on jail",
Run: func(cmd *cobra.Command, args []string) {
@ -107,7 +113,7 @@ ex: gocage list srv-db srv-web`,
ShellJail(args)
},
}
*/
setCmd = &cobra.Command{
Use: "set",
Short: "Set a jail property",
@ -194,7 +200,7 @@ You can specify multiple jails.`,
migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate jail to another zpool",
Short: "Migrate jail to another zfs pool",
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
@ -241,6 +247,7 @@ func init() {
snapshotRollbackCmd.MarkFlagRequired("snapname")
migrateCmd.Flags().StringVarP(&gMigrateDestPool, "destpool", "d", "", "Name of zfs destination pool for jail")
migrateCmd.Flags().BoolVarP(&gYesToAll, "yes", "y", false, "Answer yes to all questions")
migrateCmd.MarkFlagRequired("destpool")
// Now declare commands
@ -249,6 +256,7 @@ func init() {
listCmd.AddCommand(listPropsCmd)
rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(shellCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(snapshotCmd)