package cmd import ( "fmt" "time" "strings" ) /******************************************************************************** * List all snapshots jails have *******************************************************************************/ func ListJailsSnapshots(args []string) { var jailNames []string if len(args) > 0 { for _, a := range args { jailNames = append(jailNames, a) } } if len(jailNames) == 0 || len(args) == 0 { for _, j := range gJails { listJailSnapshots(j) } } else { for _, cj := range gJails { for _, jn := range jailNames { if strings.EqualFold(cj.Name, jn) { listJailSnapshots(cj) } } } } } /******************************************************************************** * List all snapshots a jail have *******************************************************************************/ func listJailSnapshots(jail Jail) { var snapshots []Snapshot // 1. List all datasets // TODO : Include mounted filesystems? rs := strings.Split(jail.RootPath, "/") rootDataset := strings.Join(rs[:len(rs)-1], "/") cmd := fmt.Sprintf("zfs list -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 } 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) if err != nil { fmt.Println("Error while parsing date %s:", ls[4], err) return } // Get subdir to append to snapshot name subdir := strings.Replace(strings.Split(ls[0], "@")[0], rootDataset, "", 1) snapshots = append(snapshots, Snapshot{Dsname: ls[0], Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir), Mountpoint: ls[1], Used: ls[2], Referenced: ls[3], Creation: creationts}) } } // Sort snapshots by creation date ss := initSnapshotSortStruct() SnapshotsOrderedBy(ss.CreationInc).Sort(snapshots) // TODO : Pretty display. Make an interface of displayStructFields? for _, s := range snapshots { fmt.Printf("| %s | %s | %s | %s |\n", s.Name, s.Creation.String(), s.Referenced, s.Used) } }