github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/imageengine/buildah/mount.go (about)

     1  // Copyright © 2022 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package buildah
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"os"
    21  
    22  	"github.com/sealerio/sealer/pkg/define/options"
    23  
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  func (engine *Engine) Mount(opts *options.MountOptions) ([]options.JSONMount, error) {
    28  	containers := opts.Containers
    29  	if len(containers) == 0 {
    30  		return []options.JSONMount{}, errors.Errorf("id/name of containers mube be specified")
    31  	}
    32  
    33  	store := engine.ImageStore()
    34  	var jsonMounts []options.JSONMount
    35  	var lastError error
    36  	// Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
    37  	// of the mount command.
    38  	// Differently, allow the mount if we are already in a userns, as the mount point will still
    39  	// be accessible once "buildah mount" exits.
    40  	if os.Geteuid() != 0 && store.GraphDriverName() != "vfs" {
    41  		return []options.JSONMount{}, errors.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", store.GraphDriverName())
    42  	}
    43  
    44  	for _, name := range containers {
    45  		builder, err := OpenBuilder(getContext(), store, name)
    46  		if err != nil {
    47  			if lastError != nil {
    48  				fmt.Fprintln(os.Stderr, lastError)
    49  			}
    50  			lastError = errors.Wrapf(err, "error reading build container %q", name)
    51  			continue
    52  		}
    53  		mountPoint, err := builder.Mount(builder.MountLabel)
    54  		if err != nil {
    55  			if lastError != nil {
    56  				fmt.Fprintln(os.Stderr, lastError)
    57  			}
    58  			lastError = errors.Wrapf(err, "error mounting %q container %q", name, builder.Container)
    59  			continue
    60  		}
    61  		if len(containers) > 1 {
    62  			jsonMounts = append(jsonMounts, options.JSONMount{Container: name, MountPoint: mountPoint})
    63  			continue
    64  		} else {
    65  			jsonMounts = append(jsonMounts, options.JSONMount{MountPoint: mountPoint})
    66  			continue
    67  		}
    68  	}
    69  
    70  	return jsonMounts, nil
    71  }