OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [start-linuxes.sh] - Blame information for rev 596

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 548 jeremybenn
#!/bin/bash
2
 
3
# Copyright (C) 2011 Embecosm Limited
4
 
5
# Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
6
 
7
# This file is a script to start up a group of Linux instances
8
 
9
# This program is free software; you can redistribute it and/or modify it
10
# under the terms of the GNU General Public License as published by the Free
11
# Software Foundation; either version 3 of the License, or (at your option)
12
# any later version.
13
 
14
# This program is distributed in the hope that it will be useful, but WITHOUT
15
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17
# more details.
18
 
19
# You should have received a copy of the GNU General Public License along
20
# with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
# ------------------------------------------------------------------------------
23
 
24
# Start up a batch of linux sessions ready for testing.
25
 
26
# There are a number of ways of starting up vmlinux to force a particular IP
27
# address to be used, which are specified by the first argument
28
 
29
#   DHCP   - Linux will get an IP address via DHCP. In this case we must poll
30
#            the router to get the newly created IP address.
31
 
32
#   STATIC - We force Linux to chose a static address, by an explicit ifconfig
33
#            in the etc/init.d/rcS of the initramfs. We need to select an IP
34
#            address that is not currently being used.
35
 
36
# In both cases, potentially we get in a mess if someone else is starting up
37
# sessions at this time.
38
 
39
 
40
# ------------------------------------------------------------------------------
41
# Tell the user how to use this command
42
 
43
# If we are doing STATIC startup, the <count> must be followed by <count> IP
44
# addresses.
45
# ------------------------------------------------------------------------------
46
function bad_args {
47
    echo "Usage: start-linuxes.sh DHCP|STATIC <count> <dir> [ <ip> <ip> ... ]"
48
    echo "  <count>   - Number of Linux sessions to start up"
49
    echo "  <dir>     - Linux directory"
50
    echo "  <ip>      - IP addresses (only if STATIC)"
51
}
52
 
53
 
54
# ------------------------------------------------------------------------------
55
# Check the args
56
 
57
# Remote machine name is optional. For argument usage, see function bad_args.
58
 
59
# @param[in] $1      Type of processing (DHCP or STATIC)
60
# @pmara[in] $2      Count of Linux instances to start
61
# @param[in] $3      Linux directory
62
# @param[in] $4 ...  IP addresses if static startup
63
# ------------------------------------------------------------------------------
64
function get_args {
65
    # Check We have at least the first two arguments
66
    if [ $# -lt 3 ]
67
    then
68
        bad_args
69
        exit  1
70
    fi
71
 
72
    type=$1
73
    shift
74
    count=$1
75
    shift
76
    linux_dir=$1
77
    shift
78
 
79
    # Check type and get static IP addresses
80
    case "x${type}" in
81
        "xSTATIC" )
82
            # Get all the IP addresses
83
            for (( i=0 ; i < ${count} ; i++ ))
84
            do
85
                if [ "x${i}" == "x" ]
86
                then
87
                    bad_args
88
                    exit  1
89
                fi
90
 
91
                ip_list[${i}]=$1
92
                shift
93
            done
94
            ;;
95
 
96
        "xDHCP" )
97
            # No extra args for DHCP
98
            ;;
99
 
100
        "x*" )
101
            # Not recognized
102
            bad_args
103
            exit  1
104
            ;;
105
    esac
106
}
107
 
108
 
109
# ------------------------------------------------------------------------------
110
# Allocate an unused TAP device
111
 
112
# We need /dev/tap?? that is not being used at present. We have a custom
113
# program to do this.
114
 
115
# @return  The TAP to use.
116
# ------------------------------------------------------------------------------
117
function allocate_tap {
118
    for (( i=0 ; ${i} < 20 ; i=${i} + 1 ))
119
    do
120
        if ./check-tap "tap${i}"
121
        then
122
            echo "tap${i}"
123
            exit
124
        fi
125
    done
126
}
127
 
128
 
129
# ------------------------------------------------------------------------------
130
# Start up one STATIC instance
131
 
132
# We need to edit the IP address into rcS, rebuild Linux and start Linux using
133
# the next available TAP.
134
 
135
# This could go wrong if someone else starts up a machine at the same time. We
136
# could reserve an unlikely block to avoid this, and flock this code, to avoid
137
# another instance causing trouble. For now we leave it unprotected.
138
 
139
# @param[in] $1  An index number of this instance
140
# @param[in] $2  The IP address to use
141
 
