Files
warewulf/internal/pkg/image/util.go
Jonathon Anderson 45a690ca4e Rename "container" to "image"
- Updated `wwctl upgrade` to handle updates
- Maintained `.Container` and `.ContainerName` in tstruct
- Added `ContainerName()` methods to node and profile objects
- Added `--container`, `-C` aliases to wwctl commands (`<node|profile> <add|set>`)
- Added `wwctl container` alias
- Added support for `container_exit.sh` if `image_exit.sh` is not found

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
2025-01-19 05:54:19 -07:00

117 lines
2.4 KiB
Go

package image
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwlog"
)
func ValidName(name string) bool {
if !util.ValidString(name, "^[\\w\\-\\.\\:]+$") {
wwlog.Warn("Image name has illegal characters: %s", name)
return false
}
return true
}
func ListSources() ([]string, error) {
var ret []string
err := os.MkdirAll(SourceParentDir(), 0755)
if err != nil {
return ret, errors.New("Could not create image source parent directory: " + SourceParentDir())
}
wwlog.Debug("Searching for image rootfs directories: %s", SourceParentDir())
sources, err := os.ReadDir(SourceParentDir())
if err != nil {
return ret, err
}
for _, source := range sources {
wwlog.Verbose("Found image source: %s", source.Name())
if !ValidName(source.Name()) {
continue
}
if !ValidSource(source.Name()) {
continue
}
ret = append(ret, source.Name())
}
return ret, nil
}
func DoesSourceExist(name string) bool {
fullPath := RootFsDir(name)
return util.IsDir(fullPath)
}
func ValidSource(name string) bool {
if !ValidName(name) {
return false
}
if !DoesSourceExist(name) {
wwlog.Verbose("Location is not an image source directory: %s", name)
return false
}
return true
}
/*
Delete the chroot of an image
*/
func DeleteSource(name string) error {
fullPath := SourceDir(name)
wwlog.Verbose("Removing path: %s", fullPath)
return os.RemoveAll(fullPath)
}
func Duplicate(name string, destination string) error {
fullPathImageSource := RootFsDir(name)
wwlog.Info("Copying sources...")
err := ImportDirectory(fullPathImageSource, destination)
if err != nil {
return err
}
return nil
}
/*
Delete the image of an image
*/
func DeleteImage(name string) error {
imageFile := ImageFile(name)
if util.IsFile(imageFile) {
wwlog.Verbose("removing %s for image %s", imageFile, name)
errImg := os.Remove(imageFile)
wwlog.Verbose("removing %s for image %s", imageFile+".gz", name)
errGz := os.Remove(imageFile + ".gz")
if errImg != nil {
return errors.Errorf("Problems delete %s for image %s: %s\n", imageFile, name, errImg)
}
if errGz != nil {
return errors.Errorf("Problems delete %s for image %s: %s\n", imageFile+".gz", name, errGz)
}
return nil
}
return errors.Errorf("Image %s of image %s doesn't exist\n", imageFile, name)
}
func IsWriteAble(name string) bool {
return !util.IsFile(filepath.Join(SourceDir(name), "readonly"))
}