Compare commits

..

5 Commits

7 changed files with 96 additions and 14 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

@ -132,6 +132,11 @@ To update jail patch version, use gocage update :
`gocage update test` `gocage update test`
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 :
@ -214,6 +219,5 @@ TODO
---------- ----------
gocage upgrade gocage upgrade
gocage create gocage create
gocage destroy
gocage init gocage init
create default pool with defaults.json create default pool with defaults.json

57
cmd/destroy.go Normal file
View File

@ -0,0 +1,57 @@
package cmd
import (
"fmt"
//"log"
//"errors"
"strings"
)
func DestroyJails(args []string) {
for _, a := range args {
cj, err := getJailFromArray(a, 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)})
}
// 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
}
//TODO: Delete jail named directory
}
}

View File

@ -61,7 +61,7 @@ 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)
} }
} }
@ -75,7 +75,7 @@ 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(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)
} }
} }
@ -147,7 +147,7 @@ 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
} }
@ -163,7 +163,7 @@ 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
} }
@ -179,7 +179,7 @@ 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
} }

View File

@ -15,7 +15,7 @@ import (
) )
const ( const (
gVersion = "0.34" gVersion = "0.35"
// TODO : Get from $jail_zpool/defaults.json // TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000 MIN_DYN_DEVFS_RULESET = 1000
@ -100,7 +100,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 +109,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",
@ -343,7 +343,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")
@ -381,7 +381,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)

View File

@ -1176,7 +1176,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)

View File

@ -568,7 +568,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 +576,26 @@ 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 {
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
}
/***************************************************************************** /*****************************************************************************
* *