func restartCmdRun(cmd *cobra.Command, args []string) { // requires root if !core.IsRoot() { gologit.Fatalf("Must be root to stop\n") } jail, err := core.FindJail(args[0]) if err != nil { gologit.Fatalf("No jail found by '%s'\n", args[0]) } if !jail.IsRunning() { gologit.Fatalf("Jail is not running!\n") } // create file f, err := os.OpenFile(jail.GetLogPath(), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { gologit.Fatal(err) } defer f.Close() props := jail.GetProperties() jexec := []string{fmt.Sprintf("ioc-%s", jail.HostUUID)} jexec_stop := append(jexec, core.SplitFieldsQuoteSafe(props.GetIOC("exec_stop"))...) excmd := exec.Command("/usr/sbin/jexec", jexec_stop...) excmd.Stdout = f excmd.Stderr = f err = excmd.Run() if err != nil { gologit.Printf("%s\n", err) } jexec_start := append(jexec, core.SplitFieldsQuoteSafe(props.GetIOC("exec_start"))...) excmd = exec.Command("/usr/sbin/jexec", jexec_start...) excmd.Stdout = f excmd.Stderr = f err = excmd.Run() if err != nil { gologit.Printf("%s\n", err) } // set last_started property t := time.Now() core.ZFSMust( fmt.Errorf("Error setting property"), "set", fmt.Sprintf( "org.freebsd.iocage:last_started=%s", t.Format("2006-01-02_15:04:05")), jail.Path) }
func consoleCmdRun(cmd *cobra.Command, args []string) { // requires root if !core.IsRoot() { gologit.Fatalf("Must be root to use console\n") } jail, err := core.FindJail(args[0]) if err != nil { gologit.Fatalf("No jail found by '%s'\n", args[0]) } if !jail.IsRunning() { gologit.Fatalf("Jail is not running!\n") } // get exec fib property lines := core.SplitOutput(core.ZFSMust( fmt.Errorf("Error listing jails"), "list", "-H", "-o", "org.freebsd.iocage:login_flags,org.freebsd.iocage:exec_fib", jail.Path)) loginFlags := lines[0][0] execFib := lines[0][1] jexec := []string{} if execFib != "0" { jexec = append(jexec, "/usr/sbin/setfib", execFib) } jexec = append(jexec, "/usr/sbin/jexec", fmt.Sprintf("ioc-%s", jail.HostUUID), "login") jexec = append(jexec, core.SplitFieldsQuoteSafe(loginFlags)...) // set a default path environ := []string{ "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin", } // set a term from caller environ = append(environ, fmt.Sprintf("TERM=%s", os.Getenv("TERM"))) gologit.Debugf("%#s\n", jexec) execErr := syscall.Exec(jexec[0], jexec, environ) if execErr != nil { gologit.Fatal(execErr) } }
func stopCmdRun(cmd *cobra.Command, args []string) { // requires root if !core.IsRoot() { gologit.Fatalf("Must be root to stop\n") } jail, err := core.FindJail(args[0]) if err != nil { gologit.Fatalf("No jail found by '%s'\n", args[0]) } if !jail.IsRunning() { gologit.Fatalf("Jail is not running!\n") } propertyList := []string{ "mountpoint", "org.freebsd.iocage:type", "org.freebsd.iocage:tag", "org.freebsd.iocage:prestop", "org.freebsd.iocage:exec_stop", "org.freebsd.iocage:poststop", "org.freebsd.iocage:vnet", "org.freebsd.iocage:ip4", } lines := core.SplitOutput(core.ZFSMust( fmt.Errorf("Error listing properties"), "list", "-H", "-o", strings.Join(propertyList, ","), jail.Path)) if len(lines) < 1 { gologit.Fatalf("No output from property fetch\n") } prop_mountpoint := removeDash(lines[0][0]) //prop_type := removeDash(lines[0][1]) prop_tag := removeDash(lines[0][2]) prop_prestop := removeDash(lines[0][3]) prop_exec_stop := removeDash(lines[0][4]) prop_poststop := removeDash(lines[0][5]) prop_vnet := removeDash(lines[0][6]) prop_ip4 := removeDash(lines[0][7]) // set a default path environ := []string{ "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin", } fmt.Printf("* Stopping %s (%s)\n", jail.HostUUID, prop_tag) if prop_prestop != "" { fmt.Printf(" + Running pre-stop\n") preStop := core.SplitFieldsQuoteSafe(prop_prestop) excmd := exec.Command(preStop[0], preStop[1:]...) excmd.Env = environ err := excmd.Run() if err != nil { gologit.Printf("%s\n", err) } } fmt.Printf(" + Stopping services\n") jexec := []string{"/usr/sbin/jexec"} jexec = append(jexec, fmt.Sprintf("ioc-%s", jail.HostUUID)) jexec = append(jexec, core.SplitFieldsQuoteSafe(prop_exec_stop)...) out, err := exec.Command(jexec[0], jexec[1:]...).CombinedOutput() gologit.Debugln(string(out)) if err != nil { gologit.Printf("%s\n", err) } if prop_vnet == "on" { fmt.Printf(" + Tearing down VNET\n") // stop VNET networking } else if prop_ip4 != "inherit" { // stop standard networking (legacy?) lines := core.SplitOutput(core.ZFSMust( fmt.Errorf("Error listing jails"), "list", "-H", "-o", "org.freebsd.iocage:ip4_addr,org.freebsd.iocage:ip6_addr", jail.Path)) for _, ip_config := range lines[0] { if ip_config == "none" { continue } for _, addr := range strings.Split(ip_config, ",") { item := strings.Split(addr, "|") gologit.Debugln("/sbin/ifconfig", item[0], item[1], "-alias") out, err := exec.Command("/sbin/ifconfig", item[0], item[1], "-alias").CombinedOutput() gologit.Debugln(string(out)) if err != nil { gologit.Printf("%s\n", err) } } } } fmt.Printf(" + Removing jail process\n") jrexec := []string{"/usr/sbin/jail", "-r", fmt.Sprintf("ioc-%s", jail.HostUUID)} out, err = exec.Command(jrexec[0], jrexec[1:]...).CombinedOutput() if err != nil { gologit.Printf("%s\n", err) } if prop_poststop != "" { fmt.Printf(" + Running post-stop\n") postStop := core.SplitFieldsQuoteSafe(prop_poststop) excmd := exec.Command(postStop[0], postStop[1:]...) excmd.Env = environ err := excmd.Run() if err != nil { gologit.Printf("%s\n", err) } } fmt.Printf(" + Tearing down mounts\n") umountCmd("-afvF", path.Join(prop_mountpoint, "fstab")) umountCmd(path.Join(prop_mountpoint, "root/dev/fd")) umountCmd(path.Join(prop_mountpoint, "root/dev")) umountCmd(path.Join(prop_mountpoint, "root/proc")) // TODO: basejail here? // TODO: rctl stuff here... }