Compare commits

...

7 Commits

13 changed files with 604 additions and 138 deletions

View File

@ -12,7 +12,7 @@ import (
func ShellJail(args []string) error {
// We cant shell more than one jail bc we replace gocage execution with jexec, so there wont be no return to gocage
if len(args) > 0 {
cj, err := getJailFromArray(args[0], gJails)
cj, err := getJailFromArray(args[0], []string{"jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail %s: %v\n", args[0], err)
return err

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
}
}

View File

@ -3,13 +3,14 @@ package cmd
import (
"fmt"
//"log"
"time"
//"errors"
"strings"
)
func DestroyJails(args []string) {
for _, a := range args {
cj, err := getJailFromArray(a, gJails)
cj, err := getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
return
@ -27,6 +28,8 @@ func DestroyJails(args []string) {
}
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
@ -51,7 +54,6 @@ func DestroyJails(args []string) {
fmt.Printf("Error deleting config dataset: %s\n", err)
return
}
//TODO: Delete jail named directory
fmt.Printf("Jail %s is no more!\n", cj.Name)
}
}

View File

@ -23,7 +23,9 @@ const (
)
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
@ -66,9 +68,9 @@ func fetchRelease(release string, proto string, arch string, datastore string, f
}
}
// Create download/XX.X-RELEASE dataset if necessary
thisDownloadDsName := fmt.Sprintf("%s/%s-RELEASE", downloadDsName, release)
thisDownloadDsMountPoint := fmt.Sprintf("%s/%s-RELEASE", downloadDsMountPoint, release)
// Create download/XX.X dataset if necessary
thisDownloadDsName := fmt.Sprintf("%s/%s", downloadDsName, release)
thisDownloadDsMountPoint := fmt.Sprintf("%s/%s", downloadDsMountPoint, release)
exist, err = doZfsDatasetExist(thisDownloadDsName)
if err != nil {
return fmt.Errorf("Error accessing dataset %s: %v\n", thisDownloadDsName, err)
@ -82,9 +84,9 @@ func fetchRelease(release string, proto string, arch string, datastore string, f
var fetchUrl string
if len(fetchFrom) > 0 {
fetchUrl = fmt.Sprintf("%s/%s-RELEASE", fetchFrom, release)
fetchUrl = fmt.Sprintf("%s/%s", fetchFrom, release)
} 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)
@ -153,9 +155,9 @@ func extractRelease(release string, datastore string) {
}
}
// Create releases/XX.X-RELEASE dataset if necessary
thisReleaseDsName := fmt.Sprintf("%s/%s-RELEASE", releaseDsName, release)
thisReleaseDsMountPoint := fmt.Sprintf("%s/%s-RELEASE", releaseDsMountPoint, release)
// Create releases/XX.X dataset if necessary
thisReleaseDsName := fmt.Sprintf("%s/%s", releaseDsName, release)
thisReleaseDsMountPoint := fmt.Sprintf("%s/%s", releaseDsMountPoint, release)
exist, err = doZfsDatasetExist(thisReleaseDsName)
if err != nil {
fmt.Printf("Error accessing dataset %s: %v\n", thisReleaseDsName, err)
@ -169,7 +171,7 @@ func extractRelease(release string, datastore string) {
}
}
// Create releases/XX.X-RELEASE/root dataset if necessary
// Create releases/XX.X/root dataset if necessary
thisReleaseRootDsName := fmt.Sprintf("%s/root", thisReleaseDsName)
thisReleaseRootDsMountPoint := fmt.Sprintf("%s/root", thisReleaseDsMountPoint)
exist, err = doZfsDatasetExist(thisReleaseRootDsName)
@ -185,9 +187,9 @@ func extractRelease(release string, datastore string) {
}
}
// 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)
downloadDir := fmt.Sprintf("%s/%s-RELEASE", downloadDsMountPoint, release)
downloadDir := fmt.Sprintf("%s/%s", downloadDsMountPoint, release)
d, err := os.Open(downloadDir)
defer d.Close()
@ -201,67 +203,69 @@ func extractRelease(release string, datastore string) {
return
}
// Extract every .txz files
// Extract every .txz files in FetchFiles
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 isStringInArray(FetchFiles, fi.Name()) {
ar := fmt.Sprintf("%s/%s", downloadDir, fi.Name())
fmt.Printf("Extracting file %s to %s... ", ar, thisReleaseRootDsMountPoint)
// 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 {
log.Fatal(err)
fmt.Printf("Can not open %s: %v\n", ar, err)
return
}
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)
}
// 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")
}
}
*/
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

