Compare commits
	
		
			36 Commits
		
	
	
		
			v0.37
			...
			7ca633d946
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 7ca633d946 | |||
| 199b3b1f7f | |||
| 372e92fc41 | |||
| 2afccc5a0e | |||
| 3d77209b53 | |||
| 5ce62bc7de | |||
| 0e0ab8c653 | |||
| 3b548e4c85 | |||
| 9d7db268cc | |||
| 4cc1c476aa | |||
| ce79783540 | |||
| 18d35b9224 | |||
| f41c93368d | |||
| 452b0e4b4e | |||
| dbe9622a01 | |||
| 6ead474a78 | |||
| 4edd0b7414 | |||
| b54ebfd915 | |||
| 69665fdcef | |||
| 7e1c213ff4 | |||
| 26ceb1630a | |||
| 87e9ae894a | |||
| ed5f8f0b1c | |||
| c55262690a | |||
| c5aa547e5d | |||
| cdcb466417 | |||
| bb3136c9ef | |||
| 9208102c84 | |||
| fce64b2939 | |||
| 44b877eae1 | |||
| c2277ce10c | |||
| 549d517cf9 | |||
| 14984f417c | |||
| 346fd52a8e | |||
| 684d97cc21 | |||
| a3dd0a7aa2 | 
@ -8,11 +8,11 @@ Gocage can handle multiple datastores, so you can have jails on HDD storage and
 | 
			
		||||
 | 
			
		||||
From v0.33b, due to multi ZFS pool support, gocage is no longer 100% compatible with iocage.  
 | 
			
		||||
Zfs datasets now should be specified with the ZFS pool. e.g. :  
 | 
			
		||||
<code>
 | 
			
		||||
<pre><code>
 | 
			
		||||
Config.Jail_zfs = 1
 | 
			
		||||
Config.Jail_zfs_dataset = myzfspool/poudriere
 | 
			
		||||
Config.Jail_zfs_mountpoint = none
 | 
			
		||||
</code>
 | 
			
		||||
</code></pre>
 | 
			
		||||
 | 
			
		||||
Create jails
 | 
			
		||||
------------
 | 
			
		||||
@ -22,6 +22,11 @@ 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 release, system will be nullfs read-only mounted from the release directory):
 | 
			
		||||
<pre><code>
 | 
			
		||||
gocage create -b -r 14.0-RELEASE basejail1
 | 
			
		||||
</code></pre>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
List jails
 | 
			
		||||
----------
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								TODO.md
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								TODO.md
									
									
									
									
									
								
							@ -1,8 +1,19 @@
 | 
			
		||||
Replicating jails between two servers (use zrepl)
 | 
			
		||||
Manage remote jails :
 | 
			
		||||
  - Make gocage a service
 | 
			
		||||
  - All commands should become API endpoint
 | 
			
		||||
  - How to handle authentication ?
 | 
			
		||||
 | 
			
		||||
DEBUG: 
 | 
			
		||||
- cmd/list.go:275:
 | 
			
		||||
    // FIXME ??? Shouldn't be ioc-$Name ?
 | 
			
		||||
    j.InternalName = rj.Name
 | 
			
		||||
- WriteConfigToDisk don't write neither "release" in cmd stop neither "last_started" in cmd start
 | 
			
		||||
  26/08/2023 : Last_started is updated
 | 
			
		||||
 | 
			
		||||
BUGS:
 | 
			
		||||
- unable to set values containing equal sign :
 | 
			
		||||
  # gocage set Config.Exec_poststart="jail -m allow.mount.linprocfs=1 name=ioc-poudriere-noo" poudriere-noo
 | 
			
		||||
  Error parsing args: Config.Exec_poststart=jail -m allow.mount.linprocfs=1 name=ioc-poudriere-noo
 | 
			
		||||
- Fix fstab when migrating jail  
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										96
									
								
								cmd/api.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								cmd/api.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,96 @@
 | 
			
		||||
