Chinaunix首页 | 论坛 | 博客
  • 博客访问: 282453
  • 博文数量: 176
  • 博客积分: 2516
  • 博客等级: 少校
  • 技术积分: 1350
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-01 11:18
文章分类
文章存档

2011年(1)

2010年(18)

2009年(157)

我的朋友

分类: LINUX

2009-07-08 15:12:20

学习脚本的第一个程序。
经验教训:简单问题不能复杂化。实现简易和复杂功能的区别对待,不要让自己的程序四不像。不断出现的错误可以帮助自己犯更少的错误。经验的积累来于此。

#!/bin/bash
#
#This is create by cXX
#
installvm(){
#########################################################
       虚拟机的创建                #
#########################################################

sleep 4
echo "--------create virtual machine now!-----------"
vmname=$USER
path=/home/$vmname/virual
vmpath=/home/$vmname/.VirtualBox
vms=
vms=$(VBoxManage list vms | grep 'Name:'|head -1| awk '{print $2}')
echo "$vms"

for name4 in $(ls -al /home/$vmname|grep '{*}')
do
    rm -rf $name4
done

while [ "$vms" != "" ]##[ -n "$vms" ]
do
    if [ -e "$vmpath/VDI" ];then
            for vdiname in ${vmpath}/VDI/* ##The vdiname is eq  ${vmpath}/VDI/*  when there is no file in
## the directory
            do
                    echo "delete vdi now!"
                       rm -rf $vdiname
                    echo $vdiname
            done
    fi

    echo "it is in unregistervm!"
    echo "$vms"
    if VBoxManage unregistervm $vms -delete;then
        vms=$(VBoxManage list vms | grep 'Name:'|head -1| awk '{print $2}')
    fi
    if [ -e "${path}.xml" ];then
            rm -rf ${path}.xml
            vms=""
    fi
   
   
done

if [ -e "${path}.xml" ];then
    rm -rf ${path}.xml
fi
echo "del xml sucess"
echo "the path is $path"
if VBoxManage createvm -name $vmname -register -settingsfile $path
    then echo "the virtual Machine have created!"
else  echo "it can not create virtual Machine!" ; exit 11
fi
echo "-----------create virtual machine end----------"
echo "#modify memory"
echo "10"
#########################################################
       指定内存                #
#########################################################
testmemory(){
    echo "it is in $FUNCNAME"
}

echo "-----------modify the Memory now!-----------"
memsize=512
if VBoxManage modifyvm $vmname -memory $memsize
    then echo "modify vm memory sucess!"
else echo "modify vm memory failed!" ; exit 22
fi
echo "----------modify the memory end-----------"

testdisk(){
   
    echo "it is in $FUNCNAME"
}
i=20
startdevice(){
    echo "------start the device $1 now!-----------"
    if VBoxManage modifyvm $vmname $1 $2
        then echo "start device sucess!"
    else echo "start device failed!" exit 88
    fi
   
    echo "#add device $1 now...."
   sleep 2
    echo "$i"
    let "i+=3"
    echo "--------start the device $1 end!----------"
}
#########################################################
       usb设备的必要添加            #
#########################################################
if [ -e "/dev/bus/usb/.usbfs" ]
    then echo "the file exist!"
else
    sudo mkdir -p /dev/bus/usb/.usbfs
    line=$(cat /etc/group | grep "vboxusers")
    num=${line:-5}
    num=${num:1:4}
    sudo mount -t usbfs "" /dev/bus/usb/.usbfs -o busmode=0775,devmode=0664,listmode=0644,devgid=$num
    sudo ln -s .usbfs/devices /dev/bus/usb/devices
    sudo mount --rbind /dev/bus/usb /proc/bus/usb
   
fi
   

#########################################################
       usb network dvd disk            #
       设备的关联                #
#########################################################
checkusb(){
    if VBoxManage list usbhost
        then declare -i usbj=0
        exit o
    else declare -i usbj=1
         exit 1
    fi
}
usbfiltermodify(){
    case $1 in
        VendorId)
             VBoxManage usbfilter modify $n -target $vmname -$1 $2;;
   
        ProductId)
             VBoxManage usbfilter modify $n -target $vmname -$1 $2;;
       
        Revision)
             VBoxManage usbfilter modify $n -target $vmname -$1 $2;;
       
        Manufacturer)
             VBoxManage usbfilter modify $n -target $vmname -$1 $2;;
       
        Product)
             VBoxManage usbfilter modify $n -target $vmname -$1 $2;;
       
        Address)
             VBoxManage usbfilter modify $n -target $vmname -$1 $2;;
    esac
   
}
usbfiltermodify1(){
    echo "-----------------enter the usbfiltermodify1--------------"
    VBoxManage list usbhost | awk 'BEGIN{"enter the awk function to deal this"}\
                    {
                        if($1~/^Address:/){
                               
                                system("VBoxmanage usbfilter controlvm VMNAME usbattach $2")
                        }
                    }
                    END{"END OF AWK"}' N=0 VMNAME=$vmname
    echo "-----------------exit the usbfiltermodify1-----------------"
}
checkusbfilter(){
    if [ "$?" -eq true ]
        then usbfilteradd
            if [ "$?" -eq true ]
                then echo "addusbfilter true!"
            else echo "addusbfilter failed!"
            fi       
    fi
}
echo "----------sharefolder module start----------"

echo "====test the folder permession!===="
pathname="/home/$vmname/Virtual"
echo "the pathname is $pathname"
if [ -e "$pathname" ]
    then rm -rf $pathname
    sudo mkdir $pathname
    if [ -w "$pathname" ]
        then echo "no write promession!chanage it"
        chmod 666 $pathname
    else echo "have write permession!"
    fi
    echo "rm the directory sucess,the dir is $pathname"
else mkdir $pathname ; chmod 666 $pathname
fi
foldername="Virtual"
if [ -e "$pathname" ]
    then echo "the directory is exist!"
else echo "the directory is not exist!"
fi

echo "the pathname is $pathname"
if VBoxManage sharedfolder add $vmname -name $foldername -hostpath $pathname
    then echo "add sharefolder sucess!the folder is $pathname"
else echo "add sharefolder failed!"
fi
echo "-----------sharefolder module end-------------"


startdevice -acpi on
startdevice -pae on
startdevice -hwvirtex on
startdevice -sata on

#network info start
startdevice -nic1 nat
nictype="Am79C970"
startdevice -nictype $nictype
startdevice -nictrace on
startdevice -cableconnected on
startdevice -macaddress auto
#network info end

startdevice -vrdp on
startdevice -usb on
startdevice -usbehci on
startdevice -audio oss
startdevice -audiocontroller ac97

echo "#安装虚拟硬盘大约需要2分钟"
#VBoxManage modifyvm $vmname -uart1 8 0x3F8
#startdevice -uartmode1 disconnected


if [ -e "$vmpath/VDI" ];then
    for vdiname in ${vmpath}/VDI/*
    do
        echo "delete vdi now!"
        rm -rf $vdiname
        echo $vdiname
    done
fi

echo "-----------create disk for virtual machine----------"
disksize=4096
diskname="firstvdi"
VBoxManage unregisterimage disk $diskname
if VBoxManage createvdi -filename $diskname -size $disksize -static -register -type normal
    then echo "create vdi sucess!"
else echo "create vdi failed!" ;exit 33
fi
echo "------create disk end-----------"

#if VBOxManage controlvm $vmname setvideomodehint fullscreen
   then echo "set mode sucess!"
#else echo "set mode failed!" ; exit 55
#fi

echo "----------use the virtual disk now!---------"
if VBoxManage modifyvm $vmname -hda $diskname
    then echo "set virtual disk sucess!"
else echo "set virtual disk failed!" ; exit 44
fi
echo "-----------set virtual disk end!--------"
echo "90"

echo "#add dvd device now......"
line=$(VBoxManage list hostdvds)
echo "the line is : $line"
a=$(expr index "$line" '/')
b=${#line}
echo "befor the a is $a, the b is $b"
let "b=b-a"
let "b+=1"
let "a-=1"
echo "the a is $a.the b is $b"
hostdvd=${line:$a:$b}
echo "the hostdvd is $hostdvd"
echo "--------modify vm dvd now!--------"
if VBoxManage modifyvm $vmname -dvd host:$hostdvd
    then echo "specify the vm dvd sucess!"
else echo "specify the vm dvd failed!"
fi
echo "---------modify vm dvd end --------"
#########################################################
       指定显示模式为无缝模式            #
#########################################################

#if VBoxManage setextradata $vmname seamless on
   then echo "specify seamless sucessed!"
#else echo "specify seamless failed!"
#fi
sleep 2
echo "95"
#########################################################
       启动虚拟机                     
#########################################################
}
installvm |zenity --progress --text="安装windows操作系统" --auto-close
vmname=$USER
VBoxSDL -vm $vmname -fullscreen

#########################################################
       添加具体的usb设备            #
#########################################################
阅读(868) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~