23 Commits
v0.34 ... v0.37

Author SHA1 Message Date
yo
56926f7200 README update 2023-08-05 20:06:38 +02:00
yo
9f32cda6b8 README update 2023-08-05 20:05:47 +02:00
yo
00cd0421d3 v0.37 2023-08-05 19:55:34 +02:00
yo
2c3b4b18f2 v0.36d: gocage create 2023-08-05 19:50:21 +02:00
yo
46ad79c325 v0.36d: gocage create 2023-08-05 19:49:59 +02:00
yo
534deb371c BUGFIXes on dhcp & multi net handling 2023-08-05 19:49:59 +02:00
yo
fe4192da2d bugfix printf 2023-08-05 19:49:55 +02:00
yo
26b8973c6c Fetch now wants full name (13.2-RELEASE), limit pkg to download/extract 2023-08-05 19:49:55 +02:00
yo
6a8b022165 getJailFromArray filtering by type, add fileCopy, isFirstNetDhcp 2023-08-05 19:49:55 +02:00
yo
45e1c57ce4 getJailFromArray filtering by type 2023-08-05 19:49:50 +02:00
yo
bce37e6541 v0.36c: Moved and renamed writeConfigToDisk into utils.go 2023-07-23 15:52:21 +02:00
yo
24439a8181 v0.36b: fix writeConfigToDisk 2023-07-23 15:41:37 +02:00
yo
310564b4af v0.36a: Add upgrade command 2023-07-23 15:13:16 +02:00
yo
881965e257 Add executeCommandWithStdinStdoutStderr 2023-07-23 15:11:29 +02:00
yo
853bf5fb10 Cleaning 2023-07-23 15:10:11 +02:00
yo
09b807c78e Comment DEBUG printf 2023-07-09 13:47:57 +02:00
yo
2ddf51f887 Display jail name instead of user given string 2023-07-09 13:45:20 +02:00
yo
9e057ed1c5 Add gocage destroy command, v0.35 2023-07-09 13:43:35 +02:00
yo
925c3dd96b Add gocage destroy command, v0.35 2023-07-09 13:42:24 +02:00
yo
e11fc96e05 Add gocage destroy command 2023-07-09 13:42:04 +02:00
yo
956e25c849 Display jail name instead of user given string 2023-07-09 13:41:19 +02:00
yo
f9f1d48023 Add zfsDestroy & zfsGetDatasetByMountpoint functions, rename createZfsDataset to zfsCreateDataset 2023-07-09 13:40:40 +02:00
yo
1b679bcd17 Include gocage update to README 2023-07-09 10:52:12 +02:00
16 changed files with 1161 additions and 382 deletions

View File

@ -1,3 +1,4 @@
v.0.33b : Support jailing datasets on differents pools : jail_zfs_dataset now have to include the pool name v.0.33b : Support jailing datasets on differents pools : jail_zfs_dataset now have to include the pool name
v.0.33c : Parallelize start/stop of jails with same priority v.0.33c : Parallelize start/stop of jails with same priority
v.0.34 : jail name can be shortened v.0.34 : jail name can be shortened
v.0.35 : One can now "gocage destroy"

View File

@ -14,6 +14,15 @@ Config.Jail_zfs_dataset = myzfspool/poudriere
Config.Jail_zfs_mountpoint = none Config.Jail_zfs_mountpoint = none
</code> </code>
Create jails
------------
For now, we can't pass config at creation time. We have to define config after creation:
<pre><code>
gocage create jail1 -r 13.2-RELEASE
gocage set Config.Ip4_addr="vnet0|192.168.1.91/24" Config.Vnet=1 jail1
</code></pre>
List jails List jails
---------- ----------
Nothing fancy, just use Nothing fancy, just use
@ -126,6 +135,25 @@ Stop jails
`gocage stop test` `gocage stop test`
Update jails
----------
To update jail patch version, use gocage update :
`gocage update test`
Upgrade jails
----------
To upgrade jail to newer release, use gocage upgrade :
`gocage upgrade -r 13.2-RELEASE test`
A pre-upgrade snapshot wil be made so we can rollback if needed.
Delete jails
----------
`gocage destroy test`
Multi datastore Multi datastore
---------- ----------
A datastore is a ZFS dataset mounted. It should be declared in gocage.conf.yml, specifying its ZFS mountpoint : A datastore is a ZFS dataset mounted. It should be declared in gocage.conf.yml, specifying its ZFS mountpoint :
@ -206,9 +234,6 @@ gocage fetch -r 12.3 -o iocage --from file:/iocage/download
TODO TODO
---------- ----------
gocage update gocage create from templates
gocage upgrade
gocage create
gocage destroy
gocage init gocage init
create default pool with defaults.json create default pool with defaults.json

View File