142
# @return  The Linux process ID
143
# ------------------------------------------------------------------------------
144
function start_one_static {
145
 
146
    ip=$2
147
 
148
    # A new Linux instance will require a new TAP in its config
149
    tap=`allocate_tap $*`
150
 
151
    # Edit the Or1ksim configuration file to use the allocated TAP
152
    sed -i -e "s/^\( *tap_dev *= *\).*$/\1\"$tap\"/" \
153
        ${linux_dir}/arch/openrisc/or1ksim_eth.cfg
154
 
155
    # Edit the Linux rcS file and rebuild
156
    sed -i -e "s/ifconfig eth0.*up/ifconfig eth0 ${ip} up/" \
157
        ${linux_dir}/arch/openrisc/support/initramfs/etc/init.d/rcS
158
 
159
    cd ${linux_dir}
160
    make vmlinux ARCH=openrisc CROSS_COMPILE=/opt/or32-new/bin/or32-elf- \
161
        > /dev/null 2>&1
162
 
163
    if [ $? != 0 ];
164
    then
165
        echo "start-linux.sh: 'make vmlinux' failed"
166
        exit 1
167
    fi
168
 
169
    cd - > /dev/null 2>&1
170
 
171
    # Start up Linux and allow time for the ramdisk to unpack, by waiting
172
    # until ping to the IP address is OK.
173
    or32-elf-sim -f ${linux_dir}/arch/openrisc/or1ksim_eth.cfg \
174
        ${linux_dir}/vmlinux \
175
        > `hostname`-linux-console-$$-$1.log 2>&1 & linux_pid=$!
176
 
177
    until ping -c 1 -q ${ip} > /dev/null 2>&1
178
    do
179
        sleep 1
180
    done
181
 
182
    # make us nicer than normal, then return the new process ID
183
    renice +1 ${linux_pid} > /dev/null 2>&1
184
    echo "${linux_pid}"
185
}
186
 
187
 
188
# ------------------------------------------------------------------------------
189
# Start up one DHCP instance
190
 
191
# Start up Linux using the next available TAP.
192
 
193
# @param[in] $1  An index number of this instance
194
 
195
# @return  The Linux process ID
196
# ------------------------------------------------------------------------------
197
function start_one_dhcp {
198
    # A new Linux instance will require a new TAP in its config
199
    tap=`allocate_tap $*`
200
 
201
    # Edit the Or1ksim configuration file to use the allocated TAP
202
    sed -i -e "s/^\( *tap_dev *= *\).*$/\1\"$tap\"/" \
203
        ${linux_dir}/arch/openrisc/or1ksim_eth.cfg
204
 
205
    # Start up Linux
206
    or32-elf-sim -f ${linux_dir}/arch/openrisc/or1ksim_eth.cfg \
207
        ${linux_dir}/vmlinux > `hostname`-linux-output-$$-$1 2>&1 & linux_pid=$!
208
 
209
    # make us nicer than normal, then return the new process ID
210
    renice +1 ${linux_pid} > /dev/null 2>&1
211
    echo "${linux_pid}"
212
 
213
}
214
 
215
 
216
# ------------------------------------------------------------------------------
217
# Start up using DHCP on the local machine
218
 
219
# We wait for 30 seconds between each Linux startup, to avoid flooding the
220
# DHCP server.
221
 
222
# @return  A list of IP address of the Linux instances
223
# ------------------------------------------------------------------------------
224
function start_all_dhcp {
225
    echo "DHCP startup in progress..."
226
 
227
    for (( i=0 ; ${i} < ${count} ; i=${i} + 1 ))
228
    do
229
        one_res=`start_one_dhcp ${i}`
230
        res="${res} ${one_res}"
231
        sleep 30
232
    done
233
 
234
    echo ${res}
235
}
236
 
237
 
238
# ------------------------------------------------------------------------------
239
# Start up using STATIC on the local machine
240
# ------------------------------------------------------------------------------
241
function start_all_static {
242
    echo "STATIC startup in progress..."
243
 
244
    for (( i=0 ; ${i} < ${count} ; i=${i} + 1 ))
245
    do
246
        one_res=`start_one_static ${i} ${ip_list[${i}]}`
247
        res="${res} ${one_res}"
248
    done
249
 
250
    echo ${res}
251
}
252
 
253
 
254
# ------------------------------------------------------------------------------
255
# Main program
256
# ------------------------------------------------------------------------------
257
 
258
# Some useful files
259
tmp1=/tmp/start-linuxes-1-$$
260
tmp2=/tmp/start-linuxes-2-$$
261
 
262
# Check the arguments
263
get_args $*
264
 
265
# Start Linuxes
266
case "${type}" in
267
    "DHCP" )
268
        start_all_dhcp
269
        ;;
270
 
271
    "STATIC" )
272
        start_all_static
273
        ;;
274
 
275
    "*" )
276
        echo "Abort at line ${LINENO} in ${BASH_SOURCE[0]}"
277
        exit 255
278
        ;;
279
esac

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.