github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/snapshots/overlay/check.go (about)

     1  // +build linux
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package overlay
    20  
    21  import (
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/containerd/containerd/log"
    28  	"github.com/containerd/containerd/mount"
    29  	"github.com/containerd/continuity/fs"
    30  	"github.com/pkg/errors"
    31  )
    32  
    33  // supportsMultipleLowerDir checks if the system supports multiple lowerdirs,
    34  // which is required for the overlay snapshotter. On 4.x kernels, multiple lowerdirs
    35  // are always available (so this check isn't needed), and backported to RHEL and
    36  // CentOS 3.x kernels (3.10.0-693.el7.x86_64 and up). This function is to detect
    37  // support on those kernels, without doing a kernel version compare.
    38  //
    39  // Ported from moby overlay2.
    40  func supportsMultipleLowerDir(d string) error {
    41  	td, err := ioutil.TempDir(d, "multiple-lowerdir-check")
    42  	if err != nil {
    43  		return err
    44  	}
    45  	defer func() {
    46  		if err := os.RemoveAll(td); err != nil {
    47  			log.L.WithError(err).Warnf("Failed to remove check directory %v", td)
    48  		}
    49  	}()
    50  
    51  	for _, dir := range []string{"lower1", "lower2", "upper", "work", "merged"} {
    52  		if err := os.Mkdir(filepath.Join(td, dir), 0755); err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	opts := fmt.Sprintf("lowerdir=%s:%s,upperdir=%s,workdir=%s", filepath.Join(td, "lower2"), filepath.Join(td, "lower1"), filepath.Join(td, "upper"), filepath.Join(td, "work"))
    58  	m := mount.Mount{
    59  		Type:    "overlay",
    60  		Source:  "overlay",
    61  		Options: []string{opts},
    62  	}
    63  	dest := filepath.Join(td, "merged")
    64  	if err := m.Mount(dest); err != nil {
    65  		return errors.Wrap(err, "failed to mount overlay")
    66  	}
    67  	if err := mount.UnmountAll(dest, 0); err != nil {
    68  		log.L.WithError(err).Warnf("Failed to unmount check directory %v", dest)
    69  	}
    70  	return nil
    71  }
    72  
    73  // Supported returns nil when the overlayfs is functional on the system with the root directory.
    74  // Supported is not called during plugin initialization, but exposed for downstream projects which uses
    75  // this snapshotter as a library.
    76  func Supported(root string) error {
    77  	if err := os.MkdirAll(root, 0700); err != nil {
    78  		return err
    79  	}
    80  	supportsDType, err := fs.SupportsDType(root)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	if !supportsDType {
    85  		return fmt.Errorf("%s does not support d_type. If the backing filesystem is xfs, please reformat with ftype=1 to enable d_type support", root)
    86  	}
    87  	return supportsMultipleLowerDir(root)
    88  }