1 | #!/bin/bash
|
---|
2 | # Sun xVM VirtualBox
|
---|
3 | # VirtualBox VNIC setup script for Solaris hosts with Crossbow.
|
---|
4 | #
|
---|
5 | # Copyright (C) 2007 Sun Microsystems, Inc.
|
---|
6 | #
|
---|
7 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
8 | # available from http://www.virtualbox.org. This file is free software;
|
---|
9 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
10 | # General Public License (GPL) as published by the Free Software
|
---|
11 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
12 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
13 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
14 | #
|
---|
15 | # Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
16 | # Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
17 | # additional information or have any questions.
|
---|
18 | #
|
---|
19 |
|
---|
20 | if [ -z "$1" ]; then
|
---|
21 | echo "Missing MAC address."
|
---|
22 | echo
|
---|
23 | echo "Usage: $0 macaddress [vnicname]"
|
---|
24 | echo " A new VNIC is created if no vnicname is provided."
|
---|
25 | exit 1
|
---|
26 | fi
|
---|
27 |
|
---|
28 | mac=$1
|
---|
29 | # Create the VNIC if required
|
---|
30 | if [ -z "$2" ]; then
|
---|
31 | # snv <= 82 is to handled differently (dladm format changes at 83+)
|
---|
32 | snv_str=`uname -v`
|
---|
33 | snv_num=${snv_str##*[a-z][_]}
|
---|
34 |
|
---|
35 | # Try obtain one that's currently active (82 dladm show-link doesn't indicate status; just use show-dev atm)
|
---|
36 | if [ $snv_num -le 82 ]; then
|
---|
37 | phys_nic=`/usr/sbin/dladm show-dev -p | /usr/bin/awk 'NF==4 && $2=="link=up" { print $1 }'`
|
---|
38 | elif [ $snv_num -le 95 ]; then
|
---|
39 | phys_field=`/usr/sbin/dladm show-link -p | /usr/bin/awk 'NF==5 && $4=="STATE=\"up\"" { print $1 }'`
|
---|
40 | eval $phys_field
|
---|
41 | phys_nic="$LINK"
|
---|
42 | else
|
---|
43 | phys_field=`/usr/sbin/dladm show-link -p -o link,state | /usr/bin/awk 'BEGIN{FS=":"} /up/ {print $1}'`
|
---|
44 | eval $phys_field
|
---|
45 | phys_nic="$LINK"
|
---|
46 | fi
|
---|
47 |
|
---|
48 | if [ -z "$phys_nic" ]; then
|
---|
49 | # Failed to get a currently active NIC, get the first available link.
|
---|
50 | if [ $snv_num -le 82 ]; then
|
---|
51 | phys_nic=`/usr/sbin/dladm show-link -p | /usr/bin/nawk '/legacy/ {next} {print $1; exit}'`
|
---|
52 | elif [ $snv_num -le 95 ]; then
|
---|
53 | phys_field=`/usr/sbin/dladm show-link -p | /usr/bin/awk 'NF==5 && $2=="CLASS=\"phys\"" { print $1 }'`
|
---|
54 | eval $phys_field
|
---|
55 | phys_nic="$LINK"
|
---|
56 | else
|
---|
57 | phys_field=`/usr/sbin/dladm show-link -p -o link,class | /usr/bin/awk 'BEGIN{FS=":"} /up/ {print $1}'`
|
---|
58 | eval $phys_field
|
---|
59 | phys_nic="$LINK"
|
---|
60 | fi
|
---|
61 | if [ -z "$phys_nic" ]; then
|
---|
62 | # Failed to get any NICs!
|
---|
63 | echo "Failed to get a physical NIC to bind to."
|
---|
64 | exit 1
|
---|
65 | fi
|
---|
66 | fi
|
---|
67 |
|
---|
68 | # To use a specific physical NIC, replace $phys_nic with the name of the NIC.
|
---|
69 | vnic_name=`/usr/lib/vna $phys_nic $mac`
|
---|
70 | if [ $? != 0 ]; then
|
---|
71 | echo "vna failed to bind VNIC."
|
---|
72 | exit 1
|
---|
73 | fi
|
---|
74 |
|
---|
75 | # The vna utility can return the id/name depending on the distro
|
---|
76 | case "$vnic_name" in
|
---|
77 | vnic[0-9]*)
|
---|
78 | vnic_name="$vnic_name"
|
---|
79 | ;;
|
---|
80 | *)
|
---|
81 | vnic_name="vnic$vnic_name"
|
---|
82 | ;;
|
---|
83 | esac
|
---|
84 | else
|
---|
85 | vnic_name=$2
|
---|
86 | fi
|
---|
87 | vnic_id=${vnic_name##*[a-z]}
|
---|
88 |
|
---|
89 | if [ ${vnic_id} -lt 10 ]; then
|
---|
90 | host_ip="192.168.1.10${vnic_id}"
|
---|
91 | guest_ip="192.168.1.20${vnic_id}"
|
---|
92 | elif [ ${vnic_id} -lt 256 ]; then
|
---|
93 | host_ip="192.168.1.${vnic_id}"
|
---|
94 | guest_ip="192.168.2.${vnic_id}"
|
---|
95 | elif [ ${vnic_id} -gt 899 ]; then
|
---|
96 | let "temp_id = $vnic_id % 900"
|
---|
97 | host_ip="192.168.1.10${temp_id}"
|
---|
98 | guest_ip="192.168.1.20${temp_id}"
|
---|
99 | else
|
---|
100 | # VNIC ID is probably off the scale!
|
---|
101 | host_ip="192.168.1.10"
|
---|
102 | guest_ip="192.168.1.20"
|
---|
103 | fi
|
---|
104 |
|
---|
105 | netmask="255.255.255.0"
|
---|
106 |
|
---|
107 | if [ -z "$2" ]; then
|
---|
108 | /sbin/ifconfig $vnic_name plumb
|
---|
109 | /sbin/ifconfig $vnic_name $host_ip destination $guest_ip netmask $netmask up
|
---|
110 | #else
|
---|
111 | # Do existing VNIC configuration here if needed...
|
---|
112 | fi
|
---|
113 |
|
---|
114 | echo "$vnic_name"
|
---|
115 | exit 0
|
---|
116 |
|
---|