package cmd
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	//"io"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"github.com/gin-contrib/cors"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func printJails(jails []string, fields []string) ([]byte, error) {
 | 
			
		||||
	var res []byte
 | 
			
		||||
	
 | 
			
		||||
	res = append(res, []byte("[")...)
 | 
			
		||||
	
 | 
			
		||||
	// Build a filtered jail list
 | 
			
		||||
	var filteredJails []Jail
 | 
			
		||||
	if (len(jails) >= 1 && len(jails[0]) > 0) {
 | 
			
		||||
		for _, j := range gJails {
 | 
			
		||||
			if isStringInArray(jails, j.Name) {
 | 
			
		||||
				filteredJails = append(filteredJails, j)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		filteredJails = gJails
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	for i, j := range filteredJails {
 | 
			
		||||
		out, err := j.PrintJSON(fields)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return res, err
 | 
			
		||||
		}
 | 
			
		||||
		res = append(res, out...)
 | 
			
		||||
		if i < len(filteredJails) - 1 {
 | 
			
		||||
			res = append(res, []byte(",")...)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	res = append(res, []byte("]")...)
 | 
			
		||||
	return res, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func startApi(address string, port int) {
 | 
			
		||||
	// Initialize gJails. 
 | 
			
		||||
	// FIXME: How to update this at running time without doubling the same jails?
 | 
			
		||||
	// core will do this for us in add and delete functions
 | 
			
		||||
	ListJails([]string{""}, false)
 | 
			
		||||
 | 
			
		||||
	r := gin.Default()
 | 
			
		||||
	
 | 
			
		||||
	// Cross Origin Request Support
 | 
			
		||||
	config := cors.DefaultConfig()
 | 
			
		||||
	// DANGER !
 | 
			
		||||
	config.AllowOrigins = []string{"*"}
 | 
			
		||||
	r.Use(cors.New(config))
 | 
			
		||||
 | 
			
		||||
	r.GET("/jail/list", func(c *gin.Context) {
 | 
			
		||||
		jailstr := c.Query("jail")
 | 
			
		||||
		fields := c.Query("fields")
 | 
			
		||||
		if len(fields) == 0 {
 | 
			
		||||
			// TODO : Put this default value in (user?) configuration
 | 
			
		||||
			fields = "Name,Running,Config.Release,Config.Ip4_addr"
 | 
			
		||||
		}
 | 
			
		||||
		jsout, err := printJails(strings.Split(jailstr, ","), strings.Split(fields, ","))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			c.JSON(http.StatusInternalServerError, gin.H{"status": 1, "error": err.Error()})
 | 
			
		||||
		} else {
 | 
			
		||||
			c.Data(http.StatusOK, "application/json", jsout)
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
	
 | 
			
		||||
	r.POST("/jail/start/:jail", func(c *gin.Context) {
 | 
			
		||||
		jailstr := c.Params.ByName("jail")
 | 
			
		||||
		// TODO : Capture or redirect output so we can know if jail was started or not
 | 
			
		||||
		StartJail(strings.Split(jailstr, ","))
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"status": 0, "error": ""})
 | 
			
		||||
	})
 | 
			
		||||
	r.POST("/jail/stop/:jail", func(c *gin.Context) {
 | 
			
		||||
		jailstr := c.Params.ByName("jail")
 | 
			
		||||
		// TODO : Capture or redirect output so we can know if jail was started or not
 | 
			
		||||
		StopJail(strings.Split(jailstr, ","))
 | 
			
		||||
		c.JSON(http.StatusOK, gin.H{"status": 0, "error": ""})
 | 
			
		||||
	})
 | 
			
		||||
	
 | 
			
		||||
	r.GET("/jail/properties", func(c *gin.Context) {
 | 
			
		||||
		props, err := ListJailsProps("json")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			c.JSON(http.StatusInternalServerError, gin.H{"status": 1, "error": err.Error()})
 | 
			
		||||
		} else {
 | 
			
		||||
			c.Data(http.StatusOK, "application/json", []byte(props))
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
	
 | 
			
		||||
	r.Run(fmt.Sprintf("%s:%d", address, port))
 | 
			
		||||
}
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										173
									
								
								cmd/create.go
									
									
									
									
									
								
							
							
						
						
									
										173
									
								
								cmd/create.go
									
									
									
									
									
								
							@ -5,7 +5,9 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	//"log"
 | 
			
		||||
	"time"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"strings"
 | 
			
		||||
	cp "github.com/otiai10/copy"
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@ -14,6 +16,11 @@ func CreateJail(args []string) {
 | 
			
		||||
	var err error
 | 
			
		||||
	var jtype []string
 | 
			
		||||
 | 
			
		||||
	if gCreateArgs.BaseJail && gCreateArgs.Release == "" {
 | 
			
		||||
		fmt.Println("Release should be set when creating basejail")
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(gCreateArgs.JailType) > 0 {
 | 
			
		||||
		jtype = []string{gCreateArgs.JailType}
 | 
			
		||||
	}
 | 
			
		||||
@ -46,38 +53,131 @@ func CreateJail(args []string) {
 | 
			
		||||
		} else {
 | 
			
		||||
			ds = &gDatastores[0]
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		/*
 | 
			
		||||
		// Create and populate datasets
 | 
			
		||||
		err = zfsCreateDataset(fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname), fmt.Sprintf("%s/jails/%s", ds.Mountpoint, jname), "lz4")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("ERROR: %s\n", err.Error())
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		err = zfsCreateDataset(fmt.Sprintf("%s/jails/%s/root", ds.ZFSDataset, jname), fmt.Sprintf("%s/jails/%s/root", ds.Mountpoint, jname), "lz4")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("ERROR: %s\n", err.Error())
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		*/
 | 
			
		||||
 | 
			
		||||
		// Get base template if specified
 | 
			
		||||
		if len(gCreateArgs.BaseTemplate) > 0 {
 | 
			
		||||
			log.Debugf("Jail will be created from a base template\n")
 | 
			
		||||
			/*bj, err := getJailFromArray(jname, []string{"template"}, gJails)
 | 
			
		||||
		if gCreateArgs.BaseJail {
 | 
			
		||||
			/**************************************************************************
 | 
			
		||||
			*  Create based jail from a template
 | 
			
		||||
			*/
 | 
			
		||||
			log.Debugf("Jail will be created read-only from release %s\n", gCreateArgs.Release)
 | 
			
		||||
 | 
			
		||||
			// First check if we got release on the same datastore
 | 
			
		||||
			releasePath := fmt.Sprintf("%s/releases/%s/root", ds.Mountpoint, gCreateArgs.Release)
 | 
			
		||||
			_, err := os.Stat(releasePath)
 | 
			
		||||
			if os.IsNotExist(err) {
 | 
			
		||||
				fmt.Printf("ERROR: Release locally not available. Run \"gocage fetch\"\n")
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Create jail datasets
 | 
			
		||||
			dstDset := fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname)
 | 
			
		||||
			fmt.Printf("    > Initialize dataset %s\n", dstDset)
 | 
			
		||||
			err = zfsCreateDataset(dstDset, "", "")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				if strings.EqualFold(err.Error(), "Jail not found") {
 | 
			
		||||
					
 | 
			
		||||
				} else {
 | 
			
		||||
					fmt.Printf("ERROR: %s\n", err.Error())
 | 
			
		||||
				fmt.Printf("ERROR creating dataset %s: %s\n", dstDset, err.Error())
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			// Create jail root datasets
 | 
			
		||||
			dstRootDset := fmt.Sprintf("%s/jails/%s/root", ds.ZFSDataset, jname)
 | 
			
		||||
			fmt.Printf("    > Initialize dataset %s\n", dstRootDset)
 | 
			
		||||
			err = zfsCreateDataset(dstRootDset, "", "")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fmt.Printf("ERROR creating dataset %s: %s\n", dstRootDset, err.Error())
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Create needed directories with basejail permissions
 | 
			
		||||
			fmt.Printf("    > Create base read-only directories\n")
 | 
			
		||||
			dstRootDir := fmt.Sprintf("%s/jails/%s/root", ds.Mountpoint, jname)
 | 
			
		||||
			for _, d := range append(gBaseDirs, gEmptyDirs...) {
 | 
			
		||||
				dstPath := dstRootDir
 | 
			
		||||
				srcPath := releasePath
 | 
			
		||||
				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
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Copy these from basejail
 | 
			
		||||
			fmt.Printf("    > Create base writable directories\n")
 | 
			
		||||
			for _, d := range gCopyDirs {
 | 
			
		||||
				err := cp.Copy(fmt.Sprintf("%s/%s", releasePath, d), fmt.Sprintf("%s/%s", dstRootDir, d))
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					fmt.Printf("ERROR copying %s to %s: %s\n", fmt.Sprintf("%s/%s", releasePath, d),
 | 
			
		||||
						   fmt.Sprintf("%s/%s", dstRootDir, d), err.Error())
 | 
			
		||||
					return
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				fmt.Printf("Jail exist: %s\n", jname)
 | 
			
		||||
				continue
 | 
			
		||||
			}*/
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			// Copy defaults.json...
 | 
			
		||||
			jailConfPath := fmt.Sprintf("%s/jails/%s/config.json", ds.Mountpoint, jname)
 | 
			
		||||
			err = copyFile(fmt.Sprintf("%s/defaults.json", ds.Mountpoint),
 | 
			
		||||
				       jailConfPath)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fmt.Printf("ERROR creating config.json: %s\n", err.Error())
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			// ... and update it
 | 
			
		||||
			jailConf, err := getJailConfig(jailConfPath)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println("ERROR reading jail config from %s", jailConfPath)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Build jail object from config
 | 
			
		||||
			jailRootPath := fmt.Sprintf("%s/jails/%s/%s", ds.Mountpoint, jname, "root")
 | 
			
		||||
			j := Jail{
 | 
			
		||||
				Name:       jailConf.Host_hostuuid,
 | 
			
		||||
				Config:     jailConf,
 | 
			
		||||
				ConfigPath: jailConfPath,
 | 
			
		||||
				Datastore:  ds.Name,
 | 
			
		||||
				RootPath:   jailRootPath,
 | 
			
		||||
				Running:    false,
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// We need to store the basejail template. We could :
 | 
			
		||||
			// 1. Use "origin" ?
 | 
			
		||||
			// 2. Add a json item to config ("basejail_template" p.e.), but iocage would delete it once jail is started from iocage
 | 
			
		||||
			// 3. Add a gocage specific config ("config.gocage.json" p.e.)
 | 
			
		||||
			j.Config.Jailtype = "basejail"
 | 
			
		||||
			j.Config.Origin = gCreateArgs.Release
 | 
			
		||||
			j.Config.Host_hostname = jname
 | 
			
		||||
			j.Config.Host_hostuuid = jname
 | 
			
		||||
 | 
			
		||||
			j.WriteConfigToDisk(false)
 | 
			
		||||
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			// Create fstab
 | 
			
		||||
			fstabHandle, err := os.Create(fmt.Sprintf("%s/jails/%s/fstab", ds.Mountpoint, jname))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fmt.Printf("ERROR creating fstab: %s", err.Error())
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			defer fstabHandle.Close()
 | 
			
		||||
 | 
			
		||||
			for _, d := range gBaseDirs {
 | 
			
		||||
				fmt.Fprintf(fstabHandle, "%s\t%s\tnullfs\tro\t0\t0\n", fmt.Sprintf("%s/%s", releasePath, d), fmt.Sprintf("%s/%s", dstRootDir, d))
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			fmt.Printf("  > Jail created!\n")
 | 
			
		||||
		} else {
 | 
			
		||||
			// Normal jail with its own freebsd base
 | 
			
		||||
			/**************************************************************************
 | 
			
		||||
			 *  Create normal jail with its own freebsd base
 | 
			
		||||
			 */
 | 
			
		||||
			log.Debugf("Creating jail with its own freebsd base\n")
 | 
			
		||||
 | 
			
		||||
			// First check if we got release on the same datastore
 | 
			
		||||
@ -86,10 +186,9 @@ func CreateJail(args []string) {
 | 
			
		||||
				fmt.Printf("ERROR: Release locally not available. Run \"gocage fetch\"\n")
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			//
 | 
			
		||||
			// Create and populate jail filesystem
 | 
			
		||||
			// Create and populate jail filesystem from release
 | 
			
		||||
			dstDset := fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname)
 | 
			
		||||
			fmt.Printf("    > Initialize dataset %s\n", dstDset)
 | 
			
		||||
			sNow := time.Now().Format("20060102150405")
 | 
			
		||||
@ -142,10 +241,7 @@ func CreateJail(args []string) {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			fmt.Printf("Jail filesystem successfuly initalized\n")
 | 
			
		||||
			
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			//
 | 
			
		||||
			// Copy defaults.json...
 | 
			
		||||
			jailConfPath := fmt.Sprintf("%s/jails/%s/config.json", ds.Mountpoint, jname)
 | 
			
		||||
			err = copyFile(fmt.Sprintf("%s/defaults.json", ds.Mountpoint), 
 | 
			
		||||
@ -155,15 +251,13 @@ func CreateJail(args []string) {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			//
 | 
			
		||||
			// ... and update it
 | 
			
		||||
			// Get conf from config.json
 | 
			
		||||
			jailConf, err := getJailConfig(jailConfPath)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Println("ERROR reading jail config from %s", jailConfPath)
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
			// 2. Build jail object from config
 | 
			
		||||
 | 
			
		||||
			// Build jail object from config
 | 
			
		||||
			jailRootPath := fmt.Sprintf("%s/jails/%s/%s", ds.Mountpoint, jname, "root")
 | 
			
		||||
			j := Jail{
 | 
			
		||||
				Name:       jailConf.Host_hostuuid,
 | 
			
		||||
@ -173,15 +267,15 @@ func CreateJail(args []string) {
 | 
			
		||||
				RootPath:   jailRootPath,
 | 
			
		||||
				Running:    false,
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
 | 
			
		||||
			j.Config.Release = gCreateArgs.Release
 | 
			
		||||
			j.Config.Host_hostname = jname
 | 
			
		||||
			j.Config.Host_hostuuid = jname
 | 
			
		||||
			j.Config.Jailtype = "jail"
 | 
			
		||||
						
 | 
			
		||||
			j.WriteConfigToDisk(false)
 | 
			
		||||
 | 
			
		||||
			///////////////////////////////////////////////////////////////////////
 | 
			
		||||
			//
 | 
			
		||||
			// Create fstab
 | 
			
		||||
			fstabHandle, err := os.Create(fmt.Sprintf("%s/jails/%s/fstab", ds.Mountpoint, jname))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
@ -189,8 +283,7 @@ func CreateJail(args []string) {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			defer fstabHandle.Close()
 | 
			
		||||
			fmt.Printf("  > Jail created!\n")
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		// TODO : Set JailType
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										49
									
								
								cmd/list.go
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								cmd/list.go
									
									
									
									
									
								
							@ -15,23 +15,37 @@ import (
 | 
			
		||||
 * List all properties a jail have, with their internal name
 | 
			
		||||
 * Only print properties name. To get name & values, use GetJailProperties()
 | 
			
		||||
 *******************************************************************************/
 | 
			
		||||
func ListJailsProps(args []string) {
 | 
			
		||||
func ListJailsProps(format string) (string, error) {
 | 
			
		||||
	var conf Jail
 | 
			
		||||
	var result []string
 | 
			
		||||
	var props []string
 | 
			
		||||
	var out string
 | 
			
		||||
	// Mandatory constructor to init default values
 | 
			
		||||
	jailconf, err := NewJailConfig()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Printf("Error allocating JailConfig: %s\n", err.Error())
 | 
			
		||||
		return
 | 
			
		||||
		return out, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conf.Config = jailconf
 | 
			
		||||
 | 
			
		||||
	result = getStructFieldNames(conf, result, "")
 | 
			
		||||
	props = getStructFieldNames(conf, props, "")
 | 
			
		||||
 | 
			
		||||
	for _, f := range result {
 | 
			
		||||
		fmt.Printf("%s\n", f)
 | 
			
		||||
	if format == "json" {
 | 
			
		||||
		out = "{\"properties\":["
 | 
			
		||||
		for i, p := range props {
 | 
			
		||||
			out += fmt.Sprintf("\"%s\"", p)
 | 
			
		||||
			if i < len(props) - 1 {
 | 
			
		||||
				out += ","
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		out += "]}"
 | 
			
		||||
	} else {
 | 
			
		||||
		for _, p := range props {
 | 
			
		||||
			out += fmt.Sprintf("%s\n", p)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return out, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/********************************************************************************
 | 
			
		||||
@ -52,7 +66,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 +82,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 +95,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 +110,7 @@ func ListJails(args []string, display bool) {
 | 
			
		||||
			}
 | 
			
		||||
			nameChecked = append(nameChecked, curCheck)
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		// Now check
 | 
			
		||||
		for _, a := range args {
 | 
			
		||||
			for _, n := range nameChecked {
 | 
			
		||||
@ -107,9 +121,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 +289,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
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,9 @@
 | 
			
		||||
package cmd
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"os"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
@ -20,7 +21,7 @@ func GetJailProperties(args []string) {
 | 
			
		||||
				jail, err = getJailFromArray(a, []string{""}, gJails)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					fmt.Printf("Error: %s\n", err.Error())
 | 
			
		||||
					return
 | 
			
		||||
					os.Exit(1)
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				props = append(props, a)
 | 
			
		||||
@ -105,20 +106,20 @@ func SetJailProperties(args []string) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Get jail by index to modify it
 | 
			
		||||
	for i, _ := range gJails {
 | 
			
		||||
		if gJails[i].Name == jail.Name {
 | 
			
		||||
			for _, p := range props {
 | 
			
		||||
				err := setStructFieldValue(&gJails[i], p.name, p.value)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					fmt.Printf("Error: %s\n", err.Error())
 | 
			
		||||
					return
 | 
			
		||||
				} else {
 | 
			
		||||
					fmt.Printf("%s: %s set to %s\n", gJails[i].Name, p.name, p.value)
 | 
			
		||||
					gJails[i].ConfigUpdated = true
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			writeConfigToDisk(&gJails[i], false)
 | 
			
		||||
	cj, err := getJailFromArray(jail.Name, []string{""}, gJails)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Printf("Error getting jail %s: %v\n", jail.Name, err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, p := range props {
 | 
			
		||||
		err := setStructFieldValue(cj, p.name, p.value)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Printf("Error: %s\n", err.Error())
 | 
			
		||||
			return
 | 
			
		||||
		} else {
 | 
			
		||||
			fmt.Printf("%s: %s set to %s\n", cj.Name, p.name, p.value)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	cj.WriteConfigToDisk(false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										36
									
								
								cmd/root.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								cmd/root.go
									
									
									
									
									
								
							@ -14,7 +14,7 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	gVersion = "0.37"
 | 
			
		||||
	gVersion = "0.42a"
 | 
			
		||||
	
 | 
			
		||||
	// TODO : Get from $jail_zpool/defaults.json
 | 
			
		||||
	MIN_DYN_DEVFS_RULESET = 1000
 | 
			
		||||
@ -22,12 +22,15 @@ const (
 | 
			
		||||
 | 
			
		||||
type createArgs struct {
 | 
			
		||||
	Release      string
 | 
			
		||||
	BaseTemplate string
 | 
			
		||||
	BaseJail     bool
 | 
			
		||||
	Datastore    string
 | 
			
		||||
	JailType     string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	gApiAddress string
 | 
			
		||||
	gApiPort    int
 | 
			
		||||
 | 
			
		||||
	gJailHost   JailHost
 | 
			
		||||
	gJails      []Jail
 | 
			
		||||
	gDatastores []Datastore
 | 
			
		||||
@ -65,6 +68,14 @@ var (
 | 
			
		||||
	gFetchFrom       string
 | 
			
		||||
	gUpgradeRelease  string
 | 
			
		||||
 | 
			
		||||
	// For a based jail, these are directories binded to basejail
 | 
			
		||||
	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
 | 
			
		||||
	gCopyDirs = []string{"etc", "root", "tmp", "var"}
 | 
			
		||||
 | 
			
		||||
	gMdevfs          sync.Mutex
 | 
			
		||||
 | 
			
		||||
	rootCmd = &cobra.Command{
 | 
			
		||||
@ -119,7 +130,12 @@ ex: gocage list srv-db srv-web`,
 | 
			
		||||
		Short: "Print jails properties",
 | 
			
		||||
		Long:  "Display jails properties. You can use properties to filter, get or set them.",
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			ListJailsProps(args)
 | 
			
		||||
			out, err := ListJailsProps("text")
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fmt.Printf("Error listing properties : %v\n", err)
 | 
			
		||||
			} else {
 | 
			
		||||
				fmt.Printf("%s\n", out)
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -349,6 +365,14 @@ You can specify multiple datastores.`,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	apiCmd = &cobra.Command{
 | 
			
		||||
		Use:   "startapi",
 | 
			
		||||
		Short: "Run docage as a daemon, exposing API",
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			startApi(gApiAddress, gApiPort)
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	testCmd = &cobra.Command{
 | 
			
		||||
		Use:   "test",
 | 
			
		||||
		Short: "temporary command to test some code snippet",
 | 
			
		||||
@ -412,9 +436,12 @@ func init() {
 | 
			
		||||
	upgradeCmd.MarkFlagRequired("release")
 | 
			
		||||
 | 
			
		||||
	createCmd.Flags().StringVarP(&gCreateArgs.Release, "release", "r", "", "Release for the jail (e.g.: \"13.1-RELEASE\"")
 | 
			
		||||
	createCmd.Flags().StringVarP(&gCreateArgs.BaseTemplate, "basetpl", "b", "", "Base template. This will create a jail based on basetpl, so every up(date|grade) made to basetpl will immediately propagate to new jail\n")
 | 
			
		||||
	createCmd.Flags().BoolVarP(&gCreateArgs.BaseJail, "basejail", "b", false, "Basejail. This will create a jail mounted read only from a release, so every up(date|grade) made to this release will immediately propagate to new jail.\n")
 | 
			
		||||
	createCmd.Flags().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.")
 | 
			
		||||
 | 
			
		||||
	apiCmd.Flags().StringVarP(&gApiAddress, "listen-addr", "l", "127.0.0.1", "API listening address")
 | 
			
		||||
	apiCmd.Flags().IntVarP(&gApiPort, "listen-port", "p", 1234, "API listening port")
 | 
			
		||||
 | 
			
		||||
	// Now declare commands
 | 
			
		||||
	rootCmd.AddCommand(versionCmd)
 | 
			
		||||
	rootCmd.AddCommand(listCmd)
 | 
			
		||||
@ -433,6 +460,7 @@ func init() {
 | 
			
		||||
	rootCmd.AddCommand(updateCmd)
 | 
			
		||||
	rootCmd.AddCommand(upgradeCmd)
 | 
			
		||||
	rootCmd.AddCommand(createCmd)
 | 
			
		||||
	rootCmd.AddCommand(apiCmd)
 | 
			
		||||
	
 | 
			
		||||
	rootCmd.AddCommand(testCmd)
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										127
									
								
								cmd/start.go
									
									
									
									
									
								
							
							
						
						
									
										127
									
								
								cmd/start.go
									
									
									
									
									
								
							@ -340,7 +340,7 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
 | 
			
		||||
 | 
			
		||||
		if ipproto == IPv6 {
 | 
			
		||||
			key = fmt.Sprintf("%s_ipv6", key)
 | 
			
		||||
			value = "inet6 auto_linklocal accept_rtadv autoconf"
 | 
			
		||||
			value = "\"inet6 auto_linklocal accept_rtadv autoconf\""
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if enable == true {
 | 
			
		||||
@ -363,7 +363,7 @@ func checkRtsold(jail *Jail) error {
 | 
			
		||||
	if strings.Contains(jail.Config.Ip6_addr, "accept_rtadv") == false {
 | 
			
		||||
		return fmt.Errorf("Must set at least one ip6_addr to accept_rtadv!\n")
 | 
			
		||||
	}
 | 
			
		||||
	err := enableRcKeyValue(jail.ConfigPath, "rtsold_enable", "yes")
 | 
			
		||||
	err := enableRcKeyValue(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), "rtsold_enable", "yes")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("ERROR setting rtsold_enable=YES with sysrc for jail %s: %s\n", jail.Name, err)
 | 
			
		||||
	}
 | 
			
		||||
@ -810,7 +810,9 @@ func generateMAC(jail *Jail, nic string) ([]byte, []byte, error) {
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	hsmac := append(prefix, suffix...)
 | 
			
		||||
	jsmac := append(hsmac[:5], hsmac[5]+1)
 | 
			
		||||
	jsmac := make([]byte, 6)
 | 
			
		||||
	copy(jsmac, hsmac)
 | 
			
		||||
	jsmac[5] = jsmac[5] + 1
 | 
			
		||||
	
 | 
			
		||||
	// Save MACs to config
 | 
			
		||||
	pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic))
 | 
			
		||||
@ -840,7 +842,7 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) {
 | 
			
		||||
		bridge := v[1]
 | 
			
		||||
		
 | 
			
		||||
		// Get host side MAC
 | 
			
		||||
		pname := fmt.Sprintf("Config.%s_mac", nic)
 | 
			
		||||
		pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic))
 | 
			
		||||
		var val *reflect.Value
 | 
			
		||||
		val, pname, err = getStructFieldValue(jail, pname)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@ -853,13 +855,22 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) {
 | 
			
		||||
				return []string{}, err
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			hsmac = val.Bytes()
 | 
			
		||||
			if strings.EqualFold(val.String(), "none") {
 | 
			
		||||
				hsmac, _, err = generateMAC(jail, nic)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return []string{}, err
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			hsmac, err = hex.DecodeString(strings.Split(val.String(), " ")[0])
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return []string{}, fmt.Errorf("Error converting %s to hex\n", strings.Split(val.String(), " ")[0])
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		// Get bridge MTU
 | 
			
		||||
		mtu, err := gJailHost.GetBridgeMTU(bridge)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return []string{}, fmt.Errorf("Error getting bridge mtu: %v\n", err)
 | 
			
		||||
			return []string{}, fmt.Errorf("Error getting bridge \"%s\" mtu: %v\n", bridge, err)
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		// Create epair interface
 | 
			
		||||
@ -899,7 +910,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
 | 
			
		||||
 | 
			
		||||
@ -909,7 +920,9 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
 | 
			
		||||
	
 | 
			
		||||
	for _, i := range strings.Split(jail.Config.Ip4_addr, ",") {
 | 
			
		||||
		v := strings.Split(i, "|")
 | 
			
		||||
		ip4s[v[0]] = v[1]
 | 
			
		||||
		if len(v) > 1 {
 | 
			
		||||
			ip4s[v[0]] = v[1]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	for _, i := range strings.Split(jail.Config.Ip6_addr, ",") {
 | 
			
		||||
		v := strings.Split(i, "|")
 | 
			
		||||
@ -919,7 +932,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,9 +943,11 @@ 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)
 | 
			
		||||
		pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic))
 | 
			
		||||
		var val *reflect.Value
 | 
			
		||||
		val, pname, err = getStructFieldValue(jail, pname)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@ -945,10 +960,14 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			jsmac = val.Bytes()
 | 
			
		||||
			jsmac, err = hex.DecodeString(strings.Split(val.String(), " ")[1])
 | 
			
		||||
                        if err != nil {
 | 
			
		||||
                                return fmt.Errorf("Error converting %s to hex\n", strings.Split(val.String(), " ")[1])
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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)
 | 
			
		||||
@ -957,17 +976,17 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
 | 
			
		||||
		// Get bridge MTU
 | 
			
		||||
		mtu, err := gJailHost.GetBridgeMTU(bridge)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Error getting bridge %s mtu: %v\n", bridge, err)
 | 
			
		||||
			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 +1089,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 +1185,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 +1197,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 +1359,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 +1396,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 +1420,32 @@ func StartJail(args []string) {
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		// Get a reference to current jail, to update its properties in RAM so API will get fresh data
 | 
			
		||||
		for i, j := range gJails {
 | 
			
		||||
			if strings.EqualFold(j.Name, cj.Name) && strings.EqualFold(j.Datastore, cj.Datastore) {
 | 
			
		||||
				if err = setStructFieldValue(&gJails[i], "Running", "true"); err != nil {
 | 
			
		||||
					fmt.Printf("ERROR: setting Running property to true: %s\n", err.Error())
 | 
			
		||||
				}
 | 
			
		||||
				if err = setStructFieldValue(&gJails[i], "JID", fmt.Sprintf("%d", cj.JID)); err != nil {
 | 
			
		||||
					fmt.Printf("ERROR: setting JID property to %d: %s\n", cj.JID, err.Error())
 | 
			
		||||
				}
 | 
			
		||||
				if err = setStructFieldValue(&gJails[i], "InternalName", cj.InternalName); err != nil {
 | 
			
		||||
					fmt.Printf("ERROR: Setting InternalName property: %s\n", err.Error())
 | 
			
		||||
				}
 | 
			
		||||
				// FIXME: this value of devfs_ruleset should not go in Config, it should reside in Jail root properties as it is volatile (only valid while jail is running)
 | 
			
		||||
				/*if err = setStructFieldValue(&gJails[i], "Devfs_ruleset", "0"); err != nil {
 | 
			
		||||
					fmt.Printf("ERROR: setting Devfs_ruleset property to 0: %s\n", err.Error())
 | 
			
		||||
				}*/
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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,20 +1454,20 @@ 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
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Printf("  > Setup VNet network: OK\n")
 | 
			
		||||
		
 | 
			
		||||
		// Set default route, unless main network is dhcp
 | 
			
		||||
		if ! cj.isFirstNetDhcp() {
 | 
			
		||||
		if ! cj.isFirstNetDhcp() && !strings.EqualFold(cj.Config.Ip4_addr, "none") {
 | 
			
		||||
			fmt.Printf("  > Setup default ipv4 gateway:\n")
 | 
			
		||||
			cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter)
 | 
			
		||||
			out, err := executeCommand(cmd)
 | 
			
		||||
@ -1439,7 +1477,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 +1488,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 +1503,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 +1522,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 +1533,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 +1545,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 {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										59
									
								
								cmd/stop.go
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								cmd/stop.go
									
									
									
									
									
								
							@ -7,8 +7,8 @@ import (
 | 
			
		||||
	"sync"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"slices"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	//"reflect"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	
 | 
			
		||||
@ -83,15 +83,43 @@ func umountAndUnjailZFS(jail *Jail) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func destroyVNetInterfaces(jail *Jail) error {
 | 
			
		||||
	// Wherever ipv6/ipv4 is enabled, if interface exist we destroy it
 | 
			
		||||
	var vnetnames []string
 | 
			
		||||
	for _, i := range strings.Split(jail.Config.Ip4_addr, ",") {
 | 
			
		||||
		iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID)
 | 
			
		||||
		fmt.Printf("%s: ", iname)
 | 
			
		||||
		_, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname))
 | 
			
		||||
		//_, err := executeScript(fmt.Sprintf("ifconfig %s destroy >/dev/null 2>&1", iname))
 | 
			
		||||
		if len(strings.Split(i, "|")) == 2 {
 | 
			
		||||
			iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID)
 | 
			
		||||
			if !slices.Contains(vnetnames, iname) {
 | 
			
		||||
				vnetnames = append(vnetnames, iname)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	for _, i := range strings.Split(jail.Config.Ip6_addr, ",") {
 | 
			
		||||
		if len(strings.Split(i, "|")) == 2 {
 | 
			
		||||
			iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID)
 | 
			
		||||
			if !slices.Contains(vnetnames, iname) {
 | 
			
		||||
				vnetnames = append(vnetnames, iname)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, iname := range vnetnames {
 | 
			
		||||
		fmt.Printf("    >%s: ", iname)
 | 
			
		||||
		_, err := executeCommand(fmt.Sprintf("ifconfig %s", iname))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
			if strings.Contains(err.Error(), "does not exist") {
 | 
			
		||||
				fmt.Printf("OK\n")
 | 
			
		||||
			} else {
 | 
			
		||||
				fmt.Printf("ERR: %v\n", err)
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			fmt.Printf("OK\n")
 | 
			
		||||
			_, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				fmt.Printf("ERR: %v\n", err)
 | 
			
		||||
				return err
 | 
			
		||||
			} else {
 | 
			
		||||
				fmt.Printf("OK\n")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -123,7 +151,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 +162,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 +176,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 +302,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 +436,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
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,10 @@
 | 
			
		||||
package cmd
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"sort"
 | 
			
		||||
	"time"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@ -561,3 +564,90 @@ type DatastoreSort struct {
 | 
			
		||||
	AvailableDec  datastoreLessFunc
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Field struct {
 | 
			
		||||
	Name   string
 | 
			
		||||
	MaxLen int
 | 
			
		||||
	Value  string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func (j *Jail) PrintJSON(fields []string) ([]byte, error) {
 | 
			
		||||
	return j.PrintJSONWithUpper("", fields)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (j *Jail) PrintJSONWithUpper(upper string, fields []string) ([]byte, error) {
 | 
			
		||||
	var res []byte
 | 
			
		||||
 | 
			
		||||
	if len(fields) < 1 {
 | 
			
		||||
		return res, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(upper) > 0 {
 | 
			
		||||
		res = append(res, []byte(fmt.Sprintf("\"%s\": {", upper))...)
 | 
			
		||||
	} else {
 | 
			
		||||
		res = append(res, []byte("{")...)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Reorder fields so we got fields of a sub-structure adjacents
 | 
			
		||||
	sort.Strings(fields)
 | 
			
		||||
 | 
			
		||||
	var ffname string
 | 
			
		||||
	offset := 0
 | 
			
		||||
	for i, _ := range fields {
 | 
			
		||||
		if i + offset >= len(fields) {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		// Is this struct in struct?
 | 
			
		||||
		if strings.Contains(fields[i+offset], ".") {
 | 
			
		||||
			// Count items in this struct
 | 
			
		||||
			var subfields []string
 | 
			
		||||
			var newUpper string
 | 
			
		||||
			for _, sf := range fields[i+offset:] {
 | 
			
		||||
				if strings.Split(sf, ".")[0] == strings.Split(fields[i+offset], ".")[0] {
 | 
			
		||||
					newUpper = strings.Split(sf, ".")[0]
 | 
			
		||||
					sub := strings.Join(strings.Split(string(sf), ".")[1:], ".")
 | 
			
		||||
					subfields = append(subfields, sub)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			out, err := j.PrintJSONWithUpper(newUpper, subfields)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return []byte{}, err
 | 
			
		||||
			}
 | 
			
		||||
			res = append(res, out...)
 | 
			
		||||
			offset += len(subfields)-1
 | 
			
		||||
			if i + offset < len(fields) - 1 {
 | 
			
		||||
				res = append(res, []byte(",")...)
 | 
			
		||||
			}
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if len(upper) > 0 {
 | 
			
		||||
			ffname = fmt.Sprintf("%s.%s", upper, fields[i+offset])
 | 
			
		||||
		} else {
 | 
			
		||||
			ffname = fields[i+offset]
 | 
			
		||||
		}
 | 
			
		||||
		val, _, err := getStructFieldValue(j, ffname)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return []byte{}, fmt.Errorf("Error accessing field \"%s\" : %v\n", fields[i+offset], err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		res = append(res, []byte(fmt.Sprintf("\"%s\": ", fields[i+offset]))...)
 | 
			
		||||
		switch val.Interface().(type) {
 | 
			
		||||
			case string:
 | 
			
		||||
				res = append(res, []byte(fmt.Sprintf("\"%s\"", val.Interface().(string)))...)
 | 
			
		||||
			case int:
 | 
			
		||||
				res = append(res, []byte(fmt.Sprintf("%d", val.Interface().(int)))...)
 | 
			
		||||
			case bool:
 | 
			
		||||
				res = append(res, []byte(fmt.Sprintf("%t", val.Interface().(bool)))...)
 | 
			
		||||
			default:
 | 
			
		||||
				fmt.Printf("ERREUR dans Jail.PrintJSONWithUpper() : Type inconnu : %T\n", val.Interface())
 | 
			
		||||
		}
 | 
			
		||||
		if i + offset < len(fields) - 1 {
 | 
			
		||||
			res = append(res, []byte(",")...)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res = append(res, []byte("}")...)
 | 
			
		||||
 | 
			
		||||
	return res, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,7 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	//"log"
 | 
			
		||||
	"time"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -21,7 +22,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 +31,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
 | 
			
		||||
@ -58,7 +63,13 @@ func UpdateJail(args []string) {
 | 
			
		||||
			fmt.Printf("Error getting jail: %s\n", err)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
		// We cant update basejail as system is readonly
 | 
			
		||||
		if strings.EqualFold(cj.Config.Jailtype, "basejail") {
 | 
			
		||||
			fmt.Printf("%s is a basejail using %s system files. Please update %s!\n", cj.Name, cj.Config.Origin, cj.Config.Origin)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fmt.Printf("  > Snapshot jail %s\n", cj.Name)
 | 
			
		||||
		// Set snapshot name
 | 
			
		||||
		dt := time.Now()
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										53
									
								
								cmd/utils.go
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								cmd/utils.go
									
									
									
									
									
								
							@ -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:]...)
 | 
			
		||||
@ -626,10 +628,8 @@ func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
 | 
			
		||||
		// else
 | 
			
		||||
		word = word + string(c)
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	if gDebug {
 | 
			
		||||
		fmt.Printf("DEBUG: executeCommandInJail: prepare to execute \"%s\"\n", cmd)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Debugf("executeCommandInJail: will execute \"%s\"\n", strings.Join(cmd, " "))
 | 
			
		||||
 | 
			
		||||
	out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
 | 
			
		||||
 | 
			
		||||
@ -790,9 +790,20 @@ func doZfsDatasetExist(dataset string) (bool, error) {
 | 
			
		||||
	return true, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Create ZFS dataset. mountpoint can be "none", then the dataset won't be mounted
 | 
			
		||||
/* Create ZFS dataset
 | 
			
		||||
 * mountpoint can be "none", then the dataset won't be mounted
 | 
			
		||||
 * mountpoint can be "", then it will be inherited
 | 
			
		||||
 * compression can be "", then it wil be inherited
 | 
			
		||||
 */
 | 
			
		||||
func zfsCreateDataset(dataset, mountpoint, compression string) error {
 | 
			
		||||
	cmd := fmt.Sprintf("zfs create -o mountpoint=%s -o compression=%s %s", mountpoint, compression, dataset)
 | 
			
		||||
	cmd := "zfs create"
 | 
			
		||||
	if len(mountpoint) > 0 {
 | 
			
		||||
		cmd = fmt.Sprintf("%s -o mountpoint=%s", cmd, mountpoint)
 | 
			
		||||
	}
 | 
			
		||||
	if len(compression) > 0 {
 | 
			
		||||
		cmd = fmt.Sprintf("%s -o compression=%s", cmd, compression)
 | 
			
		||||
	}
 | 
			
		||||
	cmd = fmt.Sprintf("%s %s", cmd, dataset)
 | 
			
		||||
	out, err := executeCommand(cmd)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
 | 
			
		||||
@ -821,6 +832,11 @@ func zfsDestroy(dataset string) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*****************************************************************************
 | 
			
		||||
 *
 | 
			
		||||
 * Filesystem operations
 | 
			
		||||
 *
 | 
			
		||||
 *****************************************************************************/
 | 
			
		||||
/* Copy file */
 | 
			
		||||
func copyFile(src, dst string) error {
 | 
			
		||||
	srcfinfo, err := os.Stat(src)
 | 
			
		||||
@ -845,6 +861,11 @@ func copyFile(src, dst string) error {
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get permissions of file or folder
 | 
			
		||||
func getPermissions(path string) (os.FileInfo, error) {
 | 
			
		||||
	return os.Stat(path)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*****************************************************************************
 | 
			
		||||
 *
 | 
			
		||||
 * rc.conf management
 | 
			
		||||
@ -894,7 +915,8 @@ func getFstab(path string) ([]Mount, error) {
 | 
			
		||||
	scan := bufio.NewScanner(f)
 | 
			
		||||
	for scan.Scan() {
 | 
			
		||||
		res := strings.Fields(scan.Text())
 | 
			
		||||
		if len(res) != 6 {
 | 
			
		||||
		// iocage create lines like that : "/iocage/releases/13.2-RELEASE/root/bin   /iocage/jails/smtp-router-02/root/bin   nullfs  ro      0       0 # Added by iocage on 2023-10-10 17:20:51"
 | 
			
		||||
		if (len(res) > 6 && !strings.EqualFold(res[6], "#")) || len(res) < 6 {
 | 
			
		||||
			return mounts, fmt.Errorf("Incorrect format for fstab line %s", scan.Text())
 | 
			
		||||
		}
 | 
			
		||||
		freq, err := strconv.Atoi(res[4])
 | 
			
		||||
@ -1041,7 +1063,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, "/") {
 | 
			
		||||
@ -1107,16 +1129,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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										34
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								go.mod
									
									
									
									
									
								
							@ -1,31 +1,57 @@
 | 
			
		||||
module gocage
 | 
			
		||||
 | 
			
		||||
go 1.17
 | 
			
		||||
go 1.21
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/c-robinson/iplib v1.0.3
 | 
			
		||||
	github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
 | 
			
		||||
	github.com/gin-contrib/cors v1.7.2
 | 
			
		||||
	github.com/gin-gonic/gin v1.10.0
 | 
			
		||||
	github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
 | 
			
		||||
	github.com/otiai10/copy v1.12.0
 | 
			
		||||
	github.com/sirupsen/logrus v1.8.1
 | 
			
		||||
	github.com/spf13/cobra v1.2.1
 | 
			
		||||
	github.com/spf13/viper v1.9.0
 | 
			
		||||
	golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420
 | 
			
		||||
	golang.org/x/net v0.25.0
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/bytedance/sonic v1.11.6 // indirect
 | 
			
		||||
	github.com/bytedance/sonic/loader v0.1.1 // indirect
 | 
			
		||||
	github.com/cloudwego/base64x v0.1.4 // indirect
 | 
			
		||||
	github.com/cloudwego/iasm v0.2.0 // indirect
 | 
			
		||||
	github.com/fsnotify/fsnotify v1.5.1 // indirect
 | 
			
		||||
	github.com/gabriel-vasile/mimetype v1.4.3 // indirect
 | 
			
		||||
	github.com/gin-contrib/sse v0.1.0 // indirect
 | 
			
		||||
	github.com/go-playground/locales v0.14.1 // indirect
 | 
			
		||||
	github.com/go-playground/universal-translator v0.18.1 // indirect
 | 
			
		||||
	github.com/go-playground/validator/v10 v10.20.0 // indirect
 | 
			
		||||
	github.com/goccy/go-json v0.10.2 // indirect
 | 
			
		||||
	github.com/hashicorp/hcl v1.0.0 // indirect
 | 
			
		||||
	github.com/inconshreveable/mousetrap v1.0.0 // indirect
 | 
			
		||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
			
		||||
	github.com/klauspost/cpuid/v2 v2.2.7 // indirect
 | 
			
		||||
	github.com/leodido/go-urn v1.4.0 // indirect
 | 
			
		||||
	github.com/magiconair/properties v1.8.5 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.20 // indirect
 | 
			
		||||
	github.com/mitchellh/mapstructure v1.4.2 // indirect
 | 
			
		||||
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
			
		||||
	github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
			
		||||
	github.com/pelletier/go-toml v1.9.4 // indirect
 | 
			
		||||
	github.com/pelletier/go-toml/v2 v2.2.2 // indirect
 | 
			
		||||
	github.com/spf13/afero v1.6.0 // indirect
 | 
			
		||||
	github.com/spf13/cast v1.4.1 // indirect
 | 
			
		||||
	github.com/spf13/jwalterweatherman v1.1.0 // indirect
 | 
			
		||||
	github.com/spf13/pflag v1.0.5 // indirect
 | 
			
		||||
	github.com/subosito/gotenv v1.2.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
 | 
			
		||||
	golang.org/x/text v0.3.6 // indirect
 | 
			
		||||
	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 | 
			
		||||
	github.com/ugorji/go/codec v1.2.12 // indirect
 | 
			
		||||
	golang.org/x/arch v0.8.0 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.23.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.20.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.15.0 // indirect
 | 
			
		||||
	google.golang.org/protobuf v1.34.1 // indirect
 | 
			
		||||
	gopkg.in/ini.v1 v1.63.2 // indirect
 | 
			
		||||
	gopkg.in/yaml.v2 v2.4.0 // indirect
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.1 // indirect
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user