-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchkext4
120 lines (102 loc) · 2.94 KB
/
chkext4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#-------------------------------
# chkext4
# version 1.1
# Shell script unmounts target ext4 filesystem, checks it, remounts the filesystem, and refreshes current owner and group permissions (if selected)
# June 17, 2019
# AliciaTransmuted
# https://github.com/AliciaTransmuted/chkext4
# -------------------------------
# dependancies:
show_usage (){
printf "\n"
printf "Usage: [sudo] [sh] $0 [options [parameters]]\n"
printf "\n"
printf "Options:\n"
printf " $color_on-f|--filesystem$color_off, Filesystem to be checked I.E. $color_on-f /media/$target_user/filesystem-name$color_off\n"
printf " $color_on-b|--badblocks$color_off, Check all blocks for errors (this takes a long time!) I.E. $color_on-b$color_off\n"
printf " $color_on-o|--chown$color_off, Run chown after (using current username) I.E. $color_on-o$color_off\n"
printf " $color_on-m|--chmod$color_off, Run chmod after (using 775 as a modifier) I.E. $color_on-m$color_off\n"
printf " $color_on-h|--help$color_off, Print help (this dialog) I.E. $color_on-h$color_off\n"
printf "\n"
printf "$0 unmounts the target ext4 filesystem, checks it, remounts the filesystem, and refreshes owner and group permissions\n"
printf "\n"
return 0
}
# ***** colours
color_red="\e[91m"
color_green="\e[92m"
color_yellow="\e[93m"
color_cyan="\e[96m"
color_on="${color_green}"
color_off="\e[0m"
bad_blocks="N"
do_chown="N"
do_chmod="N"
target_filesystem="empty"
target_user=$USER
if [ -n "$SUDO_USER" ]; then
target_user=$SUDO_USER
fi
while [ ! -z "$1" ]; do
case "$1" in
--filesystem|-f)
shift
if [ -n "$1" ]; then
target_filesystem="$1"
fi
;;
--badblocks|-b)
bad_blocks="Y"
;;
--chown|-o)
do_chown="Y"
;;
--chmod|-m)
do_chmod="Y"
;;
--help|-h)
show_usage
exit 1
;;
*)
show_usage
exit 1
;;
esac
shift
done
if [ "${target_filesystem}" = "empty" ];then
show_usage
exit 1
fi
###
### Find /dev addresses for file systems to be checked
###
df -h > df.txt
test_var_x=$(grep "${target_filesystem}" df.txt)
var_target="${test_var_x%% *}"
rm -f "df.txt"
printf "\n"
printf "*** File system check ${target_filesystem} on ${var_target}\n"
printf "\n"
sudo umount "${target_filesystem}"
if [ "${bad_blocks}" = "Y" ];then
sudo fsck.ext4 -cDfty -C 0 "${var_target}"
else
sudo fsck.ext4 -fty -C 0 "${var_target}"
fi
sudo mount "${var_target}" "${target_filesystem}"
if [ "${do_chown}" = "Y" ];then
printf "\n"
printf "*** chown -R ${target_user}:${target_user} ${target_filesystem} on ${var_target}\n"
sudo chown -R "${target_user}":"${target_user}" "${target_filesystem}"
fi
if [ "${do_chmod}" = "Y" ];then
printf "\n"
printf "*** chmod -R 775 ${target_filesystem} on ${var_target}\n"
sudo chmod -R 775 "${target_filesystem}"
fi
printf "\n"
printf "*** $0 done...\n"
printf "\n"