k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cluster/gce/gci/mounter/mounter.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"strings"
    25  )
    26  
    27  const (
    28  	// Location of the mount file to use
    29  	chrootCmd        = "chroot"
    30  	mountCmd         = "mount"
    31  	rootfs           = "rootfs"
    32  	nfsRPCBindErrMsg = "mount.nfs: rpc.statd is not running but is required for remote locking.\nmount.nfs: Either use '-o nolock' to keep locks local, or start statd.\nmount.nfs: an incorrect mount option was specified\n"
    33  	rpcBindCmd       = "/sbin/rpcbind"
    34  	defaultRootfs    = "/home/kubernetes/containerized_mounter/rootfs"
    35  )
    36  
    37  func main() {
    38  
    39  	if len(os.Args) < 2 {
    40  		fmt.Fprintf(os.Stderr, "Command failed: must provide a command to run.\n")
    41  		return
    42  	}
    43  	path, _ := filepath.Split(os.Args[0])
    44  	rootfsPath := filepath.Join(path, rootfs)
    45  	if _, err := os.Stat(rootfsPath); os.IsNotExist(err) {
    46  		rootfsPath = defaultRootfs
    47  	}
    48  	command := os.Args[1]
    49  	switch command {
    50  	case mountCmd:
    51  		mountErr := mountInChroot(rootfsPath, os.Args[2:])
    52  		if mountErr != nil {
    53  			fmt.Fprintf(os.Stderr, "Mount failed: %v", mountErr)
    54  			os.Exit(1)
    55  		}
    56  	default:
    57  		fmt.Fprintf(os.Stderr, "Unknown command, must be %s", mountCmd)
    58  		os.Exit(1)
    59  
    60  	}
    61  }
    62  
    63  // MountInChroot is to run mount within chroot with the passing root directory
    64  func mountInChroot(rootfsPath string, args []string) error {
    65  	if _, err := os.Stat(rootfsPath); os.IsNotExist(err) {
    66  		return fmt.Errorf("path <%s> does not exist", rootfsPath)
    67  	}
    68  	args = append([]string{rootfsPath, mountCmd}, args...)
    69  	output, err := exec.Command(chrootCmd, args...).CombinedOutput()
    70  	if err == nil {
    71  		return nil
    72  	}
    73  
    74  	if !strings.EqualFold(string(output), nfsRPCBindErrMsg) {
    75  		// Mount failed but not because of RPC bind error
    76  		return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %v\nOutput: %s", err, chrootCmd, args, string(output))
    77  	}
    78  
    79  	// Mount failed because it is NFS V3 and we need to run rpcBind
    80  	output, err = exec.Command(chrootCmd, rootfsPath, rpcBindCmd, "-w").CombinedOutput()
    81  	if err != nil {
    82  		return fmt.Errorf("mount issued for NFS V3 but unable to run rpcbind:\n Output: %s\n Error: %v", string(output), err)
    83  	}
    84  
    85  	// Rpcbind is running, try mounting again
    86  	output, err = exec.Command(chrootCmd, args...).CombinedOutput()
    87  
    88  	if err != nil {
    89  		return fmt.Errorf("mount failed for NFS V3 even after running rpcBind %s, %v", string(output), err)
    90  	}
    91  
    92  	return nil
    93  }