summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/common/testcase/led-lights.sh
blob: 50514cd2709c85d463a357a52fb44b10cd49d8e2 (plain)
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
#!/bin/bash
# $Id: led-lights.sh $
## @file
# VirtualBox guest LED demonstration test
#

#
# Copyright (C) 2021-2022 Oracle and/or its affiliates.
#
# This file is part of VirtualBox base platform packages, as
# available from https://www.virtualbox.org.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, in version 3 of the
# License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <https://www.gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-3.0-only
#

#
# Usage:
#       led-lights.sh [-a | -r]
#

#
# Test script to twiddle the console LEDs of a VirtualBox VM.
#
# This is not an automated test, just something for humans to look
# at, to convince themselves that the VM console LEDs are working.
# By default it cycles through the LED types in a specific order.
#
# '-a' twiddles all possible LEDs at the same time
# '-r' reverses the default order
#
# For instance, run the script in 2 VMs at once, one with '-r'.
#
# LEDs are not expected to track perfectly, as other OS activities
# will light them (and buffer cache effects can delay things).  Just
# make sure that all the tested ones (hard disk, optical, USB storage,
# floppy, shared folders, net) are working.  Expected activity:
#
#     - Disk & optical devices show solid 'read'
#     - Virtual USB disk & optical devices show 'write' on the USB LED
#     - Floppy devices and shared folders alternate 'read/write'
#     - Net blinks 'write'
#
# Pre-VM setup:
#
#     Download or locate a bootable Linux ISO able to be used 'live'.
#     Make a tarball of this script + extra junk:
#
#         $ dd if=/dev/zero of=junk bs=100k count=1
#         $ tar cf floppy.img led-lights.sh junk
#
#     NOTE: floppy.img must be >= 20KiB or you will get I/O errors!
#
# VM setup:
#
#     New VM; type: Linux (subtype to match ISO); create default HD.
#     VM Settings:
#         System > raise base memory to 4GiB
#         Storage > insert 'Live bootable' Linux ISO image;
#                   turn on 'Live CD/DVD'
#         Storage > add floppy controller (i82078); insert floppy.img
#         Storage > add USB controller; insert USB HD & USB CD
#         System > move Optical before Floppy in boot order
#         Shared Folders > set up one or more Shared Folders if desired
#                   (they should be permanent, auto-mount,
#                    writable, with at least 10MB free space)
#
# Boot the VM.  Open a shell, become root, optionally install
# VirtualBox Guest Utilities to access Shared Folders, then extract
# and run this script:
#
#     $ sudo bash
#     # yum install virtualbox-guest-utils   # for Shared Folders
#     # tar xf /dev/fd0 led-lights.sh
#     # ./led-lights.sh                      [-a | -r]

if [ ! -w / ]; then
    echo "Must be run as root!" 1>&2
    exit 1
fi

all_all=false
reverse=false

if [ "x$1" = "x-a" ]; then
    all_all=true
fi

if [ "x$1" = "x-r" ]; then
    reverse=true
fi

# Copy binaries to RAM tmpfs to avoid CD I/O after cache purges
MYTMP=/tmp/led-lights.$$
mkdir $MYTMP
for bin in $(which dd sleep sync); do
    case $bin in
        /*)
            cp -p $bin $MYTMP
        ;;
    esac
done
export MYTMP PATH=$MYTMP:$PATH

set -o monitor

# Make device reads keep hitting the 'hardware'
# even if the whole medium fits in cache...
drop_cache()
{
    echo 1 >/proc/sys/vm/drop_caches
}

activate()
{
    kill -CONT -$1 2>/dev/null
}

suppress()
{
    $all_all || kill -STOP -$1 2>/dev/null
}

declare -a pids pidnames
cpids=0

twiddle()
{
    let ++cpids
    new_pid=$!
    pidname=$1
    pids[$cpids]=$new_pid
    pidnames[$cpids]=$pidname
    suppress $new_pid
}

hide_stderr()
{
    exec 3>&2 2>/dev/null
}

show_stderr()
{
    exec 2>&3 3>&-
}

bail()
{
    hide_stderr
    for pid in ${pids[*]}; do
        activate $pid
        kill -TERM -$pid
        kill -TERM $pid
    done 2>/dev/null
    rm -rf $MYTMP
    kill $$
}

trap "bail" INT

drives()
{
    echo $(
        awk '$NF ~/^('$1')$/ { print $NF }' /proc/partitions
    )
}

# Prevent job control 'stopped' msgs during twiddler startup
hide_stderr

# Hard disks
for hdd in $(drives '[sh]d.'); do
    while :; do
        dd if=/dev/$hdd of=/dev/null
        drop_cache
    done 2>/dev/null &
    twiddle disk:$hdd
done

# Optical drives
for odd in $(drives 'sr.|scd.'); do
    while :; do
        dd if=/dev/$odd of=/dev/null
        drop_cache
    done 2>/dev/null &
    twiddle optical:$odd
done

# Floppy drives
for fdd in $(drives 'fd.'); do
    while :; do
        dd if=/dev/$fdd of=$MYTMP/$fdd bs=1k count=20
        dd of=/dev/$fdd if=$MYTMP/$fdd bs=1k count=20
    done 2>/dev/null &
    twiddle floppy:$fdd
done

# Shared folders
if ! lsmod | grep -q vboxsf; then
    echo
    echo "Note: to test the Shared Folders LED, install this"
    echo "distro's VirtualBox Guest Utilities package, e.g.:"
    echo
    echo "    # yum install virtualbox-guest-utils   (Red Hat family)"
    echo "    # apt install virtualbox-guest-utils   (Debian family)"
    echo
fi >&3   # original stderr
for shf in $(mount -t vboxsf | awk '{ print $3 }'); do
    while :; do
        dd if=/dev/urandom of=$shf/tmp.led-lights.$$ bs=100k count=100
        for rep in $(seq 1 10); do
            drop_cache
            dd of=/dev/zero if=$shf/tmp.led-lights.$$ bs=100k count=100
        done
        sync
        rm -f $shf/tmp.led-lights.$$
    done >/dev/null 2>&1 &
    twiddle sharedfs:$shf
done

# Network
ping -i.2 1.2.3.4 >/dev/null &
twiddle net

# Untested LED: Graphics3D -- add some day?

sleep 0.1
show_stderr

if $reverse; then
    seq=$(seq $cpids -1 1)
else
    seq=$(seq 1 $cpids)
fi

show_intr()
{
    intr=$(stty -a | sed -n '/intr/ { s/.*intr *=* *//; s/[ ;].*//p }')
    echo
    echo "[ Hit $intr to stop ]"
    echo
}

if $all_all; then
    printf "%s ...\n" ${pidnames[*]}
    show_intr
    wait
else
    CEOL=$(tput el)
    show_intr
    while :; do
        for pidx in $seq; do
            pid=${pids[$pidx]}
            pidname=${pidnames[$pidx]}
            echo -e -n "$pidname$CEOL\r"
            shift
            activate $pid
            sleep 2
            suppress $pid
            sync
            sleep .5
        done
    done
fi