@ -12,7 +12,7 @@ import (
func ShellJail(args []string) error { 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 // 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 { if len(args) > 0 {
cj, err := getJailFromArray(args[0], gJails) cj, err := getJailFromArray(args[0], []string{"jail"}, gJails)
if err != nil { if err != nil {
fmt.Printf("Error getting jail %s: %v\n", args[0], err) fmt.Printf("Error getting jail %s: %v\n", args[0], err)
return err return err
@ -33,7 +33,6 @@ func shellJail(jail *Jail) error {
jid := strconv.Itoa(jail.JID) jid := strconv.Itoa(jail.JID)
//err := syscall.Exec("/usr/sbin/jexec", []string{"jexec", jid, "/bin/csh"}, os.Environ())
err := syscall.Exec("/usr/sbin/jexec", []string{"jexec", jid, "login", "-f", "root"}, os.Environ()) err := syscall.Exec("/usr/sbin/jexec", []string{"jexec", jid, "login", "-f", "root"}, os.Environ())
// We should never get here, as syscall.Exec replace the gocage binary execution with jexec // We should never get here, as syscall.Exec replace the gocage binary execution with jexec

196
cmd/create.go Normal file
View File

@ -0,0 +1,196 @@
package cmd
import (
"os"
"fmt"
//"log"
"time"
"strings"
log "github.com/sirupsen/logrus"
)
// TODO : Add a flag to specify which parts of freebsd base we want : Slim jail only need base.txz, neither lib32 nor src.txz
func CreateJail(args []string) {
var err error
var jtype []string
if len(gCreateArgs.JailType) > 0 {
jtype = []string{gCreateArgs.JailType}
}
for _, jname := range args {
// Check if jail exist and is distinctly named
_, err = getJailFromArray(jname, jtype, gJails)
if err != nil {
if strings.EqualFold(err.Error(), "Jail not found") {
} else {
fmt.Printf("ERROR: %s\n", err.Error())
return
}
} else {
fmt.Printf("Jail exist: %s\n", jname)
continue
}
fmt.Printf(" > create jail %s\n", jname)
var ds *Datastore
if len(gCreateArgs.Datastore) > 0 {
fmt.Printf("DEBUG: Use %s datastore\n", gCreateArgs.Datastore)
ds, err = getDatastoreFromArray(gCreateArgs.Datastore, gDatastores)
if err != nil {
fmt.Printf("ERROR Getting datastore: %s\n", gCreateArgs.Datastore, err.Error())
return
}
} 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 err != nil {
if strings.EqualFold(err.Error(), "Jail not found") {
} else {
fmt.Printf("ERROR: %s\n", err.Error())
return
}
} else {
fmt.Printf("Jail exist: %s\n", jname)
continue
}*/
} else {
// 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
_, err := os.Stat(fmt.Sprintf("%s/releases/%s/root", ds.Mountpoint, gCreateArgs.Release))
if os.IsNotExist(err) {
fmt.Printf("ERROR: Release locally not available. Run \"gocage fetch\"\n")
return
}
///////////////////////////////////////////////////////////////////////
//
// Create and populate jail filesystem
dstDset := fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname)
fmt.Printf(" > Initialize dataset %s\n", dstDset)
sNow := time.Now().Format("20060102150405")
reldset := fmt.Sprintf("%s/releases/%s", ds.ZFSDataset, gCreateArgs.Release)
err = zfsSnapshot(reldset, sNow)
if err != nil {
fmt.Printf("ERROR Creating snapshot of %s: %s\n", reldset, err.Error())
return
}
err = zfsCopy(fmt.Sprintf("%s@%s", reldset, sNow), dstDset)
if err != nil {
fmt.Printf("ERROR sending snapshot to %s: %s\n", dstDset, err.Error())
return
}
// Remove snapshot of release, then snapshot of destination dataset
err = zfsDestroy(fmt.Sprintf("%s@%s", reldset, sNow))
if err != nil {
fmt.Printf("ERROR destroying snapshot %s: %s\n", reldset, err.Error())
return
}
err = zfsDestroy(fmt.Sprintf("%s@%s", dstDset, sNow))
if err != nil {
fmt.Printf("ERROR destroying snapshot %s: %s\n", dstDset, err.Error())
return
}
dstRootDset := fmt.Sprintf("%s/jails/%s/root", ds.ZFSDataset, jname)
fmt.Printf(" > Initialize dataset %s\n", dstRootDset)
relrootdset := fmt.Sprintf("%s/releases/%s/root", ds.ZFSDataset, gCreateArgs.Release)
err = zfsSnapshot(relrootdset, sNow)
if err != nil {
fmt.Printf("ERROR Creating snapshot of %s: %s\n", relrootdset, err.Error())
return
}
err = zfsCopy(fmt.Sprintf("%s@%s", relrootdset, sNow), dstRootDset)
if err != nil {
fmt.Printf("ERROR sending snapshot to %s: %s\n", dstRootDset, err.Error())
return
}
// Remove snapshot of release, then snapshot of destination dataset
err = zfsDestroy(fmt.Sprintf("%s@%s", relrootdset, sNow))
if err != nil {
fmt.Printf("ERROR destroying snapshot %s: %s\n", relrootdset, err.Error())
return
}
err = zfsDestroy(fmt.Sprintf("%s@%s", dstRootDset, sNow))
if err != nil {
fmt.Printf("ERROR destroying snapshot %s: %s\n", dstRootDset, err.Error())
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),
jailConfPath)
if err != nil {
fmt.Printf("ERROR creating config.json: %s\n", err.Error())
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
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,
}
j.Config.Release = 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()
}
// TODO : Set JailType
}
}

59
cmd/destroy.go Normal file
View File

@ -0,0 +1,59 @@
package cmd
import (
"fmt"
//"log"
"time"
//"errors"
"strings"
)
func DestroyJails(args []string) {
for _, a := range args {
cj, err := getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
return
}
if cj.Running == true {
fmt.Printf("Jail %s is running\n", cj.Name)
if gForce == false {
var answer string
fmt.Printf("Stop jail and delete? (y/n) ")
fmt.Scanln(&answer)
if false == strings.EqualFold(answer, "y") {
return
}
}
fmt.Printf("Stopping jail %s\n", cj.Name)
StopJail([]string{fmt.Sprintf("%s/%s", cj.Datastore, cj.Name)})
// Give some time to the host OS to free all mounts accessing processes
time.Sleep(1 * time.Second)
}
// Get root and config datasets, then destroy
dsRootName, err := zfsGetDatasetByMountpoint(cj.RootPath)
if err != nil {
fmt.Printf("Error getting root dataset: %s\n", err)
return
}
//fmt.Printf("DEBUG: Prepare to zfs destroy %s\n", dsRootName)
if err = zfsDestroy(dsRootName); err != nil {
fmt.Printf("Error deleting root dataset: %s\n", err)
return
}
dsConfName, err := zfsGetDatasetByMountpoint(cj.ConfigPath)
if err != nil {
fmt.Printf("Error getting config dataset: %s\n", err)
return
}
//fmt.Printf("DEBUG: Prepare to zfs destroy %s\n", dsConfName)
if err = zfsDestroy(dsConfName); err != nil {
fmt.Printf("Error deleting config dataset: %s\n", err)
return
}
fmt.Printf("Jail %s is no more!\n", cj.Name)
}
}

View File

@ -23,7 +23,9 @@ const (
) )
var ( var (
FetchFiles = []string{"base.txz", "lib32.txz", "src.txz"} // TODO : Make this a config/cmd line setting
//FetchFiles = []string{"base.txz", "lib32.txz", "src.txz"}
FetchFiles = []string{"base.txz"}
) )
// TODO: Check if files already exist // TODO: Check if files already exist
@ -61,30 +63,30 @@ func fetchRelease(release string, proto string, arch string, datastore string, f
} }
if false == exist { if false == exist {
// Then create dataset // Then create dataset
if err := createZfsDataset(downloadDsName, downloadDsMountPoint, "lz4"); err != nil { if err := zfsCreateDataset(downloadDsName, downloadDsMountPoint, "lz4"); err != nil {
return fmt.Errorf("Error creating dataset %s: %v\n", downloadDsName, err) return fmt.Errorf("Error creating dataset %s: %v\n", downloadDsName, err)
} }
} }
// Create download/XX.X-RELEASE dataset if necessary // Create download/XX.X dataset if necessary
thisDownloadDsName := fmt.Sprintf("%s/%s-RELEASE", downloadDsName, release) thisDownloadDsName := fmt.Sprintf("%s/%s", downloadDsName, release)
thisDownloadDsMountPoint := fmt.Sprintf("%s/%s-RELEASE", downloadDsMountPoint, release) thisDownloadDsMountPoint := fmt.Sprintf("%s/%s", downloadDsMountPoint, release)
exist, err = doZfsDatasetExist(thisDownloadDsName) exist, err = doZfsDatasetExist(thisDownloadDsName)
if err != nil { if err != nil {
return fmt.Errorf("Error accessing dataset %s: %v\n", thisDownloadDsName, err) return fmt.Errorf("Error accessing dataset %s: %v\n", thisDownloadDsName, err)
} }
if false == exist { if false == exist {
// Then create dataset // Then create dataset
if err := createZfsDataset(thisDownloadDsName, thisDownloadDsMountPoint, "lz4"); err != nil { if err := zfsCreateDataset(thisDownloadDsName, thisDownloadDsMountPoint, "lz4"); err != nil {
return fmt.Errorf("Error creating dataset %s: %v\n", thisDownloadDsName, err) return fmt.Errorf("Error creating dataset %s: %v\n", thisDownloadDsName, err)
} }
} }
var fetchUrl string var fetchUrl string
if len(fetchFrom) > 0 { if len(fetchFrom) > 0 {
fetchUrl = fmt.Sprintf("%s/%s-RELEASE", fetchFrom, release) fetchUrl = fmt.Sprintf("%s/%s", fetchFrom, release)
} else { } else {
fetchUrl = fmt.Sprintf("%s://%s/%s/%s/%s-RELEASE", proto, ReleaseServer, ReleaseRootDir, arch, release) fetchUrl = fmt.Sprintf("%s://%s/%s/%s/%s", proto, ReleaseServer, ReleaseRootDir, arch, release)
} }
log.Debugf("FetchURL = %s", fetchUrl) log.Debugf("FetchURL = %s", fetchUrl)
@ -147,15 +149,15 @@ func extractRelease(release string, datastore string) {
} }
if false == exist { if false == exist {
// Then create dataset // Then create dataset
if err := createZfsDataset(releaseDsName, releaseDsMountPoint, "lz4"); err != nil { if err := zfsCreateDataset(releaseDsName, releaseDsMountPoint, "lz4"); err != nil {
fmt.Printf("Error creating dataset %s: %v\n", releaseDsName, err) fmt.Printf("Error creating dataset %s: %v\n", releaseDsName, err)
return return
} }
} }
// Create releases/XX.X-RELEASE dataset if necessary // Create releases/XX.X dataset if necessary
thisReleaseDsName := fmt.Sprintf("%s/%s-RELEASE", releaseDsName, release) thisReleaseDsName := fmt.Sprintf("%s/%s", releaseDsName, release)
thisReleaseDsMountPoint := fmt.Sprintf("%s/%s-RELEASE", releaseDsMountPoint, release) thisReleaseDsMountPoint := fmt.Sprintf("%s/%s", releaseDsMountPoint, release)
exist, err = doZfsDatasetExist(thisReleaseDsName) exist, err = doZfsDatasetExist(thisReleaseDsName)
if err != nil { if err != nil {
fmt.Printf("Error accessing dataset %s: %v\n", thisReleaseDsName, err) fmt.Printf("Error accessing dataset %s: %v\n", thisReleaseDsName, err)
@ -163,13 +165,13 @@ func extractRelease(release string, datastore string) {
} }
if false == exist { if false == exist {
// Then create dataset // Then create dataset
if err := createZfsDataset(thisReleaseDsName, thisReleaseDsMountPoint, "lz4"); err != nil { if err := zfsCreateDataset(thisReleaseDsName, thisReleaseDsMountPoint, "lz4"); err != nil {
fmt.Printf("Error creating dataset %s: %v\n", thisReleaseDsName, err) fmt.Printf("Error creating dataset %s: %v\n", thisReleaseDsName, err)
return return
} }
} }
// Create releases/XX.X-RELEASE/root dataset if necessary // Create releases/XX.X/root dataset if necessary
thisReleaseRootDsName := fmt.Sprintf("%s/root", thisReleaseDsName) thisReleaseRootDsName := fmt.Sprintf("%s/root", thisReleaseDsName)
thisReleaseRootDsMountPoint := fmt.Sprintf("%s/root", thisReleaseDsMountPoint) thisReleaseRootDsMountPoint := fmt.Sprintf("%s/root", thisReleaseDsMountPoint)
exist, err = doZfsDatasetExist(thisReleaseRootDsName) exist, err = doZfsDatasetExist(thisReleaseRootDsName)
@ -179,15 +181,15 @@ func extractRelease(release string, datastore string) {
} }
if false == exist { if false == exist {
// Then create dataset // Then create dataset
if err := createZfsDataset(thisReleaseRootDsName, thisReleaseRootDsMountPoint, "lz4"); err != nil { if err := zfsCreateDataset(thisReleaseRootDsName, thisReleaseRootDsMountPoint, "lz4"); err != nil {
fmt.Printf("Error creating dataset %s: %v\n", thisReleaseRootDsName, err) fmt.Printf("Error creating dataset %s: %v\n", thisReleaseRootDsName, err)
return return
} }
} }
// Now extract download/$RELEASE/*.txz to releases/XX.X-RELEASE/root // Now extract download/$RELEASE/*.txz to releases/XX.X/root
downloadDsMountPoint := fmt.Sprintf("%s/download", ds.Mountpoint) downloadDsMountPoint := fmt.Sprintf("%s/download", ds.Mountpoint)
downloadDir := fmt.Sprintf("%s/%s-RELEASE", downloadDsMountPoint, release) downloadDir := fmt.Sprintf("%s/%s", downloadDsMountPoint, release)
d, err := os.Open(downloadDir) d, err := os.Open(downloadDir)
defer d.Close() defer d.Close()
@ -201,67 +203,69 @@ func extractRelease(release string, datastore string) {
return return
} }
// Extract every .txz files // Extract every .txz files in FetchFiles
for _, fi := range files { for _, fi := range files {
if false == fi.IsDir() { if false == fi.IsDir() {
if strings.HasSuffix(fi.Name(), ".txz") { if strings.HasSuffix(fi.Name(), ".txz") {
ar := fmt.Sprintf("%s/%s", downloadDir, fi.Name()) if isStringInArray(FetchFiles, fi.Name()) {
fmt.Printf("Extracting file %s... ", ar) ar := fmt.Sprintf("%s/%s", downloadDir, fi.Name())
// pure Go method, sorry this is so slow. Also I did not handle permissions in this fmt.Printf("Extracting file %s to %s... ", ar, thisReleaseRootDsMountPoint)
/* f, err := os.Open(ar) // pure Go method, sorry this is so slow. Also I did not handle permissions in this
defer f.Close() /* f, err := os.Open(ar)
if err != nil { defer f.Close()
fmt.Printf("Can not open %s: %v\n", ar, err)
return
}
// xz reader
r, err := xz.NewReader(f)
if err != nil {
fmt.Printf("Can not read %s: %v\n", ar, err)
return
}
// tar reader
tr := tar.NewReader(r)
// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil { if err != nil {
log.Fatal(err) fmt.Printf("Can not open %s: %v\n", ar, err)
return
} }
switch hdr.Typeflag { // xz reader
case tar.TypeDir: r, err := xz.NewReader(f)
// create a directory if err != nil {
dest := fmt.Sprintf("%s/%s", thisReleaseRootDsMountPoint, hdr.Name) fmt.Printf("Can not read %s: %v\n", ar, err)
// FIXME: Access rights? return
err = os.MkdirAll(dest, 0777) }
if err != nil { // tar reader
log.Fatal(err) tr := tar.NewReader(r)
} // Iterate through the files in the archive.
case tar.TypeReg, tar.TypeRegA: for {
// write a file hdr, err := tr.Next()
dest := fmt.Sprintf("%s/%s", thisReleaseRootDsMountPoint, hdr.Name) if err == io.EOF {
w, err := os.Create(dest) // end of tar archive
defer w.Close() break
if err != nil { }
log.Fatal(err) if err != nil {
} log.Fatal(err)
_, err = io.Copy(w, tr) }
if err != nil { switch hdr.Typeflag {
log.Fatal(err) case tar.TypeDir:
} // create a directory
dest := fmt.Sprintf("%s/%s", thisReleaseRootDsMountPoint, hdr.Name)
// FIXME: Access rights?
err = os.MkdirAll(dest, 0777)
if err != nil {
log.Fatal(err)
}
case tar.TypeReg, tar.TypeRegA:
// write a file
dest := fmt.Sprintf("%s/%s", thisReleaseRootDsMountPoint, hdr.Name)
w, err := os.Create(dest)
defer w.Close()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(w, tr)
if err != nil {
log.Fatal(err)
}
}
}
*/
cmd := fmt.Sprintf("/usr/bin/tar xpf %s -C %s", ar, thisReleaseRootDsMountPoint)
out, err := executeCommand(cmd)
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf("Done\n")
} }
}
*/
cmd := fmt.Sprintf("/usr/bin/tar xpf %s -C %s", ar, thisReleaseRootDsMountPoint)
out, err := executeCommand(cmd)
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf("Done\n")
} }
} }
} }

