VMware {code} Community
kumars17
Contributor
Contributor

Remove a virtual disk from VM without deleting the backing file

Thru the VI Client we are given option while removing the virtual disk whether to keep the backing file or delete it, but thru VI SDK I am not able to do it. It allows me to remove the disk, but I have to delete the backing file also.

Do anyone have any sample of hot to remove the Virtual disk without deleting the backing file.

ns2__VirtualDisk newdisk;

// newdisk.deviceInfo=&descr;

// newdisk.capacityInKB=2097152;

newdisk.backing=&virtDisk;

newdisk.controllerKey=&ckey;

newdisk.unitNumber=&uno;

newdisk.key=2001;

newdisk.connectable=&conninfo;

ns2__VirtualDeviceConfigSpecFileOperation foptype=ns2__VirtualDeviceConfigSpecFileOperation__destroy; -


> If I use replace or create, it fails

ns2__VirtualDeviceConfigSpecOperation optype=ns2__VirtualDeviceConfigSpecOperation__remove;

ns2__VirtualDeviceConfigSpec *cspec=new ns2__VirtualDeviceConfigSpec () ;

cspec->operation=&optype;

cspec->fileOperation=&foptype;

cspec->device=&newdisk;

-Sunil

0 Kudos
1 Reply
jrackliffe
Hot Shot
Hot Shot

This is similar to functionality I was implementing on formatting a VMDK and had an undesired effect of leaving the old one VMDK which may work for your requirements. Please note this is a complete kludge and should not replace an RFE for FileManagement APIs on DataStores.

No code for you, but I think the flow is pretty simple.

If you do a VMConfigSpec w a DeviceSpec.Op.edit and a DeviceSpec.fileOp.create and provide a new backing info (new distinct name) to the existing VirtualDisk.backing it will create a new backing file under the new name and leave the old one on the store. Now you may want to shrink the disk or provision the new backing as Thin so it doesn't provision the big file before immediately destroying it, but you can play w that.

Then once the existing VirtualDisk is attached to the new backing you should be able to destroy the disk and leave the old backing just in the file system.

Again this is a kludge workaround, but it might do what you need.

J

0 Kudos