본문 바로가기

가상화/VMware Esxi

[VMware ESXi] ssh를 통한 VM 생성, 삭제 하기(2)

반응형

default 값 수정 및 변수 추가

  • esxi_vm_functions.py
    • VIRTDEV, NETDRIVER : 파리미터로 받아 변수 지정으로 위하여 default 값을 지정
    • VIRTDEV : vSCSI Controller
    • NETDRIVER : Network Adapter(Driver)
    • 그 밖에 자주 사용하는 VM spec(cpu,memory,disk) 변경, DISKFORMAT 변경
def setup_config():

    #
    #   System wide defaults
    #
    ConfigData = dict(

        #   Your logfile
        LOG= os.path.expanduser("~") + "/esxi-vm.log",

        #  Enable/Disable dryrun by default
        isDryRun=False,

        #  Enable/Disable Verbose output by default
        isVerbose=False,

        #  Enable/Disable exit summary by default
        isSummary=False,

        #  ESXi host/IP, root login & password
        HOST="",
        USER="root",
        PASSWORD="",

        #  Default number of vCPU's, GB Mem, & GB boot disk
        CPU=8,
        MEM=8,
        HDISK=100,

        #  Default Disk format thin, zeroedthick, eagerzeroedthick
        DISKFORMAT="zeroedthick",

        #  Virtual Disk device type. pvscsi, lsisas1068
        VIRTDEV="pvscsi",

        # Default Network Adapter Driver. vmxnet3, e1000e
        NETDRIVER="e1000e",

        #  Specify default Disk store to "LeastUsed"
        STORE="LeastUsed",

        #  Default Network Interface (vswitch)
        NET="None",

        #  Default ISO
        ISO="None",

        #  Default GuestOS type.  (See VMware documentation for all available options)
        GUESTOS="windows8srv-64",

        # Extra VMX options
        VMXOPTS=""
    )

 

 

parser 아규먼트 추가

  • esxi-vm-create
    • DISKFORMAT, VIRTDEV, NETDRIVER 변수를 입력 값으로 받음.
parser.add_argument("-f", "--diskprovisioning", dest='DISKFORMAT', type=str, help="virturl disk provisioning type. thin, zeroedthick, eagerzerothick | default  ("  + str(DISKFORMAT) + ")")
parser.add_argument("-s", "--scsicontroller", dest='VIRTDEV', type=str, help="vSCSI Controller. pvscsi, lsisas1068 | default ("  + str(VIRTDEV) + ")")
parser.add_argument("-a", "--networkadapter", dest='NETDRIVER', type=str, help="network adapter. e1000e, vmxnet3 | default  ("  + str(NETDRIVER) + ")")
...
if args.DISKFORMAT:
    DISKFORMAT= args.DISKFORMAT
if args.VIRTDEV:
    VIRTDEV=args.VIRTDEV
if args.NETDRIVER:
    NETDRIVER=args.NETDRIVER

 

 

vmx profile 값 수정

  • esxi-vm-create
    • nvram 추가 : bios 설정 값을 기억
    • cdrom을 ide -> sata 으로 변경
    • VIRTDEV, NETDRIVER 변수 값으로 수정
    • usb 추가
VMX = []
VMX.append('config.version = "8"')
VMX.append('virtualHW.version = "14"')
VMX.append('vmci0.present = "TRUE"')
VMX.append('displayName = "' + NAME + '"')
VMX.append('nvram = "' + NAME + '.nvram"')
VMX.append('floppy0.present = "FALSE"')
VMX.append('numvcpus = "' + str(CPU) + '"')
VMX.append('scsi0.present = "TRUE"')
VMX.append('scsi0.sharedBus = "none"')
VMX.append('scsi0.virtualDev = "' + VIRTDEV + '"')
VMX.append('memsize = "' + str(MEM * 1024) + '"')
VMX.append('scsi0:0.present = "TRUE"')
VMX.append('scsi0:0.fileName = "' + NAME + '.vmdk"')
VMX.append('scsi0:0.deviceType = "scsi-hardDisk"')
VMX.append('sata0.present = "TRUE"')
if ISO == "":
    VMX.append('sata0:0.present = "TRUE"')
    VMX.append('sata0:0.fileName = "emptyBackingString"')
    VMX.append('sata0:0.deviceType = "atapi-cdrom"')
    VMX.append('sata0:0.startConnected = "FALSE"')
    VMX.append('sata0:0.clientDevice = "TRUE"')
