|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +g_ls="${SUDO_ALIAS} ls" |
| 4 | +g_ps="${SUDO_ALIAS} ps" |
| 5 | +g_cat="${SUDO_ALIAS} cat" |
| 6 | +g_tee="${SUDO_ALIAS} tee" |
| 7 | +g_umount="${SUDO_ALIAS} umount" |
| 8 | +g_wipefs="${SUDO_ALIAS} wipefs" |
| 9 | +g_bcache_super_show="${SUDO_ALIAS} bcache-super-show" |
| 10 | + |
| 11 | + |
| 12 | +set_value() |
| 13 | +{ |
| 14 | + local value=$1 |
| 15 | + local path=$2 |
| 16 | + echo ${value} | ${g_tee} ${path} &> /dev/null |
| 17 | +} |
| 18 | + |
| 19 | +pre_check() |
| 20 | +{ |
| 21 | + #check chunkserver is running |
| 22 | + pid=$(${g_ps} -ef | grep chunkserver | grep -v grep | awk '{print $2}') |
| 23 | + if [ -n "${pid}" ]; then |
| 24 | + echo "chunkserver is running, please stop it first" |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | + |
| 28 | + #check bcache dirty data |
| 29 | + for bcache in $(${g_ls} /sys/block | grep bcache) |
| 30 | + do |
| 31 | + if [ "$(${g_cat} /sys/block/${bcache}/bcache/dirty_data)" != "0.0k" ]; then |
| 32 | + echo "${bcache} has dirty data, please stop chunkserver and wait it cleaned" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + done |
| 36 | + |
| 37 | + echo "pre_check success" |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +stop_bcache() |
| 42 | +{ |
| 43 | + ${g_umount} /data/chunkserver* &> /dev/null |
| 44 | + ${g_umount} /data/wal/chunkserver* &> /dev/null |
| 45 | + |
| 46 | + bcache_devs=$(${g_ls} /sys/block | grep bcache) |
| 47 | + for bcache in ${bcache_devs} |
| 48 | + do |
| 49 | + backdev=/dev/$(${g_cat} /sys/block/${bcache}/bcache/backing_dev_name) |
| 50 | + uuid=$(${g_bcache_super_show} ${backdev} |grep cset |awk '{print $NF}') |
| 51 | + |
| 52 | + set_value 1 /sys/block/${bcache}/bcache/detach |
| 53 | + set_value 1 /sys/fs/bcache/${uuid}/unregister |
| 54 | + set_value 1 /sys/block/${bcache}/bcache/stop |
| 55 | + done |
| 56 | + |
| 57 | + set_value 1 /sys/fs/bcache/pendings_cleanup |
| 58 | + |
| 59 | + sleep 1 |
| 60 | + |
| 61 | + bcache_devs=$(${g_ls} /sys/block | grep bcache) |
| 62 | + cache_sets=$(${g_ls} /sys/fs/bcache | grep "-") |
| 63 | + if [ -n "${bcache_devs}" ] || [ -n "${cache_sets}" ]; then |
| 64 | + # need retry to wait bcache stop |
| 65 | + echo "stop bcache failed" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + echo "stop bcache success" |
| 69 | +} |
| 70 | + |
| 71 | +clean_bcache_data() |
| 72 | +{ |
| 73 | + if [ x"${CLEAN_DATA}" != x"true" ]; then |
| 74 | + echo "no need to clean data" |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | + |
| 78 | + for hdd in ${BACKING_DEV} |
| 79 | + do |
| 80 | + ${g_wipefs} -a --force ${hdd} &> /dev/null |
| 81 | + if [ $? != 0 ]; then |
| 82 | + echo "wipefs backing device ${hdd} failed" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + done |
| 86 | + |
| 87 | + for cache in ${CACHE_DEV} |
| 88 | + do |
| 89 | + ${g_wipefs} -a --force ${cache} &> /dev/null |
| 90 | + if [ $? != 0 ]; then |
| 91 | + echo "wipefs cache device ${cache} failed" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + done |
| 95 | + |
| 96 | + echo "clean backing and cache devices data success" |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +pre_check |
| 101 | +stop_bcache |
| 102 | +clean_bcache_data |
| 103 | + |
0 commit comments