File: drbl-add-vir-netdev

package info (click to toggle)
drbl 5.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,996 kB
  • sloc: sh: 43,555; perl: 8,820; xml: 867; makefile: 135
file content (73 lines) | stat: -rwxr-xr-x 2,010 bytes parent folder | download | duplicates (4)
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
#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to add a virtual network device.

# Load DRBL setting and functions
# Setting
# Source function library.
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions

#
USAGE() {
    echo "$ocs - To add a virtual network interface"
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs NET_INTERFACE VIR_NET_INTERFACE"
    echo "NET_INTERFACE is the existing physical network device which the virtual network device will be bound to"
    echo "VIR_NET_INTERFACE is the virtual network device name" 
    echo "Ex:"
    echo "To add a virtual network device drbl0 on existing network card \"eth1\", run:"
    echo "   $ocs eth1 drbl0"
    echo
} # end of USAGE

####################
### Main program ###
####################

ocs_file="$0"
ocs=`basename $ocs_file`
#
while [ $# -gt 0 ]; do
 case "$1" in
   -*)     echo "${0}: ${1}: invalid option" >&2
           USAGE >& 2
           exit 2 ;;
   *)      break ;;
 esac
done

phy_ethdev="$1"
vir_netdev="$2"

#
if [ -z "$phy_ethdev" -o -z "$vir_netdev" ]; then
  USAGE
  exit 1
fi
random_MAC="$(LC_ALL=C echo -n 02; od -t x1 -An -N 5 /dev/urandom | tr ' ' ':')"
if [ -n "$random_MAC" ]; then
  ip link add link $phy_ethdev name $vir_netdev address $random_MAC type macvlan mode bridge
  ip link set $phy_ethdev promisc on
  ip link set $vir_netdev up
fi

if [ -e "/sys/class/net/$vir_netdev" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo "This virtual network device was created successfully: $vir_netdev"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  ip addr show $vir_netdev
  exit 0
else
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Failed to create virtual network device: $vir_netdev!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop!"
  my_ocs_exit 1
fi