github.com/Cloud-Foundations/Dominator@v0.3.4/imageunpacker/unpacker/prepareForCopy.go (about)

     1  package unpacker
     2  
     3  import (
     4  	"errors"
     5  	"path"
     6  	"syscall"
     7  
     8  	proto "github.com/Cloud-Foundations/Dominator/proto/imageunpacker"
     9  )
    10  
    11  func (u *Unpacker) prepareForCopy(streamName string) error {
    12  	u.updateUsageTime()
    13  	defer u.updateUsageTime()
    14  	streamInfo := u.getStream(streamName)
    15  	if streamInfo == nil {
    16  		return errors.New("unknown stream")
    17  	}
    18  	errorChannel := make(chan error)
    19  	request := requestType{
    20  		request:      requestPrepareForCopy,
    21  		errorChannel: errorChannel,
    22  	}
    23  	streamInfo.requestChannel <- request
    24  	return <-errorChannel
    25  }
    26  
    27  func (stream *streamManagerState) prepareForCopy() error {
    28  	if err := stream.getDevice(); err != nil {
    29  		return err
    30  	}
    31  	return stream.unmount()
    32  }
    33  
    34  func (stream *streamManagerState) unmount() error {
    35  	streamInfo := stream.streamInfo
    36  	switch streamInfo.status {
    37  	case proto.StatusStreamNoDevice:
    38  		return errors.New("no device")
    39  	case proto.StatusStreamNotMounted:
    40  		return nil // Nothing to do.
    41  	case proto.StatusStreamMounted:
    42  		// Unmount.
    43  	case proto.StatusStreamScanning:
    44  		return errors.New("stream scan in progress")
    45  	case proto.StatusStreamScanned:
    46  		// Unmount.
    47  	case proto.StatusStreamFetching:
    48  		return errors.New("fetch in progress")
    49  	case proto.StatusStreamUpdating:
    50  		return errors.New("update in progress")
    51  	case proto.StatusStreamPreparing:
    52  		return errors.New("preparing to capture")
    53  	case proto.StatusStreamNoFileSystem:
    54  		return errors.New("no file-system")
    55  	default:
    56  		panic("invalid status")
    57  	}
    58  	mountPoint := path.Join(stream.unpacker.baseDir, "mnt")
    59  	if err := syscall.Unmount(mountPoint, 0); err != nil {
    60  		return err
    61  	}
    62  	stream.streamInfo.status = proto.StatusStreamNotMounted
    63  	streamInfo.dualLogger.Printf("Unmounted(%s)\n", stream.streamName)
    64  	return nil
    65  }