@ -249,7 +249,7 @@ func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
jailConfPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "config.json")
jailConf, err := getJailConfig(jailConfPath)
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

View File

@ -26,7 +26,7 @@ func MigrateJail(args []string) {
}
for _, jn := range jailNames {
cj, err := getJailFromArray(jn, gJails)
cj, err := getJailFromArray(jn, []string{""}, gJails)
if cj == nil {
fmt.Printf("Error getting jail %s: Not found\n", jn)
return
@ -177,7 +177,7 @@ func CleanMigrateMess(args []string) error {
}
for _, jn := range jailNames {
cj, err := getJailFromArray(jn, gJails)
cj, err := getJailFromArray(jn, []string{""}, gJails)
if cj == nil {
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 {
// Last arg is the jail name
if i == len(args)-1 {
jail, err = getJailFromArray(a, gJails)
jail, err = getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return

View File

@ -14,12 +14,19 @@ import (
)
const (
gVersion = "0.36c"
gVersion = "0.36d"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
)
type createArgs struct {
Release string
BaseTemplate string
Datastore string
JailType string
}
var (
gJailHost JailHost
gJails []Jail
@ -28,6 +35,8 @@ var (
gUseSudo bool
gForce bool
gDebug bool
gCreateArgs createArgs
gConfigFile string
gDisplayJColumns string
@ -79,6 +88,20 @@ It support iocage jails and can coexist with iocage.`,
fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor)
},
}
/* 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{
Use: "list",
@ -317,6 +340,15 @@ You can specify multiple datastores.`,
},
}
createCmd = &cobra.Command{
Use: "create",
Short: "Create jail",
Run: func(cmd *cobra.Command, args []string) {
ListJails(args, false)
CreateJail(args)
},
}
testCmd = &cobra.Command{
Use: "test",
Short: "temporary command to test some code snippet",
@ -329,7 +361,7 @@ You can specify multiple datastores.`,
// TODO : Init log level and log output
func init() {
var err error
cobra.OnInitialize(initConfig)
// Global switches
@ -352,12 +384,12 @@ func init() {
snapshotListCmd.Flags().BoolVarP(&gNoSnapLineSep, "nolinesep", "l", false, "Do not display line separator between snapshots")
snapshotListCmd.Flags().StringVarP(&gFilterSnaps, "filter", "f", "none", "Only display snapshots with these values. Ex: \"gocage snapshot list -f Config.Boot=1\" will only list started on boot jails")
snapshotListCmd.Flags().StringVarP(&gSortSnapFields, "sort", "s", "none", "Display snapshots sorted by field values. Ex: \"gocage snapshot list -s +Jailname,-Used\" will sort snapshots by jail decreasing name, then increasing used space. 3 critera max supported.")
datastoreListCmd.Flags().StringVarP(&gDisplayDColumns, "outcol", "o", "Name,Mountpoint,ZFSDataset,Available,Used,Referenced", "Show these columns in output")
datastoreListCmd.Flags().BoolVarP(&gNoDSLineSep, "nolinesep", "l", false, "Do not display line separator between datastores")
datastoreListCmd.Flags().StringVarP(&gFilterDS, "filter", "f", "none", "Only display datastores with these values. Ex: \"gocage datastore list -f Config.Boot=1\" will only list started on boot jails")
datastoreListCmd.Flags().StringVarP(&gSortDSFields, "sort", "s", "none", "Display datastores sorted by field values. Ex: \"gocage datastore list -s +Jailname,-Used\" will sort snapshots by jail decreasing name, then increasing used space. 3 critera max supported.")
// This is local flag : Only available to gocage snapshot create command
snapshotCreateCmd.Flags().StringVarP(&gSnapshotName, "snapname", "n", "", "Name of the snapshot to create")
snapshotCreateCmd.MarkFlagRequired("snapname")
@ -369,16 +401,20 @@ func init() {
migrateCmd.Flags().StringVarP(&gMigrateDestDatastore, "datastore", "d", "", "Path of destination datastore for jail (Ex: \"/iocage\")")
migrateCmd.Flags().BoolVarP(&gYesToAll, "yes", "y", false, "Answer yes to all questions")
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(&gFetchFrom, "from", "d", "", "Repository to download from. Should contain XY.Z-RELEASE. File protocol supported")
fetchCmd.MarkFlagRequired("release")
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
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(listCmd)
@ -396,6 +432,7 @@ func init() {
rootCmd.AddCommand(fetchCmd)
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(upgradeCmd)
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(testCmd)

View File

@ -330,8 +330,10 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
for _, n := range nics {
// vnet0 is epair0b inside jail
if strings.Contains(n, "vnet") {
n = fmt.Sprintf("%sb", strings.Replace(n, "vnet", "epair", 1))
//if strings.Contains(n, "vnet") {
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)
value := "SYNCDHCP"
@ -342,12 +344,12 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
}
if enable == true {
err := enableRcKeyValue(jail.ConfigPath, key, value)
err := enableRcKeyValue(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), key, value)
if err != nil {
return fmt.Errorf("ERROR setting %s=%s with sysrc for jail %s: %s\n", key, value, jail.Name, err)
}
} else {
err := disableRcKey(jail.ConfigPath, key)
err := disableRcKey(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), key)
if err != nil {
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
// 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)
}
log.Debugf("setupVnetInterfaceHostSide: returning %v\n", epairs)
return epairs, nil
}
func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
func setupVnetInterfaceJailSide(jail *Jail) error {
var jsmac []byte
var err error
@ -928,7 +930,7 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
// inside jail final nic name
jnic := strings.Replace(v[0], "vnet", "epair", 1)
jnic = jnic + "b"
// Get jail side MAC
pname := fmt.Sprintf("Config.%s_mac", nic)
var val *reflect.Value
@ -945,42 +947,39 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
} else {
jsmac = val.Bytes()
}
lasta := strings.LastIndex(hsepair, "a")
jsepair := hsepair[:lasta] + strings.Replace(hsepair[lasta:], "a", "b", 1)
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jsepair, jail.InternalName)
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jnic, jail.InternalName)
_, err := executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err)
}
// Get bridge MTU
mtu, err := gJailHost.GetBridgeMTU(bridge)
if err != nil {
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)
if err != nil {
return fmt.Errorf("Error setting mtu: %v\n", err)
}
// rename epairXXb to epair0b (or opair1b, ...)
cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, 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)
if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err)
}
cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s link %s", jail.Config.Exec_fib,
jail.JID, jnic, hex.EncodeToString(jsmac))
_, err = executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error setting mac: %v\n", err)
}
// TODO: Move outside of this function
// add interface to bridge
if jail.Config.Nat == 0 {
@ -990,16 +989,16 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
return fmt.Errorf("Error adding member %s to %s: %v: %s\n", nic, bridge, err, out)
}
}
// Check we have an IP for the nic, and set it into jail
if len(ip4s[nic]) > 0 {
err = setJailVnetIp(jail, jnic, ip4s[nic])
}
if len(ip6s[nic]) > 0 {
err = setJailVnetIp(jail, jnic, ip6s[nic])
}
// finally up interface
if jail.Config.Nat == 0 {
cmd := fmt.Sprintf("/sbin/ifconfig %s.%d up", nic, jail.JID)
@ -1009,7 +1008,9 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
}
}
}
log.Debugf("setupVnetInterfaceJailSide: return with success\n")
return nil
}
@ -1165,7 +1166,7 @@ func StartJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails)
cj, err = getJailFromArray(a, []string{"jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
@ -1415,28 +1416,28 @@ func StartJail(args []string) {
}
fmt.Printf(" > Setup VNet network:\n")
hsepairs, err := setupVnetInterfaceHostSide(cj);
_, err = setupVnetInterfaceHostSide(cj);
if err != nil {
fmt.Printf("Error setting VNet interface host side: %v\n", err)
return
}
for _, ep := range hsepairs {
if err = setupVnetInterfaceJailSide(cj, ep); err != nil {
fmt.Printf("Error setting VNet interface jail side: %v\n", err)
return
}
if err = setupVnetInterfaceJailSide(cj); err != nil {
fmt.Printf("Error setting VNet interface jail side: %v\n", err)
return
}
fmt.Printf(" > Setup VNet network: OK\n")
// TODO: Handle DHCP
fmt.Printf(" > Setup default ipv4 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter)
out, err := executeCommand(cmd)
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf(" > Setup default ipv4 gateway: OK\n")
// Set default route, unless main network is dhcp
if ! cj.isFirstNetDhcp() {
fmt.Printf(" > Setup default ipv4 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter)
out, err := executeCommand(cmd)
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf(" > Setup default ipv4 gateway: OK\n")
}
}
if cj.Config.Ip6_addr != "none" {
@ -1477,7 +1478,7 @@ func StartJail(args []string) {
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)
err := executeCommandNonBlocking(cmd)
if err != nil && len(out) > 0 {
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf(" > Start services: OK\n")

View File

@ -270,7 +270,7 @@ func StopJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails)
cj, err = getJailFromArray(a, []string{"jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue

View File

@ -53,7 +53,7 @@ func UpdateJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails)
cj, err = getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue

View File

@ -91,7 +91,7 @@ func UpgradeJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, gJails)
cj, err = getJailFromArray(a, []string{""}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue

View File

@ -24,6 +24,141 @@ const (
ifconfigipv4re = `inet[[:space:]](` + ipv4re + `)`
// Maximum thread qty for start/stop
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 +353,8 @@ func executeCommand(cmdline string) (string, error) {
// else
word = word + string(c)
}
log.Debugf("executeCommand: %s\n", strings.Join(cmd, " "))
if len(cmd) > 1 {
out, err = exec.Command(cmd[0], cmd[1:]...).CombinedOutput()
@ -545,8 +682,11 @@ func zfsSnapshot(dataset string, snapname string) error {
return nil
}
// Copy snapshot to a new dataset
// TODO : Intercept death of sending process, then kill receiving
func zfsCopy(src string, dest string) error {
// First, declare sending process & pipe
log.Debugf("Execute: zfs send %s\n", src)
cmd_send := exec.Command("zfs", "send", src)
stdout_send, err := cmd_send.StdoutPipe()
if err != nil {
@ -555,6 +695,7 @@ func zfsCopy(src string, dest string) error {
}
// then declare receiving process & pipe
log.Debugf("Execute: zfs receive %s\n", dest)
cmd_recv := exec.Command("zfs", "receive", dest)
stdin_recv, err := cmd_recv.StdinPipe()
if err != nil {
@ -568,16 +709,19 @@ func zfsCopy(src string, dest string) error {
// then start processes and wait for finish
if err := cmd_recv.Start(); err != nil {
//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))
}
//fmt.Printf("DEBUG: Start \"zfs send %s\"\n", dsconf)
if err := cmd_send.Start(); err != nil {
//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))
}
//fmt.Printf("DEBUG: Wait for zfs send to finish\n")
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)
return errors.New(fmt.Sprintf("send halted with: %v\n", err))
}
@ -668,6 +812,7 @@ func zfsGetDatasetByMountpoint(mountpoint string) (string, error) {
// 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 {
@ -676,6 +821,30 @@ func zfsDestroy(dataset string) error {
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
}
/*****************************************************************************
*
* rc.conf management
@ -818,7 +987,7 @@ func getValueFromRunningConfig(jname string, param string) (string, error) {
if err != nil {
return "", err
}
for _, line := range strings.Split(string(content), "\n") {
if strings.Contains(line, fmt.Sprintf("%s = ", param)) {
split := strings.Split(line, "=")
@ -833,7 +1002,7 @@ func getValueFromRunningConfig(jname string, param string) (string, error) {
}
}
}
return "", fmt.Errorf("Parameter not found: %s", param)
}
@ -852,14 +1021,29 @@ func isStringInArray(strarr []string, searched string) bool {
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
* 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 jails []Jail
if (len(jailtypes) == 1 && len(jailtypes[0]) == 0) || len(jailtypes) == 0 {
jailtypes = []string{"jail", "basetpl"}
}
if strings.Contains(name, "/") {
split := strings.Split(name, "/")
if len(split) != 2 {
@ -872,16 +1056,17 @@ func getJailFromArray(name string, jarray []Jail) (*Jail, error) {
}
for i, j := range jarray {
//if jail == j.Name {
if strings.HasPrefix(j.Name, jail) {
if len(ds) > 0 {
if strings.EqualFold(ds, j.Datastore) {
return &jarray[i], nil
if isStringInArray(jailtypes, j.Config.Jailtype) {
if len(ds) > 0 {
if strings.EqualFold(ds, j.Datastore) {
return &jarray[i], nil
} else {
continue
}
} else {
continue
jails = append(jails, j)
}
} else {
jails = append(jails, j)
}
}
}
@ -913,7 +1098,7 @@ func setJailConfigUpdated(jail *Jail) error {
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 {
return err
}
@ -987,6 +1172,47 @@ func writeConfigToDisk(j *Jail, changeauto bool) {
}
}
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
*****************************************************************************/
@ -1002,7 +1228,7 @@ func countOfJailsWithThisName(name string) int {
func isNameDistinctive(name string, jails []Jail) bool {
_, err := getJailFromArray(name, jails)
_, err := getJailFromArray(name, []string{""}, jails)
if err != nil {
return false
} else {