github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/imageunpacker/unpacker/load.go (about)

     1  package unpacker
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  	"path"
     9  	"syscall"
    10  	"time"
    11  
    12  	"github.com/Cloud-Foundations/Dominator/lib/log"
    13  )
    14  
    15  const (
    16  	dirPerms = syscall.S_IRWXU | syscall.S_IRGRP | syscall.S_IXGRP |
    17  		syscall.S_IROTH | syscall.S_IXOTH
    18  )
    19  
    20  func load(baseDir string, imageServerAddress string, logger log.DebugLogger) (
    21  	*Unpacker, error) {
    22  	err := syscall.Mount("none", "/", "", syscall.MS_REC|syscall.MS_PRIVATE, "")
    23  	if err != nil {
    24  		return nil, fmt.Errorf("error making mounts private: %s", err)
    25  	}
    26  	if err := os.MkdirAll(path.Join(baseDir, "mnt"), dirPerms); err != nil {
    27  		return nil, err
    28  	}
    29  	u := &Unpacker{
    30  		baseDir:            baseDir,
    31  		imageServerAddress: imageServerAddress,
    32  		logger:             logger,
    33  	}
    34  	u.updateUsageTimeWithLock()
    35  	file, err := os.Open(path.Join(baseDir, stateFile))
    36  	if err != nil {
    37  		if os.IsNotExist(err) {
    38  			u.pState.Devices = make(map[string]deviceInfo)
    39  			u.pState.ImageStreams = make(map[string]*imageStreamInfo)
    40  			return u, nil
    41  		}
    42  		return nil, err
    43  	}
    44  	defer file.Close()
    45  	decoder := json.NewDecoder(bufio.NewReader(file))
    46  	if err := decoder.Decode(&u.pState); err != nil {
    47  		return nil, err
    48  	}
    49  	// Fill in sizes.
    50  	for deviceName, device := range u.pState.Devices {
    51  		if err := updateDeviceSize(&device); err != nil {
    52  			return nil, err
    53  		}
    54  		u.pState.Devices[deviceName] = device
    55  	}
    56  	// Set up streams.
    57  	for streamName := range u.pState.ImageStreams {
    58  		if _, err := u.setupStream(streamName); err != nil {
    59  			return nil, err
    60  		}
    61  	}
    62  	u.updateUsageTimeWithLock()
    63  	return u, nil
    64  }
    65  
    66  func (u *Unpacker) updateUsageTime() {
    67  	u.rwMutex.Lock()
    68  	defer u.rwMutex.Unlock()
    69  	u.updateUsageTimeWithLock()
    70  }
    71  
    72  func (u *Unpacker) updateUsageTimeWithLock() {
    73  	u.lastUsedTime = time.Now()
    74  }