Compare commits

...

9 Commits

9 changed files with 121 additions and 70 deletions

View File

@ -22,6 +22,14 @@ gocage create jail1 -r 13.2-RELEASE
gocage set Config.Ip4_addr="vnet0|192.168.1.91/24" Config.Vnet=1 jail1
</code></pre>
Create basejail (jail based on a template, system will be nullfs read-only mounted from the template):
<pre><code>
# First create basetpl from 13.2-RELEASE
gocage create -r 13.2-RELEASE basetpl
# Then create basejail1 based on basetpl
gocage create -b basetpl basejail1
</code></pre>
List jails
----------

View File

@ -12,7 +12,7 @@ import (
func ShellJail(args []string) error {
// 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 {
cj, err := getJailFromArray(args[0], []string{"jail"}, gJails)
cj, err := getJailFromArray(args[0], []string{"basejail", "jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail %s: %v\n", args[0], err)
return err

View File

@ -5,6 +5,7 @@ import (
"fmt"
//"log"
"time"
"errors"
"strings"
cp "github.com/otiai10/copy"
log "github.com/sirupsen/logrus"
@ -85,15 +86,24 @@ func CreateJail(args []string) {
fmt.Printf(" > Create base read-only directories\n")
dstRootDir := fmt.Sprintf("%s/jails/%s/root", ds.Mountpoint, jname)
for _, d := range append(gBaseDirs, gEmptyDirs...) {
srcPerm, err := getPermissions(fmt.Sprintf("%s/%s", bj.RootPath, d))
if err != nil {
fmt.Printf("ERROR getting permissions of %s/%s: %s\n", bj.RootPath, d, err.Error())
return
}
err = os.Mkdir(fmt.Sprintf("%s/%s", dstRootDir, d), srcPerm.Mode().Perm())
if err != nil {
fmt.Printf("ERROR creating directory %s/%s: %s\n", dstRootDir, d, err.Error())
return
dstPath := dstRootDir
srcPath := bj.RootPath
for _, cd := range strings.Split(d, "/") {
srcPath = fmt.Sprintf("%s/%s", srcPath, cd)
dstPath = fmt.Sprintf("%s/%s", dstPath, cd)
_, err := os.Stat(dstPath)
if errors.Is(err, os.ErrNotExist) {
srcPerm, err := getPermissions(srcPath)
if err != nil {
fmt.Printf("ERROR getting permissions of %s: %s\n", srcPath, err.Error())
return
}
err = os.Mkdir(dstPath, srcPerm.Mode().Perm())
if err != nil {
fmt.Printf("ERROR creating directory %s: %s\n", dstPath, err.Error())
return
}
}
}
}
@ -154,6 +164,11 @@ func CreateJail(args []string) {
return
}
defer fstabHandle.Close()
for _, d := range gBaseDirs {
fmt.Fprintf(fstabHandle, "%s\t%s\tnullfs\tro\t0\t0\n", fmt.Sprintf("%s/%s", bj.RootPath, d), fmt.Sprintf("%s/%s", dstRootDir, d))
}
fmt.Printf(" > Jail created!\n")
} else {
/**************************************************************************
@ -252,6 +267,7 @@ func CreateJail(args []string) {
j.Config.Release = gCreateArgs.Release
j.Config.Host_hostname = jname
j.Config.Host_hostuuid = jname
j.Config.Jailtype = "jail"
j.WriteConfigToDisk(false)
@ -265,6 +281,5 @@ func CreateJail(args []string) {
defer fstabHandle.Close()
fmt.Printf(" > Jail created!\n")
}
// TODO : Set JailType
}
}

View File

@ -52,7 +52,7 @@ func ListJails(args []string, display bool) {
for _, ds := range gDatastores {
listJailsFromDatastore(ds, args, display)
}
// Only when displaying jails, we accept to process multiple same name jails
if false == display {
for _, j := range gJails {
@ -68,11 +68,11 @@ func ListJails(args []string, display bool) {
break
}
}
if true == skip {
continue
}
// Initialize if not found in nameChecked
if false == found {
curCheck = &uniqueJailName{jail: j.Name,
@ -81,11 +81,11 @@ func ListJails(args []string, display bool) {
} else {
found = false
}
if countOfJailsWithThisName(j.Name) > 1 {
//fmt.Printf("DEBUG: Jail %s exist multiple times, now checking if specified with full name\n", j.Name)
curCheck.unique = false
for _, a := range args {
//fmt.Printf("DEBUG: comparing %s/%s with %s\n", j.Datastore, j.Name, a)
if strings.EqualFold(a, fmt.Sprintf("%s/%s", j.Datastore, j.Name)) {
@ -96,7 +96,7 @@ func ListJails(args []string, display bool) {
}
nameChecked = append(nameChecked, curCheck)
}
// Now check
for _, a := range args {
for _, n := range nameChecked {
@ -107,9 +107,9 @@ func ListJails(args []string, display bool) {
}
}
}
fields := strings.Split(gDisplayJColumns, ",")
// This is the structure we will filter, then display
var jails []Jail
@ -275,6 +275,13 @@ func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
// FIXME ??? Shouldn't be ioc-$Name ?
j.InternalName = rj.Name
j.Devfs_ruleset = rj.Devfs_ruleset
// Update release
r, err := getVersion(&j)
if err != nil {
fmt.Printf("ERROR getting jail %s version: %s\n", j.Name, err.Error())
} else {
j.Config.Release = r
}
break
}
}

View File

@ -14,7 +14,7 @@ import (
)
const (
gVersion = "0.37a"
gVersion = "0.38"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
@ -66,8 +66,8 @@ var (
gUpgradeRelease string
// For a based jail, these are directories binded to basejail
gBaseDirs = []string{"bin", "boot", "lib", "libexec", "rescue", "sbin", "usr", "usr/bin",
"usr/include", "usr/lib", "usr/lib32", "usr/libdata", "usr/libexec", "usr/sbin", "usr/share"}
gBaseDirs = []string{"bin", "boot", "lib", "libexec", "rescue", "sbin", "usr/bin", "usr/include",
"usr/lib", "usr/lib32", "usr/libdata", "usr/libexec", "usr/sbin", "usr/share"}
// These directories are to be created empty
gEmptyDirs = []string{"dev", "media", "mnt", "net", "proc"}
// Copy these from base template

View File

@ -899,7 +899,7 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) {
return epairs, nil
}
func setupVnetInterfaceJailSide(jail *Jail) error {
func setupVnetInterfaceJailSide(jail *Jail, hostepairs []string) error {
var jsmac []byte
var err error
@ -919,7 +919,7 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
}
// Loop through configured interfaces
for _, nicCnf := range strings.Split(jail.Config.Interfaces, ",") {
for i, nicCnf := range strings.Split(jail.Config.Interfaces, ",") {
v := strings.Split(nicCnf, ":")
if len(v) != 2 {
return fmt.Errorf("Invalid value for Interfaces: %s\n", nicCnf)
@ -930,6 +930,8 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
// inside jail final nic name
jnic := strings.Replace(v[0], "vnet", "epair", 1)
jnic = jnic + "b"
// host side associated jail nic name
jsepair := fmt.Sprintf("%sb", strings.TrimSuffix(hostepairs[i], "a"))
// Get jail side MAC
pname := fmt.Sprintf("Config.%s_mac", nic)
@ -948,7 +950,7 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
jsmac = val.Bytes()
}
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jnic, jail.InternalName)
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jsepair, jail.InternalName)
_, err := executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err)
@ -960,14 +962,14 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
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, jnic, mtu)
cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jsepair, mtu)
_, err = executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error setting mtu: %v\n", err)
}
// rename epairXXb to epair0b (or opair1b, ...)
cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jnic, jnic)
cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jsepair, jnic)
_, err = executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err)
@ -1070,9 +1072,9 @@ func StartJailsAtBoot() {
var curThNb int
var curPri int
// Get boot enabled jails
// Get boot enabled non-template jails
for _, j := range gJails {
if j.Config.Boot > 0 {
if j.Config.Boot > 0 && !strings.EqualFold(j.Config.Jailtype, "template") {
startList = append(startList, j)
}
}
@ -1166,7 +1168,7 @@ func StartJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, []string{"jail"}, gJails)
cj, err = getJailFromArray(a, []string{"basejail", "jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
@ -1178,7 +1180,7 @@ func StartJail(args []string) {
}
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)
@ -1340,34 +1342,34 @@ func StartJail(args []string) {
cj.Config.Defaultrouter = ip4[0]
}
}
// See https://github.com/iocage/iocage/blob/e94863d4c54f02523fb09e62e48be7db9ac92eda/iocage_lib/ioc_start.py:401
if cj.Config.Vnet == 0 {
// Not supported
fmt.Printf("Only VNet jails supported\n")
return
}
var net []string
if false == strings.EqualFold(cj.Config.Vnet_interfaces, "none") {
net = append(net, strings.Split(cj.Config.Vnet_interfaces, " ")...)
}
err, dynrs := buildDevfsRuleSet(cj, &gMdevfs)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
err = buildJailParameters(cj, dynrs)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
// Synchronize jail config to disk
writeConfigToDisk(cj, false)
cj.WriteConfigToDisk(false)
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
//TODO: handle start_env & prestart_env, could be used by iocage plugins
@ -1377,17 +1379,17 @@ func StartJail(args []string) {
fmt.Printf("Aborting jail start\n")
return
}
fmt.Printf(" > Start jail:\n")
_, err = executeCommand(start_cmd)
if err != nil {
fmt.Printf("Error starting jail %s: %v\n", cj.Name, err)
return
}
fmt.Printf(" > Start jail: OK\n")
fmt.Printf(" > With devfs ruleset %d\n", dynrs)
// Update running state, JID and Devfs_ruleset
cj.Running = true
cj.Devfs_ruleset = dynrs
@ -1401,13 +1403,13 @@ func StartJail(args []string) {
break
}
}
hostInt, err := gJailHost.GetInterfaces()
if err != nil {
fmt.Printf("Error listing jail host interfaces: %v\n", err)
return
}
if false == strings.EqualFold(cj.Config.Vnet_default_interface, "auto") &&
false == strings.EqualFold(cj.Config.Vnet_default_interface, "none") &&
false == isStringInArray(hostInt, cj.Config.Vnet_default_interface) {
@ -1416,13 +1418,13 @@ func StartJail(args []string) {
}
fmt.Printf(" > Setup VNet network:\n")
_, err = setupVnetInterfaceHostSide(cj);
hsepairs, err := setupVnetInterfaceHostSide(cj);
if err != nil {
fmt.Printf("Error setting VNet interface host side: %v\n", err)
return
}
if err = setupVnetInterfaceJailSide(cj); err != nil {
if err = setupVnetInterfaceJailSide(cj, hsepairs); err != nil {
fmt.Printf("Error setting VNet interface jail side: %v\n", err)
return
}
@ -1439,7 +1441,7 @@ func StartJail(args []string) {
fmt.Printf(" > Setup default ipv4 gateway: OK\n")
}
}
if cj.Config.Ip6_addr != "none" {
fmt.Printf(" > Setup default ipv6 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add -6 default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter6)
@ -1450,7 +1452,7 @@ func StartJail(args []string) {
fmt.Printf(" > Setup default ipv6 gateway: OK\n")
}
}
if cj.Config.Jail_zfs > 0 {
fmt.Printf(" > Jail ZFS datasets:\n")
err = jailZfsDatasets(cj)
@ -1465,14 +1467,14 @@ func StartJail(args []string) {
if err != nil {
fmt.Printf("%s\n", err)
}
if cj.Config.Host_time > 0 {
err = copyLocalTime(cj)
if err != nil {
fmt.Printf("%s\n", err)
}
}
// Start services
if len(cj.Config.Exec_start) > 0 {
fmt.Printf(" > Start services:\n")
@ -1484,7 +1486,7 @@ func StartJail(args []string) {
fmt.Printf(" > Start services: OK\n")
}
}
if cj.Config.Rtsold > 0 || strings.EqualFold(cj.Config.Ip6_addr, "accept_rtadv") {
fmt.Printf(" > Start rtsold:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d service rtsold start", cj.Config.Exec_fib, cj.JID)
@ -1495,7 +1497,7 @@ func StartJail(args []string) {
fmt.Printf(" > Start rtsold: OK\n")
}
}
// TODO: Execute Exec_poststart
if len(cj.Config.Exec_poststart) > 0 {
fmt.Printf(" > Execute post-start:\n")
@ -1507,19 +1509,18 @@ func StartJail(args []string) {
fmt.Printf(" > Execute post-start: OK\n")
}
}
// WIP 10/07/2022 : https://github.com/iocage/iocage/blob/master/iocage_lib/ioc_start.py#L891
// TODO: Handle dhcp
// TODO: Apply rctl
// Update last_started
// 23/07/2023 : This is not working, when writing to disk the old value is used
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
cj.Config.Last_started = curDate
writeConfigToDisk(cj, false)
/*
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
if err == nil && len(out) > 0 {

View File

@ -123,7 +123,7 @@ func deleteDevfsRuleset(ruleset int) error {
return nil
}
func umountJailFsFromHost(jail *Jail, mountpoint string) error {
func umountFsFromHost(mountpoint string) error {
cmd := "mount -p"
out, err := executeCommand(cmd)
if err != nil {
@ -134,11 +134,11 @@ func umountJailFsFromHost(jail *Jail, mountpoint string) error {
for _, l := range strings.Split(out, "\n") {
f := strings.Split(remSpPtrn.ReplaceAllString(l, " "), " ")
if len(f) > 2 {
if strings.EqualFold(f[1], fmt.Sprintf("%s%s", jail.RootPath, mountpoint)) {
cmd = fmt.Sprintf("umount %s%s", jail.RootPath, mountpoint)
if strings.EqualFold(f[1], mountpoint) {
cmd = fmt.Sprintf("umount %s", mountpoint)
_, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error umounting %s%s: %s", jail.RootPath, mountpoint, err.Error()))
return errors.New(fmt.Sprintf("Error umounting %s: %s", mountpoint, err.Error()))
}
return nil
}
@ -148,6 +148,10 @@ func umountJailFsFromHost(jail *Jail, mountpoint string) error {
return nil
}
func umountJailFsFromHost(jail *Jail, mountpoint string) error {
return umountFsFromHost(fmt.Sprintf("%s%s", jail.RootPath, mountpoint))
}
// Internal usage only
func stopJail(jail *Jail) error {
cmd := "jail -q"
@ -270,7 +274,7 @@ func StopJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, []string{"jail"}, gJails)
cj, err = getJailFromArray(a, []string{"basejail", "jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
@ -404,7 +408,8 @@ func StopJail(args []string) {
fmt.Printf(" > Umount mountpoints from %s\n", fstab)
errs := 0
for _, m := range mounts {
err = umountJailFsFromHost(cj, m.Mountpoint)
log.Debugf("Umounting %s\n", m.Mountpoint)
err = umountFsFromHost(m.Mountpoint)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
errs += 1

View File

@ -21,7 +21,7 @@ func updateJail(jail *Jail) error {
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Folder containing update/uipgrade temporary files. Common so we save bandwith when upgrading multiple jails
// Folder containing update/upgrade temporary files. Common so we save bandwith when upgrading multiple jails
// TODO: Variabilize /iocage/freebsd-update
_, err = os.Stat("/iocage/freebsd-update")
if os.IsNotExist(err) {
@ -30,11 +30,15 @@ func updateJail(jail *Jail) error {
}
}
cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch install",
cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch",
cfgFile.Name(), jail.RootPath, jail.Config.Release)
err = executeCommandWithOutputToStdout(cmd)
if err != nil {
return err
}
//fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd)
cmd = fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s install",
cfgFile.Name(), jail.RootPath, jail.Config.Release)
err = executeCommandWithOutputToStdout(cmd)
if err != nil {
return err

View File

@ -474,6 +474,8 @@ func executeCommandWithOutputToStdout(cmdline string) (error) {
word = word + string(c)
}
log.Debugf("executeCommandWithOutputToStdout: will execute \"%s\"\n", strings.Join(cmd, " "))
var execHandle *exec.Cmd
if len(cmd) > 1 {
execHandle = exec.Command(cmd[0], cmd[1:]...)
@ -1062,7 +1064,7 @@ func getJailFromArray(name string, jailtypes []string, jarray []Jail) (*Jail, er
var jails []Jail
if (len(jailtypes) == 1 && len(jailtypes[0]) == 0) || len(jailtypes) == 0 {
jailtypes = []string{"jail", "basetpl"}
jailtypes = []string{"basejail", "jail", "template"}
}
if strings.Contains(name, "/") {
@ -1128,16 +1130,25 @@ func setJailConfigUpdated(jail *Jail) error {
return nil
}
func getVersion(jail *Jail) (string, error) {
cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath))
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return "", err
}
return strings.TrimRight(cvers, "\n"), nil
}
func updateVersion(jail *Jail) error {
cvers, err := executeCommandInJail(jail, "/bin/freebsd-version")
cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath))
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return err
}
cvers = strings.TrimRight(cvers, "\n")
jail.Config.Release = cvers
writeConfigToDisk(jail, false)
jail.WriteConfigToDisk(false)
return nil
}