github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/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  	streamInfo := stream.streamInfo
    32  	switch streamInfo.status {
    33  	case proto.StatusStreamNoDevice:
    34  		return errors.New("no device")
    35  	case proto.StatusStreamNotMounted:
    36  		return nil // Nothing to do.
    37  	case proto.StatusStreamMounted:
    38  		// Unmount.
    39  	case proto.StatusStreamScanning:
    40  		return errors.New("stream scan in progress")
    41  	case proto.StatusStreamScanned:
    42  		// Unmount.
    43  	case proto.StatusStreamFetching:
    44  		return errors.New("fetch in progress")
    45  	case proto.StatusStreamUpdating:
    46  		return errors.New("update in progress")
    47  	case proto.StatusStreamPreparing:
    48  		return errors.New("preparing to capture")
    49  	case proto.StatusStreamNoFileSystem:
    50  		return errors.New("no file-system")
    51  	default:
    52  		panic("invalid status")
    53  	}
    54  	mountPoint := path.Join(stream.unpacker.baseDir, "mnt")
    55  	if err := syscall.Unmount(mountPoint, 0); err != nil {
    56  		return err
    57  	}
    58  	stream.streamInfo.status = proto.StatusStreamNotMounted
    59  	stream.unpacker.logger.Printf("Unmounted(%s)\n", stream.streamName)
    60  	return nil
    61  }