-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_test_chattr.sh
executable file
·119 lines (100 loc) · 3.79 KB
/
run_test_chattr.sh
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
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Run tests comparing the results of using my_chattr and chattr commands on all
# file systems mounted in the test directory.
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Establish the script environment:
# 1. Exit on unset shell variables
# 2. Ensure that the findmnt and chattr command is available
# 3. Ensure that the script has been invoked by the root user
# ------------------------------------------------------------------------------
set -o nounset
if [[ -z "$(command -v findmnt)" ]]
then
printf 'The findmnt utility must be installed.\nIt is available in the util-linux package.\n' >&2
exit 1
fi
if [[ -z "$(command -v chattr)" ]]
then
printf 'The chattr utility must be installed.\nIt is available in the e2fsprogs package.\n' >&2
exit 1
fi
eff_uid=$(id --user)
if [[ "${eff_uid}" -ne 0 ]]
then
printf '%s must be run as root\n' "$0" >&2
exit 2
fi
# ------------------------------------------------------------------------------
# Local configuration
# ------------------------------------------------------------------------------
my_dir="$(dirname $0)"
pushd "${my_dir}" &>/dev/null
if [[ $? -ne 0 ]]
then
printf '%s: Unable to change directory to %s\n' "$0" "${my_dir}" >&2
exit 3
fi
parm_file="${my_dir}/my_chattr.dat"
fs_dir="/mount/test_fs"
# ------------------------------------------------------------------------------
# Validate configuration
# ------------------------------------------------------------------------------
if [[ ! -r "${parm_file}" ]]
then
printf '%s: parm file, ''%s'', is either not found nor readable.\n' \
"$0" "${parm_file}" >&2
exit 1
fi
if [[ ! -d "${fs_dir}" ]]
then
printf '%s: directory, ''%s'', for file systens is either not found nor readable.\n' \
"$0" "${fs_dir}" >&2
exit 1
fi
# ------------------------------------------------------------------------------
# Get a list of target file systems
# ------------------------------------------------------------------------------
fs_list=$(find "${fs_dir}" -type f -name target 2>/dev/null)
if [[ -z "${fs_list}" ]]
then
printf '%s: No target file systems found.\n' "$0"
exit 4
fi
# ------------------------------------------------------------------------------
# Do a test for each file system
# ------------------------------------------------------------------------------
mkdir -p base_results mine_results
exec 3<"${parm_file}"
while read -u3 mode
do
for fs_file in ${fs_list}
do
case "${fs_file}" in
*base*)
chattr "${mode}" "${fs_file}"
result_file=$(echo "${fs_file}" | \
sed -re 's!.*/([^/]+)_base/target!\1!')"${mode}"
printf '%s: %s %s\n' "${mode}" $(lsattr "${fs_file}") | \
sed -re 's!.*: (.*) .*/target!\1!' \
>"base_results/${result_file}"
;;
*mine*)
./my_chattr "${mode}" "${fs_file}"
result_file=$(echo "${fs_file}" | \
sed -re 's!.*/([^/]+)_mine/target!\1!')"${mode}"
printf '%s: %s %s\n' "${mode}" $(lsattr "${fs_file}") | \
sed -re 's!.*: (.*) .*/target!\1!' \
>"mine_results/${result_file}"
;;
*) printf '%s: invalid file name (%s)\n' "$0" "${fs_file}" >&2
;;
esac
done
done
# ------------------------------------------------------------------------------
# Compare results
# ------------------------------------------------------------------------------
diff base_results/ mine_results/
popd &>/dev/null