//go:build libvirt package hypervisor import ( "context" "encoding/json" "errors" "os" "path/filepath" "syscall" "git.luoli.live/root/virt/agent/operation" "libvirt.org/go/libvirt" "libvirt.org/go/libvirtxml" ) type storagePoolBinding struct { Role string Directory string PoolName string } var storagePoolNamesByRole = map[string]string{ StorageRoleManaged: managedStoragePoolName, StorageRoleTemplate: templateStoragePoolName, StorageRoleSeed: seedStoragePoolName, StorageRoleUserISO: userISOStoragePoolName, StorageRoleRescue: rescueStoragePoolName, } func (l *Libvirt) storagePoolName(role string) string { return storagePoolNamesByRole[role] } func (l *Libvirt) ConfigureStorage(ctx context.Context, request StorageConfigurationRequest) (StorageConfigurationResult, error) { if err := validateStorageDirectories(request, l.local.StoragePathRoots); err != nil { return StorageConfigurationResult{}, storageConfigurationNotApplied(err) } release, err := acquireStorageConfigurationLock(l.local.StorageConfigurationLockPath) if err != nil { return StorageConfigurationResult{}, storageConfigurationNotApplied(err) } defer release() if err := configureStorageRoles(ctx, request, func(role, path string) error { _, err := l.resolveStoragePool(role, path) return err }); err != nil { return StorageConfigurationResult{}, err } return StorageConfigurationResult{Configured: true}, nil } func acquireStorageConfigurationLock(path string) (func(), error) { if path == "" || !filepath.IsAbs(path) || filepath.Clean(path) != path { return nil, NewError(ErrUnavailable, "storage_configuration_lock_unavailable", "storage configuration lock path is invalid", nil) } lock, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600) if err != nil { return nil, NewError(ErrUnavailable, "storage_configuration_lock_unavailable", "storage configuration lock could not be opened", err) } if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { _ = lock.Close() if errors.Is(err, syscall.EWOULDBLOCK) || errors.Is(err, syscall.EAGAIN) { return nil, NewError(ErrConflict, "storage_configuration_busy", "another storage configuration helper is still active", nil) } return nil, NewError(ErrUnavailable, "storage_configuration_lock_unavailable", "storage configuration lock could not be acquired", err) } return func() { _ = syscall.Flock(int(lock.Fd()), syscall.LOCK_UN) _ = lock.Close() }, nil } // resolveStoragePool is deliberately monotonic. Once a fixed pool has been // defined, a later failure leaves that definition in place so another // operation can validate and finish it. Rolling pools back is unsafe after a // timeout because the old helper may resume after a newer operation succeeds. func (l *Libvirt) resolveStoragePool(role, directory string) (storagePoolBinding, error) { name := l.storagePoolName(role) binding := storagePoolBinding{Role: role, Directory: directory, PoolName: name} existing, err := l.conn.LookupStoragePoolByName(name) if err == nil { defer existing.Free() if err := l.ensureStoragePoolReady(existing, binding); err != nil { return storagePoolBinding{}, err } return binding, nil } if !isNoStoragePool(err) { return storagePoolBinding{}, translateLibvirtError("lookup fixed storage pool", err) } document, err := (&libvirtxml.StoragePool{ Type: "dir", Name: name, Target: &libvirtxml.StoragePoolTarget{Path: directory}, }).Marshal() if err != nil { return storagePoolBinding{}, NewError(ErrInvalid, "storage_pool_definition_invalid", "storage pool definition could not be generated", err) } pool, err := l.conn.StoragePoolDefineXML(document, 0) if err != nil { return storagePoolBinding{}, mutationLibvirtError("define storage pool", err) } defer pool.Free() if err := l.ensureStoragePoolReady(pool, binding); err != nil { return storagePoolBinding{}, err } return binding, nil } func storagePoolDefinition(pool storagePoolReader) (libvirtxml.StoragePool, error) { document, err := pool.GetXMLDesc(0) if err != nil { return libvirtxml.StoragePool{}, translateLibvirtError("read storage pool definition", err) } var definition libvirtxml.StoragePool if err := definition.Unmarshal(document); err != nil { return libvirtxml.StoragePool{}, NewError(ErrConflict, "storage_pool_definition_invalid", "storage pool definition is invalid", err) } return definition, nil } func storagePoolTargetMatches(definition libvirtxml.StoragePool, directory string) bool { if definition.Type != "dir" || definition.Target == nil { return false } target := definition.Target.Path return filepath.IsAbs(target) && filepath.Clean(target) == target && target == directory } // fixedStoragePoolBinding returns an owned handle only after validating its // persistent directory mapping. The caller must free a non-nil handle. func (l *Libvirt) fixedStoragePoolBinding(role string) (storagePoolBinding, *libvirt.StoragePool, error) { name := l.storagePoolName(role) pool, err := l.conn.LookupStoragePoolByName(name) if isNoStoragePool(err) { return storagePoolBinding{}, nil, nil } if err != nil { return storagePoolBinding{}, nil, translateLibvirtError("lookup fixed storage pool", err) } binding, err := storagePoolBindingFromHandle(pool, role, name) if err != nil { pool.Free() return storagePoolBinding{}, nil, err } return binding, pool, nil } func storagePoolBindingFromHandle(pool storagePoolReader, role, name string) (storagePoolBinding, error) { persistent, err := pool.IsPersistent() if err != nil { return storagePoolBinding{}, translateLibvirtError("inspect storage pool persistence", err) } definition, err := storagePoolDefinition(pool) if err != nil { return storagePoolBinding{}, err } if !persistent || definition.Type != "dir" || definition.Target == nil { return storagePoolBinding{}, NewError(ErrConflict, "storage_pool_path_conflict", "fixed storage pool is not a persistent directory pool", nil) } directory := definition.Target.Path if !storagePoolTargetMatches(definition, directory) { return storagePoolBinding{}, NewError(ErrConflict, "storage_pool_path_conflict", "fixed storage pool target is not a clean absolute directory", nil) } return storagePoolBinding{Role: role, Directory: directory, PoolName: name}, nil } func (l *Libvirt) ensureStoragePoolReady(pool *libvirt.StoragePool, binding storagePoolBinding) error { persistent, err := pool.IsPersistent() if err != nil { return translateLibvirtError("inspect storage pool persistence", err) } definition, err := storagePoolDefinition(pool) if err != nil { return err } if !persistent || !storagePoolTargetMatches(definition, binding.Directory) { return NewError(ErrConflict, "storage_pool_path_conflict", "fixed storage pool does not match its requested persistent directory", nil) } active, err := pool.IsActive() if err != nil { return translateLibvirtError("inspect storage pool activity", err) } if !active { if err := pool.Create(0); err != nil { return mutationLibvirtError("start storage pool", err) } } if err := pool.SetAutostart(true); err != nil { return mutationLibvirtError("enable storage pool autostart", err) } if err := pool.Refresh(0); err != nil { return mutationLibvirtError("refresh storage pool", err) } ready, err := l.storagePoolHandleReady(pool, binding) if err != nil { return err } if !ready { return NewError(ErrUnavailable, "storage_pool_configuration_failed", "storage pool did not reach the required persistent active autostart state", nil) } return nil } // Read-only inspections share the validated binding and handle. A mutation // must still re-read persistence and XML afterwards via storagePoolHandleReady. func storagePoolReady(pool storagePoolReader, binding storagePoolBinding) (bool, error) { if _, err := validateRealDirectory(binding.Directory); err != nil { return false, nil } return storagePoolActiveAutostart(pool) } type storagePoolReader interface { IsPersistent() (bool, error) IsActive() (bool, error) GetAutostart() (bool, error) GetXMLDesc(libvirt.StorageXMLFlags) (string, error) GetInfo() (*libvirt.StoragePoolInfo, error) } func storagePoolActiveAutostart(pool storagePoolReader) (bool, error) { active, err := pool.IsActive() if err != nil { return false, translateLibvirtError("inspect storage pool activity", err) } autostart, err := pool.GetAutostart() if err != nil { return false, translateLibvirtError("inspect storage pool autostart", err) } return active && autostart, nil } func (l *Libvirt) storagePoolHandleReady(pool storagePoolReader, binding storagePoolBinding) (bool, error) { persistent, err := pool.IsPersistent() if err != nil { return false, translateLibvirtError("inspect storage pool persistence", err) } ready, err := storagePoolActiveAutostart(pool) if err != nil { return false, err } definition, err := storagePoolDefinition(pool) if err != nil { return false, err } return persistent && ready && storagePoolTargetMatches(definition, binding.Directory), nil } func isNoStoragePool(err error) bool { var libvirtErr libvirt.Error return errors.As(err, &libvirtErr) && libvirtErr.Code == libvirt.ERR_NO_STORAGE_POOL } func (l *Libvirt) reconcileStorageConfiguration(record operation.Record) (operation.Resolution, bool, error) { if record.Action != "configure-storage" { return operation.Resolution{}, false, nil } expected := record.ExpectedState if expected == nil || expected.Kind != operation.ExpectedStorageConfiguration { return operation.Resolution{}, true, NewError(ErrConflict, "storage_reconciliation_descriptor_missing", "storage reconciliation evidence is missing", nil) } digests := map[string]string{ StorageRoleManaged: expected.ManagedPathDigest, StorageRoleTemplate: expected.TemplatePathDigest, StorageRoleSeed: expected.SeedPathDigest, StorageRoleUserISO: expected.UserISOPathDigest, StorageRoleRescue: expected.RescuePathDigest, } request := StorageConfigurationRequest{} for _, role := range storageRoles() { binding, pool, err := l.fixedStoragePoolBinding(role) if err != nil { if errors.Is(err, ErrConflict) { return operation.Resolution{Resolved: true, ErrorCode: "storage_configuration_mismatch", ErrorMessage: "fixed storage pool conflicts with the requested configuration"}, true, nil } return operation.Resolution{}, true, err } if pool == nil { return operation.Resolution{Resolved: true, ErrorCode: "storage_configuration_incomplete", ErrorMessage: "host storage pool registration did not complete"}, true, nil } defer pool.Free() if storagePathDigest(binding.Directory) != digests[role] { return operation.Resolution{Resolved: true, ErrorCode: "storage_configuration_mismatch", ErrorMessage: "fixed storage pool targets a different directory"}, true, nil } ready, err := storagePoolReady(pool, binding) if err != nil { return operation.Resolution{}, true, err } if !ready { return operation.Resolution{Resolved: true, ErrorCode: "storage_configuration_incomplete", ErrorMessage: "host storage pool registration did not complete"}, true, nil } setStorageRequestDirectory(&request, binding) } if err := validateStorageDirectories(request, l.local.StoragePathRoots); err != nil { return operation.Resolution{Resolved: true, ErrorCode: "storage_configuration_mismatch", ErrorMessage: "configured storage directories do not satisfy the Agent safety policy"}, true, nil } encoded, err := json.Marshal(StorageConfigurationResult{Configured: true}) if err != nil { return operation.Resolution{}, true, err } return operation.Resolution{Resolved: true, Succeeded: true, Result: encoded}, true, nil } func setStorageRequestDirectory(request *StorageConfigurationRequest, binding storagePoolBinding) { switch binding.Role { case StorageRoleManaged: request.ManagedDirectory = binding.Directory case StorageRoleTemplate: request.TemplateDirectory = binding.Directory case StorageRoleSeed: request.SeedDirectory = binding.Directory case StorageRoleUserISO: request.UserISODirectory = binding.Directory case StorageRoleRescue: request.RescueDirectory = binding.Directory } }