github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/volumes/flexvolume/lvm (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2015 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # Notes:
    18  #  - Please install "jq" package before using this driver.
    19  usage() {
    20  	err "Invalid usage. Usage: "
    21  	err "\t$0 init"
    22  	err "\t$0 attach <json params> <nodename>"
    23  	err "\t$0 detach <mount device> <nodename>"
    24  	err "\t$0 waitforattach <mount device> <json params>"
    25  	err "\t$0 mountdevice <mount dir> <mount device> <json params>"
    26  	err "\t$0 unmountdevice <mount dir>"
    27  	err "\t$0 isattached <json params> <nodename>"
    28  	exit 1
    29  }
    30  
    31  err() {
    32  	echo -ne $* 1>&2
    33  }
    34  
    35  log() {
    36  	echo -ne $* >&1
    37  }
    38  
    39  ismounted() {
    40  	MOUNT=`findmnt -n ${MNTPATH} 2>/dev/null | cut -d' ' -f1`
    41  	if [ "${MOUNT}" == "${MNTPATH}" ]; then
    42  		echo "1"
    43  	else
    44  		echo "0"
    45  	fi
    46  }
    47  
    48  getdevice() {
    49  	VOLUMEID=$(echo ${JSON_PARAMS} | jq -r '.volumeID')
    50  	VG=$(echo ${JSON_PARAMS}|jq -r '.volumegroup')
    51  
    52  	# LVM substitutes - with --
    53  	VOLUMEID=`echo $VOLUMEID|sed s/-/--/g`
    54  	VG=`echo $VG|sed s/-/--/g`
    55  
    56  	DMDEV="/dev/mapper/${VG}-${VOLUMEID}"
    57  	echo ${DMDEV}
    58  }
    59  
    60  attach() {
    61  	JSON_PARAMS=$1
    62  	SIZE=$(echo $1 | jq -r '.size')
    63  
    64  	DMDEV=$(getdevice)
    65  	if [ ! -b "${DMDEV}" ]; then
    66  		err "{\"status\": \"Failure\", \"message\": \"Volume ${VOLUMEID} does not exist\"}"
    67  		exit 1
    68  	fi
    69  	log "{\"status\": \"Success\", \"device\":\"${DMDEV}\"}"
    70  	exit 0
    71  }
    72  
    73  detach() {
    74  	log "{\"status\": \"Success\"}"
    75  	exit 0
    76  }
    77  
    78  waitforattach() {
    79  	shift
    80  	attach $*
    81  }
    82  
    83  domountdevice() {
    84  	MNTPATH=$1
    85  	DMDEV=$2
    86  	FSTYPE=$(echo $3|jq -r '.["kubernetes.io/fsType"]')
    87  
    88  	if [ ! -b "${DMDEV}" ]; then
    89  		err "{\"status\": \"Failure\", \"message\": \"${DMDEV} does not exist\"}"
    90  		exit 1
    91  	fi
    92  
    93  	if [ $(ismounted) -eq 1 ] ; then
    94  		log "{\"status\": \"Success\"}"
    95  		exit 0
    96  	fi
    97  
    98  	VOLFSTYPE=`blkid -o udev ${DMDEV} 2>/dev/null|grep "ID_FS_TYPE"|cut -d"=" -f2`
    99  	if [ "${VOLFSTYPE}" == "" ]; then
   100  		mkfs -t ${FSTYPE} ${DMDEV} >/dev/null 2>&1
   101  		if [ $? -ne 0 ]; then
   102  			err "{ \"status\": \"Failure\", \"message\": \"Failed to create fs ${FSTYPE} on device ${DMDEV}\"}"
   103  			exit 1
   104  		fi
   105  	fi
   106  
   107  	mkdir -p ${MNTPATH} &> /dev/null
   108  
   109  	mount ${DMDEV} ${MNTPATH} &> /dev/null
   110  	if [ $? -ne 0 ]; then
   111  		err "{ \"status\": \"Failure\", \"message\": \"Failed to mount device ${DMDEV} at ${MNTPATH}\"}"
   112  		exit 1
   113  	fi
   114  	log "{\"status\": \"Success\"}"
   115  	exit 0
   116  }
   117  
   118  unmountdevice() {
   119  	MNTPATH=$1
   120  	if [ ! -d ${MNTPATH} ]; then
   121  		log "{\"status\": \"Success\"}"
   122  		exit 0
   123  	fi
   124  
   125  	if [ $(ismounted) -eq 0 ] ; then
   126  		log "{\"status\": \"Success\"}"
   127  		exit 0
   128  	fi
   129  
   130  	umount ${MNTPATH} &> /dev/null
   131  	if [ $? -ne 0 ]; then
   132  		err "{ \"status\": \"Failed\", \"message\": \"Failed to unmount volume at ${MNTPATH}\"}"
   133  		exit 1
   134  	fi
   135  
   136  	log "{\"status\": \"Success\"}"
   137  	exit 0
   138  }
   139  
   140  isattached() {
   141  	log "{\"status\": \"Success\", \"attached\":true}"
   142  	exit 0
   143  }
   144  
   145  op=$1
   146  
   147  if [ "$op" = "init" ]; then
   148  	log "{\"status\": \"Success\"}"
   149  	exit 0
   150  fi
   151  
   152  if [ $# -lt 2 ]; then
   153  	usage
   154  fi
   155  
   156  shift
   157  
   158  case "$op" in
   159  	attach)
   160  		attach $*
   161  		;;
   162  	detach)
   163  		detach $*
   164  		;;
   165  	waitforattach)
   166  		waitforattach $*
   167  		;;
   168  	mountdevice)
   169  		domountdevice $*
   170  		;;
   171  	unmountdevice)
   172  		unmountdevice $*
   173  		;;
   174  	isattached)
   175                  isattached $*
   176                  ;;
   177  	*)
   178  		log "{ \"status\": \"Not supported\" }"
   179  		exit 0
   180  esac
   181  
   182  exit 1