github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/build/sources/conveyorPacker_local.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package sources
     7  
     8  import (
     9  	"fmt"
    10  	"path/filepath"
    11  
    12  	"github.com/sylabs/singularity/internal/pkg/sylog"
    13  	"github.com/sylabs/singularity/pkg/build/types"
    14  	"github.com/sylabs/singularity/pkg/image"
    15  	"github.com/sylabs/singularity/pkg/util/loop"
    16  )
    17  
    18  // LocalConveyor only needs to hold the conveyor to have the needed data to pack
    19  type LocalConveyor struct {
    20  	src string
    21  	b   *types.Bundle
    22  }
    23  
    24  // LocalPacker ...
    25  type LocalPacker interface {
    26  	Pack() (*types.Bundle, error)
    27  }
    28  
    29  // LocalConveyorPacker only needs to hold the conveyor to have the needed data to pack
    30  type LocalConveyorPacker struct {
    31  	LocalConveyor
    32  	LocalPacker
    33  }
    34  
    35  // GetLocalPacker ...
    36  func GetLocalPacker(src string, b *types.Bundle) (LocalPacker, error) {
    37  
    38  	imageObject, err := image.Init(src, false)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	info := new(loop.Info64)
    44  
    45  	switch imageObject.Type {
    46  	case image.SIF:
    47  		sylog.Debugf("Packing from SIF")
    48  
    49  		return &SIFPacker{
    50  			srcfile: src,
    51  			b:       b,
    52  		}, nil
    53  	case image.SQUASHFS:
    54  		sylog.Debugf("Packing from Squashfs")
    55  
    56  		info.Offset = imageObject.Partitions[0].Offset
    57  		info.SizeLimit = imageObject.Partitions[0].Size
    58  
    59  		return &SquashfsPacker{
    60  			srcfile: src,
    61  			b:       b,
    62  			info:    info,
    63  		}, nil
    64  	case image.EXT3:
    65  		sylog.Debugf("Packing from Ext3")
    66  
    67  		info.Offset = imageObject.Partitions[0].Offset
    68  		info.SizeLimit = imageObject.Partitions[0].Size
    69  
    70  		return &Ext3Packer{
    71  			srcfile: src,
    72  			b:       b,
    73  			info:    info,
    74  		}, nil
    75  	case image.SANDBOX:
    76  		sylog.Debugf("Packing from Sandbox")
    77  
    78  		return &SandboxPacker{
    79  			srcdir: src,
    80  			b:      b,
    81  		}, nil
    82  	default:
    83  		return nil, fmt.Errorf("invalid image format")
    84  	}
    85  }
    86  
    87  // Get just stores the source
    88  func (cp *LocalConveyorPacker) Get(b *types.Bundle) (err error) {
    89  	// insert base metadata before unpacking fs
    90  	if err = makeBaseEnv(b.Rootfs()); err != nil {
    91  		return fmt.Errorf("While inserting base environment: %v", err)
    92  	}
    93  
    94  	cp.src = filepath.Clean(b.Recipe.Header["from"])
    95  
    96  	cp.LocalPacker, err = GetLocalPacker(cp.src, b)
    97  	return err
    98  }