Add support for undefining a domain

This commit is contained in:
Ben LeMasurier 2016-09-28 15:29:46 -06:00
parent d473469e69
commit d556125e20
4 changed files with 73 additions and 0 deletions

View File

@ -42,6 +42,7 @@ const (
ProcAuthList = 66 ProcAuthList = 66
ProcConnectGetLibVersion = 157 ProcConnectGetLibVersion = 157
ProcDomainMigrateSetMaxSpeed = 207 ProcDomainMigrateSetMaxSpeed = 207
ProcDomainUndefineFlags = 231
ProcConnectListAllDomains = 273 ProcConnectListAllDomains = 273
ProcMigratePerformParams = 305 ProcMigratePerformParams = 305
) )

View File

@ -128,6 +128,20 @@ const (
MigrateFlagRDMAPinAll MigrateFlagRDMAPinAll
) )
// UndefineFlags specifies options available when undefining a domain.
type UndefineFlags uint32
const (
// UndefineFlagManagedSave removes all domain managed save data.
UndefineFlagManagedSave UndefineFlags = 1 << iota
// UndefineFlagSnapshotsMetadata removes all domain snapshot metadata.
UndefineFlagSnapshotsMetadata
// UndefineFlagNVRAM removes all domain NVRAM files.
UndefineFlagNVRAM
)
// Connect establishes communication with the libvirt server. // Connect establishes communication with the libvirt server.
// The underlying libvirt socket connection must be previously established. // The underlying libvirt socket connection must be previously established.
func (l *Libvirt) Connect() error { func (l *Libvirt) Connect() error {
@ -399,6 +413,42 @@ func (l *Libvirt) Run(dom string, cmd []byte) ([]byte, error) {
return bytes.TrimRight(data[4:], "\x00"), nil return bytes.TrimRight(data[4:], "\x00"), nil
} }
// Undefine undefines the domain specified by dom, e.g., 'prod-lb-01'.
// The flags argument allows additional options to be specified such as
// cleaning up snapshot metadata. For more information on available
// flags, see UndefineFlag*.
func (l *Libvirt) Undefine(dom string, flags UndefineFlags) error {
d, err := l.lookup(dom)
if err != nil {
return err
}
payload := struct {
Domain Domain
Flags UndefineFlags
}{
Domain: *d,
Flags: flags,
}
buf, err := encode(&payload)
if err != nil {
return err
}
resp, err := l.request(constants.ProcDomainUndefineFlags, constants.ProgramRemote, &buf)
if err != nil {
return err
}
r := <-resp
if r.Status != StatusOK {
return decodeError(r.Payload)
}
return nil
}
// Version returns the version of the libvirt daemon. // Version returns the version of the libvirt daemon.
func (l *Libvirt) Version() (string, error) { func (l *Libvirt) Version() (string, error) {
resp, err := l.request(constants.ProcConnectGetLibVersion, constants.ProgramRemote, nil) resp, err := l.request(constants.ProcConnectGetLibVersion, constants.ProgramRemote, nil)

View File

@ -205,6 +205,16 @@ func TestRunFail(t *testing.T) {
} }
} }
func TestUndefine(t *testing.T) {
conn := libvirttest.New()
l := New(conn)
var flags UndefineFlags
if err := l.Undefine("test", flags); err != nil {
t.Fatalf("unexpected undefine error: %v", err)
}
}
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
conn := libvirttest.New() conn := libvirttest.New()
l := New(conn) l := New(conn)

View File

@ -205,6 +205,16 @@ var testDomainsReply = []byte{
0x00, 0x00, 0x02, 0x00, 0x00, 0x02,
} }
var testUndefineReply = []byte{
0x00, 0x00, 0x00, 0x1c, // length
0x20, 0x00, 0x80, 0x86, // program
0x00, 0x00, 0x00, 0x01, // version
0x00, 0x00, 0x00, 0xe7, // procedure
0x00, 0x00, 0x00, 0x01, // type
0x00, 0x00, 0x00, 0x00, // serial
0x00, 0x00, 0x00, 0x00, // status
}
var testVersionReply = []byte{ var testVersionReply = []byte{
0x00, 0x00, 0x00, 0x24, // length 0x00, 0x00, 0x00, 0x24, // length
0x20, 0x00, 0x80, 0x86, // program 0x20, 0x00, 0x80, 0x86, // program
@ -277,6 +287,8 @@ func (m *MockLibvirt) handleRemote(procedure uint32, conn net.Conn) {
conn.Write(m.reply(testSetSpeedReply)) conn.Write(m.reply(testSetSpeedReply))
case constants.ProcMigratePerformParams: case constants.ProcMigratePerformParams:
conn.Write(m.reply(testMigrateReply)) conn.Write(m.reply(testMigrateReply))
case constants.ProcDomainUndefineFlags:
conn.Write(m.reply(testUndefineReply))
} }
} }