// Create creates a disk without a parent (and doesn't attach it). func (m *Manager) Create(ctx context.Context, newDiskURI string, capacityKB int64) (*VirtualDisk, error) { defer trace.End(trace.Begin(newDiskURI)) vdm := object.NewVirtualDiskManager(m.vm.Client()) d, err := NewVirtualDisk(newDiskURI) if err != nil { return nil, errors.Trace(err) } spec := &types.FileBackedVirtualDiskSpec{ VirtualDiskSpec: types.VirtualDiskSpec{ DiskType: string(types.VirtualDiskTypeThin), AdapterType: string(types.VirtualDiskAdapterTypeLsiLogic), }, CapacityKb: capacityKB, } log.Infof("Creating vmdk for layer or volume %s", d.DatastoreURI) err = tasks.Wait(ctx, func(ctx context.Context) (tasks.Waiter, error) { return vdm.CreateVirtualDisk(ctx, d.DatastoreURI, nil, spec) }) if err != nil { return nil, errors.Trace(err) } return d, nil }
func (d *Helper) Rm(ctx context.Context, pth string) error { f := path.Join(d.RootURL, pth) log.Infof("Removing %s", pth) return tasks.Wait(context.TODO(), func(ctx context.Context) (tasks.Task, error) { return d.fm.DeleteDatastoreFile(ctx, f, d.s.Datacenter) }) }
func (d *Datastore) Mv(ctx context.Context, fromPath, toPath string) error { from := path.Join(d.RootURL, fromPath) to := path.Join(d.RootURL, toPath) err := tasks.Wait(ctx, func(context.Context) (tasks.Waiter, error) { return d.fm.MoveDatastoreFile(ctx, from, d.s.Datacenter, to, d.s.Datacenter, true) }) return err }
func (d *Helper) Mv(ctx context.Context, fromPath, toPath string) error { from := path.Join(d.RootURL, fromPath) to := path.Join(d.RootURL, toPath) log.Infof("Moving %s to %s", from, to) err := tasks.Wait(ctx, func(context.Context) (tasks.Task, error) { return d.fm.MoveDatastoreFile(ctx, from, d.s.Datacenter, to, d.s.Datacenter, true) }) return err }
func (m *Manager) Detach(ctx context.Context, d *VirtualDisk) error { defer trace.End(trace.Begin(d.DevicePath)) log.Infof("Detaching disk %s", d.DevicePath) d.lock() defer d.unlock() if !d.Attached() { log.Infof("Disk %s is already detached", d.DevicePath) return nil } if err := d.canBeDetached(); err != nil { return errors.Trace(err) } spec := types.VirtualMachineConfigSpec{} disk, err := findDisk(ctx, m.vm, d.DatastoreURI) if err != nil { return errors.Trace(err) } config := []types.BaseVirtualDeviceConfigSpec{ &types.VirtualDeviceConfigSpec{ Device: disk, Operation: types.VirtualDeviceConfigSpecOperationRemove, }, } spec.DeviceChange = config err = tasks.Wait(ctx, func(ctx context.Context) (tasks.Waiter, error) { return m.vm.Reconfigure(ctx, spec) }) if err != nil { log.Warnf("detach for %s failed with %s", d.DevicePath, errors.ErrorStack(err)) return errors.Trace(err) } func() { select { case <-m.maxAttached: default: } }() return d.setDetached() }
func DSsetup(t *testing.T) (context.Context, *Helper, func()) { ctx := context.Background() sess := Session(ctx, t) ds, err := NewHelper(ctx, sess, sess.Datastore, TestName("dstests")) if !assert.NoError(t, err) { return ctx, nil, nil } f := func() { log.Infof("Removing test root %s", ds.RootURL) err := tasks.Wait(ctx, func(context.Context) (tasks.Waiter, error) { return ds.fm.DeleteDatastoreFile(ctx, ds.RootURL, sess.Datacenter) }) if err != nil { log.Errorf(err.Error()) return } } return ctx, ds, f }
func dSsetup(t *testing.T) (context.Context, *Datastore, func()) { ctx := context.Background() sess := Session(ctx, t) log.SetLevel(log.DebugLevel) ds, err := NewDatastore(ctx, sess, sess.Datastore, RandomString(10)+"dstests") if !assert.NoError(t, err) { return ctx, nil, nil } f := func() { log.Debugf("Removing test root %s", ds.RootURL) err := tasks.Wait(ctx, func(context.Context) (tasks.Waiter, error) { return ds.fm.DeleteDatastoreFile(ctx, ds.RootURL, sess.Datacenter) }) if err != nil { log.Errorf(err.Error()) return } } return ctx, ds, f }