View File

@ -0,0 +1,85 @@
package cmd
const (
fbsdUpdateConfig = `
# $FreeBSD$
# Trusted keyprint. Changing this is a Bad Idea unless you've received
# a PGP-signed email from <security-officer@FreeBSD.org> telling you to
# change it and explaining why.
KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5
# Server or server pool from which to fetch updates. You can change
# this to point at a specific server if you want, but in most cases
# using a "nearby" server won't provide a measurable improvement in
# performance.
ServerName update.FreeBSD.org
# Components of the base system which should be kept updated.
Components world
# Example for updating the userland and the kernel source code only:
# Components src/base src/sys world
# Paths which start with anything matching an entry in an IgnorePaths
# statement will be ignored.
IgnorePaths
# Paths which start with anything matching an entry in an IDSIgnorePaths
# statement will be ignored by "freebsd-update IDS".
IDSIgnorePaths /usr/share/man/cat
IDSIgnorePaths /usr/share/man/whatis
IDSIgnorePaths /var/db/locate.database
IDSIgnorePaths /var/log
# Paths which start with anything matching an entry in an UpdateIfUnmodified
# statement will only be updated if the contents of the file have not been
# modified by the user (unless changes are merged; see below).
UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile
# When upgrading to a new FreeBSD release, files which match MergeChanges
# will have any local changes merged into the version from the new release.
MergeChanges /etc/
### Default configuration options:
# Directory in which to store downloaded updates and temporary
# files used by FreeBSD Update.
WorkDir /iocage/freebsd-update
# Destination to send output of "freebsd-update cron" if an error
# occurs or updates have been downloaded.
# MailTo root
# Is FreeBSD Update allowed to create new files?
# AllowAdd yes
# Is FreeBSD Update allowed to delete files?
# AllowDelete yes
# If the user has modified file ownership, permissions, or flags, should
# FreeBSD Update retain this modified metadata when installing a new version
# of that file?
# KeepModifiedMetadata yes
# When upgrading between releases, should the list of Components be
# read strictly (StrictComponents yes) or merely as a list of components
# which *might* be installed of which FreeBSD Update should figure out
# which actually are installed and upgrade those (StrictComponents no)?
StrictComponents yes
# When installing a new kernel perform a backup of the old one first
# so it is possible to boot the old kernel in case of problems.
BackupKernel no
# If BackupKernel is enabled, the backup kernel is saved to this
# directory.
# BackupKernelDir /boot/kernel.old
# When backing up a kernel also back up debug symbol files?
BackupKernelSymbolFiles no
# Create a new boot environment when installing patches
CreateBootEnv no
`
)

View File

@ -249,7 +249,7 @@ func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
jailConfPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "config.json") jailConfPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "config.json")
jailConf, err := getJailConfig(jailConfPath) jailConf, err := getJailConfig(jailConfPath)
if err != nil { if err != nil {
log.Println("ERROR reading jail config for %s", jailConfPath) fmt.Printf("ERROR reading jail config from %s\n", jailConfPath)
} }
// 2. Build jail object from config // 2. Build jail object from config

View File

@ -26,7 +26,7 @@ func MigrateJail(args []string) {
} }
for _, jn := range jailNames { for _, jn := range jailNames {
cj, err := getJailFromArray(jn, gJails) cj, err := getJailFromArray(jn, []string{""}, gJails)
if cj == nil { if cj == nil {
fmt.Printf("Error getting jail %s: Not found\n", jn) fmt.Printf("Error getting jail %s: Not found\n", jn)
return return
@ -177,7 +177,7 @@ func CleanMigrateMess(args []string) error {
} }
for _, jn := range jailNames { for _, jn := range jailNames {
cj, err := getJailFromArray(jn, gJails) cj, err := getJailFromArray(jn, []string{""}, gJails)
if cj == nil { if cj == nil {
return errors.New(fmt.Sprintf("Error getting jail %s: Not found\n", jn)) return errors.New(fmt.Sprintf("Error getting jail %s: Not found\n", jn))
} }

View File

@ -17,7 +17,7 @@ func GetJailProperties(args []string) {
for i, a := range args { for i, a := range args {
// Last arg is the jail name // Last arg is the jail name
if i == len(args)-1 { if i == len(args)-1 {
jail, err = getJailFromArray(a, gJails) jail, err = getJailFromArray(a, []string{""}, gJails)
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err.Error()) fmt.Printf("Error: %s\n", err.Error())
return return
@ -118,6 +118,7 @@ func SetJailProperties(args []string) {
gJails[i].ConfigUpdated = true gJails[i].ConfigUpdated = true
} }
} }
writeConfigToDisk(&gJails[i], false)
} }
} }
} }

