Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3342929
  • 博文数量: 815
  • 博客积分: 12898
  • 博客等级: 上将
  • 技术积分: 7883
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-25 09:57
文章分类

全部博文(815)

文章存档

2014年(1)

2011年(46)

2010年(192)

2009年(121)

2008年(70)

2007年(385)

分类: BSD

2008-04-10 16:43:58

VMware Server通过/usr/lib/vmware/net-services.sh完成网络服务的设置.

bridge=vmnet-bridge
dhcpd=vmnet-dhcpd
netifup=vmnet-netifup
natd=vmnet-natd
ping=vmware-ping
vnet=vmnet

# Create a virtual host ethernet interface and connect it to a virtual
# ethernet hub
vmware_start_netifup() {
   local vHostIf="$1" # IN
   local vHubNr="$2"  # IN

   cd "$vmdb_answer_BINDIR" && "$vmdb_answer_BINDIR"/"$netifup" \
      -d /var/run/"$netifup"-"$vHostIf".pid /dev/vmnet"$vHubNr" "$vHostIf"
}

# Disconnect a virtual host ethernet interface from a virtual ethernet hub
# and destroy the virtual host ethernet interface
vmware_stop_netifup() {
   local vHostIf="$1" # IN

   if vmware_stop_pidfile /var/run/"$netifup"-"$vHostIf".pid; then
      rm -f /var/run/"$netifup"-"$vHostIf".pid
   fi
}

# Connect a physical host ethernet interface to a virtual ethernet hub
vmware_start_bridge() {
   local vHubNr="$1"  # IN
   local pHostIf="$2" # IN

   cd "$vmdb_answer_BINDIR" && "$vmdb_answer_BINDIR"/"$bridge" \
      -d /var/run/"$bridge"-"$vHubNr".pid /dev/vmnet"$vHubNr" "$pHostIf"
}

# Disconnect a physical host ethernet interface from a virtual ethernet hub
vmware_stop_bridge() {
   local vHubNr="$1"  # IN

   if vmware_stop_pidfile /var/run/"$bridge"-"$vHubNr".pid; then
      rm -f /var/run/"$bridge"-"$vHubNr".pid
   fi
}

# Start a DHCP server on a private IP network
vmware_start_dhcpd() {
   local vHostIf="$1" # IN

   # The daemon already logs its output in the system log, so we can safely
   # trash it
   cd "$vmdb_answer_BINDIR" && "$vmdb_answer_BINDIR"/"$dhcpd" \
      -cf "$vmware_etc_dir"/"$vHostIf"/dhcpd/dhcpd.conf \
      -lf "$vmware_etc_dir"/"$vHostIf"/dhcpd/dhcpd.leases \
      -pf /var/run/"$dhcpd"-"$vHostIf".pid "$vHostIf" >/dev/null 2>&1
}

# Stop a DHCP server on a private IP network
vmware_stop_dhcpd() {
   local vHostIf="$1" # IN

   if vmware_stop_pidfile /var/run/"$dhcpd"-"$vHostIf".pid; then
      rm -f /var/run/"$dhcpd"-"$vHostIf".pid
   fi
}

# Start the host-only network user service
vmware_start_hostonly() {
   local vHubNr="$1"    # IN
   local vHostIf="$2"   # IN
   local ifIp="$3"      # IN
   local ifMask="$4"    # IN
   local run_dhcpd="$5" # IN
   local ifNet

   #
   # Do a cursory check to see if the host-only network
   # configuration is still ok.  We do this so that mobile
   # hosts don't get setup at install time and then moved to
   # a new locale where the host-only network config is no
   # longer valid.
   #
   # NB: This really needs to be done at power-on time when
   #     VM is configured to use host-only networking so that
   #     we aren't fooled by dynamic changes in the network.
   #
   # XXX ping takes 10 seconds to timeout if nobody answers
   #     that slows boot too much so we do this bit in the
   #     background.
   #
   if lookForHostOnlyNetwork "$ifIp"; then
    echo 'Host-only networking disabled because '"$ifIp"
    echo 'appears to be a real, physical, existing address.'
    echo 'Please run "'"$vmdb_answer_BINDIR"'/vmware-config.pl" to'
    echo 'modify your host-only network configuration.'
    exit 1
   fi

   vmware_start_netifup "$vHostIf" "$vHubNr" || exit 1

   # Configure the virtual host ethernet interface and define the private IP
   # network
   #
   # . We provide the broadcast address explicitly because versions of ifconfig
   #   prior to 1.39 (1999-03-18) seem to miscompute it
   # . 2.0.x kernels don't install a route when the interface is marked up, but
   #   2.2.x kernel do. Since we want to see any errors from route we don't
   #   just discard messages from route, but instead check if the route got
   #   installed before manually adding one.
   ifNet=`ipv4_subnet "$ifIp" "$ifMask"`
   if ifconfig "$vHostIf" inet "$ifIp" netmask "$ifMask" \
         broadcast "`ipv4_broadcast "$ifIp" "$ifMask"`" up \
         && noRoutePresent "$ifNet" "$vHostIf"; then
      route add -net "$ifNet" netmask "$ifMask" "$vHostIf"
   fi

   if [ "$run_dhcpd" = 'yes' ]; then
      vmware_start_dhcpd "$vHostIf" || exit 1
   fi

   exit 0
}

