|
#!/usr/bin/python
import os,sys
# A tool for creating a virtual machine on virtualbox
# By youngtrips @ 2010-05-21
vm_name = raw_input('Enter virtual machine name:');
vm_hd_filename = raw_input('Enter hd filename for VM:');
vm_hd_size = raw_input('Enter hd filename size(MB):');
vm_memory_size = raw_input('Enter vitrual machine memory size(MB):');
vm_nic_number = int(raw_input('Enter vitrual machine nic number:'));
vm_nic_type = [];
type_hash = {"nat":1, "bridged":1, "intnet":1, "hostonly":1, "vde":1};
for i in range(vm_nic_number):
msg = 'Enter type for nic%d(nat|bridged|intnet|hostonly|vde):' % (i + 1);
vm_nic_type.append(raw_input(msg));
while type_hash.get(vm_nic_type) == None:
msg = 'Enter type for nic%d(nat|bridged|intnet|hostonly|vde):' % (i + 1);
vm_nic_type = raw_input(msg);
have_iso = raw_input('Do you want to use iso file(yes/no):');
if cmp(have_iso, 'yes') == 0:
iso_file = raw_input('Enter iso filename:');
#create a vm
msg = 'VBoxManage createvm --name \"%s\" --register' % (vm_name);
os.system(msg);
#create hd
msg = 'VBoxManage createhd --filename \"%s.vdi\" --size %s --remember' % (vm_hd_filename, vm_hd_size);
os.system(msg);
msg = 'VBoxManage modifyvm \"%s\" --memory %s' % (vm_name, vm_memory_size);
os.system(msg);
msg = 'VBoxManage modifyvm \"%s\" --acpi on --accelerate3d on' % (vm_name);
os.system(msg);
#create nic
for i in range(vm_nic_number):
msg = 'VBoxManage modifyvm \"%s\" --nic%d %s' % (vm_name, i + 1, vm_nic_type);
os.system(msg);
msg = 'VBoxManage modifyvm \"%s\" --sata on --sataport1 \"%s.vdi\"' % (vm_name, vm_hd_filename);
os.system(msg);
#create ide controller
msg = 'VBoxManage storagectl \"%s\" --name "IDE Controller" --add ide' % (vm_name);
os.system(msg);
#attach iso file to ide controller
if cmp(have_iso, 'yes') == 0:
msg = 'VBoxManage storageattach \"%s\" --storagectl \"IDE Controller\" --port 1 --device 0 --type dvddrive --medium %s' % (vm_name, iso_file);
os.system(msg);
print 'create vm %s sucess.' % vm_name;
#print information of vm
msg = 'VBoxManage showvminfo %s' % (vm_name);
os.system(msg);
#start vm
start = raw_input('Do you want to start VM now(yes/no):');
if cmp(start, 'yes'):
msg = 'VBoxHeadless --startvm \"%s\" --vrdp on' % (vm_name);
os.system(msg);
print 'vm %s is running.' % vm_name;
print 'you can use rdesktop on linux or mstsc on windows to manage vm.';
刚学习python, 有错误之处,请指正。 |
|