github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/virtualbox/attach_volume.go (about)

     1  package virtualbox
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  	"github.com/emc-advanced-dev/pkg/errors"
     6  	"github.com/solo-io/unik/pkg/providers/common"
     7  	"github.com/solo-io/unik/pkg/providers/virtualbox/virtualboxclient"
     8  	"github.com/solo-io/unik/pkg/types"
     9  )
    10  
    11  func (p *VirtualboxProvider) AttachVolume(id, instanceId, mntPoint string) error {
    12  	volume, err := p.GetVolume(id)
    13  	if err != nil {
    14  		return errors.New("retrieving volume "+id, err)
    15  	}
    16  	instance, err := p.GetInstance(instanceId)
    17  	if err != nil {
    18  		return errors.New("retrieving instance "+instanceId, err)
    19  	}
    20  	image, err := p.GetImage(instance.ImageId)
    21  	if err != nil {
    22  		return errors.New("retrieving image for instance", err)
    23  	}
    24  	controllerPort, err := common.GetControllerPortForMnt(image, mntPoint)
    25  	if err != nil {
    26  		return errors.New("getting controller port for mnt point", err)
    27  	}
    28  	logrus.Debugf("using storage controller %s", image.RunSpec.StorageDriver)
    29  
    30  	if err := virtualboxclient.RefreshDiskUUID(getVolumePath(volume.Name)); err != nil {
    31  		return errors.New("refreshing disk uuid", err)
    32  	}
    33  
    34  	if err := virtualboxclient.AttachDisk(instance.Id, getVolumePath(volume.Name), controllerPort, image.RunSpec.StorageDriver); err != nil {
    35  		return errors.New("attaching disk to vm", err)
    36  	}
    37  
    38  	if err := p.state.ModifyVolumes(func(volumes map[string]*types.Volume) error {
    39  		volume, ok := volumes[volume.Id]
    40  		if !ok {
    41  			return errors.New("no record of "+volume.Id+" in the state", nil)
    42  		}
    43  		volume.Attachment = instance.Id
    44  		return nil
    45  	}); err != nil {
    46  		return errors.New("modifying volumes in state", err)
    47  	}
    48  	return nil
    49  }