View File

@ -6,7 +6,6 @@ import (
"sync" "sync"
"strings" "strings"
"io/ioutil" "io/ioutil"
"encoding/json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -15,12 +14,19 @@ import (
) )
const ( const (
gVersion = "0.34" gVersion = "0.37"
// TODO : Get from $jail_zpool/defaults.json // TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000 MIN_DYN_DEVFS_RULESET = 1000
) )
type createArgs struct {
Release string
BaseTemplate string
Datastore string
JailType string
}
var ( var (
gJailHost JailHost gJailHost JailHost
gJails []Jail gJails []Jail
@ -30,6 +36,8 @@ var (
gForce bool gForce bool
gDebug bool gDebug bool
gCreateArgs createArgs
gConfigFile string gConfigFile string
gDisplayJColumns string gDisplayJColumns string
gDisplaySColumns string gDisplaySColumns string
@ -55,6 +63,7 @@ var (
gFetchRelease string gFetchRelease string
gFetchIntoDS string gFetchIntoDS string
gFetchFrom string gFetchFrom string
gUpgradeRelease string
gMdevfs sync.Mutex gMdevfs sync.Mutex
@ -80,6 +89,20 @@ It support iocage jails and can coexist with iocage.`,
}, },
} }
/* TODO
Initialize datastore(s) /iocage, /iocage/jails
Put defaults.json, update it with hostid,interfaces, and maybe other necessary fields
Initialize bridge
initCmd = &cobra.Command{
Use: "init",
Short: "Initialize GoCage",
//Long: `Let this show you how much fail I had to get this *cough* perfect`,
Run: func(cmd *cobra.Command, args []string) {
fv, _ := getFreeBSDVersion()
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
},
}*/
listCmd = &cobra.Command{ listCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "Print jails", Short: "Print jails",
@ -100,7 +123,7 @@ ex: gocage list srv-db srv-web`,
}, },
} }
/* destroyCmd = &cobra.Command{ destroyCmd = &cobra.Command{
Use: "destroy", Use: "destroy",
Short: "destroy jails", Short: "destroy jails",
Long: `Destroy jail filesystem, snapshots and configuration file.`, Long: `Destroy jail filesystem, snapshots and configuration file.`,
@ -109,7 +132,7 @@ ex: gocage list srv-db srv-web`,
DestroyJails(args) DestroyJails(args)
}, },
} }
*/
stopCmd = &cobra.Command{ stopCmd = &cobra.Command{
Use: "stop", Use: "stop",
Short: "stop jail", Short: "stop jail",
@ -136,12 +159,9 @@ ex: gocage list srv-db srv-web`,
} else { } else {
StartJail(args) StartJail(args)
} }
WriteConfigToDisk("", false, false)
}, },
} }
restartCmd = &cobra.Command{ restartCmd = &cobra.Command{
Use: "restart", Use: "restart",
Short: "restart jail", Short: "restart jail",
@ -150,7 +170,6 @@ ex: gocage list srv-db srv-web`,
ListJails(args, false) ListJails(args, false)
StopJail(args) StopJail(args)
StartJail(args) StartJail(args)
WriteConfigToDisk("", false, false)
}, },
} }
@ -173,7 +192,6 @@ Multiples properties can be specified, separated with space (Ex: gocage set allo
// Load inventory // Load inventory
ListJails(args, false) ListJails(args, false)
SetJailProperties(args) SetJailProperties(args)
WriteConfigToDisk("", true, false)
}, },
} }
@ -255,7 +273,6 @@ You can specify multiple jails.`,
// Load inventory // Load inventory
ListJails(args, false) ListJails(args, false)
MigrateJail(args) MigrateJail(args)
WriteConfigToDisk("", false, false)
}, },
} }
@ -302,16 +319,34 @@ You can specify multiple datastores.`,
} else { } else {
extractRelease(gFetchRelease, gFetchIntoDS) extractRelease(gFetchRelease, gFetchIntoDS)
} }
}, },
} }
UpdateCmd = &cobra.Command{ updateCmd = &cobra.Command{
Use: "update", Use: "update",
Short: "Update FreeBSD release", Short: "Update FreeBSD release",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
ListJails(args, false) ListJails(args, false)
UpdateJail(args) UpdateJail(args)
}, },
}
upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade FreeBSD release",
Run: func(cmd *cobra.Command, args []string) {
ListJails(args, false)
UpgradeJail(args)
},
}
createCmd = &cobra.Command{
Use: "create",
Short: "Create jail",
Run: func(cmd *cobra.Command, args []string) {
ListJails(args, false)
CreateJail(args)
},
} }
testCmd = &cobra.Command{ testCmd = &cobra.Command{
@ -343,7 +378,7 @@ func init() {
listCmd.Flags().StringVarP(&gFilterJails, "filter", "f", "none", "Only display jails with these values. Ex: \"gocage list -f Config.Boot=1\" will only list started on boot jails") listCmd.Flags().StringVarP(&gFilterJails, "filter", "f", "none", "Only display jails with these values. Ex: \"gocage list -f Config.Boot=1\" will only list started on boot jails")
listCmd.Flags().StringVarP(&gSortJailFields, "sort", "s", "none", "Display jails sorted by field values. Ex: \"gocage list -s +Name,-Config.Priority\" will sort jails by their decreasing name, then increasing start priority. 3 critera max supported.") listCmd.Flags().StringVarP(&gSortJailFields, "sort", "s", "none", "Display jails sorted by field values. Ex: \"gocage list -s +Name,-Config.Priority\" will sort jails by their decreasing name, then increasing start priority. 3 critera max supported.")
// destroyCmd.Flags().BoolVarP(&gForce, "force", "f", false, "Force stop jail if running") destroyCmd.Flags().BoolVarP(&gForce, "force", "f", false, "Force stop jail if running")
snapshotListCmd.Flags().StringVarP(&gDisplaySColumns, "outcol", "o", "Jailname,Name,Creation,Referenced,Used", "Show these columns in output") snapshotListCmd.Flags().StringVarP(&gDisplaySColumns, "outcol", "o", "Jailname,Name,Creation,Referenced,Used", "Show these columns in output")
snapshotListCmd.Flags().BoolVarP(&gNoSnapLineSep, "nolinesep", "l", false, "Do not display line separator between snapshots") snapshotListCmd.Flags().BoolVarP(&gNoSnapLineSep, "nolinesep", "l", false, "Do not display line separator between snapshots")
@ -367,12 +402,18 @@ func init() {
migrateCmd.Flags().BoolVarP(&gYesToAll, "yes", "y", false, "Answer yes to all questions") migrateCmd.Flags().BoolVarP(&gYesToAll, "yes", "y", false, "Answer yes to all questions")
migrateCmd.MarkFlagRequired("datastore") migrateCmd.MarkFlagRequired("datastore")
fetchCmd.Flags().StringVarP(&gFetchRelease, "release", "r", "", "Release to fetch (e.g.: \"13.1\"") fetchCmd.Flags().StringVarP(&gFetchRelease, "release", "r", "", "Release to fetch (e.g.: \"13.1-RELEASE\"")
fetchCmd.Flags().StringVarP(&gFetchIntoDS, "datastore", "o", "", "Datastore release will be saved to") fetchCmd.Flags().StringVarP(&gFetchIntoDS, "datastore", "o", "", "Datastore release will be saved to")
fetchCmd.Flags().StringVarP(&gFetchFrom, "from", "d", "", "Repository to download from. Should contain XY.Z-RELEASE. File protocol supported") fetchCmd.Flags().StringVarP(&gFetchFrom, "from", "d", "", "Repository to download from. Should contain XY.Z-RELEASE. File protocol supported")
fetchCmd.MarkFlagRequired("release") fetchCmd.MarkFlagRequired("release")
fetchCmd.MarkFlagRequired("datastore") fetchCmd.MarkFlagRequired("datastore")
upgradeCmd.Flags().StringVarP(&gUpgradeRelease, "release", "r", "", "Release to upgrade to (e.g.: \"13.1-RELEASE\"")
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().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.")
// Now declare commands // Now declare commands
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
@ -381,7 +422,7 @@ func init() {
rootCmd.AddCommand(stopCmd) rootCmd.AddCommand(stopCmd)
rootCmd.AddCommand(startCmd) rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(restartCmd) rootCmd.AddCommand(restartCmd)
// rootCmd.AddCommand(destroyCmd) rootCmd.AddCommand(destroyCmd)
rootCmd.AddCommand(shellCmd) rootCmd.AddCommand(shellCmd)
rootCmd.AddCommand(getCmd) rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(setCmd) rootCmd.AddCommand(setCmd)
@ -389,7 +430,9 @@ func init() {
rootCmd.AddCommand(migrateCmd) rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(datastoreCmd) rootCmd.AddCommand(datastoreCmd)
rootCmd.AddCommand(fetchCmd) rootCmd.AddCommand(fetchCmd)
rootCmd.AddCommand(UpdateCmd) rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(upgradeCmd)
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(testCmd) rootCmd.AddCommand(testCmd)
@ -472,66 +515,6 @@ func initConfig() {
} }
} }
/********************************************************************************
* Write jail(s) config which been updated to disk.
* If name is specified, work on the jail. If name is empty string, work on all.
* If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current
* default route, so if route change on jailhost this will reflect on jail next
* start)
*******************************************************************************/
func WriteConfigToDisk(jailName string, changeauto bool, forceWrite bool) {
for _, j := range gJails {
if len(jailName) > 0 && j.Name == jailName || len(jailName) == 0 {
if j.ConfigUpdated || forceWrite {
log.Debug("%s config has changed, write changes to disk\n", j.Name)
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
//fmt.Printf("DEBUG: Will write config to disk, with content:\n")
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
}
}
}
}
}
func Execute() { func Execute() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {

View File

@ -330,8 +330,10 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
for _, n := range nics { for _, n := range nics {
// vnet0 is epair0b inside jail // vnet0 is epair0b inside jail
if strings.Contains(n, "vnet") { //if strings.Contains(n, "vnet") {
n = fmt.Sprintf("%sb", strings.Replace(n, "vnet", "epair", 1)) if strings.HasPrefix(n, "vnet") {
splitd := strings.Split(n, "|")
n = fmt.Sprintf("%sb", strings.Replace(splitd[0], "vnet", "epair", 1))
} }
key := fmt.Sprintf("ifconfig_%s", n) key := fmt.Sprintf("ifconfig_%s", n)
value := "SYNCDHCP" value := "SYNCDHCP"
@ -342,12 +344,12 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
} }
if enable == true { if enable == true {
err := enableRcKeyValue(jail.ConfigPath, key, value) err := enableRcKeyValue(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), key, value)
if err != nil { if err != nil {
return fmt.Errorf("ERROR setting %s=%s with sysrc for jail %s: %s\n", key, value, jail.Name, err) return fmt.Errorf("ERROR setting %s=%s with sysrc for jail %s: %s\n", key, value, jail.Name, err)
} }
} else { } else {
err := disableRcKey(jail.ConfigPath, key) err := disableRcKey(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), key)
if err != nil { if err != nil {
return fmt.Errorf("ERROR deleting %s with sysrc for jail %s: %v\n", key, jail.Name, err) return fmt.Errorf("ERROR deleting %s with sysrc for jail %s: %v\n", key, jail.Name, err)
} }
@ -508,7 +510,7 @@ func buildDevfsRuleSet(jail *Jail, m *sync.Mutex) (error, int) {
} }
} }
log.Debug("buildDevfsRuleSet: Build ruleset %d\n", ruleset) log.Debugf("buildDevfsRuleSet: Build ruleset %d\n", ruleset)
// Get default devfs_ruleset for the datastore // Get default devfs_ruleset for the datastore
// UPDATE: We don't need this as every jail have a default Devfs_ruleset value // UPDATE: We don't need this as every jail have a default Devfs_ruleset value
@ -893,11 +895,11 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) {
} }
epairs = append(epairs, hsepair) epairs = append(epairs, hsepair)
} }
log.Debugf("setupVnetInterfaceHostSide: returning %v\n", epairs)
return epairs, nil return epairs, nil
} }
func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error { func setupVnetInterfaceJailSide(jail *Jail) error {
var jsmac []byte var jsmac []byte
var err error var err error
@ -946,10 +948,7 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
jsmac = val.Bytes() jsmac = val.Bytes()
} }
lasta := strings.LastIndex(hsepair, "a") cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jnic, jail.InternalName)
jsepair := hsepair[:lasta] + strings.Replace(hsepair[lasta:], "a", "b", 1)
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jsepair, jail.InternalName)
_, err := executeCommand(cmd) _, err := executeCommand(cmd)
if err != nil { if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err) return fmt.Errorf("Error linking interface to jail: %v\n", err)
@ -961,14 +960,14 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
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, jsepair, mtu) cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jnic, mtu)
_, err = executeCommand(cmd) _, err = executeCommand(cmd)
if err != nil { if err != nil {
return fmt.Errorf("Error setting mtu: %v\n", err) return fmt.Errorf("Error setting mtu: %v\n", err)
} }
// rename epairXXb to epair0b (or opair1b, ...) // 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, jsepair, jnic) cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jnic, jnic)
_, err = executeCommand(cmd) _, err = executeCommand(cmd)
if err != nil { if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err) return fmt.Errorf("Error linking interface to jail: %v\n", err)
@ -1010,6 +1009,8 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
} }
} }
log.Debugf("setupVnetInterfaceJailSide: return with success\n")
return nil return nil
} }
@ -1165,7 +1166,7 @@ func StartJail(args []string) {
for _, a := range args { for _, a := range args {
// Check if jail exist and is distinctly named // Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails) cj, err = getJailFromArray(a, []string{"jail"}, gJails)
if err != nil { if err != nil {
fmt.Printf("Error getting jail: %s\n", err) fmt.Printf("Error getting jail: %s\n", err)
continue continue
@ -1176,7 +1177,7 @@ func StartJail(args []string) {
continue continue
} }
fmt.Printf("> Starting jail %s\n", a) fmt.Printf("> Starting jail %s\n", cj.Name)
// Set InternalName as it is used by some of these // Set InternalName as it is used by some of these
cj.InternalName = fmt.Sprintf("ioc-%s", cj.Name) cj.InternalName = fmt.Sprintf("ioc-%s", cj.Name)
@ -1365,7 +1366,7 @@ func StartJail(args []string) {
} }
// Synchronize jail config to disk // Synchronize jail config to disk
WriteConfigToDisk(cj.Name, false, false) writeConfigToDisk(cj, false)
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName) start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
@ -1415,28 +1416,28 @@ func StartJail(args []string) {
} }
fmt.Printf(" > Setup VNet network:\n") fmt.Printf(" > Setup VNet network:\n")
hsepairs, err := setupVnetInterfaceHostSide(cj); _, err = setupVnetInterfaceHostSide(cj);
if err != nil { if err != nil {
fmt.Printf("Error setting VNet interface host side: %v\n", err) fmt.Printf("Error setting VNet interface host side: %v\n", err)
return return
} }
for _, ep := range hsepairs { if err = setupVnetInterfaceJailSide(cj); err != nil {
if err = setupVnetInterfaceJailSide(cj, ep); err != nil { fmt.Printf("Error setting VNet interface jail side: %v\n", err)
fmt.Printf("Error setting VNet interface jail side: %v\n", err) return
return
}
} }
fmt.Printf(" > Setup VNet network: OK\n") fmt.Printf(" > Setup VNet network: OK\n")
// TODO: Handle DHCP // Set default route, unless main network is dhcp
fmt.Printf(" > Setup default ipv4 gateway:\n") if ! cj.isFirstNetDhcp() {
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter) fmt.Printf(" > Setup default ipv4 gateway:\n")
out, err := executeCommand(cmd) cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter)
if err != nil && len(out) > 0 { out, err := executeCommand(cmd)
fmt.Printf("Error: %v: %s\n", err, out) if err != nil && len(out) > 0 {
} else { fmt.Printf("Error: %v: %s\n", err, out)
fmt.Printf(" > Setup default ipv4 gateway: OK\n") } else {
fmt.Printf(" > Setup default ipv4 gateway: OK\n")
}
} }
if cj.Config.Ip6_addr != "none" { if cj.Config.Ip6_addr != "none" {
@ -1477,7 +1478,7 @@ func StartJail(args []string) {
fmt.Printf(" > Start services:\n") fmt.Printf(" > Start services:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_start) cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_start)
err := executeCommandNonBlocking(cmd) err := executeCommandNonBlocking(cmd)
if err != nil && len(out) > 0 { if err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
} else { } else {
fmt.Printf(" > Start services: OK\n") fmt.Printf(" > Start services: OK\n")
@ -1512,10 +1513,11 @@ func StartJail(args []string) {
// TODO: Apply rctl // TODO: Apply rctl
// Update last_started // Update last_started
// 23/07/2023 : This is not working, when writing to disk the old value is used
dt := time.Now() dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05")) curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
fmt.Sprintf(cj.Config.Last_started, curDate) cj.Config.Last_started = curDate
WriteConfigToDisk(cj.Name, false, true) writeConfigToDisk(cj, false)
/* /*

View File

@ -270,7 +270,7 @@ func StopJail(args []string) {
for _, a := range args { for _, a := range args {
// Check if jail exist and is distinctly named // Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails) cj, err = getJailFromArray(a, []string{"jail"}, gJails)
if err != nil { if err != nil {
fmt.Printf("Error getting jail: %s\n", err) fmt.Printf("Error getting jail: %s\n", err)
continue continue
@ -281,25 +281,10 @@ func StopJail(args []string) {
continue continue
} }
fmt.Printf("> Stopping jail %s\n", a) fmt.Printf("> Stopping jail %s\n", cj.Name)
// Get current version to update config.json // Get and write new release into config.json
cvers, err := executeCommandInJail(cj, "/bin/freebsd-version") updateVersion(cj)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return
}
cvers = strings.TrimRight(cvers, "\n")
//fmt.Sprintf(cj.Config.Release, cvers)
//cj.Config.Release = cvers
//cj.ConfigUpdated = true
// This is working in this context, but value is not available in WriteConfigToDisk context :/
setStructFieldValue(cj, "Config.Release", cvers)
fmt.Printf("DEBUG: release was set, now is : %s\n", cj.Config.Release)
// We need to get the real Config object, not a copy of it
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName)) out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
if err == nil && len(out) > 0 { if err == nil && len(out) > 0 {
@ -464,9 +449,7 @@ func StopJail(args []string) {
} }
} }
} }
writeConfigToDisk(cj, false)
fmt.Printf("DEBUG: release = %s\n", cj.Config.Release)
WriteConfigToDisk(cj.Name, false, true)
} }
} }

View File

@ -7,93 +7,9 @@ import (
"time" "time"
) )
const (
fbsdUpdateConfig = `
# $FreeBSD: releng/12.2/usr.sbin/freebsd-update/freebsd-update.conf 337338 2018-08-04 22:25:41Z brd $
# Trusted keyprint. Changing this is a Bad Idea unless you've received
# a PGP-signed email from <security-officer@FreeBSD.org> telling you to
# change it and explaining why.
KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5
# Server or server pool from which to fetch updates. You can change
# this to point at a specific server if you want, but in most cases
# using a "nearby" server won't provide a measurable improvement in
# performance.
ServerName update.FreeBSD.org
# Components of the base system which should be kept updated.
Components world
# Example for updating the userland and the kernel source code only:
# Components src/base src/sys world
# Paths which start with anything matching an entry in an IgnorePaths
# statement will be ignored.
IgnorePaths
# Paths which start with anything matching an entry in an IDSIgnorePaths
# statement will be ignored by "freebsd-update IDS".
IDSIgnorePaths /usr/share/man/cat
IDSIgnorePaths /usr/share/man/whatis
IDSIgnorePaths /var/db/locate.database
IDSIgnorePaths /var/log
# Paths which start with anything matching an entry in an UpdateIfUnmodified
# statement will only be updated if the contents of the file have not been
# modified by the user (unless changes are merged; see below).
UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile
# When upgrading to a new FreeBSD release, files which match MergeChanges
# will have any local changes merged into the version from the new release.
MergeChanges /etc/
### Default configuration options:
# Directory in which to store downloaded updates and temporary
# files used by FreeBSD Update.
# WorkDir /var/db/freebsd-update
# Destination to send output of "freebsd-update cron" if an error
# occurs or updates have been downloaded.
# MailTo root
# Is FreeBSD Update allowed to create new files?
# AllowAdd yes
# Is FreeBSD Update allowed to delete files?
# AllowDelete yes
# If the user has modified file ownership, permissions, or flags, should
# FreeBSD Update retain this modified metadata when installing a new version
# of that file?
# KeepModifiedMetadata yes
# When upgrading between releases, should the list of Components be
# read strictly (StrictComponents yes) or merely as a list of components
# which *might* be installed of which FreeBSD Update should figure out
# which actually are installed and upgrade those (StrictComponents no)?
# StrictComponents no
# When installing a new kernel perform a backup of the old one first
# so it is possible to boot the old kernel in case of problems.
# BackupKernel yes
# If BackupKernel is enabled, the backup kernel is saved to this
# directory.
# BackupKernelDir /boot/kernel.old
# When backing up a kernel also back up debug symbol files?
# BackupKernelSymbolFiles no
# Create a new boot environment when installing patches
# CreateBootEnv yes
`
)
// Internal usage only // Internal usage only
func updateJail(jail *Jail) error { func updateJail(jail *Jail) error {
// Create default config as temporary file // Create default config as temporary file
cfgFile, err := os.CreateTemp("", "gocage-jail-update-") cfgFile, err := os.CreateTemp("", "gocage-jail-update-")
if err != nil { if err != nil {
@ -103,12 +19,21 @@ func updateJail(jail *Jail) error {
cfgFile.Write([]byte(fbsdUpdateConfig)) cfgFile.Write([]byte(fbsdUpdateConfig))
defer cfgFile.Close() defer cfgFile.Close()
//defer os.Remove(cfgFile.Name()) defer os.Remove(cfgFile.Name())
// Folder containing update/uipgrade 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) {
if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil {
return err
}
}
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 install",
cfgFile.Name(), jail.RootPath, jail.Config.Release) cfgFile.Name(), jail.RootPath, jail.Config.Release)
fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd) //fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd)
err = executeCommandWithOutputToStdout(cmd) err = executeCommandWithOutputToStdout(cmd)
if err != nil { if err != nil {
@ -116,7 +41,7 @@ func updateJail(jail *Jail) error {
} }
// Get and write new release into config.json // Get and write new release into config.json
updateVersion(jail)
return nil return nil
} }
@ -128,7 +53,7 @@ func UpdateJail(args []string) {
for _, a := range args { for _, a := range args {
// Check if jail exist and is distinctly named // Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails) cj, err = getJailFromArray(a, []string{""}, gJails)
if err != nil { if err != nil {
fmt.Printf("Error getting jail: %s\n", err) fmt.Printf("Error getting jail: %s\n", err)
continue continue

125
cmd/upgrade.go Normal file
View File

@ -0,0 +1,125 @@
package cmd
import (
"os"
"fmt"
//"log"
"time"
"strings"
)
// Internal usage only
func upgradeJail(jail *Jail, version string) error {
// Create default config as temporary file
cfgFile, err := os.CreateTemp("", "gocage-jail-upgrade-")
if err != nil {
return err
}
cfgFile.Write([]byte(fbsdUpdateConfig))
defer cfgFile.Close()
defer os.Remove(cfgFile.Name())
// Folder containing update/uipgrade 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) {
if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil {
return err
}
}
// Get current version. Won't work on stopped jail.
fbsdvers, err := executeCommandInJail(jail, "/bin/freebsd-version")
if err != nil {
fmt.Printf("ERROR executeCommandInJail: %s\n", err.Error())
return err
}
fbsdvers = strings.TrimRight(fbsdvers, "\n")
//fbsdvers := jail.Config.Release
cmd := fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s upgrade",
cfgFile.Name(), jail.RootPath, fbsdvers, version)
//fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd)
// Need to give user control, bc there could be merge edit needs
err = executeCommandWithStdinStdoutStderr(cmd)
if err != nil {
return err
}
cmd = fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s install",
cfgFile.Name(), jail.RootPath, fbsdvers, version)
//fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd)
err = executeCommandWithStdinStdoutStderr(cmd)
if err != nil {
return err
}
cmd = fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s install",
cfgFile.Name(), jail.RootPath, fbsdvers, version)
//fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd)
err = executeCommandWithStdinStdoutStderr(cmd)
if err != nil {
return err
}
cmd = fmt.Sprintf("/usr/local/sbin/pkg-static -j %d install -q -f -y pkg", jail.JID)
err = executeCommandWithStdinStdoutStderr(cmd)
if err != nil {
return err
}
// Get and write new release into config.json
updateVersion(jail)
return nil
}
func UpgradeJail(args []string) {
// Current jail were stopping
var cj *Jail
var err error
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
}
if cj.Running == false {
fmt.Printf("Error: jail must be running for upgrade.\n")
return
}
fmt.Printf(" > Snapshot jail %s\n", cj.Name)
// Set snapshot name
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05"))
gSnapshotName = fmt.Sprintf("goc_upgrade_%s_%s", cj.Config.Release, curDate)
err := createJailSnapshot(*cj)
if err != nil {
fmt.Printf(" > Snapshot jail %s: ERROR: %s\n", cj.Name, err.Error())
return
}
fmt.Printf(" > Snapshot jail %s: OK\n", cj.Name)
fmt.Printf(" > Upgrade jail %s to %s\n", cj.Name, gUpgradeRelease)
err = upgradeJail(cj, gUpgradeRelease)
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
} else {
fmt.Printf(" > Upgrade jail %s: OK\n", cj.Name)
}
}
}

View File

@ -13,6 +13,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"io/ioutil" "io/ioutil"
"encoding/json"
"github.com/google/shlex" "github.com/google/shlex"
"github.com/c2h5oh/datasize" "github.com/c2h5oh/datasize"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -23,6 +24,141 @@ const (
ifconfigipv4re = `inet[[:space:]](` + ipv4re + `)` ifconfigipv4re = `inet[[:space:]](` + ipv4re + `)`
// Maximum thread qty for start/stop // Maximum thread qty for start/stop
gMaxThreads = 4 gMaxThreads = 4
gDefaultsJson = ` {
"CONFIG_VERSION": "27",
"allow_chflags": 0,
"allow_mlock": 0,
"allow_mount": 0,
"allow_mount_devfs": 0,
"allow_mount_fusefs": 0,
"allow_mount_nullfs": 0,
"allow_mount_procfs": 0,
"allow_mount_tmpfs": 0,
"allow_mount_zfs": 0,
"allow_quotas": 0,
"allow_raw_sockets": 0,
"allow_set_hostname": 1,
"allow_socket_af": 0,
"allow_sysvipc": 0,
"allow_tun": 0,
"allow_vmm": 0,
"assign_localhost": 0,
"available": "readonly",
"basejail": 0,
"boot": 0,
"bpf": 0,
"children_max": "0",
"comment": "none",
"compression": "lz4",
"compressratio": "readonly",
"coredumpsize": "off",
"count": "1",
"cpuset": "off",
"cputime": "off",
"datasize": "off",
"dedup": "off",
"defaultrouter": "auto",
"defaultrouter6": "auto",
"depends": "none",
"devfs_ruleset": "4",
"dhcp": 0,
"enforce_statfs": "2",
"exec_clean": 1,
"exec_created": "/usr/bin/true",
"exec_fib": "0",
"exec_jail_user": "root",
"exec_poststart": "/usr/bin/true",
"exec_poststop": "/usr/bin/true",
"exec_prestart": "/usr/bin/true",
"exec_prestop": "/usr/bin/true",
"exec_start": "/bin/sh /etc/rc",
"exec_stop": "/bin/sh /etc/rc.shutdown",
"exec_system_jail_user": "0",
"exec_system_user": "root",
"exec_timeout": "60",
"host_domainname": "none",
"host_time": 1,
"hostid": "36353536-3135-5a43-4a34-313130315a56",
"hostid_strict_check": 0,
"interfaces": "vnet0:bridge0",
"ip4": "new",
"ip4_addr": "none",
"ip4_saddrsel": 1,
"ip6": "new",
"ip6_addr": "none",
"ip6_saddrsel": 1,
"ip_hostname": 0,
"jail_zfs": 0,
"jail_zfs_mountpoint": "none",
"last_started": "none",
"localhost_ip": "none",
"login_flags": "-f root",
"mac_prefix": "2c44fd",
"maxproc": "off",
"memorylocked": "off",
"memoryuse": "off",
"min_dyn_devfs_ruleset": "1000",
"mount_devfs": 1,
"mount_fdescfs": 1,
"mount_linprocfs": 0,
"mount_procfs": 0,
"mountpoint": "readonly",
"msgqqueued": "off",
"msgqsize": "off",
"nat": 0,
"nat_backend": "ipfw",
"nat_forwards": "none",
"nat_interface": "none",
"nat_prefix": "172.16",
"nmsgq": "off",
"notes": "none",
"nsem": "off",
"nsemop": "off",
"nshm": "off",
"nthr": "off",
"openfiles": "off",
"origin": "readonly",
"owner": "root",
"pcpu": "off",
"plugin_name": "none",
"plugin_repository": "none",
"priority": "99",
"pseudoterminals": "off",
"quota": "none",
"readbps": "off",
"readiops": "off",
"reservation": "none",
"resolver": "/etc/resolv.conf",
"rlimits": "off",
"rtsold": 0,
"securelevel": "2",
"shmsize": "off",
"stacksize": "off",
"stop_timeout": "30",
"swapuse": "off",
"sync_state": "none",
"sync_target": "none",
"sync_tgt_zpool": "none",
"sysvmsg": "new",
"sysvsem": "new",
"sysvshm": "new",
"template": 0,
"type": "jail",
"used": "readonly",
"vmemoryuse": "off",
"vnet": 0,
"vnet0_mac": "none",
"vnet1_mac": "none",
"vnet2_mac": "none",
"vnet3_mac": "none",
"vnet_default_interface": "auto",
"vnet_interfaces": "none",
"wallclock": "off",
"writebps": "off",
"writeiops": "off"
}
`
) )
/***************************************************************************** /*****************************************************************************
@ -218,6 +354,8 @@ func executeCommand(cmdline string) (string, error) {
word = word + string(c) word = word + string(c)
} }
log.Debugf("executeCommand: %s\n", strings.Join(cmd, " "))
if len(cmd) > 1 { if len(cmd) > 1 {
out, err = exec.Command(cmd[0], cmd[1:]...).CombinedOutput() out, err = exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
} else { } else {
@ -365,9 +503,83 @@ func executeCommandWithOutputToStdout(cmdline string) (error) {
return fmt.Errorf("Unknown error: you shouldn't be here!\n") return fmt.Errorf("Unknown error: you shouldn't be here!\n")
} }
/* Execute command plugging stdin and stdout to those of the running command.
* Blocking while the command run
*/
func executeCommandWithStdinStdoutStderr(cmdline string) (error) {
var cmd []string
if gUseSudo {
cmd = append(cmd, "sudo")
}
var word string
var in_escaped bool
// Split by words, or " enclosed words
for i, c := range (cmdline) {
if string(c) == "\"" {
if in_escaped {
// This is the closing "
cmd = append(cmd, word)
in_escaped = false
} else {
in_escaped = true
}
continue
}
if string(c) == " " {
if in_escaped {
word = word + string(c)
continue
} else {
cmd = append(cmd, word)
word = ""
continue
}
}
if i == (len(cmdline) - 1) {
word = word + string(c)
cmd = append(cmd, word)
break
}
// else
word = word + string(c)
}
var command *exec.Cmd
if len(cmd) > 1 {
command = exec.Command(cmd[0], cmd[1:]...)
} else {
command = exec.Command(cmd[0])
}
// Get environment
command.Env = os.Environ()
// Connect command to current stdin/out/err
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
if err := command.Start(); err != nil {
return err
}
err := command.Wait()
return err
}
func executeCommandInJail(jail *Jail, cmdline string) (string, error) { func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
var cmd []string var cmd []string
// We can't execute on non-running jail
if jail.Running == false {
return "", errors.New("Can't execute command on stopped jail")
}
if gUseSudo { if gUseSudo {
cmd = append(cmd, "sudo") cmd = append(cmd, "sudo")
} }
@ -415,6 +627,10 @@ func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
word = word + string(c) word = word + string(c)
} }
if gDebug {
fmt.Printf("DEBUG: executeCommandInJail: prepare to execute \"%s\"\n", cmd)
}
out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput() out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
return string(out), err return string(out), err
@ -466,8 +682,11 @@ func zfsSnapshot(dataset string, snapname string) error {
return nil return nil
} }
// Copy snapshot to a new dataset
// TODO : Intercept death of sending process, then kill receiving
func zfsCopy(src string, dest string) error { func zfsCopy(src string, dest string) error {
// First, declare sending process & pipe // First, declare sending process & pipe
log.Debugf("Execute: zfs send %s\n", src)
cmd_send := exec.Command("zfs", "send", src) cmd_send := exec.Command("zfs", "send", src)
stdout_send, err := cmd_send.StdoutPipe() stdout_send, err := cmd_send.StdoutPipe()
if err != nil { if err != nil {
@ -476,6 +695,7 @@ func zfsCopy(src string, dest string) error {
} }
// then declare receiving process & pipe // then declare receiving process & pipe
log.Debugf("Execute: zfs receive %s\n", dest)
cmd_recv := exec.Command("zfs", "receive", dest) cmd_recv := exec.Command("zfs", "receive", dest)
stdin_recv, err := cmd_recv.StdinPipe() stdin_recv, err := cmd_recv.StdinPipe()
if err != nil { if err != nil {
@ -489,16 +709,19 @@ func zfsCopy(src string, dest string) error {
// then start processes and wait for finish // then start processes and wait for finish
if err := cmd_recv.Start(); err != nil { if err := cmd_recv.Start(); err != nil {
//fmt.Printf("Error: %v\n", err) //fmt.Printf("Error: %v\n", err)
log.Debugf("zfs receive %s started: %v", dest, err)
return errors.New(fmt.Sprintf("Error starting receive process: %v\n", err)) return errors.New(fmt.Sprintf("Error starting receive process: %v\n", err))
} }
//fmt.Printf("DEBUG: Start \"zfs send %s\"\n", dsconf) //fmt.Printf("DEBUG: Start \"zfs send %s\"\n", dsconf)
if err := cmd_send.Start(); err != nil { if err := cmd_send.Start(); err != nil {
//fmt.Printf("Error: %v\n", err) //fmt.Printf("Error: %v\n", err)
log.Debugf("zfs send %s started: %v", src, err)
return errors.New(fmt.Sprintf("Error starting send process: %v\n", err)) return errors.New(fmt.Sprintf("Error starting send process: %v\n", err))
} }
//fmt.Printf("DEBUG: Wait for zfs send to finish\n") //fmt.Printf("DEBUG: Wait for zfs send to finish\n")
if err := cmd_send.Wait(); err != nil { if err := cmd_send.Wait(); err != nil {
log.Debugf("zfs send %s stopped with %v", err)
//fmt.Printf("Error: zfs send halted with %v\n", err) //fmt.Printf("Error: zfs send halted with %v\n", err)
return errors.New(fmt.Sprintf("send halted with: %v\n", err)) return errors.New(fmt.Sprintf("send halted with: %v\n", err))
} }
@ -568,7 +791,7 @@ func doZfsDatasetExist(dataset string) (bool, error) {
} }
// 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
func createZfsDataset(dataset, mountpoint, compression string) error { func zfsCreateDataset(dataset, mountpoint, compression string) error {
cmd := fmt.Sprintf("zfs create -o mountpoint=%s -o compression=%s %s", mountpoint, compression, dataset) cmd := fmt.Sprintf("zfs create -o mountpoint=%s -o compression=%s %s", mountpoint, compression, dataset)
out, err := executeCommand(cmd) out, err := executeCommand(cmd)
if err != nil { if err != nil {
@ -576,6 +799,51 @@ func createZfsDataset(dataset, mountpoint, compression string) error {
} }
return nil return nil
} }
// Return dataset name for a given mountpoint
func zfsGetDatasetByMountpoint(mountpoint string) (string, error) {
cmd := fmt.Sprintf("zfs list -p -r -H -o name %s", mountpoint)
out, err := executeCommand(cmd)
if err != nil {
return "", errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
}
return strings.TrimSuffix(out, "\n"), nil
}
// Delete a ZFS Dataset by name
func zfsDestroy(dataset string) error {
log.Debugf("execute \"zfs destroy -r %s\"\n", dataset)
cmd := fmt.Sprintf("zfs destroy -r %s", dataset)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
}
return nil
}
/* Copy file */
func copyFile(src, dst string) error {
srcfinfo, err := os.Stat(src)
if err != nil {
return fmt.Errorf("Cannot find source file: %s", err.Error())
}
if !srcfinfo.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
srcHandle, err := os.Open(src)
if err != nil {
return fmt.Errorf("Cannot open source file: %s", err.Error())
}
defer srcHandle.Close()
dstHandle, err := os.Create(dst)
if err != nil {
return fmt.Errorf("Cannot create destination file: %s", err.Error())
}
defer dstHandle.Close()
_, err = io.Copy(dstHandle, srcHandle)
return err
}
/***************************************************************************** /*****************************************************************************
* *
@ -682,6 +950,33 @@ func copyDevfsRuleset(ruleset int, srcrs int) error {
return nil return nil
} }
/********************************************************************************
* Add a rule to specified ruleset
* Ex.: addDevfsRuleToRuleset("path bpf* unhide", 1002)
*******************************************************************************/
func addDevfsRuleToRuleset(rule string, ruleset int) error {
// TODO: Check if rule not already enabled. We will need to recurse into includes.
// Get last rule index
rules := getDevfsRuleset(ruleset)
if len(rules) == 0 {
fmt.Printf("Error listing ruleset %d\n", ruleset)
return errors.New(fmt.Sprintf("Error listing rueset %d\n", ruleset))
}
f := strings.Fields(rules[(len(rules)-1)])
//fmt.Printf("Dernier index du ruleset %d: %s\n", ruleset, f[0])
index, _ := strconv.Atoi(f[0])
index += 100
cmd := fmt.Sprintf("/sbin/devfs rule -s %d add %d %s", ruleset, index, rule)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error adding rule \"%s\" to ruleset %d: %s", rule, ruleset, out))
}
return nil
}
/******************************************************************************** /********************************************************************************
* Returns value of parameter as read in /var/run/jail.$InternalName.conf * Returns value of parameter as read in /var/run/jail.$InternalName.conf
* Directives without value will return "true" if found * Directives without value will return "true" if found
@ -710,32 +1005,6 @@ func getValueFromRunningConfig(jname string, param string) (string, error) {
return "", fmt.Errorf("Parameter not found: %s", param) return "", fmt.Errorf("Parameter not found: %s", param)
} }
/********************************************************************************
* Add a rule to specified ruleset
* Ex.: addDevfsRuleToRuleset("path bpf* unhide", 1002)
*******************************************************************************/
func addDevfsRuleToRuleset(rule string, ruleset int) error {
// TODO: Check if rule not already enabled. We will need to recurse into includes.
// Get last rule index
rules := getDevfsRuleset(ruleset)
if len(rules) == 0 {
fmt.Printf("Error listing ruleset %d\n", ruleset)
return errors.New(fmt.Sprintf("Error listing rueset %d\n", ruleset))
}
f := strings.Fields(rules[(len(rules)-1)])
//fmt.Printf("Dernier index du ruleset %d: %s\n", ruleset, f[0])
index, _ := strconv.Atoi(f[0])
index += 100
cmd := fmt.Sprintf("/sbin/devfs rule -s %d add %d %s", ruleset, index, rule)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error adding rule \"%s\" to ruleset %d: %s", rule, ruleset, out))
}
return nil
}
/****************************************************************************** /******************************************************************************
@ -752,14 +1021,29 @@ func isStringInArray(strarr []string, searched string) bool {
return false return false
} }
func (j Jail) isFirstNetDhcp() bool {
for _, n := range strings.Split(j.Config.Ip4_addr, ",") {
splitd := strings.Split(n, "|")
if len(splitd) > 1 && strings.EqualFold(splitd[1], "dhcp") {
return true
}
}
return false
}
/******************************************************************************** /********************************************************************************
* Get a specific jail reference, to update properties after a range loop * Get a specific jail reference, to update properties after a range loop
* Name can be short or long form ("myjail" vs "mystore/myjail") * Name can be short or long form ("myjail" vs "mystore/myjail")
* An empty jailtype means "all types"
*******************************************************************************/ *******************************************************************************/
func getJailFromArray(name string, jarray []Jail) (*Jail, error) { func getJailFromArray(name string, jailtypes []string, jarray []Jail) (*Jail, error) {
var ds, jail string var ds, jail string
var jails []Jail var jails []Jail
if (len(jailtypes) == 1 && len(jailtypes[0]) == 0) || len(jailtypes) == 0 {
jailtypes = []string{"jail", "basetpl"}
}
if strings.Contains(name, "/") { if strings.Contains(name, "/") {
split := strings.Split(name, "/") split := strings.Split(name, "/")
if len(split) != 2 { if len(split) != 2 {
@ -772,16 +1056,17 @@ func getJailFromArray(name string, jarray []Jail) (*Jail, error) {
} }
for i, j := range jarray { for i, j := range jarray {
//if jail == j.Name {
if strings.HasPrefix(j.Name, jail) { if strings.HasPrefix(j.Name, jail) {
if len(ds) > 0 { if isStringInArray(jailtypes, j.Config.Jailtype) {
if strings.EqualFold(ds, j.Datastore) { if len(ds) > 0 {
return &jarray[i], nil if strings.EqualFold(ds, j.Datastore) {
return &jarray[i], nil
} else {
continue
}
} else { } else {
continue jails = append(jails, j)
} }
} else {
jails = append(jails, j)
} }
} }
} }
@ -813,7 +1098,7 @@ func setJailConfigUpdated(jail *Jail) error {
return errors.New(fmt.Sprintf("No config path for jail %s", jail.Name)) return errors.New(fmt.Sprintf("No config path for jail %s", jail.Name))
} }
j, err := getJailFromArray(jail.Name, gJails) j, err := getJailFromArray(jail.Name, []string{""}, gJails)
if err != nil { if err != nil {
return err return err
} }
@ -822,6 +1107,112 @@ func setJailConfigUpdated(jail *Jail) error {
return nil return nil
} }
func updateVersion(jail *Jail) error {
cvers, err := executeCommandInJail(jail, "/bin/freebsd-version")
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return err
}
cvers = strings.TrimRight(cvers, "\n")
jail.Config.Release = cvers
writeConfigToDisk(jail, false)
return nil
}
/********************************************************************************
* Write jail(s) config which been updated to disk.
* If name is specified, work on the jail. If name is empty string, work on all.
* If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current
* default route, so if route change on jailhost this will reflect on jail next
* start)
*******************************************************************************/
func writeConfigToDisk(j *Jail, changeauto bool) {
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
//fmt.Printf("DEBUG: Will write config to disk, with content:\n")
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
}
}
func (j Jail) WriteConfigToDisk(changeauto bool) {
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
}
}
/****************************************************************************** /******************************************************************************
* Return the quantity of jails with the name passed as parameter * Return the quantity of jails with the name passed as parameter
*****************************************************************************/ *****************************************************************************/
@ -837,7 +1228,7 @@ func countOfJailsWithThisName(name string) int {
func isNameDistinctive(name string, jails []Jail) bool { func isNameDistinctive(name string, jails []Jail) bool {
_, err := getJailFromArray(name, jails) _, err := getJailFromArray(name, []string{""}, jails)
if err != nil { if err != nil {
return false return false
} else { } else {