FIX: remove stray quote
[ta/infra-ansible.git] / roles / partfs_rootdisk / scripts / vol_mgmt.sh
1 #!/bin/bash
2
3 # Copyright 2019 Nokia
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 # This tool is meant to stop processes running in a given directory.
18 # Evacuate its contents to a temp location.
19 # Mount a given LVM to the Directory
20 # optionally change ownership of the root of the LVM
21 # Start those services again
22
23 #arg1: Directory to evacuate
24 #arg2: Volume dev path to mount
25 #arg3/arg4 (optional, must provide both): owner/group of root directory of new mount
26
27 exec >> /tmp/vol_mgmt_logfile
28 exec 2>&1
29
30 if [[ $# == 4 ]];then
31   evac_dir=$1
32   mount_vol_dev=$2
33   owner=$3
34   group=$4
35 elif [[ $# == 2 ]]; then
36   evac_dir=$1
37   mount_vol_dev=$2
38   # they didn't provide a u/g.  use the owner of the mountpoint if
39   # it exists or our own user/group (almost certaimly root) if not.
40   owner=`stat -c '%U' ${evac_dir} 2>/dev/null || id -un`
41   group=`stat -c '%G' ${evac_dir} 2>/dev/null || id -gn`
42 else
43   echo "Improper number of arguments passed!!"
44   exit 1
45 fi
46
47 echo "Trying to mount $mount_vol_dev on $evac_dir"
48
49 # Run partprobe to load new volumes
50 /usr/sbin/partprobe
51
52 if [ ! -d $evac_dir ];then
53   echo "Provided directory $evac_dir does not exist, so lets create it."
54   mkdir -p "$evac_dir"
55 fi
56
57 # mirror the permissions of the existing directory
58 oPerm=`stat -c '%a' ${evac_dir}`
59
60 if [ ! -b $mount_vol_dev ];then
61   echo "Provided volume $mount_vol_dev is not a block device!!"
62   exit 1
63 fi
64
65 # check if given directory is already mounted from needed volume
66 df -k $evac_dir | grep $mount_vol_dev > /dev/null 2>&1
67 if [[ $? == 0 ]];then
68   echo "$evac_dir is already mounted from $mount_vol_dev"
69   exit 0
70 fi
71
72 running_process_list=`lsof | grep ${evac_dir} | awk -F " " {'print $2'}| uniq`
73 systemd_service_list=`systemctl --state=active --type=socket,service | grep "loaded active" | awk -F " " {'print $1'}`
74
75 declare -a matching_systemd_services
76 for process in ${running_process_list}; do
77   for systemd_service in ${systemd_service_list}; do
78     systemd_MainPID=`systemctl show -p MainPID ${systemd_service} | awk -F "MainPID=" {'print $2'}`
79     if [[ $process == $systemd_MainPID ]];then
80       matching_systemd_services+=(${systemd_service})
81       break
82     fi
83   done
84 done
85
86 # Stop running services which were using old dir
87 echo "Stopping services ${matching_systemd_services[@]}"
88 for service_name in ${matching_systemd_services[@]}; do
89   systemctl stop $service_name > /dev/null 2>&1
90   if [[ $? != 0 ]];then
91     service_base_name=`echo $service_name | awk -F ".service" {'print $1'}`
92     service $service_base_name stop > /dev/null 2>&1
93   fi
94
95   service_wait_count=10
96   systemd_status=0
97   while [[ $service_wait_count != 0 && $systemd_status == 0 ]]; do
98     echo "waiting for $service_name to stop"
99     sleep 1
100     ((service_wait_count--))
101     systemctl status $service_name > /dev/null 2>&1
102     systemd_status=$?
103   done
104
105   if [[ $service_wait_count == 0 ]];then
106     echo "$service_name did not stop gracefully. Stopping it forcefully."
107     systemctl -f stop $service_name > /dev/null 2>&1
108   fi
109 done
110
111 # Move old dir contents to tmp location
112 tmp_dir="/tmp/`basename ${evac_dir}`"
113 mkdir -p $tmp_dir
114 cp -rpf ${evac_dir}/* $tmp_dir
115 rm -rf ${evac_dir}/*
116
117 # Mount the volume on dir
118 mount $evac_dir
119
120 chown ${owner}:${group} ${evac_dir}
121 chmod ${oPerm} ${evac_dir}
122
123 cp -rpf $tmp_dir/* ${evac_dir}/
124 rm -rf $tmp_dir
125
126 # Start the services again
127 echo "Starting services ${matching_systemd_services[@]}"
128 for service_name in ${matching_systemd_services[@]}; do
129   systemctl start $service_name > /dev/null 2>&1
130 done