在进行Kickstart实验时,可能需要多次安装虚拟机。新建的没有系统的虚拟机启动时会逐个测试BIOS里面的启动项,当不能从硬盘或者光驱引导时会自动尝试网络引导,所以无需设置BIOS即可自动从网络启动,安装完成之后由于硬盘是第一启动项,可以直接进入系统。但是重装就比较麻烦了,因为硬盘里有系统,安装前需要进BIOS设置网络启动,安装完成后需要再次设置BIOS从硬盘启动。否则将不断重装,也就是说,Kickstart无人值守这时候需要值守了。
这时候可以考虑2种方案,一是直接重新建立虚拟机。二是仅删除硬盘,然后重新添加硬盘。相对来说二比较容易。但是仍然需要点很多下鼠标。好在VMware提供命令行工具,可以编写脚本自动化完成这些步骤。
命令行操作VMware虚拟硬盘
VMware Workstation安装目录下,找到vmware-vdiskmanager.exe命令。此命令功能很多,包括新建硬盘,修改硬盘大小,磁盘整理等等。下面是vmware-vdiskmanager.exe的帮助信息:
VMware Virtual Disk Manager - build 1744117.
Usage: vmware-vdiskmanager.exe OPTIONS <disk-name> | <mount-point>
Offline disk manipulation utility
Operations, only one may be specified at a time:
-c : create disk. Additional creation options must
be specified. Only local virtual disks can be
created.
-d : defragment the specified virtual disk. Only
local virtual disks may be defragmented.
-k : shrink the specified virtual disk. Only local
virtual disks may be shrunk.
-n <source-disk> : rename the specified virtual disk; need to
specify destination disk-name. Only local virtual
disks may be renamed.
-p : prepare the mounted virtual disk specified by
the mount point for shrinking.
-r <source-disk> : convert the specified disk; need to specify
destination disk-type. For local destination disks
the disk type must be specified.
-x <new-capacity> : expand the disk to the specified capacity. Only
local virtual disks may be expanded.
-R : check a sparse virtual disk for consistency and attempt
to repair any errors.
-e : check for disk chain consistency.
-D : make disk deletable. This should only be used on disks
that have been copied from another product.
Other Options:
-q : do not log messages
Additional options for create and convert:
-a <adapter> : (for use with -c only) adapter type
(ide, buslogic, lsilogic). Pass lsilogic for other adapter types.
-s <size> : capacity of the virtual disk
-t <disk-type> : disk type id
Disk types:
0 : single growable virtual disk
1 : growable virtual disk split in 2GB files
2 : preallocated virtual disk
3 : preallocated virtual disk split in 2GB files
4 : preallocated ESX-type virtual disk
5 : compressed disk optimized for streaming
6 : thin provisioned virtual disk - ESX 3.x and above
The capacity can be specified in sectors, KB, MB or GB.
The acceptable ranges:
ide/scsi adapter : [1MB, 8192.0GB]
buslogic adapter : [1MB, 2040.0GB]
ex 1: vmware-vdiskmanager.exe -c -s 850MB -a ide -t 0 myIdeDisk.vmdk
ex 2: vmware-vdiskmanager.exe -d myDisk.vmdk
ex 3: vmware-vdiskmanager.exe -r sourceDisk.vmdk -t 0 destinationDisk.vmdk
ex 4: vmware-vdiskmanager.exe -x 36GB myDisk.vmdk
ex 5: vmware-vdiskmanager.exe -n sourceName.vmdk destinationName.vmdk
ex 6: vmware-vdiskmanager.exe -r sourceDisk.vmdk -t 4 -h esx-name.mycompany.com \
-u username -f passwordfile "[storage1]/path/to/targetDisk.vmdk"
ex 7: vmware-vdiskmanager.exe -k myDisk.vmdk
ex 8: vmware-vdiskmanager.exe -p <mount-point>
(A virtual disk first needs to be mounted at <mount-point>)
这里我们只关注新建硬盘的方法。
新建硬盘使用 -c选项,还必须用-a, -s 和 -t 选项并指定选项参数,然后需要指定所要创建的虚拟磁盘文件的文件名。[1]
-a [ ide | buslogic | lsilogic ] 指定磁盘适配器的类型。你在创建新的虚拟磁盘时必须指定其类型。选择以下类型之一: ide —— IDE接口适配器 buslogic —— BusLogic SCSI接口适配器 lsilogic —— LSI Logic SCSI接口适配器 -s <n> [GB|MB] 指定虚拟磁盘的大小。确定大小用GB或MB做单位。你必须在创建磁盘时指定其大小。 尽管你必须指定虚拟磁盘的大小,但当你增长它的大小时,你不能用-s这个选项。 可以指定的磁盘大小规定:IDE和SCSI适配器都为最小100MB,最大950GB。 -t [0|1|2|3] 你在创建一个新的虚拟磁盘或者重新配置一个虚拟磁盘时必须指定虚拟磁盘的类型。指定以下类型之一: 0 —— 创建一个包含在单一虚拟文件中的可增长虚拟磁盘 1 —— 创建一个被分割为每个文件2GB大小的可增长虚拟磁盘 2 —— 创建一个包含在单一虚拟文件中的预分配虚拟磁盘 3 —— 创建一个被分割为每个文件2GB大小的预分配虚拟磁盘
一个例子
vmware-vdiskmanager -c -t 0 -s 40GB -a ide myDisk.vmdk
这个命令将创建一个40GB大小IDE接口的名字为myDisk的虚拟硬盘。虚拟磁盘包含在一个单一文件中。这个虚拟磁盘没有被预分配磁盘空间。
自动化脚本
脚本代码如下:
#!/bin/bash
# Usage: 重置虚拟机磁盘
# History:
#
#空格以 "-" 代替
DIR=(
/d/Virtual-Machines/Master/
/d/Virtual-Machines/Slave1/
/d/Virtual-Machines/Slave2/
)
read -p "Dangerous! Are you sure? (y/n) " choice
if [ "$choice"x = "y"x ]; then
for id in ${DIR[*]}
do
DISKDIR=`echo $id | tr -s "-" "\ "`
cd "$DISKDIR"
rm -f *.vmdk
DISKFILE=`cat *.vmx |grep vmdk | awk -F '["]' '{print $2}'`
/d/Program\ Files/VMware/Vmware\ Workstation/vmware-vdiskmanager.exe -c -t 1 -s 20GB -a lsilogic $DISKFILE &>/dev/null && echo -e "\033[36m[SUCC]: $DISKFILE OK! \033[0m"
done
echo -e "\033[36m[SUCC]: All OK!, exit... \033[0m"
else
echo -e "\033[36m[SUCC]: exit... \033[0m" && exit 0
fi
windows下使用MinGW运行。
注意事项
shell中处理带空格的文件名,需要加引号。[2]
参考资料
[1]. CSDN博客. http://blog.csdn.net/xiaoshubiao/article/details/18079779
[2]. 知行近思. http://www.annhe.net/article-2153.html
发表回复