-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathprocess_config.sh
executable file
·454 lines (384 loc) · 11.5 KB
/
process_config.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Default configuration file; do not edit this file, but the file .S-config.sh
# in your home directory. The latter gets created on the very first execution
# of some benchmark script (even if only the option -h is passed to the script).
# first, a little code to to automate stuff; configuration parameters
# then follow
if [[ "$1" != "-h" && "$(id -u)" -ne "0" && -z $BATS_VERSION ]]; then
echo "You are currently executing me as $(whoami),"
echo "but I need root privileges (e.g., to switch"
echo "between schedulers)."
echo "Please run me as root."
exit 1
else
FIRST_PARAM=$1
fi
function print_dev_help
{
# find right path to config file, used in help messages
if [ "$SUDO_USER" != "" ]; then
eval REALHOME=~$SUDO_USER
else
eval REALHOME=~
fi
CONFPATH=${REALHOME}/.S-config.sh
echo
echo To address this issue, you can
echo - either set the parameter BASE_DIR, in $CONFPATH, to a directory
echo " that you know to be in a local filesystem (such a local filesystem"
echo " must be mounted on a supported physical or virtual device);"
echo - or set the parameter TEST_DEV, in $CONFPATH, to \
the \(supported\) device
echo " or partition you want to use for your tests."
echo
echo See the comments in $CONFPATH for details and more options.
}
function get_partition_info
{
PART_INFO=
if [[ -e $1 ]]; then
PART_INFO=$(df $1 | egrep $1)
else
# most likely linux live os
PART_INFO=$(df | egrep $1)
fi
echo $PART_INFO
}
function find_partition_for_dir
{
PART=$(df "$1" | tail -1 | awk '{print $1;}')
echo $PART
}
function find_dev_for_dir
{
if [[ "$PART" == "" ]]; then
PART=$(find_partition_for_dir $1)
fi
if [[ "$PART" == "" ]]; then
echo Sorry, failed to find the partition containing the directory
echo $1.
print_dev_help
exit
fi
REALPATH=$PART
if [[ -e $PART ]]; then
REALPATH=$(readlink -f $PART) # moves to /dev/dm-X in case of device mapper
if [[ "$REALPATH" == "" ]]; then
echo The directory where you want me store my test files,
echo namely $1,
echo is contained in the following partition:
echo $PART.
echo Unfortunately, such a partition does not seem to correspond
echo to any local partition \(it is probably a remote filesystem\).
print_dev_help
exit
fi
fi
BASEPART=$(basename $PART)
REALPART=$(basename $REALPATH)
BACKING_DEVS=
if [[ "$(echo $BASEPART | egrep loop)" != "" ]]; then
# loopback device: $BASEPART is already equal to the device name
BACKING_DEVS=$BASEPART
elif cat /proc/1/cgroup | tail -1 | egrep -q "container"; then
# is container. lsblk will return block devices of the host
# so let's use the host drive.
BACKING_DEVS=$(lsblk | egrep -m 1 "disk" | awk '{print $1;}')
elif ! egrep -q $BASEPART /proc/partitions; then
# is linux live OS. Use cd drive
BACKING_DEVS=$(lsblk | egrep -m 1 "rom" | awk '{print $1;}')
else
# get devices from partition
for dev in $(ls /sys/block/); do
if ! lsblk /dev/$dev | egrep -q "$BASEPART|$REALPART"; then
# the block device does not contain the partition we're
# attempting to run benchmarks on.
continue
fi
disk_line=$(lsblk -n -i /dev/$dev | egrep disk | egrep -v "^ |^\`|\|")
if [[ "$disk_line" != "" && \
( "$(lsblk -n -o TRAN /dev/$dev 2> /dev/null)" != "" || \
$(echo $dev | egrep "mmc|sda|nvme") != "" \
) ]]; then
BACKING_DEVS="$BACKING_DEVS $dev"
if [[ "$HIGH_LEV_DEV" == "" ]]; then
HIGH_LEV_DEV=$dev # make md win in setting HIGH_LEV_DEV
fi
fi
if lsblk /dev/$dev | grep -q "md.*raid"; then
if [[ "$(echo $HIGH_LEV_DEV | egrep md)" != "" ]]; then
echo -n Stacked raids not supported
echo " ($HIGH_LEV_DEV + $dev), sorry."
print_dev_help
exit
fi
HIGH_LEV_DEV=$dev # set unconditionally as high-level
# dev (the one used, e.g., to
# measure aggregate throughput)
fi
done
fi
if [[ "$BACKING_DEVS" == "" ]]; then
echo Block devices for partition $BASEPART or $REALPART unrecognized.
print_dev_help
exit
fi
}
function check_create_mount_part
{
if [[ $(echo $BACKING_DEVS | egrep "mmc|nvme") != "" ]]; then
extra_char=p
fi
TARGET_PART=${BACKING_DEVS}${extra_char}1
if [[ ! -b $TARGET_PART ]]; then
(
echo o # Create a new empty DOS partition table
echo n # Add a new partition
echo p # Primary partition
echo 1 # Partition number
echo # First sector (Accept default: 1)
echo # Last sector (Accept default: varies)
echo w # Write changes
) | fdisk $BACKING_DEVS > /dev/null
fi
BASE_DIR=$1
if [[ "$(mount | egrep $BASE_DIR)" == "" ]]; then
fsck.ext4 -n $TARGET_PART
if [[ $? -ne 0 ]]; then
mkfs.ext4 -F $TARGET_PART
if [ $? -ne 0 ]; then
echo Filesystem creation failed, aborting.
exit
fi
fi
mkdir -p $BASE_DIR
mount $TARGET_PART $BASE_DIR
if [ $? -ne 0 ]; then
echo Mount failed, aborting.
exit
fi
fi
BACKING_DEVS=$(basename $BACKING_DEVS)
HIGH_LEV_DEV=$BACKING_DEVS
}
function use_nullb_dev
{
lsmod | grep null_blk > /dev/null
if [ $? -eq 0 ]; then
modprobe -r null_blk 2> /dev/null
if [ $? -eq 1 ]; then # null_blk is not a module but built-in
echo "ERROR: failed to unload null_blk module"
exit 1
fi
fi
modprobe null_blk queue_mode=2 irqmode=0 completion_nsec=0 \
nr_devices=1
if [ $? -ne 0 ]; then
echo "ERROR: failed to load null_blk module"
exit 1
fi
BACKING_DEVS=nullb0
HIGH_LEV_DEV=$BACKING_DEVS
BASE_DIR= # empty, to signal that there is no fs and no file to create
}
function use_scsi_debug_dev
{
../utilities/check_dependencies.sh lsscsi mkfs.ext4 fsck.ext4 sfdisk
if [[ $? -ne 0 ]]; then
exit 1
fi
if [[ "$(lsmod | egrep scsi_debug)" == "" ]]; then
echo -n Setting up scsi_debug, this may take a little time ...
sudo modprobe scsi_debug ndelay=1600000 dev_size_mb=1000 max_queue=4
if [[ $? -ne 0 ]]; then
echo
echo "Failed to load scsi_debug module (maybe not installed?)"
exit 1
fi
echo " done"
fi
BACKING_DEVS=$(lsscsi | egrep scsi_debug | sed 's<\(.*\)/dev/</dev/<')
BACKING_DEVS=$(echo $BACKING_DEVS | awk '{print $1}')
check_create_mount_part /mnt/scsi_debug
}
function format_and_use_test_dev
{
../utilities/check_dependencies.sh mkfs.ext4 fsck.ext4 sfdisk
if [[ $? -ne 0 ]]; then
exit 1
fi
BACKING_DEVS=/dev/$TEST_DEV
check_create_mount_part /mnt/S-testfs
}
function get_max_affordable_file_size
{
if [[ "$FIRST_PARAM" == "-h" ]]; then
echo
exit
fi
if [[ "$BASE_DIR" == "" ]]; then
TOT_SIZE=$(blockdev --getsize64 /dev/$HIGH_LEV_DEV)
TOT_SIZE_MB=$(( $TOT_SIZE / 1000000 ))
echo $(( $TOT_SIZE_MB / 100 ))
exit
fi
if [[ ! -d $BASE_DIR ]]; then
echo
exit
fi
if [[ "$PART" == "" ]]; then
PART=$(find_partition_for_dir $BASE_DIR)
fi
if [[ "$(get_partition_info $PART)" == "" ]]; then # it must be /dev/root
PART=/dev/root
fi
BASE_DIR_SIZE=$(du -s $BASE_DIR | awk '{print $1}')
FREESPACE=$(get_partition_info $PART | awk '{print $4}' | head -n 1)
MAXTOTSIZE=$((($FREESPACE + $BASE_DIR_SIZE) / 2))
MAXTOTSIZE_MiB=$(($MAXTOTSIZE / 1024))
MAXSIZE_MiB=$((MAXTOTSIZE_MiB / 15))
MAXSIZE_MiB=$(( $MAXSIZE_MiB<500 ? $MAXSIZE_MiB : 500 ))
if [[ -f ${BASE_FILE_PATH}0 ]]; then
file_size=$(du --apparent-size -B 1024 ${BASE_FILE_PATH}0 |\
col -x | cut -f 1 -d " ")
file_size_MiB=$(($file_size / 1024))
else
file_size_MiB=$MAXSIZE_MiB
fi
echo $(( $MAXSIZE_MiB>$file_size_MiB ? $file_size_MiB : $MAXSIZE_MiB ))
}
function find_partition {
lsblk -rno MOUNTPOINT /dev/$TEST_DEV \
> mountpoints 2> /dev/null
cur_line=$(tail -n +2 mountpoints | head -n 1)
i=3
while [[ "$cur_line" == "" && \
$i -lt $(cat mountpoints | wc -l) ]]; do
cur_line=$(tail -n +$i mountpoints | head -n 1)
i=$(( i+1 ))
done
rm mountpoints
echo $cur_line
}
function prepare_basedir
{
# NOTE: the following cases are mutually exclusive
if [[ "$FIRST_PARAM" == "-h" ]]; then
return
fi
if [[ "$SCSI_DEBUG" == yes ]]; then
use_scsi_debug_dev # this will set BASE_DIR
return
fi
if [[ "$NULLB" == yes ]]; then
use_nullb_dev
return
fi
if [[ "$TEST_DEV" != "" ]]; then
# strip /dev/ if present
TEST_DEV=$(echo $TEST_DEV | sed 's</dev/<<')
if [[ "${TEST_DEV: -1}" == [0-9] ]]; then
parent_dev=$(readlink /sys/class/block/$TEST_DEV)
parent_dev=${parent_dev%/*}
parent_dev=${parent_dev##*/}
if [[ "$parent_dev" == block ]]; then # not a partition
parent_dev=
fi
fi
if [[ "$parent_dev" != "" ]]; then
TEST_PARTITION=/dev/$TEST_DEV
TEST_PARTITION=$(readlink -f $TEST_PARTITION)
TEST_PARTITION=$(echo $TEST_PARTITION | sed 's</dev/<<')
TEST_DEV=$parent_dev
else
TEST_DEV=$(readlink -f /dev/$TEST_DEV)
TEST_DEV=$(echo $TEST_DEV | sed 's</dev/<<')
fi
DISK=$(lsblk -o TYPE /dev/$TEST_DEV | egrep disk)
if [[ "$DISK" != "" ]]; then
FORMAT_DISK=$FORMAT
fi
if [[ $TEST_PARTITION != "" ]]; then
mntpoint=$(lsblk -no MOUNTPOINT /dev/$TEST_PARTITION)
else
mntpoint=$(find_partition)
if [[ "$mntpoint" == "" ]]; then
# check whether whole dev is used as a degenerate partition
mntpoint=$(lsblk -no MOUNTPOINT /dev/$TEST_DEV)
fi
fi
if [[ "$mntpoint" == "" && "$FORMAT_DISK" != yes ]]; then
echo -n "Sorry, no mountpoint found for partitions "
echo in $TEST_DEV,
echo or no partition in $TEST_DEV at all.
echo Set FORMAT=yes and TEST_DEV=\<actual drive\> if you want
echo me to format the drive, create a fs and mount it for you.
echo Aborting.
exit
elif [[ "$mntpoint" == "" ]]; then # implies $FORMAT_DISK == yes
format_and_use_test_dev
mntpoint=$BASE_DIR
fi
mntpoint=${mntpoint%/} # hate to see consecutive / in paths :)
BASE_DIR="$mntpoint/var/lib/S"
fi
if [[ ! -d $BASE_DIR ]]; then
mkdir -p $BASE_DIR
fi
if [[ ! -w $BASE_DIR && "$TEST_PARTITION" != "" ]]; then
echo Sorry, $BASE_DIR not writeable for test partition $TEST_PARTITION
echo Aborting.
exit
fi
if [[ ! -w $BASE_DIR ]]; then
echo "$BASE_DIR is not writeable, reverting to /tmp/test"
BASE_DIR=/tmp/test
mkdir -p $BASE_DIR
fi
if [[ "$PART" == "" ]]; then
PART=$(find_partition_for_dir $BASE_DIR)
fi
if [[ "$(get_partition_info $PART)" == "" ]]; then # it must be /dev/root
PART=/dev/root
fi
FREESPACE=$(get_partition_info $PART | awk '{print $4}' | head -n 1)
BASE_DIR_SIZE=$(du -s $BASE_DIR | awk '{print $1}')
if [[ $(( ($FREESPACE + $BASE_DIR_SIZE) / 1024 )) -lt 500 ]]; then
echo Not enough free space for test files in $BASE_DIR: \
I need at least 500MB
exit
fi
if [[ "$TEST_DEV" == "" && -d $BASE_DIR ]]; then
find_dev_for_dir $BASE_DIR
else
# in case no path setting BACKING_DEVS has been followed:
BACKING_DEVS=$TEST_DEV
HIGH_LEV_DEV=$BACKING_DEVS
fi
}
# MAIN
prepare_basedir
# paths of files to read/write in the background
if [[ "$BASE_DIR" != "" ]]; then
BASE_FILE_PATH=$BASE_DIR/largefile
fi
if [[ "$DEVS" == "" ]]; then
DEVS=$BACKING_DEVS
fi
if [[ "$FIRST_PARAM" != "-h" && -z $BATS_VERSION ]]; then
# test target devices
for dev in $DEVS; do
cat /sys/block/$dev/queue/scheduler >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -n "There is something wrong with the device /dev/$dev, "
echo which should be
echo a device on which your test directory $BASE_DIR
echo is mounted.
echo -n "Try setting your target devices manually "
echo \(and correctly\) in ~/.S-config.sh
exit
fi
done
fi
if [[ "$FILE_SIZE_MB" == "" ]]; then
FILE_SIZE_MB=$(get_max_affordable_file_size)
fi