LoginSignup
0
2

More than 5 years have passed since last update.

pyvmomiでVMのクローンをしてみる

Last updated at Posted at 2017-07-16

pyvmomiを使ってVMwareのクローンを実行してみました。ここでは、簡単なコピーをするだけのスクリプトしかありません。
Windowsのsysprepを実行するような処理(カスタマイズ仕様)については、今度書こうと思います。

こういうスクリプトを作っておけば、ストレージの負荷検証ツール(クローン処理を同時に実行)などにも使えたりします :-)

実行環境

項目 バージョン
vCenter 6.5.0 ビルド 4602587
pyvmomi 6.5.0.2017.5.post1

検証方針

  • centos という仮想マシンを元に TEST という仮想マシンを作成する。
  • クローン先は esxi-03.local というホストを指定します。

ソース

#!/usr/bin/python3
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
import ssl
import atexit

# 接続先情報
host = 'ESXi or vCenter IP'
username = 'username'
password = 'password'

# クローン情報
clone_target_vm = 'クローン元VM名'
target_host = '展開先ESXiホスト名'
new_vm_name = '新しいVM名'

def get_mob_info(content, mob, target=''):
    """
    Management Objectを取得する。
    targetが指定されていない場合はContainerView(https://goo.gl/WXMfJK)で返す。

    :type content: vim.ServiceInstanceContent
    :param content: ServiceContent(https://goo.gl/oMtVFh)

    :type mob: Management Object
    :param mob: 取得する対象のManagement Objectを指定

    :type target: str
    :param target: 返すmobの名前を指定

    :rtype: Management Object
    :return: 指定したManagement Object又はContainerViewを返す
    """
    r = content.viewManager.CreateContainerView(content.rootFolder,
                                                [mob],
                                                True)

    # 返すmobを名前で指定する場合
    if(target):
        for i in r.view:
            if(i.name == target):
                r = i

    return r

def main():
    # SSL証明書対策
    context = None
    if hasattr(ssl, '_create_unverified_context'):
        context = ssl._create_unverified_context()

    # 接続
    si = SmartConnect(host = host,
                      user = username,
                      pwd = password,
                      sslContext = context)

    # 処理完了時にvCenterから切断
    atexit.register(Disconnect, si)

    # ServiceContent(Data Object)を取得
    content = si.content

    # RelocateSpecを作成 https://goo.gl/sJdBRH
    relocate_spec = vim.vm.RelocateSpec()
    relocate_spec.host = get_mob_info(content, vim.HostSystem, target_host)

    # CloneSpec作成 https://goo.gl/7ueuqR
    clone_spec = vim.vm.CloneSpec()
    clone_spec.location = relocate_spec
    clone_spec.powerOn = False
    clone_spec.template = False

    # Clone実行
    target_vm = get_mob_info(content, vim.VirtualMachine, clone_target_vm)
    target_vm.CloneVM_Task(folder = target_vm.parent, name = new_vm_name, spec = clone_spec)

if __name__ == '__main__':
    main()

各specの詳細については、ソースのURLで確認してください。

実行

(1) 変更必要な箇所を修正します。以下は、クローン情報の例です。

(snip)
# クローン情報
clone_target_vm = 'centos'
target_host = 'esxi-03.local'
new_vm_name = 'TEST'
(snip)

(2) 実行します。

[root@localhost pyvmomi]# ./vm_clone.py

(3) 仮想マシンが作成されたことを確認します。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2