Add datastore to snapshots, force Datastore display when jail exist on multi datastores

This commit is contained in:
yo
2022-06-18 18:24:09 +02:00
parent 1c04f62ed8
commit 9218ffafe1
6 changed files with 145 additions and 92 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"reflect"
"time"
@ -23,6 +24,10 @@ func ListJailsSnapshots(args []string) {
/**************************************************************/
if len(args) > 0 {
for _, a := range args {
/*if countOfJailsWithThisName(a) > 1 {
fmt.Printf("Nope")
return
}*/
jailNames = append(jailNames, a)
}
}
@ -99,23 +104,27 @@ func listJailSnapshots(jail Jail) []Snapshot {
// 1. List all datasets
// TODO : Include mounted filesystems?
rs := strings.Split(jail.RootPath, "/")
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
cmd := fmt.Sprintf("zfs list -r -H -o name,mountpoint,used,referenced,creation -t snapshot %s", rootDataset)
curDS, err := getDatastoreFromArray(jail.Datastore, gDatastores)
if err != nil {
fmt.Printf("Error getting datastore \"%s\": %v\n", jail.Datastore, err)
return snapshots
}
rootDataset := fmt.Sprintf("%s/%s/%s", curDS.ZFSDataset, "jails", jail.Name)
cmd := fmt.Sprintf("zfs list -p -r -H -o name,mountpoint,used,referenced,creation -t snapshot %s", rootDataset)
out, err := executeCommand(cmd)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return snapshots
}
dateLayout := "Mon Jan _2 15:04 2006"
loc, _ := time.LoadLocation(gTimeZone)
for _, line := range strings.Split(out, "\n") {
if len(line) > 0 {
ls := strings.Split(line, "\t")
// Parse creation date so we can use it to sort snapshots
creationts, err := time.ParseInLocation(dateLayout, ls[4], loc)
//creationts, err := time.ParseInLocation(dateLayout, ls[4], loc)
creationts, err := strconv.ParseInt(ls[4], 10, 64)
if err != nil {
fmt.Println("Error while parsing date %s:", ls[4], err)
return snapshots
@ -123,13 +132,15 @@ func listJailSnapshots(jail Jail) []Snapshot {
// Get subdir to append to snapshot name
subdir := strings.Replace(strings.Split(ls[0], "@")[0], rootDataset, "", 1)
snapshots = append(snapshots, Snapshot{Dsname: ls[0],
u, _ := strconv.ParseUint(ls[2], 10, 64)
r, _ := strconv.ParseUint(ls[3], 10, 64)
snapshots = append(snapshots, Snapshot{Datastore: curDS.Name,
Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir),
Jailname: jail.Name,
Mountpoint: ls[1],
Used: ls[2],
Referenced: ls[3],
Creation: creationts})
Used: u,
Referenced: r,
Creation: time.Unix(creationts, 0)})
}
}
@ -165,8 +176,8 @@ func CreateJailSnapshot(args []string) {
* Create snapshot for a jail
*******************************************************************************/
func createJailSnapshot(jail Jail) error {
rs := strings.Split(jail.RootPath, "/")
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
curDS, _ := getDatastoreFromArray(jail.Datastore, gDatastores)
rootDataset := fmt.Sprintf("%s/%s/%s", curDS.ZFSDataset, "jails", jail.Name)
cmd := fmt.Sprintf("zfs snapshot -r %s@%s", rootDataset, gSnapshotName)
_, err := executeCommand(cmd)
@ -207,8 +218,8 @@ func deleteJailSnapshot(jail Jail) error {
var snaptodel []string
// Get all recursive snapshots
rs := strings.Split(jail.RootPath, "/")
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
curDS, _ := getDatastoreFromArray(jail.Datastore, gDatastores)
rootDataset := fmt.Sprintf("%s/%s/%s", curDS.ZFSDataset, "jails", jail.Name)
cmd := fmt.Sprintf("zfs list -r -H -o name -t snapshot %s", rootDataset)
out, err := executeCommand(cmd)
if err != nil {
@ -284,8 +295,8 @@ func rollbackJailSnapshot(jail Jail) error {
// We need to rollback parent and childs
// Get all recursive snapshots
rs := strings.Split(jail.RootPath, "/")
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
curDS, _ := getDatastoreFromArray(jail.Datastore, gDatastores)
rootDataset := fmt.Sprintf("%s/%s/%s", curDS.ZFSDataset, "jails", jail.Name)
cmd := fmt.Sprintf("zfs list -r -H -o name -t snapshot %s", rootDataset)
out, err := executeCommand(cmd)
if err != nil {