b1ac984b30df411fcbb0c0027b4f6e066f407e2b
[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 # Start those services again
21
22 #arg1: Directory to evacuate
23 #arg2: Volume dev path to mount
24
25 exec >> /tmp/vol_mgmt_logfile
26 exec 2>&1
27
28
29 if [[ $# != 2 ]];then
30   echo "Improper number of arguments passed!!"
31   exit 1
32 fi
33
34 evac_dir=$1
35 mount_vol_dev=$2
36
37 echo "Trying to mount $mount_vol_dev on $evac_dir"
38
39 # Run partprobe to load new volumes
40 /usr/sbin/partprobe
41
42 if [ ! -d $evac_dir ];then
43   echo "Provided directory $evac_dir does not exist, so lets create it."
44   mkdir -p "$evac_dir"
45 fi
46
47 if [ ! -b $mount_vol_dev ];then
48   echo "Provided volume $mount_vol_dev is not a block device!!"
49   exit 1
50 fi
51
52 # check if given directory is already mounted from needed volume
53 df -k $evac_dir | grep $mount_vol_dev > /dev/null 2>&1
54 if [[ $? == 0 ]];then
55   echo "$evac_dir is already mounted from $mount_vol_dev"
56   exit 0
57 fi
58
59 running_process_list=`lsof | grep ${evac_dir} | awk -F " " {'print $2'}| uniq`
60 systemd_service_list=`systemctl --state=active --type=socket,service | grep "loaded active" | awk -F " " {'print $1'}`
61
62 declare -a matching_systemd_services
63 for process in ${running_process_list}; do
64   for systemd_service in ${systemd_service_list}; do
65     systemd_MainPID=`systemctl show -p MainPID ${systemd_service} | awk -F "MainPID=" {'print $2'}`
66     if [[ $process == $systemd_MainPID ]];then
67       matching_systemd_services+=(${systemd_service})
68       break
69     fi
70   done
71 done
72
73 # Stop running services which were using old dir
74 echo "Stopping services ${matching_systemd_services[@]}"
75 for service_name in ${matching_systemd_services[@]}; do
76   systemctl stop $service_name > /dev/null 2>&1
77   if [[ $? != 0 ]];then
78     service_base_name=`echo $service_name | awk -F ".service" {'print $1'}`
79     service $service_base_name stop > /dev/null 2>&1
80   fi
81
82   service_wait_count=10
83   systemd_status=0
84   while [[ $service_wait_count != 0 && $systemd_status == 0 ]]; do
85     echo "waiting for $service_name to stop"
86     sleep 1
87     ((service_wait_count--))
88     systemctl status $service_name > /dev/null 2>&1
89     systemd_status=$?
90   done
91
92   if [[ $service_wait_count == 0 ]];then
93     echo "$service_name did not stop gracefully. Stopping it forcefully."
94     systemctl -f stop $service_name > /dev/null 2>&1
95   fi
96 done
97
98 # Move old dir contents to tmp location
99 tmp_dir="/tmp/`basename ${evac_dir}`"
100 mkdir -p $tmp_dir
101 cp -rpf ${evac_dir}/* $tmp_dir
102 rm -rf ${evac_dir}/*
103
104 # Mount the volume on dir
105 mount $evac_dir
106
107 cp -rpf $tmp_dir/* ${evac_dir}/
108 rm -rf $tmp_dir
109
110 # Start the services again
111 echo "Starting services ${matching_systemd_services[@]}"
112 for service_name in ${matching_systemd_services[@]}; do
113   systemctl start $service_name > /dev/null 2>&1
114 done