else:
    VMX.append('sata0:0.present = "TRUE"')
    VMX.append('sata0:0.fileName = "' + ISO + '"')
    VMX.append('sata0:0.deviceType = "cdrom-image"')
VMX.append('pciBridge0.present = "TRUE"')
VMX.append('pciBridge4.present = "TRUE"')
VMX.append('pciBridge4.virtualDev = "pcieRootPort"')
VMX.append('pciBridge4.functions = "8"')
VMX.append('pciBridge5.present = "TRUE"')
VMX.append('pciBridge5.virtualDev = "pcieRootPort"')
VMX.append('pciBridge5.functions = "8"')
VMX.append('pciBridge6.present = "TRUE"')
VMX.append('pciBridge6.virtualDev = "pcieRootPort"')
VMX.append('pciBridge6.functions = "8"')
VMX.append('pciBridge7.present = "TRUE"')
VMX.append('pciBridge7.virtualDev = "pcieRootPort"')
VMX.append('pciBridge7.functions = "8"')
VMX.append('guestOS = "' + GUESTOS + '"')
if NET != "None":
    VMX.append('ethernet0.virtualDev = "' + NETDRIVER + '"')
    VMX.append('ethernet0.present = "TRUE"')
    VMX.append('ethernet0.networkName = "' + NET + '"')
    if MAC == "":
        VMX.append('ethernet0.addressType = "generated"')
    else:
        VMX.append('ethernet0.addressType = "static"')
        VMX.append('ethernet0.address = "' + MAC + '"')
VMX.append('usb.present = "TRUE"')
VMX.append('usb:0.present = "TRUE"')
VMX.append('usb:0.deviceType = "hid"')
VMX.append('usb:0.port = "0"')
VMX.append('usb:0.parent = "-1"')
VMX.append('usb:1.speed = "2"')
VMX.append('usb:1.present = "TRUE"')
VMX.append('usb:1.deviceType = "hub"')
VMX.append('usb:1.port = "1"')
VMX.append('usb:1.parent = "-1"')
VMX.append('svga.present = "TRUE"')

 

 

vmdk 생성 부분 변경

  • esxi-vm-create
    • paramiko -> subprocess 로 변경하고 sshpass를 사용
import subprocess

if not isDryRun and not CheckHasErrors:
    try:

...

        # Create vmdk
        if isVerbose:
            print "Create " + NAME + ".vmdk file"
        #(stdin, stdout, stderr) = ssh.exec_command("vmkfstools -c " + str(HDISK) + "G -d " + DISKFORMAT + " " + MyVM + ".vmdk")
        #type(stdin)
        config = "vmkfstools -c {a}G -d {b} {c}.vmdk".format(a=HDISK,b=DISKFORMAT,c=MyVM)
        if PASSWORD:
            cmd = 'sshpass -p\'{pw}\' ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no {ids}@{ip} \"{a}\"'.format(pw=PASSWORD,ids=USER,ip=HOST,a=config)
        else:
            cmd = 'sshpass ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no {ids}@{ip} \"{a}\"'.format(pw=PASSWORD,ids=USER,ip=HOST,a=config)
        print(cmd)
        result = subprocess.check_output(cmd, shell=True, universal_newlines=True)
...

 

 

vmx 파일 생성 부분 변경

  • esxi-vm-create
    • .vmx 파일의 VMX list가 paramiko를 통한 ssh로 put 할 시 간헐적인 입력값 누락으로 오류 발생
    •  echo를 통해 VMs list 값을 하나씩 밀어넣는 방식에서 local에서 vmx파일을 완성해서 scp로 복사하는 방법으로 변경
