add more codes

This commit is contained in:
2025-11-23 22:37:27 +07:00
parent b506a64ed1
commit 4fcd71ca05
10 changed files with 448 additions and 52 deletions

58
pkg/storage/pools.go Normal file
View File

@@ -0,0 +1,58 @@
package storage
import (
"fmt"
"os"
"path/filepath"
)
// PoolConfig describes a configured storage pool.
type PoolConfig struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"` // dir|lvm|zfs
Path string `json:"path" yaml:"path"` // for dir/zfs
VG string `json:"vg" yaml:"vg"` // for lvm
}
// AttachRequest describes a volume attachment for a VM/CT.
type AttachRequest struct {
Pool string
Volume string
Path string // resolved path for libvirt/LXC
}
// ResolveVolume maps pool+vol name to a path for dir pools.
func ResolveVolume(pools []PoolConfig, poolName, vol string) (string, error) {
for _, p := range pools {
if p.Name != poolName {
continue
}
// dir pool: join path/vol
if p.Type == "dir" {
if p.Path == "" {
return "", fmt.Errorf("dir pool %s missing path", poolName)
}
target := filepath.Join(p.Path, vol)
return target, nil
}
// TODO: lvm/zfs support
return "", fmt.Errorf("pool type %s not yet supported", p.Type)
}
return "", fmt.Errorf("pool %s not found", poolName)
}
// PoolExists checks if a pool is present in config and, for dir pools, if the path exists.
func PoolExists(pools []PoolConfig, name string) bool {
for _, p := range pools {
if p.Name == name {
if p.Type == "dir" && p.Path != "" {
if _, err := os.Stat(p.Path); err == nil {
return true
}
}
// accept as present even if path missing to allow creation elsewhere
return true
}
}
return false
}