Compare commits
13 Commits
v0.29f
...
667c73216e
Author | SHA1 | Date | |
---|---|---|---|
667c73216e | |||
9e506145a8 | |||
d636d963ff | |||
56b4d8ea84 | |||
abaa4a11f9 | |||
74602dc0df | |||
be756edea7 | |||
546382ded7 | |||
07eccffbd1 | |||
7809107ea4 | |||
5ab0a59db4 | |||
1b27753718 | |||
c97f5317dd |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
|||||||
gocage
|
gocage
|
||||||
go.sum
|
go.sum
|
||||||
|
|
||||||
|
28
README.md
28
README.md
@ -143,7 +143,7 @@ gocage datastore list
|
|||||||
+------------+-------------+------------+-----------+----------+------------+
|
+------------+-------------+------------+-----------+----------+------------+
|
||||||
</pre></code>
|
</pre></code>
|
||||||
|
|
||||||
#### Filter datastores
|
### Filter datastores
|
||||||
As with jails and snapshots, you can filter by name:
|
As with jails and snapshots, you can filter by name:
|
||||||
<pre><code>
|
<pre><code>
|
||||||
gocage datastore list iocage
|
gocage datastore list iocage
|
||||||
@ -154,7 +154,7 @@ gocage datastore list iocage
|
|||||||
+------------+-------------+------------+-----------+----------+------------+
|
+------------+-------------+------------+-----------+----------+------------+
|
||||||
</pre></code>
|
</pre></code>
|
||||||
|
|
||||||
#### Sort datastores
|
### Sort datastores
|
||||||
You can sort datastores:
|
You can sort datastores:
|
||||||
<pre><code>
|
<pre><code>
|
||||||
gocage datastore list -s -Available
|
gocage datastore list -s -Available
|
||||||
@ -175,6 +175,11 @@ With multi datastore comes the need to migrate a jail between datastores.
|
|||||||
Migration can be done with a minimal downtime, using zfs differential send/receive.
|
Migration can be done with a minimal downtime, using zfs differential send/receive.
|
||||||
Source jail datasets are sent to the destination datastore, jail is stopped and a last differential sync is done before starting jail on new datastore.
|
Source jail datasets are sent to the destination datastore, jail is stopped and a last differential sync is done before starting jail on new datastore.
|
||||||
|
|
||||||
|
### Warning
|
||||||
|
Be aware the moment you migrate a jail to another datastore than /iocage default, you lose compatibility with iocage.
|
||||||
|
Then you need to disable iocage service, and enable gocage so the jails will start automatically at boot.
|
||||||
|
Also make sure, if you don't destroy source jail, that it won't have the "boot" property set or you will have the 2 jails up at boot.
|
||||||
|
|
||||||
<pre><code>
|
<pre><code>
|
||||||
gocage migrate -d fastiocage srv-random
|
gocage migrate -d fastiocage srv-random
|
||||||
Snapshot data/iocage/jails/srv-random: Done
|
Snapshot data/iocage/jails/srv-random: Done
|
||||||
@ -182,3 +187,22 @@ Snapshot data/iocage/jails/srv-random/root: Done
|
|||||||
Migrate jail config dataset to fastdata/iocage/jails/srv-random: Done
|
Migrate jail config dataset to fastdata/iocage/jails/srv-random: Done
|
||||||
Migrate jail filesystem dataset to fastdata/iocage/jails/srv-random/root: Done
|
Migrate jail filesystem dataset to fastdata/iocage/jails/srv-random/root: Done
|
||||||
</pre></code>
|
</pre></code>
|
||||||
|
|
||||||
|
|
||||||
|
Fetch
|
||||||
|
----------
|
||||||
|
Files can be fetched from custom repository, or from local directory with "from" option.
|
||||||
|
For example if you destroyed releases/12.3-RELEASE and still have the downloaded files in /iocage/download/12.3-RELEASE:
|
||||||
|
<pre><code>
|
||||||
|
gocage fetch -r 12.3 -o iocage --from file:/iocage/download
|
||||||
|
</pre></code>
|
||||||
|
|
||||||
|
|
||||||
|
TODO
|
||||||
|
----------
|
||||||
|
gocage update
|
||||||
|
gocage upgrade
|
||||||
|
gocage create
|
||||||
|
gocage destroy
|
||||||
|
gocage init
|
||||||
|
create default pool with defaults.json
|
||||||
|
374
cmd/fetch.go
Normal file
374
cmd/fetch.go
Normal file
@ -0,0 +1,374 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
//"errors"
|
||||||
|
"strings"
|
||||||
|
"net/http"
|
||||||
|
//"archive/tar"
|
||||||
|
"encoding/hex"
|
||||||
|
"crypto/sha256"
|
||||||
|
|
||||||
|
//"github.com/ulikunitz/xz"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ReleaseServer = "download.freebsd.org"
|
||||||
|
ReleaseRootDir = "ftp/releases"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
FetchFiles = []string{"base.txz", "lib32.txz", "src.txz"}
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: Check if files already exist
|
||||||
|
// Fetch release files, verify, put in datastore under ${datastore}/download
|
||||||
|
// Only support http and file protocols
|
||||||
|
func fetchRelease(release string, proto string, arch string, datastore string, fetchFrom string) error {
|
||||||
|
var ds Datastore
|
||||||
|
|
||||||
|
log.SetReportCaller(true)
|
||||||
|
|
||||||
|
if len(fetchFrom) > 0 {
|
||||||
|
proto = strings.Split(fetchFrom, ":")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
if false == strings.EqualFold(proto, "http") &&
|
||||||
|
false == strings.EqualFold(proto, "file") {
|
||||||
|
return fmt.Errorf("Unsupported protocol: %s\n", proto)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ds = range gDatastores {
|
||||||
|
if strings.EqualFold(datastore, ds.Name) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if false == strings.EqualFold(datastore, ds.Name) {
|
||||||
|
return fmt.Errorf("Datastore not found: %s\n", datastore)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check datastore have a download dataset, and it is mounted
|
||||||
|
downloadDsName := fmt.Sprintf("%s/download", ds.ZFSDataset)
|
||||||
|
downloadDsMountPoint := fmt.Sprintf("%s/download", ds.Mountpoint)
|
||||||
|
exist, err := doZfsDatasetExist(downloadDsName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error accessing dataset %s: %v\n", downloadDsName, err)
|
||||||
|
}
|
||||||
|
if false == exist {
|
||||||
|
// Then create dataset
|
||||||
|
if err := createZfsDataset(downloadDsName, downloadDsMountPoint, "lz4"); err != nil {
|
||||||
|
return fmt.Errorf("Error creating dataset %s: %v\n", downloadDsName, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create download/XX.X-RELEASE dataset if necessary
|
||||||
|
thisDownloadDsName := fmt.Sprintf("%s/%s-RELEASE", downloadDsName, release)
|
||||||
|
thisDownloadDsMountPoint := fmt.Sprintf("%s/%s-RELEASE", downloadDsMountPoint, release)
|
||||||
|
exist, err = doZfsDatasetExist(thisDownloadDsName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error accessing dataset %s: %v\n", thisDownloadDsName, err)
|
||||||
|
}
|
||||||
|
if false == exist {
|
||||||
|
// Then create dataset
|
||||||
|
if err := createZfsDataset(thisDownloadDsName, thisDownloadDsMountPoint, "lz4"); err != nil {
|
||||||
|
return fmt.Errorf("Error creating dataset %s: %v\n", thisDownloadDsName, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fetchUrl string
|
||||||
|
if len(fetchFrom) > 0 {
|
||||||
|
fetchUrl = fmt.Sprintf("%s/%s-RELEASE", fetchFrom, release)
|
||||||
|
} else {
|
||||||
|
fetchUrl = fmt.Sprintf("%s://%s/%s/%s/%s-RELEASE", proto, ReleaseServer, ReleaseRootDir, arch, release)
|
||||||
|
}
|
||||||
|
log.Debugf("FetchURL = %s", fetchUrl)
|
||||||
|
|
||||||
|
// check if proto/server/arch/release is available
|
||||||
|
if strings.EqualFold(proto, "http") {
|
||||||
|
resp, err := http.Get(fetchUrl)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Can not get %s: %v\n", fetchUrl, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("Get %s returned %d, check release name\n", fetchUrl, resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch files
|
||||||
|
// Get MANIFEST so we get sha256 sums
|
||||||
|
if err := fetchFile(proto, fetchUrl, "MANIFEST", thisDownloadDsMountPoint, []byte{}); err != nil {
|
||||||
|
return fmt.Errorf("%v\n", err)
|
||||||
|
}
|
||||||
|
// Build an array of "file;checksum"
|
||||||
|
checksumMap, err := buildFileChecksumFromManifest(fmt.Sprintf("%s/MANIFEST", thisDownloadDsMountPoint), FetchFiles)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch remaining files, verify integrity and write to disk
|
||||||
|
for f, c := range checksumMap {
|
||||||
|
if err := fetchFile(proto, fetchUrl, f, thisDownloadDsMountPoint, c); err != nil {
|
||||||
|
return fmt.Errorf("%v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract release files stored in iocage/download/$RELEASE/ to iocage/releases/$RELEASE/root/
|
||||||
|
func extractRelease(release string, datastore string) {
|
||||||
|
log.SetReportCaller(true)
|
||||||
|
|
||||||
|
var ds Datastore
|
||||||
|
|
||||||
|
for _, ds = range gDatastores {
|
||||||
|
if strings.EqualFold(datastore, ds.Name) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if false == strings.EqualFold(datastore, ds.Name) {
|
||||||
|
fmt.Printf("Datastore not found: %s\n", datastore)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check datastore have a releases dataset, and it is mounted
|
||||||
|
releaseDsName := fmt.Sprintf("%s/releases", ds.ZFSDataset)
|
||||||
|
releaseDsMountPoint := fmt.Sprintf("%s/releases", ds.Mountpoint)
|
||||||
|
exist, err := doZfsDatasetExist(releaseDsName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error accessing dataset %s: %v\n", releaseDsName, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if false == exist {
|
||||||
|
// Then create dataset
|
||||||
|
if err := createZfsDataset(releaseDsName, releaseDsMountPoint, "lz4"); err != nil {
|
||||||
|
fmt.Printf("Error creating dataset %s: %v\n", releaseDsName, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create releases/XX.X-RELEASE dataset if necessary
|
||||||
|
thisReleaseDsName := fmt.Sprintf("%s/%s-RELEASE", releaseDsName, release)
|
||||||
|
thisReleaseDsMountPoint := fmt.Sprintf("%s/%s-RELEASE", releaseDsMountPoint, release)
|
||||||
|
exist, err = doZfsDatasetExist(thisReleaseDsName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error accessing dataset %s: %v\n", thisReleaseDsName, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if false == exist {
|
||||||
|
// Then create dataset
|
||||||
|
if err := createZfsDataset(thisReleaseDsName, thisReleaseDsMountPoint, "lz4"); err != nil {
|
||||||
|
fmt.Printf("Error creating dataset %s: %v\n", thisReleaseDsName, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create releases/XX.X-RELEASE/root dataset if necessary
|
||||||
|
thisReleaseRootDsName := fmt.Sprintf("%s/root", thisReleaseDsName)
|
||||||
|
thisReleaseRootDsMountPoint := fmt.Sprintf("%s/root", thisReleaseDsMountPoint)
|
||||||
|
exist, err = doZfsDatasetExist(thisReleaseRootDsName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error accessing dataset %s: %v\n", thisReleaseRootDsName, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if false == exist {
|
||||||
|
// Then create dataset
|
||||||
|
if err := createZfsDataset(thisReleaseRootDsName, thisReleaseRootDsMountPoint, "lz4"); err != nil {
|
||||||
|
fmt.Printf("Error creating dataset %s: %v\n", thisReleaseRootDsName, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now extract download/$RELEASE/*.txz to releases/XX.X-RELEASE/root
|
||||||
|
downloadDsMountPoint := fmt.Sprintf("%s/download", ds.Mountpoint)
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s-RELEASE", downloadDsMountPoint, release)
|
||||||
|
|
||||||
|
d, err := os.Open(downloadDir)
|
||||||
|
defer d.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Can not read %s directory: %v\n", downloadDir, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
files, err := d.Readdir(0)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Can not browse %s directory: %v\n", downloadDir, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract every .txz files
|
||||||
|
for _, fi := range files {
|
||||||
|
if false == fi.IsDir() {
|
||||||
|
if strings.HasSuffix(fi.Name(), ".txz") {
|
||||||
|
ar := fmt.Sprintf("%s/%s", downloadDir, fi.Name())
|
||||||
|
fmt.Printf("Extracting file %s... ", ar)
|
||||||
|
// pure Go method, sorry this is so slow. Also I did not handle permissions in this
|
||||||
|
/* f, err := os.Open(ar)
|
||||||
|
defer f.Close()
|
||||||
|
if err != nil {
|
||||||
|
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 {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
switch hdr.Typeflag {
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchFile(proto, baseUrl, fileName, storeDir string, checksum []byte) error {
|
||||||
|
// Check storeDir exist
|
||||||
|
_, err := os.Stat(storeDir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("Directory does not exist: %s\n", storeDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/%s", baseUrl, fileName)
|
||||||
|
fmt.Printf("Fetching %s...", url)
|
||||||
|
|
||||||
|
var body []byte
|
||||||
|
if strings.EqualFold(proto, "http") {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf(" Error\n")
|
||||||
|
return fmt.Errorf("Can not get %s: %v\n", url, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err = io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Can not read %s response body: %v\n", url, err)
|
||||||
|
}
|
||||||
|
} else if strings.EqualFold(proto, "file") {
|
||||||
|
url = strings.Replace(url, "file:", "", 1)
|
||||||
|
f, err := os.Open(url)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error accessing file %s", url)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
body, err = io.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Can not read file %s: %v\n", url, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check integrity
|
||||||
|
if len(checksum) > 0 {
|
||||||
|
err = checkIntegrity(body, checksum)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Error checking integrity")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dest := fmt.Sprintf("%s/%s", storeDir, fileName)
|
||||||
|
|
||||||
|
f, err := os.Create(dest) // creates a file at current directory
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Can not create file %s: %v\n", dest, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
f.Write(body)
|
||||||
|
fmt.Printf(" Done\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func checkIntegrity(data []byte, checksum []byte) error {
|
||||||
|
sum := sha256.Sum256(data)
|
||||||
|
|
||||||
|
if false == bytes.Equal(checksum[:],sum[:]) {
|
||||||
|
return fmt.Errorf("Invalid checksum: %x != %x", sum, checksum)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get checksum from manifest, for each file in fileList
|
||||||
|
/* MANIFEST format:
|
||||||
|
* base-dbg.txz a5b51f3d54686509e91ca9c30e9f1cd93dc757f25c643609b3c35e7119c0531d 1654 base_dbg "Base system (Debugging)" off
|
||||||
|
* base.txz e85b256930a2fbc04b80334106afecba0f11e52e32ffa197a88d7319cf059840 26492 base "Base system (MANDATORY)" on
|
||||||
|
* kernel-dbg.txz 6b47a6cb83637af1f489aa8cdb802d9db936ea864887188cfc69d8075762214e 912 kernel_dbg "Kernel (Debugging)" on
|
||||||
|
*/
|
||||||
|
func buildFileChecksumFromManifest(manifest string, fileList []string) (map[string][]byte, error) {
|
||||||
|
var ckarr = make(map[string][]byte)
|
||||||
|
|
||||||
|
rm, err := os.Open(manifest)
|
||||||
|
if err != nil {
|
||||||
|
return ckarr, fmt.Errorf("Unable to open MANIFEST: %v", err)
|
||||||
|
}
|
||||||
|
fscan := bufio.NewScanner(rm)
|
||||||
|
fscan.Split(bufio.ScanLines)
|
||||||
|
|
||||||
|
// For each MANIFEST line...
|
||||||
|
for fscan.Scan() {
|
||||||
|
fields := strings.Fields(fscan.Text())
|
||||||
|
fn := fields[0]
|
||||||
|
fck := fields[1]
|
||||||
|
hexSum, err := hex.DecodeString(fck)
|
||||||
|
if err != nil {
|
||||||
|
return ckarr, fmt.Errorf("Invalid value for checksum %s", fck)
|
||||||
|
}
|
||||||
|
// ... Find the corresponding file in fileList, then add to checksum array ckarr
|
||||||
|
for _, f := range fileList {
|
||||||
|
if strings.EqualFold(f, fn) {
|
||||||
|
ckarr[fn] = hexSum
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ckarr) < len(fileList) {
|
||||||
|
return ckarr, fmt.Errorf("Missing file in MANIFEST")
|
||||||
|
}
|
||||||
|
return ckarr, nil
|
||||||
|
}
|
@ -179,6 +179,15 @@ func getHostId() (string, error) {
|
|||||||
return strings.Split(string(content), "\n")[0], nil
|
return strings.Split(string(content), "\n")[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getArch() (string, error) {
|
||||||
|
out, err := executeCommand("/usr/bin/uname -p")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Error executing \"/usr/bin/uname -p\": %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Split(out, "\n")[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
func getFreeBSDVersion() (FreeBSDVersion, error) {
|
func getFreeBSDVersion() (FreeBSDVersion, error) {
|
||||||
var version FreeBSDVersion
|
var version FreeBSDVersion
|
||||||
regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?`
|
regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?`
|
||||||
@ -219,6 +228,9 @@ func NewJailHost() (JailHost, error) {
|
|||||||
if jh.hostname, err = getHostname(); err != nil {
|
if jh.hostname, err = getHostname(); err != nil {
|
||||||
return jh, err
|
return jh, err
|
||||||
}
|
}
|
||||||
|
if jh.arch, err = getArch(); err != nil {
|
||||||
|
return jh, err
|
||||||
|
}
|
||||||
if jh.hostid, err = getHostId(); err != nil {
|
if jh.hostid, err = getHostId(); err != nil {
|
||||||
return jh, err
|
return jh, err
|
||||||
}
|
}
|
||||||
|
@ -273,6 +273,7 @@ func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
|
|||||||
j.JID = rj.Jid
|
j.JID = rj.Jid
|
||||||
j.Running = true
|
j.Running = true
|
||||||
j.InternalName = rj.Name
|
j.InternalName = rj.Name
|
||||||
|
j.Devfs_ruleset = rj.Devfs_ruleset
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
52
cmd/root.go
52
cmd/root.go
@ -10,11 +10,11 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
// TODO : Use log
|
// TODO : Use log
|
||||||
//log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
gVersion = "0.29g"
|
gVersion = "0.32a"
|
||||||
|
|
||||||
// TODO : Get from $jail_zpool/defaults.json
|
// TODO : Get from $jail_zpool/defaults.json
|
||||||
MIN_DYN_DEVFS_RULESET = 1000
|
MIN_DYN_DEVFS_RULESET = 1000
|
||||||
@ -27,6 +27,7 @@ var (
|
|||||||
|
|
||||||
gUseSudo bool
|
gUseSudo bool
|
||||||
gForce bool
|
gForce bool
|
||||||
|
gDebug bool
|
||||||
|
|
||||||
gConfigFile string
|
gConfigFile string
|
||||||
gDisplayJColumns string
|
gDisplayJColumns string
|
||||||
@ -50,6 +51,11 @@ var (
|
|||||||
gMigrateDestDatastore string
|
gMigrateDestDatastore string
|
||||||
gYesToAll bool
|
gYesToAll bool
|
||||||
|
|
||||||
|
gFetchRelease string
|
||||||
|
gFetchIntoDS string
|
||||||
|
gFetchFrom string
|
||||||
|
|
||||||
|
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
Use: "gocage",
|
Use: "gocage",
|
||||||
Short: "GoCage is a FreeBSD Jail management tool",
|
Short: "GoCage is a FreeBSD Jail management tool",
|
||||||
@ -109,7 +115,11 @@ ex: gocage list srv-db srv-web`,
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Load inventory
|
// Load inventory
|
||||||
ListJails(args, false)
|
ListJails(args, false)
|
||||||
StopJail(args)
|
if len(args) == 0 {
|
||||||
|
StopAllRunningJails()
|
||||||
|
} else {
|
||||||
|
StopJail(args)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,11 +129,17 @@ ex: gocage list srv-db srv-web`,
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Load inventory
|
// Load inventory
|
||||||
ListJails(args, false)
|
ListJails(args, false)
|
||||||
StartJail(args)
|
if len(args) == 0 {
|
||||||
|
StartJailsAtBoot()
|
||||||
|
} else {
|
||||||
|
StartJail(args)
|
||||||
|
}
|
||||||
WriteConfigToDisk(false)
|
WriteConfigToDisk(false)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
restartCmd = &cobra.Command{
|
restartCmd = &cobra.Command{
|
||||||
Use: "restart",
|
Use: "restart",
|
||||||
Short: "restart jail",
|
Short: "restart jail",
|
||||||
@ -274,6 +290,19 @@ You can specify multiple datastores.`,
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fetchCmd = &cobra.Command{
|
||||||
|
Use: "fetch",
|
||||||
|
Short: "Fetch FreeBSD release to local datastore",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
err := fetchRelease(gFetchRelease, "http", gJailHost.arch, gFetchIntoDS, gFetchFrom)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%v\n", err)
|
||||||
|
} else {
|
||||||
|
extractRelease(gFetchRelease, gFetchIntoDS)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
testCmd = &cobra.Command{
|
testCmd = &cobra.Command{
|
||||||
Use: "test",
|
Use: "test",
|
||||||
Short: "temporary command to test some code snippet",
|
Short: "temporary command to test some code snippet",
|
||||||
@ -293,6 +322,7 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/usr/local/etc/gocage.conf.yml", "GoCage configuration file")
|
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/usr/local/etc/gocage.conf.yml", "GoCage configuration file")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&gUseSudo, "sudo", "u", false, "Use sudo to run commands")
|
rootCmd.PersistentFlags().BoolVarP(&gUseSudo, "sudo", "u", false, "Use sudo to run commands")
|
||||||
rootCmd.PersistentFlags().StringVarP(&gTimeZone, "timezone", "t", "", "Specify timezone. Will get from /var/db/zoneinfo if not set.")
|
rootCmd.PersistentFlags().StringVarP(&gTimeZone, "timezone", "t", "", "Specify timezone. Will get from /var/db/zoneinfo if not set.")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&gDebug, "debug", false, "Debug mode")
|
||||||
|
|
||||||
// Command dependant switches
|
// Command dependant switches
|
||||||
|
|
||||||
@ -326,6 +356,13 @@ 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(&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.MarkFlagRequired("release")
|
||||||
|
fetchCmd.MarkFlagRequired("datastore")
|
||||||
|
|
||||||
|
|
||||||
// Now declare commands
|
// Now declare commands
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
rootCmd.AddCommand(listCmd)
|
rootCmd.AddCommand(listCmd)
|
||||||
@ -340,6 +377,7 @@ func init() {
|
|||||||
rootCmd.AddCommand(snapshotCmd)
|
rootCmd.AddCommand(snapshotCmd)
|
||||||
rootCmd.AddCommand(migrateCmd)
|
rootCmd.AddCommand(migrateCmd)
|
||||||
rootCmd.AddCommand(datastoreCmd)
|
rootCmd.AddCommand(datastoreCmd)
|
||||||
|
rootCmd.AddCommand(fetchCmd)
|
||||||
|
|
||||||
rootCmd.AddCommand(testCmd)
|
rootCmd.AddCommand(testCmd)
|
||||||
|
|
||||||
@ -414,6 +452,12 @@ func initConfig() {
|
|||||||
fmt.Printf("More than 3 sort criteria is not supported!\n")
|
fmt.Printf("More than 3 sort criteria is not supported!\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if gDebug {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
log.Debugf("Debug mode enabled\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
|
33
cmd/start.go
33
cmd/start.go
@ -1049,6 +1049,34 @@ func cleanAfterStartCrash() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start all jails with boot=true, in priority order
|
||||||
|
func StartJailsAtBoot() {
|
||||||
|
var startList []Jail
|
||||||
|
|
||||||
|
// Get boot enabled jails
|
||||||
|
for _, j := range gJails {
|
||||||
|
if j.Config.Boot > 0 {
|
||||||
|
startList = append(startList, j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Order by priority
|
||||||
|
js := initJailSortStruct()
|
||||||
|
fct, _, err := getStructFieldValue(js, "Config.PriorityInc")
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("ERROR getting JailSort struct field \"Config.PriorityInc\"\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
JailsOrderedBy(fct.Interface().(jailLessFunc)).Sort(startList)
|
||||||
|
|
||||||
|
for _, j := range startList {
|
||||||
|
jFullName := fmt.Sprintf("%s/%s", j.Datastore, j.Name)
|
||||||
|
log.Debugf("Starting %s with priority %s\n", jFullName, j.Config.Priority)
|
||||||
|
StartJail([]string{jFullName})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Start jail:
|
Start jail:
|
||||||
Check jail fstab?
|
Check jail fstab?
|
||||||
@ -1088,7 +1116,7 @@ func StartJail(args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cj.Running == true {
|
if cj.Running == true {
|
||||||
fmt.Printf("Jail %s is already running!\n", cj.Name)
|
fmt.Printf("Jail %s/%s is already running!\n", cj.Datastore, cj.Name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1303,8 +1331,9 @@ func StartJail(args []string) {
|
|||||||
fmt.Printf(" > Start jail: OK\n")
|
fmt.Printf(" > Start jail: OK\n")
|
||||||
fmt.Printf(" > With devfs ruleset %d\n", dynrs)
|
fmt.Printf(" > With devfs ruleset %d\n", dynrs)
|
||||||
|
|
||||||
// Update running state and JID
|
// Update running state, JID and Devfs_ruleset
|
||||||
cj.Running = true
|
cj.Running = true
|
||||||
|
cj.Devfs_ruleset = dynrs
|
||||||
rjails, err := jail.GetJails()
|
rjails, err := jail.GetJails()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: Unable to list running jails\n")
|
fmt.Printf("Error: Unable to list running jails\n")
|
||||||
|
45
cmd/stop.go
45
cmd/stop.go
@ -10,6 +10,8 @@ import (
|
|||||||
//"reflect"
|
//"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO : Use SYS_RCTL_GET_RACCT syscall
|
// TODO : Use SYS_RCTL_GET_RACCT syscall
|
||||||
@ -166,6 +168,33 @@ func stopJail(jail *Jail) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop all running jails by reverse priority
|
||||||
|
func StopAllRunningJails() {
|
||||||
|
var stopList []Jail
|
||||||
|
|
||||||
|
// Get boot enabled jails
|
||||||
|
for _, j := range gJails {
|
||||||
|
if j.Running == true {
|
||||||
|
stopList = append(stopList, j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Order by priority
|
||||||
|
js := initJailSortStruct()
|
||||||
|
fct, _, err := getStructFieldValue(js, "Config.PriorityDec")
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("ERROR getting JailSort struct field \"Config.PriorityDec\"\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
JailsOrderedBy(fct.Interface().(jailLessFunc)).Sort(stopList)
|
||||||
|
|
||||||
|
for _, j := range stopList {
|
||||||
|
jFullName := fmt.Sprintf("%s/%s", j.Datastore, j.Name)
|
||||||
|
log.Debugf("Stopping %s with priority %s\n", jFullName, j.Config.Priority)
|
||||||
|
StopJail([]string{jFullName})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Stop jail:
|
Stop jail:
|
||||||
Remove rctl rules
|
Remove rctl rules
|
||||||
@ -258,19 +287,12 @@ func StopJail(args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get currently used ruleset from /var/run/jail.$internal_name.conf
|
fmt.Printf(" > Remove devfs ruleset %d: \n", cj.Devfs_ruleset)
|
||||||
ruleset, err := getValueFromRunningConfig(cj.InternalName, "devfs_ruleset")
|
err = deleteDevfsRuleset(cj.Devfs_ruleset)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("ERROR getting current devfs ruleset: %s\n", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rsi, _ := strconv.Atoi(ruleset)
|
|
||||||
fmt.Printf(" > Remove devfs ruleset %d: \n", rsi)
|
|
||||||
err = deleteDevfsRuleset(rsi)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR: %s\n", err.Error())
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf(" > Remove devfsruleset %d: OK\n", rsi)
|
fmt.Printf(" > Remove devfsruleset %d: OK\n", cj.Devfs_ruleset)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" > Stop jail %s:\n", cj.Name)
|
fmt.Printf(" > Stop jail %s:\n", cj.Name)
|
||||||
@ -372,6 +394,9 @@ func StopJail(args []string) {
|
|||||||
if err = setStructFieldValue(&gJails[i], "InternalName", ""); err != nil {
|
if err = setStructFieldValue(&gJails[i], "InternalName", ""); err != nil {
|
||||||
fmt.Printf("ERROR: clearing InternalName property: %s\n", err.Error())
|
fmt.Printf("ERROR: clearing InternalName property: %s\n", err.Error())
|
||||||
}
|
}
|
||||||
|
if err = setStructFieldValue(&gJails[i], "Devfs_ruleset", "0"); err != nil {
|
||||||
|
fmt.Printf("ERROR: setting Devfs_ruleset property to 0: %s\n", err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ type Jail struct {
|
|||||||
Running bool
|
Running bool
|
||||||
// No need, Config.Release always represent what is running (plus it know release for non-running jails)
|
// No need, Config.Release always represent what is running (plus it know release for non-running jails)
|
||||||
//Release string
|
//Release string
|
||||||
|
Devfs_ruleset int // The effective devfs ruleset generated at runtime
|
||||||
Zpool string
|
Zpool string
|
||||||
Datastore string
|
Datastore string
|
||||||
}
|
}
|
||||||
@ -212,6 +213,7 @@ type FreeBSDVersion struct {
|
|||||||
type JailHost struct {
|
type JailHost struct {
|
||||||
hostname string
|
hostname string
|
||||||
hostid string
|
hostid string
|
||||||
|
arch string
|
||||||
default_gateway4 string
|
default_gateway4 string
|
||||||
default_gateway6 string
|
default_gateway6 string
|
||||||
default_interface string
|
default_interface string
|
||||||
@ -247,6 +249,8 @@ type JailSort struct {
|
|||||||
DatastoreDec jailLessFunc
|
DatastoreDec jailLessFunc
|
||||||
ZpoolInc jailLessFunc
|
ZpoolInc jailLessFunc
|
||||||
ZpoolDec jailLessFunc
|
ZpoolDec jailLessFunc
|
||||||
|
Devfs_rulesetInc jailLessFunc
|
||||||
|
Devfs_rulesetDec jailLessFunc
|
||||||
Config JailConfigSort
|
Config JailConfigSort
|
||||||
}
|
}
|
||||||
|
|
||||||
|
28
cmd/utils.go
28
cmd/utils.go
@ -4,7 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
//"log"
|
||||||
"sort"
|
"sort"
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
@ -15,6 +15,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"github.com/google/shlex"
|
"github.com/google/shlex"
|
||||||
"github.com/c2h5oh/datasize"
|
"github.com/c2h5oh/datasize"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -411,6 +412,31 @@ func zfsCopyIncremental(firstsnap string, secondsnap string, dest string) error
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return true if dataset exist, false if not, false & error if anything else
|
||||||
|
func doZfsDatasetExist(dataset string) (bool, error) {
|
||||||
|
cmd := fmt.Sprintf("zfs list %s", dataset)
|
||||||
|
out, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
if strings.HasSuffix(strings.TrimSuffix(out, "\n"), "dataset does not exist") {
|
||||||
|
return false, nil
|
||||||
|
} else {
|
||||||
|
return false, errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create ZFS dataset. mountpoint can be "none", then the dataset won't be mounted
|
||||||
|
func createZfsDataset(dataset, mountpoint, compression string) error {
|
||||||
|
cmd := fmt.Sprintf("zfs create -o mountpoint=%s -o compression=%s %s", mountpoint, compression, dataset)
|
||||||
|
out, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
*
|
*
|
||||||
* rc.conf management
|
* rc.conf management
|
||||||
|
36
jail/main.go
36
jail/main.go
@ -14,16 +14,17 @@ package jail
|
|||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
// "syscall"
|
// "syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Jail struct {
|
type Jail struct {
|
||||||
Name string
|
Name string
|
||||||
Jid int
|
Jid int
|
||||||
Path string
|
Path string
|
||||||
|
Devfs_ruleset int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -33,8 +34,8 @@ func GetJails() ([]Jail, error) {
|
|||||||
var jl Jail
|
var jl Jail
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Make "params" a list of 4 jails parameters
|
// Make "params" a list of 5 jails parameters
|
||||||
params := make([]C.struct_jailparam, 4)
|
params := make([]C.struct_jailparam, 5)
|
||||||
|
|
||||||
// initialize parameter names
|
// initialize parameter names
|
||||||
csname := C.CString("name")
|
csname := C.CString("name")
|
||||||
@ -43,27 +44,31 @@ func GetJails() ([]Jail, error) {
|
|||||||
defer C.free(unsafe.Pointer(csjid))
|
defer C.free(unsafe.Pointer(csjid))
|
||||||
cspath := C.CString("path")
|
cspath := C.CString("path")
|
||||||
defer C.free(unsafe.Pointer(cspath))
|
defer C.free(unsafe.Pointer(cspath))
|
||||||
|
csdevfsrs := C.CString("devfs_ruleset")
|
||||||
|
defer C.free(unsafe.Pointer(csdevfsrs))
|
||||||
cslastjid := C.CString("lastjid")
|
cslastjid := C.CString("lastjid")
|
||||||
defer C.free(unsafe.Pointer(cslastjid))
|
defer C.free(unsafe.Pointer(cslastjid))
|
||||||
|
|
||||||
|
|
||||||
// initialize params struct with parameter names
|
// initialize params struct with parameter names
|
||||||
C.jailparam_init(¶ms[0], csname)
|
C.jailparam_init(¶ms[0], csname)
|
||||||
C.jailparam_init(¶ms[1], csjid)
|
C.jailparam_init(¶ms[1], csjid)
|
||||||
C.jailparam_init(¶ms[2], cspath)
|
C.jailparam_init(¶ms[2], cspath)
|
||||||
|
C.jailparam_init(¶ms[3], csdevfsrs)
|
||||||
|
|
||||||
// The key to retrieve jail. lastjid = 0 returns first jail and its jid as jailparam_get return value
|
// The key to retrieve jail. lastjid = 0 returns first jail and its jid as jailparam_get return value
|
||||||
C.jailparam_init(¶ms[3], cslastjid)
|
C.jailparam_init(¶ms[4], cslastjid)
|
||||||
|
|
||||||
lastjailid := 0
|
lastjailid := 0
|
||||||
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
||||||
defer C.free(unsafe.Pointer(cslastjidval))
|
defer C.free(unsafe.Pointer(cslastjidval))
|
||||||
|
|
||||||
C.jailparam_import(¶ms[3], cslastjidval)
|
C.jailparam_import(¶ms[4], cslastjidval)
|
||||||
|
|
||||||
// loop on existing jails
|
// loop on existing jails
|
||||||
for lastjailid >= 0 {
|
for lastjailid >= 0 {
|
||||||
// get parameter values
|
// get parameter values
|
||||||
lastjailid = int(C.jailparam_get(¶ms[0], 4, 0))
|
lastjailid = int(C.jailparam_get(¶ms[0], 5, 0))
|
||||||
if lastjailid > 0 {
|
if lastjailid > 0 {
|
||||||
nametmp := C.jailparam_export(¶ms[0])
|
nametmp := C.jailparam_export(¶ms[0])
|
||||||
jl.Name = C.GoString(nametmp)
|
jl.Name = C.GoString(nametmp)
|
||||||
@ -75,23 +80,28 @@ func GetJails() ([]Jail, error) {
|
|||||||
// Memory mgmt : Non gere par Go
|
// Memory mgmt : Non gere par Go
|
||||||
C.free(unsafe.Pointer(jidtmp))
|
C.free(unsafe.Pointer(jidtmp))
|
||||||
|
|
||||||
pathtmp := C.jailparam_export(¶ms[2])
|
pathtmp := C.jailparam_export(¶ms[2])
|
||||||
jl.Path = C.GoString(pathtmp)
|
jl.Path = C.GoString(pathtmp)
|
||||||
// Memory mgmt : Non gere par Go
|
// Memory mgmt : Non gere par Go
|
||||||
C.free(unsafe.Pointer(pathtmp))
|
C.free(unsafe.Pointer(pathtmp))
|
||||||
|
|
||||||
|
drstmp := C.jailparam_export(¶ms[3])
|
||||||
|
jl.Devfs_ruleset, _ = strconv.Atoi(C.GoString(drstmp))
|
||||||
|
// Memory mgmt : Non gere par Go
|
||||||
|
C.free(unsafe.Pointer(drstmp))
|
||||||
|
|
||||||
jls = append(jls, jl)
|
jls = append(jls, jl)
|
||||||
//log.Debug("Got jid " + strconv.Itoa(jl.jid) + " with name " + jl.name)
|
//log.Debug("Got jid " + strconv.Itoa(jl.jid) + " with name " + jl.name)
|
||||||
|
|
||||||
// Prepare next loop iteration
|
// Prepare next loop iteration
|
||||||
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
||||||
defer C.free(unsafe.Pointer(cslastjidval))
|
defer C.free(unsafe.Pointer(cslastjidval))
|
||||||
C.jailparam_import(¶ms[3], cslastjidval)
|
C.jailparam_import(¶ms[4], cslastjidval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free 4 items of params list
|
// Free 5 items of params list
|
||||||
C.jailparam_free(¶ms[0], 4)
|
C.jailparam_free(¶ms[0], 5)
|
||||||
|
|
||||||
return jls, err
|
return jls, err
|
||||||
}
|
}
|
||||||
|
43
service/gocage
Executable file
43
service/gocage
Executable file
@ -0,0 +1,43 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# $FreeBSD$
|
||||||
|
#
|
||||||
|
|
||||||
|
# PROVIDE: gocage
|
||||||
|
# REQUIRE: LOGIN cleanvar
|
||||||
|
# KEYWORD: shutdown
|
||||||
|
|
||||||
|
# Add the following lines to /etc/rc.conf to enable :
|
||||||
|
#
|
||||||
|
# gocage_enable="YES"
|
||||||
|
#
|
||||||
|
# gocage_conf="/usr/local/etc/gocage.conf.yml"
|
||||||
|
#
|
||||||
|
|
||||||
|
. /etc/rc.subr
|
||||||
|
|
||||||
|
name="gocage"
|
||||||
|
rcvar=gocage_enable
|
||||||
|
|
||||||
|
# read configuration and set defaults
|
||||||
|
load_rc_config "$name"
|
||||||
|
|
||||||
|
: ${gocage_enable:="NO"}
|
||||||
|
: ${gocage_conf="/usr/local/etc/gocage.conf.yml"}
|
||||||
|
|
||||||
|
start_cmd=${name}_start
|
||||||
|
stop_cmd=${name}_stop
|
||||||
|
|
||||||
|
gocage_start()
|
||||||
|
{
|
||||||
|
echo "Gocage starting jails... "
|
||||||
|
/usr/local/bin/gocage -c ${gocage_conf} start
|
||||||
|
}
|
||||||
|
|
||||||
|
gocage_stop()
|
||||||
|
{
|
||||||
|
echo "Gocage stopping jails... "
|
||||||
|
/usr/local/bin/gocage -c ${gocage_conf} stop
|
||||||
|
}
|
||||||
|
|
||||||
|
run_rc_command "$1"
|
Reference in New Issue
Block a user