import os

...

if not isDryRun and not CheckHasErrors:
    try:

        # Create NAME.vmx
        if isVerbose:
            print "Create " + NAME + ".vmx file"
        (stdin, stdout, stderr) = ssh.exec_command("mkdir " + FullPath )
        type(stdin)
        fp = open('/tmp/'+NAME+'.vmx', 'a')
        for line in VMX:
            fp.write(line)
            fp.write('\n')
        fp.close()
        from scp import SCPClient
        scp = SCPClient(ssh.get_transport())
        scp.put('/tmp/'+NAME+'.vmx',MyVM+'.vmx')

        # Create vmdk
...

        # Register VM
...

        # Power on VM
...

        # Get Generated MAC
...

        # Delete local vmx file
        if os.path.exists('/tmp/'+NAME+'.vmx'):
            os.remove('/tmp/'+NAME+'.vmx')
    except:
...

 

 

참조 한 vmx profile

  • centos7_x64.vmx
    • DISKFORMAT = pvscsi, NETDRIVER=vmxnet3
config.version = "8"
virtualHW.version = "14"
vmci0.present = "TRUE"
displayName = "test"
floppy0.present = "FALSE"
numvcpus = "8"
scsi0.present = "TRUE"
scsi0.sharedBus = "none"
scsi0.virtualDev = "pvscsi"
memsize = "8192"
scsi0:0.present = "TRUE"
scsi0:0.fileName = "test.vmdk"
scsi0:0.deviceType = "scsi-hardDisk"
sata0:0.deviceType = "cdrom-image"
sata0:0.fileName = "/vmfs/volumes/...."
sata0:0.present = "TRUE"
sata0.present = "TRUE"
usb.present = "TRUE"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
guestOS = "centos7-64"
ethernet0.virtualDev = "vmxnet3"
ethernet0.present = "TRUE"
ethernet0.networkName = "ME2ON_Private"
ethernet0.addressType = "generated"
usb:0.present = "TRUE"
usb:0.deviceType = "hid"
usb:0.port = "0"
usb:0.parent = "-1"
usb:1.speed = "2"
usb:1.present = "TRUE"
usb:1.deviceType = "hub"
usb:1.port = "1"
usb:1.parent = "-1"
nvram = "test.nvram"
svga.present = "TRUE"

 

  • windows_2k12_x64.vmx
    • DISKFORMAT = lsisas1068, NETDRIVER=e1000e
config.version = "8"
virtualHW.version = "14"
vmci0.present = "TRUE"
displayName = "test"
floppy0.present = "FALSE"
numvcpus = "8"
scsi0.present = "TRUE"
scsi0.sharedBus = "none"
scsi0.virtualDev = "lsisas1068"
memsize = "8192"
scsi0:0.present = "TRUE"
scsi0:0.fileName = "test.vmdk"
scsi0:0.deviceType = "scsi-hardDisk"
sata0:0.deviceType = "cdrom-image"
sata0:0.fileName = "/vmfs/volumes/5c44f05b-6666f3d2-6c02-40b076da5e19/UTIL/OS/20220524_auto_w2012x64_ko_clean.ISO"
sata0:0.present = "TRUE"
sata0.present = "TRUE"
usb.present = "TRUE"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
guestOS = "windows8srv-64"
ethernet0.virtualDev = "e1000e"
ethernet0.present = "TRUE"
ethernet0.networkName = "ME2ON_Private"
ethernet0.addressType = "generated"
usb:0.present = "TRUE"
usb:0.deviceType = "hid"
usb:0.port = "0"
usb:0.parent = "-1"
usb:1.speed = "2"
usb:1.present = "TRUE"
usb:1.deviceType = "hub"
usb:1.port = "1"
usb:1.parent = "-1"
nvram = "test.nvram"
svga.present = "TRUE"

 

 

최종 수정 파일

esxi-vm-create.tgz
0.01MB

반응형