/
io-set-sched
executable file
·130 lines (104 loc) · 3.23 KB
/
io-set-sched
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
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
###########################################################################
#
# Copyright (c) 2018 Timothy Savannah All Rights Reserved
#
# Licensed under terms of the GNU General Public License Version 3
# which should have been supplied with this distribution as LICENSE
#
# If not, you can access the latest LICENSE at:
# https://raw.githubusercontent.com/kata198/ioschedset/master/LICENSE
#
#
# io-set-sched - Alters the current I/O scheduler for block devices
#
###########################################################################
PROG_NAME="${BASH_SOURCE[0]}"
PROG_PATH="$(realpath "$0")"
PROG_BASENAME="$(basename "${PROG_PATH}")"
VERSION="1.1.1"
get_available_schedulers() {
pushd /sys/block >/dev/null 2>&1
FIRST_BLOCK_DEVICE="$(echo * | tr ' ' '\n' | head -n1)"
popd >/dev/null 2>&1
AVAILABLE_SCHEDULERS="$(cat "/sys/block/${FIRST_BLOCK_DEVICE}/queue/scheduler" | sed -e 's|\[||g' -e 's|\]||g')"
echo "${AVAILABLE_SCHEDULERS}"
}
usage() {
AVAILABLE_SCHEDULERS="$(get_available_schedulers)"
cat 2>&1 <<EOT
Usage: ${PROG_BASENAME} [scheduler] ([block device name1] ... [block device nameN])
Sets the IO scheduler for block devices
If no arguments are provided, will set for all devices.
Otherwise, will apply to every block device listed
You may also call: \`${PROG_BASENAME} --list'
to list available schedulers.
Choice of schedulers: ${AVAILABLE_SCHEDULERS}
${PROG_BASENAME} version ${VERSION} by Timothy Savannah
EOT
}
cd /sys/block
if [ $# -eq 0 ];
then
printf "Not enough arguments.\n\n" >&2
usage
exit 1;
fi
for arg in "$@";
do
if [[ "${arg}" = '--help' ]];
then
usage;
exit 1;
elif [[ "${arg}" = '--list' ]];
then
AVAILABLE_SCHEDULERS="$(get_available_schedulers)"
printf "%s\n" "${AVAILABLE_SCHEDULERS}"
exit 0;
elif [[ "${arg}" = "--version" ]];
then
printf "\n${PROG_BASENAME} version ${VERSION} by Timothy Savannah\n\n" >&2
exit 0;
fi
done
# Do this AFTER the arg parsing, we don't need to be root to read the usage
# nor to query available scheulers
if [ "`whoami`" != "root" ];
then
printf "Must be root to set IO Scheduler. Rerunning under sudo...\n\n" >&2
sudo "${PROG_PATH}" "$@"
exit $?
fi
IO_SCHED="$1"
shift
if [ $# -eq 0 ];
then
# No args after io scheduler, apply to all
BLOCK_DEVS="$(echo *)"
else
# Otherwise, apply only to specified devices
BLOCK_DEVS="$@"
fi
exitCode=0
for blockDev in ${BLOCK_DEVS};
do
# Incase we got passed /dev/sda or whatever, do the basename
blockDev="$(basename "${blockDev}")"
if [ ! -d "${blockDev}" ];
then
printf "No such block device: '%s'.\n\tRun \`io-get-sched' without agruments for a list of all devices\n" "${blockDev}" >&2
# set exit code to ENOENT ( No Such File or Directory )
exitCode=2
continue
fi
echo "${IO_SCHED}" > "./${blockDev}/queue/scheduler"
RET=$?
if [ $RET -eq 0 ];
then
printf "+ Successfully set %s to '%s'!\n" "${blockDev}" "${IO_SCHED}"
else
printf "%s FAILED to set %s to '%s' [%d]!\n" '--' "${blockDev}" "${IO_SCHED}" "${RET}"
exitCode=${RET}
fi
done
#vim: set ts=4 sw=4 st=4 expandtab :