# Stop the host-only network user service
vmware_stop_hostonly() {
   local vHostIf="$1"   # IN
   local ifIp="$2"      # IN
   local ifMask="$3"    # IN
   local ifNet

   # Terminate the private network
   ifNet=`ipv4_subnet "$ifIp" "$ifMask"`
   noRoutePresent "$ifNet" "$vHostIf" || \
      route del -net "$ifNet" netmask "$ifMask" || exit 1
   # To test if the interface exists, we can not just look at the exitcode
   # because old versions of ifconfig don't exit with 1 when invoked with a
   # non-existing interface
   if [ "`ifconfig "$vHostIf" 2>/dev/null`" != '' ]; then
      ifconfig "$vHostIf" down || exit 1
   fi

   vmware_stop_netifup "$vHostIf" || exit 1

   exit 0
}

# Start the NAT network user service
vmware_start_nat() {
   local vHubNr="$1"    # IN

   cd "$vmdb_answer_BINDIR" && "$vmdb_answer_BINDIR"/"$natd" \
      -d /var/run/"$natd"-"$vHubNr".pid \
      -m /var/run/"$natd"-"$vHubNr".mac \
      -c "$vmware_etc_dir"/vmnet"$vHubNr"/nat/nat.conf
}

# Stop the NAT network user service
vmware_stop_nat() {
   local vHubNr="$1"   # IN

   if vmware_stop_pidfile /var/run/"$natd"-"$vHubNr".pid; then
      rm -f /var/run/"$natd"-"$vHubNr".pid
      rm -f /var/run/"$natd"-"$vHubNr".mac
   fi
}

