
VMX 경로를 전달하여 해당 VM의 상태를 확인해 볼 수 있습니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
if VixVM.power_state == VixVM.VIX_POWERSTATE_POWERED_OFF: print("power off")
elif VixVM.power_state == VixVM.VIX_POWERSTATE_POWERED_ON: print("power on")
except vix.VixError as e:
print("Error : {0}".format(e))
power_state는 전원 ON/OFF 및 일시정지 여부 등 다양한 상태 정보를 확인할 수 있지만, 전원 ON/OFF만 판단하고 싶다면 다음과 같이 확인할 수 있습니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
if VixVM.is_running: print("power on")
else: print("power off")
except vix.VixError as e:
print("Error : {0}".format(e))
다음의 표와 같이 다양한 상태로 변경시킬 수 있는 함수들이 존재합니다.
| 상태 | 함수 |
|---|---|
| 전원 종료 | vm.power_off() |
| 전원 켜기 | vm.power_on() |
| 일시 정지 | vm.suspend() |
| 재부팅 | vm.reset() |
| 멈춤 | vm.pause() |
| 재생 | vm.unpause() |
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
if VixVM.is_running:
VixVM.power_off()
print("Command : power off")
else:
VixVM.power_on()
print("Command : power on")
except vix.VixError as e:
print("Error : {0}".format(e))
VM의 스냅샷의 생성, 제거, 복구 등 시킬 수 있습니다.
스냅샷을 생성해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.create_snapshot(name="Vix Snapshot Test", description="hello world", include_memory=True)
except vix.VixError as e:
print("Error : {0}".format(e))
create_snapshot의 주석을 확인하면 모든 VMware 제품을 지원하지 않는다고 나와있습니다.
따라서 함수의 리턴 값이 반환되지 않거나 에러가 날 수 있습니다.
"""Create a VM snapshot.
:param str name: Name of snapshot.
:param str description: Snapshot description.
:param bool include_memory: True to include RAM, otherwise False.
:returns: Instance of the created snapshot
:rtype: .VixSnapshot
:raises vix.VixError: On failure to create snapshot.
.. note:: This method is not supported by all VMware products.
"""
스냅샷의 이름을 통해서 스냅샷 정보를 획득할 수 있습니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixSnapshot = VixVM.snapshot_get_named("Vix Snapshot Test")
print(VixSnapshot.name)
print(VixSnapshot.description)
print(VixSnapshot.power_state)
print(VixSnapshot.get_num_children())
except vix.VixError as e:
print("Error : {0}".format(e))
스냅샷 복구를 수행해 봅니다.
복구도 모든 VMware 제품을 지원하지 않는다고 나와있습니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixSnapshot = VixVM.snapshot_get_named("Vix Snapshot Test")
VixVM.snapshot_revert(snapshot=VixSnapshot)
except vix.VixError as e:
print("Error : {0}".format(e))
이번에는 스냅샷을 제거해 봅니다.
children에 옵션을 주면 하위 스냅샷도 모두 제거가 됩니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixSnapshot = VixVM.snapshot_get_named("Vix Snapshot Test")
VixVM.snapshot_remove(snapshot=VixSnapshot, remove_children=True)
except vix.VixError as e:
print("Error : {0}".format(e))
dest_vmx에 복제될 경로를 주면 폴더가 존재하지 않더라도 자동으로 생성해 줍니다.
또한 특정 스냅샷 상태를 복제하고 싶다면 스냅샷 객체를 넘겨주면 됩니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
Cloned = VixVM.clone(dest_vmx=Target.replace(MachineName, MachineName+"-clone"), snapshot=None, linked=False)
Cloned.power_on()
except vix.VixError as e:
print("Error : {0}".format(e))
Guest VM에 로그인 후 폴더 구조를 확인해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
for directory in VixVM.dir_list("."):
print("- " + directory.name)
print("\tSize : {}".format(directory.size))
print("\tmDate : {}".format(directory.last_mod))
print("\tDirectory : {}".format(directory.is_dir))
print("\tSymbolic : {}".format(directory.is_sym))
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
폴더의 유무를 확인한 뒤 Host의 파일을 Guest로 복사해 봅니다.
Guest로 복사되는 파일은 766 권한이 부여됩니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
if not VixVM.dir_exists(path="/home/user/VixTest"): VixVM.create_directory(path="/home/user/VixTest")
VixVM.copy_host_to_guest(host_path="hello.txt", guest_path="/home/user/VixTest/world.txt")
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
파일의 이름을 변경하고 정보를 확인해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
if not VixVM.file_exists(path="/home/user/VixTest/world.txt"): print("world.txt not exist")
else:
VixVM.file_rename(old_name="/home/user/VixTest/world.txt", new_name="/home/user/VixTest/VixTest.txt")
FileInfo = VixVM.get_file_info(path="/home/user/VixTest/VixTest.txt")
print("- " + FileInfo.name)
print("\tSize : {}".format(FileInfo.size))
print("\tmDate : {}".format(FileInfo.last_mod))
print("\tDirectory : {}".format(FileInfo.is_dir))
print("\tSymbolic : {}".format(FileInfo.is_sym))
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
이번에는 Guest의 파일을 호스트로 복사해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
VixVM.copy_guest_to_host(guest_path="/home/user/VixTest/VixTest.txt", host_path=".\\test.txt")
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
파일과 폴더를 삭제해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
VixVM.file_delete(path="/home/user/VixTest/VixTest.txt")
VixVM.dir_delete(path="/home/user/VixTest")
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
Guest VM에 로그인 후 프로세스 목록을 확인해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
for process in VixVM.proc_list():
print("- " + process.name)
print("\tPID : {}".format(process.pid))
print("\tOwner : {}".format(process.owner))
print("\tCMD : {}".format(process.cmd))
print("\tDebug : {}".format(process.is_debug))
print("\tStartDate : {}".format(process.start_time))
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
프로세스를 실행시켜보기 위해 스크립트 하나를 작성해 봅니다.
#!/bin/bash
i=0
while :
do
i=$(expr $i + 1)
echo "Hello World $i"
sleep 1
done
다음과 같이 작성한 스크립트를 실행시킬 수 있습니다.
should_block이 True 이면 프로세스의 반환 값을 얻을 때까지 기다리며
False 이면 기다리지 않습니다.
테스트를 위한 스크립트는 무한 반복문이기 때문에 False 값을 줍니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
VixVM.proc_run(program_name="/home/user/TestScript.sh", command_line=None, should_block=False)
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
무한 반복문이기 때문에 Guest에서 스크립트가 실행되고 있을 것입니다.
해당 프로세스의 정보를 이용하여 PID를 알아낸 후 프로세스를 죽여 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
for process in VixVM.proc_list():
if process.cmd == "/bin/bash /home/user/TestScript.sh":
VixVM.proc_kill(process.pid)
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
Guest 화면을 캡처 후 이미지로 저장할 수 있습니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
VixVM.capture_screen_image(filename="ScreenShot.png")
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))
filename 인자가 None이라면 이미지 데이터가 반환되며 이를 이용해 별도로 저장해 봅니다.
import os
import vix
BaseDirectory = "D:\\2.VM"
MachineName = "ubuntu-develop"
Target = os.path.join(os.path.join(BaseDirectory, MachineName),MachineName) + '.vmx'
try:
Host = vix.VixHost()
VixVM = Host.open_vm(Target)
VixVM.login(username="user", password="user")
with open("ScreenShot.png", "wb") as fp:
fp.write(VixVM.capture_screen_image())
VixVM.logout()
except vix.VixError as e:
print("Error : {0}".format(e))