aboutsummaryrefslogtreecommitdiffstats
path: root/sysconfig/network-scripts/ifup-aliases
blob: fce77cb567da857a1a63c27d1f2671e256c4bd74 (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
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
#!/bin/bash

#
# configures aliases of device $1
#
# This script goes out of its way to arrive at the configuration of ip
# aliases described in the ifcfg-$DEV:* and ifcfg-$DEV-range* files from
# whatever existing configuration it may be given: existing aliases not
# specified in the configuration will be removed, netmasks and broadcast
# addrs will be updated on existing aliases, and new aliases will be setup.
#
#   range specification files:
#
# One can specify ranges of alised ipaddress using ifcfg-$DEV-range* files.
# Specify multiple ranges using multiple files, such as ifcfg-eth0-range0 and
# ifcfg-eth0-range1, etc. In these files, the following configuration variables 
# specify the range:
#
#    IPADDR_START    -- ipaddr to start range at. eg "192.168.30.1"
#    IPADDR_END      -- ipaddr to end range at. eg "192.168.30.254"
#    CLONENUM_START  -- interface clone number to start using for this range. eg "0"
#
# The above example values create the interfaces eth0:0 through eth0:253 using
# ipaddrs 192.168.30.1 through 192.168.30.254, inclusive.
#
# Other configuration variables such as NETMASK and BROADCAST may be specified
# in the range file and will apply to all of the ipaddresses in the range. Range
# files also inherit configuration from the ifcfg-$DEV file just like normal.
#
# Note that IPADDR_START and IPADR_END are required to be in the same class-c
# block. I.e. IPADDR_START=192.168.30.1 and IPADDR_END=192.168.31.255 is
# not valid.
#
#   speed with large sets of interfaces:
#
# Considerable effort was spent making this script fast. It can efficiently 
# handle a thousand ip aliases on one interface.
# 
# With large sets of ipaddresses the NO_ALIASROUTING=yes configuration is
# highly recommended. (This can be specified in ifcfg-$DEV and inherited.) This
# prevents this script from setting up routing details for the virtual
# interfaces, which I don't think is needed, because outgoing traffic can use the
# main interface. However, make your own conclusions on what you need.
# 
# My test setup of four class C address blocks on a P166 took 25 seconds of 
# which 16 seconds of this was spent in the ifcconfig calls. Without the 
# NO_ALIASROUTING=yes config an additional 12 seconds is spent in route calls.
#
#   notes on internals:
#
# This script uses the bash "eval" command to lookup shell variables with names
# which are generated from other shell variables. This allows us to, in effect,
# create hashes using the shell variable namesspace by just including the hash
# key in the name of the variable.
#
# This script originally written by: David Harris <dharris@drh.net> 
#                                    Principal Engineer, DRH Internet
#                                    June 30, 1999
#
#            modified by: Bill Nottingham <notting@redhat.com>
TEXTDOMAIN=initscripts

device=$1
if [ "$device" = "" ]; then
	echo $"usage: ifup-aliases <net-device>\n"
	exit 1
fi

parent_device=$device

. network-functions

       #
       # Grab the current configuration of any running aliases device info is placed into 
       # variables in the form: rdev_$DEVICE_addr, dev_$DEVICE_mb and rdevip_$IPGLOP.
       # A list of all the devices is created in rdev_LIST.
       #

# sorry this is all one line -- it didn't work on multiple lines for me
eval `LC_ALL= LANG= ifconfig | sed -e ' /^[^[:space:]]*:[0-9]*/ { s|^\([^:]*\):\([0-9]*\).*$|X=x; TMP="\2"; [ "$device" != "\1" ] \|\| |g; b; }; /^[^[:space:]]/ { s|.*|X=x; [ 0 = 0 ] \|\| |g; b; }; /inet addr:[0-9]*\.[0-9]*\.[0-9]*\.[0-9]* *Bcast:[0-9.]* *Mask:[0-9.]*/ { s|^.*inet addr:\(\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\) *Bcast:\([0-9.]*\) *Mask:\([0-9.]*\).*$|eval " rdev_${TMP}_addr=\1; rdev_${TMP}_mb=\7_\6; rdevip_\2_\3_\4_\5=${TMP}; rdev_LIST=\\\\"\\\\$rdev_LIST \\\\${TMP}\\\\"; " \|\| |g; b; }; s|^.*$||g; ' ` X=x;

if [ -z "$rdev_LIST" ]; then
       no_devices_are_up=yes
fi

##for DEVNUM in $rdev_LIST ; do
##eval "
##echo \"rdev_${DEVNUM}_addr = \$rdev_${DEVNUM}_addr\"
##echo \"rdev_${DEVNUM}_mb = \$rdev_${DEVNUM}_mb\" ";
##done
##echo ""; echo "-----"; echo ""

       #
       # Store configuration of the parent device and network
       #

# read from the /etc/sysconfig/network
eval ` (
       . /etc/sysconfig/network;
       echo network_GATEWAY=$GATEWAY\;;
       echo network_GATEWAYDEV=$GATEWAYDEV\;;
) `

# read defaults from the parent config file
PARENTCONFIG=ifcfg-$device
[ -f $PARENTCONFIG ] || {
       echo $"Missing config file $PARENTCONFIG." >&2
       exit 1
}
eval ` (
       . $PARENTCONFIG;
       echo default_NETMASK=$NETMASK\;;
       echo default_BROADCAST=$BROADCAST\;;
       echo default_GATEWAY=$GATEWAY\;;
       echo default_GATEWAYDEV=$GATEWAYDEV\;;
       echo default_NO_ALIASROUTING=$NO_ALIASROUTING\;;
) `

function ini_env ()
{
       DEVICE=""
       IPADDR=""
       NETMASK=$default_NETMASK
       BROADCAST=$default_BROADCAST
       GATEWAY=$default_GATEWAY
       GATEWAYDEV=$default_GATEWAYDEV
       NO_ALIASROUTING=$default_NO_ALIASROUTING
}

       #
       # Read the alias configuation files and enable each aliased
       # device using new_interface()
       #

function new_interface ()
{

       ipa=$IPADDR; ipb=${ipa#*.}; ipc=${ipb#*.};
       IPGLOP="${ipa%%.*}_${ipb%%.*}_${ipc%%.*}_${ipc#*.}";
       DEVNUM=${DEVICE#*:}

       eval "
              ipseen=\$ipseen_${IPGLOP}; devseen=\$devseen_${DEVNUM};
              ipseen_${IPGLOP}=$FILE; devseen_${DEVNUM}=$FILE;
       ";

       if [ -n "$ipseen" ]; then
              echo $"error in $FILE: already seen ipaddr $IPADDR in $ipseen" >&2;exit 0
       fi

       if [ -n "$devseen" ]; then
              echo $"error in $FILE: already seen device $parent_device:$DEVNUM in $devseen" >&2; exit 0
       fi
       
       if [ -z "$DEVICE" -o -z "$IPADDR" ]; then
              echo $"error in $FILE: didn't specify device or ipaddr" >&2 ; exit 0
       fi

       if [ -z "$NETMASK" ]; then
               eval `/bin/ipcalc --netmask ${IPADDR}`
       fi

       if [ -z "$BROADCAST" ]; then
               eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
               nma=$NETMASK; nmb=${nma#*.}; nmc=${nmb#*.};
               NMGLOP="${nma%%.*}_${nmb%%.*}_${nmc%%.*}_${nmc#*.}";
               if [ ${nma%%.*} -eq 0 ]; then ipnm_a=0; else ipnm_a=${ipa%%.*}; fi
               if [ ${nmb%%.*} -eq 0 ]; then ipnm_b=0; else ipnm_b=${ipb%%.*}; fi
               if [ ${nmc%%.*} -eq 0 ]; then ipnm_c=0; else ipnm_c=${ipc%%.*}; fi
               if [ ${nmc#*.}  -eq 0 ]; then ipnm_d=0; else ipnm_d=${ipc#*.};  fi
               CACHENAME="${NMGLOP}_${ipnm_a}_${ipnm_b}_${ipnm_c}_${ipnm_d}"
               eval "
                       if [ -z \"\$bcastcache_${CACHENAME}\" ]; then
                               eval `/bin/ipcalc --broadcast \$IPADDR \$NETMASK`
                               bcastcache_${CACHENAME}=\$BROADCAST
                       else
                               BROADCAST=\$bcastcache_${CACHENAME}
                       fi
               ";
       fi

##echo "";
##echo "----------------------------"
##echo "device = $DEVICE"
##echo "ipaddr = $IPADDR"
##echo "netmask = $NETMASK"
##echo "broadcast = $BROADCAST"
##echo "devnum = $DEVNUM";
##echo "";

       if [ "$no_devices_are_up" = "yes" ]; then
               setup_this=yes
       else

               setup_this=""

               eval "
                       rdev_addr=\$rdev_${DEVNUM}_addr;
                       rdev_mb=\$rdev_${DEVNUM}_mb;
                       rdev_mark=\$rdev_${DEVNUM}_mark;
                       rdevip=\$rdevip_${IPGLOP};
               ";

               if [ -n "$rdev_addr" ]; then
                       if [ "$rdev_addr" = "${IPADDR}" ]; then
                               newmark=keep
                               if [ "$rdev_mb" != "${NETMASK}_${BROADCAST}" ]; then
                                       setup_this=freshen
                               else
                                       setup_this=no
                               fi
                       else
                               if [ "$rdev_mark" != "remove" ]; then
##echo "removing device $parent_device:${DEVNUM} (devnum conflict)"
                                       /sbin/ifconfig $parent_device:${DEVNUM} down
                                       do_netreport=yes
                               fi
                               newmark=remove
                               setup_this=yes
                       fi
                       if [ -n "$rdev_mark" -a "$rdev_mark" != "$newmark" ]; then
                               echo $"error in ifcfg-${parent_device}: files" >&2 ; exit 0
                       fi
                       eval " rdev_${DEVNUM}_mark=\$newmark ";
               else
                       setup_this=yes
               fi

               if [ -n "$rdevip" -a "$rdevip" != "${DEVNUM}" ]; then
                       eval " mark_remove=\$rdev_${rdevip}_mark ";
                       if [ -n "$mark_remove" -a "$mark_remove" != "remove" ]; then
                               echo $"error in ifcfg-${parent_device}: files" >&2 ; exit 0
                       fi
                       if [ "$mark_remove" != "remove" ]; then
                               eval " rdev_${rdevip}_mark=remove ";
##echo "removing device $parent_device:$rdevip (ipaddr conflict)"
                               /sbin/ifconfig $parent_device:$rdevip down
                               do_netreport=yes
                       fi
               fi

       fi

       if [ "$setup_this" = "freshen" ] ; then
               # we can do the freshen stuff right now
##echo "freshening device $DEVICE"
               /sbin/ifconfig $DEVICE netmask $NETMASK broadcast $BROADCAST;
       fi

       if [ "$setup_this" = "yes" ] ; then

##echo "setting up device $DEVICE"

               ifconfig ${DEVICE} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}

               if [ "$NO_ALIASROUTING" != yes ]; then

                       # this is broken! it's only here for compatibility with old RH systems
                       if [ "${GATEWAY}" != "" -a "${GATEWAY}" != "none" ]; then
                               route add default gw ${GATEWAY} metric 1 ${DEVICE}
                       fi

                       GATEWAY=$network_GATEWAY;
                       GATEWAYDEV=$network_GATEWAYDEV;

                       if [ "${GATEWAY}" != "" ]; then
                               if [ "${GATEWAYDEV}" = "" -o "${GATEWAYDEV}" = "${DEVICE}" ]; then
                                       # set up default gateway, if it isn't already there
				       CURGW=`route -n | grep ^0\.0\.0\.0 | awk '{ print $2 }'`
				       if [ "$CURGW" != "$GATEWAY" ]; then
                                       		route add default gw ${GATEWAY} ${DEVICE}
				       fi
                               fi
                       fi

                       if [ -f /etc/sysconfig/static-routes ]; then
                               #note the trailing space in the grep gets rid of aliases
                               grep "^$1 " /etc/sysconfig/static-routes | while read device args; do
                                       /sbin/route add -$args $device
                               done
                       fi

                       do_netreport=yes
                       ifuplocal_queue="$ifuplocal_queue $DEVICE"

               fi

	fi

}

if [ "$BASH_VERSINFO" ]; then shopt -s nullglob; else allow_null_glob_expansion=foo; fi

# Is there any linuxconf-style alias files.
linuxconfstyle=0
for FILE in ifcfg-${parent_device}:* ; do

       ini_env;
       . $FILE;
       if [ -z "$DEVICE" ]; then
          # it's a linuxconf file.
	  # I don't think this script will handle both style correctly
	  # though. Well, those who knows about the other style don't know
	  # the other style and vice versa
	  linuxconfstyle=1
       else
         new_interface;
       fi
done
if [ "$linuxconfstyle" = "1" ] ; then
	linuxconf --hint ipalias $device \
		| while read command ali ip mask bcast
	do
		if [ "$command" = "del" ] ; then
			/sbin/ifconfig $ali down
			do_netreport=yes
		elif [ "$command" = "add" ] ; then
			/sbin/ifconfig $ali $ip netmask $mask broadcast $bcast
			do_netreport=yes
		else
			echo Incomatible hinting: $command $rest >&2
		fi
	  done
fi


for FILE in ifcfg-${parent_device}-range* ; do

       ini_env;
       . $FILE;

       ipaddr_prefix=${IPADDR_START%.*}
       ipaddr_startnum=${IPADDR_START##*.}
       ipaddr_endnum=${IPADDR_END##*.}

       if [ "${IPADDR_START%.*}" != "${IPADDR_END%.*}" ]; then
               echo $"error in $FILE: IPADDR_START and IPADDR_END don't argree" >&2; exit 0
       fi

       if [ $ipaddr_startnum -gt $ipaddr_endnum ]; then
               echo $"error in $FILE: IPADDR_START greater than IPADDR_END" >&2; exit 0
       fi

       ipaddr_num=$ipaddr_startnum
       ipaddr_clonenum=$CLONENUM_START

       while [ $ipaddr_num -le $ipaddr_endnum ]; do
               IPADDR="$ipaddr_prefix.$ipaddr_num"
               DEVICE="$parent_device:$ipaddr_clonenum"
               new_interface;
               let 'ipaddr_num=ipaddr_num+1'
               let 'ipaddr_clonenum=ipaddr_clonenum+1'
       done
       
done

       #
       # Remove any devices that should not be around
       #
if [ "$linuxconfstyle" = "0" ] ; then
	for DEVNUM in $rdev_LIST ; do
	       eval " rdev_mark=\$rdev_${DEVNUM}_mark ";
	       if [ -z "$rdev_mark" ]; then
	##echo "removing device $parent_device:${DEVNUM} (lingering)"
	               /sbin/ifconfig $parent_device:${DEVNUM} down
        	       do_netreport=yes
	       fi
	done
fi
       #
       # Notify of new device creation
       #

if [ -n "$do_netreport" ]; then
       do_netreport
fi

if [ -x /sbin/ifup-local ]; then
       for DEVICE in $ifuplocal_queue ; do
               /sbin/ifup-local ${DEVICE}
       done
fi

'#n769'>769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
package detect_devices;

use diagnostics;
use strict;
use vars qw($pcitable_addons $usbtable_addons);

#-######################################################################################
#- misc imports
#-######################################################################################
use log;
use MDK::Common; # help perl_checker
use common;
use devices;
use run_program;
use modules;
use c;
use feature 'state';


=head1 SYNOPSYS

The B<detect_devices> modules offers a high level API for detecting devices.*
It mostly relies on the L<c> modules for gluing  libldetect back into the Perl world, and thus
being able to enumerate DMI/HID/PCI/USB devices.

Other devices are mostly detected through C</proc> & C</sys>.

Then the L<list_modules> enables to map modules into categories such as:

=over 4

=item * C<network/ethernet,>

=item * C<network/wireless,>

=item * C<network/wifi,>

=item * C<disk/sata,>

=item * C<disk/scsi,>

=item * ...

=back

This enables to detect a category by mapping drivers to categories.

=head1 Listing block devices

=cut

#-#####################################################################################
#- Globals
#-#####################################################################################
my %serialprobe;

#-######################################################################################
#- Functions
#-######################################################################################

=over

=item get()

Returns a list of all block devices (IDE, SCSI, SATA, virtual, RAID, ...)

=cut

sub get() {
    #- Detect the default BIOS boot harddrive is kind of tricky. We may have IDE,
    #- SCSI and RAID devices on the same machine. From what I see so far, the default
    #- BIOS boot harddrive will be
    #- 1. The first IDE device if IDE exists. Or
    #- 2. The first SCSI device if SCSI exists. Or
    #- 3. The first RAID device if RAID exists.

    getIDE(), getSCSI(), getMmcBlk(), getXenBlk(), getVirtIO(), getDAC960(), getCompaqSmartArray();
}
sub hds()         { grep { may_be_a_hd($_) } get() }
sub tapes()       { grep { $_->{media_type} eq 'tape' } get() }
sub cdroms()      { grep { $_->{media_type} eq 'cdrom' } get() }
sub burners()     { grep { isBurner($_) } cdroms() }
sub dvdroms()     { grep { isDvdDrive($_) } cdroms() }
sub raw_zips()    { grep { member($_->{media_type}, 'fd', 'hd') && isZipDrive($_) } get() }
sub ls120s()      { grep { member($_->{media_type}, 'fd', 'hd') && isLS120Drive($_) } get() }
sub zips()        {
    map { 
	$_->{device} .= 4; 
	$_;
    } raw_zips();
}

sub floppies {
    my ($o_not_detect_legacy_floppies) = @_;
    require modules;
    state @fds;
    state $legacy_already_detected;

    $o_not_detect_legacy_floppies = 1 if arch() =~ /mips|arm/;

    if (!$o_not_detect_legacy_floppies && !$legacy_already_detected) {
        $legacy_already_detected = 1;
        if ($::isInstall) {
            eval { modules::load("floppy") };
            system(qw(udevadm settle)) if !$@;
        }
        #- do not bother probing /dev/fd0 and loading floppy device uselessly,
        #- it takes time and it is already done by boot process (if not in install):
        #-   /dev/fd0 is created by start_udev (/etc/udev/devices.d/default.nodes)
        #-   then hal probes /dev/fd0 and triggers floppy module loading through kernel's kmod
        if (any { (split)[1] eq 'fd' } cat_("/proc/devices")) {
            @fds = map {
                my $info = c::floppy_info("/dev/fd$_");
                if_($info && $info ne '(null)', { device => "fd$_", media_type => 'fd', info => $info });
            } qw(0 1);
        }
    }
        
    my @ide = ls120s() and eval { modules::load("ide_floppy") };

    eval { modules::load("usb_storage") } if $::isInstall && usbStorage();
    my @scsi = grep { $_->{media_type} eq 'fd' } getSCSI();
    @ide, @scsi, @fds;
}
sub floppies_dev() { map { $_->{device} } floppies() }
sub floppy() { first(floppies_dev()) }
#- example ls120, model = "LS-120 SLIM 02 UHD Floppy"

sub removables() {
    floppies(), cdroms(), zips();
}

sub get_sys_cdrom_info {
    my (@drives) = @_;

    my @drives_order;
    foreach (cat_("/proc/sys/dev/cdrom/info")) {
	my ($t, $l) = split ':';
	my @l;
	@l = split(' ', $l) if $l;
	if ($t eq 'drive name') {
	    @drives_order = map {
		my $dev = $_;
		find { $_->{device} eq $dev } @drives;
	    } @l;
	} else {
	    my $capacity;
	    if ($t eq 'Can write CD-R') {
		$capacity = 'burner';
	    } elsif ($t eq 'Can read DVD') {
		$capacity = 'DVD';
	    }
	    if ($capacity) {
		each_index {
		    ($drives_order[$::i] || {})->{capacity} .= "$capacity " if $_;
		} @l;
	    }
	}
    }
}

sub complete_usb_storage_info {
    my (@l) = @_;

    my @usb = grep { exists $_->{usb_vendor} } @l;

    foreach my $usb (usb_probe()) {
	if (my $e = find { !$_->{found} && $_->{usb_vendor} == $usb->{vendor} && $_->{usb_id} == $usb->{id} } @usb) {
         my $host = get_sysfs_usbpath_for_block($e->{device});
         if ($host) {
             my $file = "/sys/block/$host/../serial";
             $e->{info} = chomp_(cat_($file)) if -e $file;
             $e->{usb_description} = join('|', 
                                          chomp_(cat_("/sys/block/$host/../manufacturer")),
                                          chomp_(cat_("/sys/block/$host/../product")));
         }
         local $e->{found} = 1;
	    $e->{"usb_$_"} ||= $usb->{$_} foreach keys %$usb;
	}
    }
}

sub isBurner { 
    my ($e) = @_;
    $e->{capacity} =~ /burner/ and return 1;
      
    #- do not work for SCSI
    my $f = tryOpen($e->{device}); #- SCSI burner are not detected this way.
    $f && c::isBurner(fileno($f));
}
sub isDvdDrive {
    my ($e) = @_;
    $e->{capacity} =~ /DVD/ || $e->{info} =~ /DVD/ and return 1;

    #- do not work for SCSI
    my $f = tryOpen($e->{device});
    $f && c::isDvdDrive(fileno($f));
}
sub isZipDrive { $_[0]{info} =~ /ZIP\s+\d+/ } #- accept ZIP 100, untested for bigger ZIP drive.
sub isLS120Drive { $_[0]{info} =~ /LS-?120|144MB/ }
sub isKeyUsb { begins_with($_[0]{usb_media_type} || '', 'Mass Storage') && $_[0]{media_type} eq 'hd' }
sub isFloppyUsb { $_[0]{usb_driver} && $_[0]{usb_driver} eq 'Removable:floppy' }
sub may_be_a_hd { 
    my ($e) = @_;
    $e->{media_type} eq 'hd' && !(
	isZipDrive($e) 
           || isLS120Drive($e)
           || begins_with($e->{usb_media_type} || '', 'Mass Storage|Floppy (UFI)')
    );
}

sub get_sysfs_field_from_link {
    my ($device, $field) = @_;
    my $l = readlink("$device/$field");
    $l =~ s!.*/!!;
    $l;
}

sub get_sysfs_usbpath_for_block {
    my ($device) = @_;
    my $host = readlink("/sys/block/$device");
    $host =~ s!/host.*!!;
    $host;
}

sub get_scsi_driver {
    my (@l) = @_;
    # find driver of host controller from sysfs:
    foreach (@l) {
	next if $_->{driver};
	my $host = get_sysfs_usbpath_for_block($_->{device});
	require list_modules;
	$_->{driver} = list_modules::filename2modname(get_sysfs_field_from_link("/sys/block/$host", 'driver'));
    }
}

=item getSCSI()

Returns a list of all SCSI device.

=cut

sub getSCSI() {
    my $dev_dir = '/sys/bus/scsi/devices';

    my @scsi_types = (
	"Direct-Access",
	"Sequential-Access",
	"Printer",
	"Processor",
	"WORM",
	"CD-ROM",
	"Scanner",
	"Optical Device",
	"Medium Changer",
	"Communications",
    );

    my @l;
    foreach (all($dev_dir)) {
	my ($host, $channel, $id, $lun) = split ':';
	defined $lun or next;

	my $dir = "$dev_dir/$_";

	# handle both old and new kernels:
	my $node =  find { -e $_ } "$dir/block", top(glob_("$dir/block*")), "$dir/tape", top(glob_("$dir/scsi_generic*"));
	my ($device) = readlink($node) =~ m!/(?:scsi_(?:generic|tape)|block)/(.*)!;
        if (!$device) {
            ($device) = top(glob_("$node/*")) =~ m!/(?:scsi_(?:generic|tape)|block)/(.*)!;
        }
	warn("cannot get info for device ($_)"), next if !$device;

	my $usb_dir = readlink($dir) =~ m!/usb! && "$dir/../../../..";
	my $get_usb = sub { chomp_(cat_("$usb_dir/$_[0]")) };

	my $get = sub {
	    my $s = cat_("$dir/$_[0]");
	    $s =~ s/\s+$//;
	    $s;
	};

	# Old HP scanners report themselves as "Processor"s
	# (see linux/include/scsi/scsi.h and sans-find-scanner.1)
	my $raw_type = $scsi_types[$get->('type')];

	my $media_type = ${{ st => 'tape', sr => 'cdrom', sd => 'hd', sg => 'generic' }}{substr($device, 0, 2)} ||
	  $raw_type =~ /Scanner|Processor/ && 'scanner';

	my ($vendor, $model) = ($get->('vendor'), $get->('model'));
	my ($v, $m) = _get_hd_vendor($model);
	if ($v && $m) {
            ($vendor, $model) = ($v, $m);
	}
	push @l, { info =>  $vendor . ' ' . $model, host => $host, channel => $channel, id => $id, lun => $lun, 
	  description => join('|', $vendor, $model),
	  bus => 'SCSI', media_type => $media_type, device => $device,
	    $usb_dir ? (
	  usb_vendor => hex($get_usb->('idVendor')), usb_id => hex($get_usb->('idProduct')),
	    ) : (),
        };
    } 

    @l = sort { $a->{host} <=> $b->{host} || $a->{channel} <=> $b->{channel} || $a->{id} <=> $b->{id} || $a->{lun} <=> $b->{lun} } @l;

    complete_usb_storage_info(@l);

    foreach (@l) {
	$_->{media_type} = 'fd' if $_->{media_type} eq 'hd' && isFloppyUsb($_);
    }

    get_sys_cdrom_info(@l);
    get_scsi_driver(@l);
    @l;
}


my %hd_vendors = (
    "ASUS" => "Asus",
    "ATA Maxtor" => "Maxtor",
    "CD-ROM CDU" => "Sony",
    "CD-ROM Drive/F5D" => "ASUSTeK",
    "Compaq" => "Compaq",
    "CONNER" => "Conner Peripherals",
    "IBM" => "IBM",
    "INTEL" => "Intel",
    "FUJITSU" => "Fujitsu",
    "HITACHI" => "Hitachi",
    "Lite-On" => "Lite-On Technology Corp.",
    "LITE-ON" => "Lite-On Technology Corp.",
    "LTN" => "Lite-On Technology Corp.",
    "IOMEGA" => "Iomega",
    "MAXTOR" => "Maxtor",
    "Maxtor" => "Maxtor",
    "Micropolis" => "Micropolis",
    "Pioneer" => "Pioneer",
    "PLEXTOR" => "Plextor",
    "QUANTUM" => "Quantum", 
    "SAMSUNG" => "Samsung",
    "Seagate " => "Seagate Technology",
    "ST3" => "Seagate Technology",
    "TEAC" => "Teac",
    "TOSHIBA" => "Toshiba",
    "WDC" => "Western Digital Corp.",
);

# return ($vendor, $model)
sub _get_hd_vendor {
    my ($info) = @_;
    foreach my $name (keys %hd_vendors) {
        next if !$name;
        return ($hd_vendors{$name}, $2) if $info =~ /^$name(-|\s)*(.*)/;
    }
    return ("Hitachi", $info) if $info =~ /^HD[ST][0-9]/;
}

=item getIDE()

Returns a list of all IDE device.

=cut

sub getIDE() {
    my @idi;

    #- what about a system with absolutely no IDE on it, like some sparc machine.
    -e "/proc/ide" or return ();

    #- Great. 2.2 kernel, things are much easier and less error prone.
    foreach my $d (sort @{[glob_('/proc/ide/hd*')]}) {
	my $t = chomp_(cat_("$d/media"));
	my $type = ${{ disk => 'hd', cdrom => 'cdrom', tape => 'tape', floppy => 'fd' }}{$t} or next;
	my $info = chomp_(cat_("$d/model")) || "(none)";

	my $num = ord(($d =~ /(.)$/)[0]) - ord 'a';
	my ($vendor, $model) = _get_hd_vendor($info);

	my $host = $num;
	($host, my $id) = divide($host, 2);
	($host, my $channel) = divide($host, 2);