# See how we were called.
case "$1" in
   start)
      if [ -e "$vmware_etc_dir"/not_configured ]; then
         echo "`vmware_product_name`"' is installed, but it has not been (correctly) configured'
         echo 'for the running kernel. To (re-)configure it, invoke the'
         echo 'following command: '"$vmdb_answer_BINDIR"'/vmware-config.pl.'
         echo

         exit 1
      fi

      if [ "$vmdb_answer_NETWORKING" = 'yes' ]; then
         if [ "`isLoaded "$vnet"`" = 'no' ]; then
            echo 'Module '"$vnet"' is not loaded.  Please verify that it is loaded before'
            echo 'running this script.'
            echo

            exit 1
         fi

         exitcode='0'

         vHubNr=0
         while [ $vHubNr -lt 256 ]; do
            eval 'interface="$vmdb_answer_VNET_'"$vHubNr"'_INTERFACE"'
            eval 'hostaddr="$vmdb_answer_VNET_'"$vHubNr"'_HOSTONLY_HOSTADDR"'
            eval 'netmask="$vmdb_answer_VNET_'"$vHubNr"'_HOSTONLY_NETMASK"'
            if [ -n "$interface" ]; then
               vmware_create_vmnet "$vHubNr"
               vmware_exec 'Bridged networking on /dev/vmnet'"$vHubNr" \
                  vmware_start_bridge "$vHubNr" "$interface"
               exitcode=$(($exitcode + $?))
            elif [ -n "$hostaddr" -a -n "$netmask" ]; then
               vmware_create_vmnet "$vHubNr"
               vmware_bg_exec 'Host-only networking on /dev/vmnet'"$vHubNr" \
                  vmware_start_hostonly "$vHubNr" 'vmnet'"$vHubNr" \
                  "$hostaddr" "$netmask" 'yes'
               exitcode=$(($exitcode + $?))

               eval 'nat="$vmdb_answer_VNET_'"$vHubNr"'_NAT"'
               if [ "$nat" = 'yes' ]; then
                  vmware_exec 'NAT service on /dev/vmnet'"$vHubNr" \
                     vmware_start_nat "$vHubNr"
                  exitcode=$(($exitcode + $?))
               fi
            fi
            vHubNr=$(($vHubNr + 1))
         done
      fi

      if [ "$exitcode" -gt 0 ]; then
         exit 1
      fi
   ;;

   stop)
      if [ "$vmdb_answer_NETWORKING" = "yes" ]; then
         exitcode='0'
         vHubNr=0
         while [ $vHubNr -lt 256 ]; do
            eval 'interface="$vmdb_answer_VNET_'"$vHubNr"'_INTERFACE"'
            eval 'hostaddr="$vmdb_answer_VNET_'"$vHubNr"'_HOSTONLY_HOSTADDR"'
            eval 'netmask="$vmdb_answer_VNET_'"$vHubNr"'_HOSTONLY_NETMASK"'
            if [ -n "$interface" ]; then
               vmware_exec "Bridged networking on /dev/vmnet$vHubNr" \
                                           vmware_stop_bridge "$vHubNr"
               exitcode=$(($exitcode + $?))
            elif [ -n "$hostaddr" -a -n "$netmask" ]; then
               vmware_exec "DHCP server on /dev/vmnet$vHubNr" vmware_stop_dhcpd \
                  "vmnet$vHubNr"
               exitcode=$(($exitcode + $?))

               eval 'nat="$vmdb_answer_VNET_'"$vHubNr"'_NAT"'
               if [ "$nat" = "yes" ]; then
                  vmware_exec 'NAT service on /dev/vmnet'"$vHubNr" \
                     vmware_stop_nat "$vHubNr"
                  exitcode=$(($exitcode + $?))
               fi

               vmware_exec 'Host-only networking on /dev/vmnet'"$vHubNr" \
                  vmware_stop_hostonly 'vmnet'"$vHubNr" "$hostaddr" "$netmask"
            fi
            vHubNr=$(($vHubNr + 1))
         done
      fi

      if [ "$exitcode" -gt 0 ]; then
         exit 1
      fi
   ;;

   status)
      exitcode='0'
      if [ "$vmdb_answer_NETWORKING" = "yes" ]; then
         vHubNr=0
         while [ $vHubNr -lt 256 ]; do
            eval 'interface="$vmdb_answer_VNET_'"$vHubNr"'_INTERFACE"'
            eval 'hostaddr="$vmdb_answer_VNET_'"$vHubNr"'_HOSTONLY_HOSTADDR"'
            eval 'netmask="$vmdb_answer_VNET_'"$vHubNr"'_HOSTONLY_NETMASK"'
            if [ -n "$interface" ]; then
                    echo -n 'Bridged networking on /dev/vmnet'"$vHubNr"
                    if vmware_check_pidfile '/var/run/'"$bridge"'-'"$vHubNr"'.pid'; then
                       echo ' is running'
                    else
                       echo ' is not running'
                            exitcode=$(($exitcode + 1))
                    fi
            elif [ -n "$hostaddr" -a -n "$netmask" ]; then
                    echo -n 'Host-only networking on /dev/vmnet'"$vHubNr"
                    if vmware_check_pidfile '/var/run/'"$netifup"'-vmnet'"$vHubNr".'pid'; then
                       echo ' is running'
                    else
                       echo ' is not running'
                            exitcode=$(($exitcode + 1))
                    fi

               eval 'nat="$vmdb_answer_VNET_'"$vHubNr"'_NAT"'
               if [ "$nat" = 'yes' ]; then
                       echo -n 'NAT networking on /dev/vmnet'"$vHubNr"
                       if vmware_check_pidfile '/var/run/'"$natd"'-'"$vHubNr"'.pid'; then
                          echo ' is running'
                            else
                          echo ' is not running'
                               exitcode=$(($exitcode + 1))
                       fi
               fi
            fi
            vHubNr=$(($vHubNr + 1))
         done
      fi
 
      if [ "$exitcode" -gt 0 ]; then
         exit 1
      fi
   ;;
   restart)
      "$0" stop && "$0" start
   ;;

   *)
      echo "Usage: `basename "$0"` {start|stop|status|restart}"
      exit 1
esac

exit 0

阅读(3515) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~