LoginSignup
1
0

More than 5 years have passed since last update.

Azure CLI 1.0を使ったLinux仮想マシンのイメージ化

Last updated at Posted at 2017-02-07

前提条件

・Azure上でCentOSの仮想マシンが作成されていることを前提としています。
・CentOSは、今回CentOS 7.3 を利用して動作確認しました。
・この仮想マシンは、ポータルサイトから作成したものになります。
・AzureCLI1.0を利用できる環境が準備されていることを前提としています。

プロビジョニングの解除

注意)これを実行するとこの仮想マシンは起動できなくなるようです。Windowsのsysprep実行と似た感じです。
1.仮想マシンにSSHでログインして、次のコマンドを実行します。

# sudo su - (rootになりましょう)
# waagent -deprovision+user
 WARNING! The waagent service will be stopped.
 WARNING! All SSH host key pairs will be deleted.
 WARNING! Cached DHCP leases will be deleted.
 WARNING! root password will be disabled. You will not be able to login as root.
 WARNING! /etc/resolv.conf will be deleted.
 WARNING! swradmin account and entire home directory will be deleted.
Do you want to proceed (y/n)y  ★ y を入力します。

※ 裏ではこのようなことが実行されています。
・すべての SSH ホスト キーの削除 (構成ファイルで Provisioning.RegenerateSshHostKeyPair が 'y' の場合)
・/etc/resolv.conf 内のネームサーバー構成の消去
・/etc/shadow の root ユーザーのパスワードの削除 (構成ファイルで Provisioning.DeleteRootPassword が "y" の場合)
・キャッシュされた DHCP クライアントのリースの削除
・ホスト名を localhost.localdomain にリセット
・(/var/lib/waagent から取得した) 前回プロビジョニングされたユーザー アカウント および関連データの削除

2.エラーなく終了したら、SSH接続を終了します。

Azure CLIを利用したイメージ(キャプチャ)取得

3.Azure CLIでサブスクリプションにログインします。
# azure login -u <MSアカウント>

4.該当のサブスクリプションがセットされていない場合は、アカウントセットを変更してください。

<設定> # azure account set <サブスクリプション名>
<確認> # azure account list

★ 該当のサブスクリプションでCurrent列 が true になっていることを確認してください。

5.リソースマネージャーモードになっていることを確認します。

<確認> 
# azure config list
info:    Executing command config list
info:    Getting config settings
data:    Setting  Value
data:    -------  -----
data:    mode     arm                 ★ Default はarmのはずです。
info:    config list command OK

<設定>
# azure config mode arm      ★ 必要であれば、設定してください。

6.仮想マシンをシャットダウンします。

# azure vm deallocate -g <リソースグループ名> -n <仮想マシン名>

Sample)
# azure vm deallocate -g staging -n stg-ap01
info:    Executing command vm deallocate
+ Looking up the VM "stg-ap01"
+ Deallocating the virtual machine "stg-ap01"
info:    vm deallocate command OK

7.VMを汎用化します。

# azure vm generalize -g <リソースグループ名> -n <仮想マシン名>

Sample)
# azure vm generalize -g staging -n stg-ap01
info:    Executing command vm generalize
+ Looking up the VM "stg-ap01"
+ Generalizing the virtual machine "stg-ap01"
info:    vm generalize command OK

★ シャットダウンがちゃんと完了してからでないと、こんなエラー出ちゃいます。ポータルからアクティビティログを見て確認するのがいいです。

info:    Executing command vm generalize
+ Looking up the VM "stg-ap01"
+ Generalizing the virtual machine "stg-ap01"
error:   Operation 'generalize' is not allowed on VM 'stg-ap01' since the VM is running. Please power off explicitly in
case you shut down the VM from inside the guest operating system.
error:   Error information has been recorded to C:\Users\神都\.azure\azure.err
error:   vm generalize command failed

8.ハイ!チーズ!!

# azure vm capture -g <リソースグループ名> -n <仮想マシン名> -p <任意の名前>
-p オプションの後に付ける名前は、イメージファイルの頭につきます。

Sample)
# azure vm capture -g staging -n stg-ap01 -p test
info:    Executing command vm capture
Virtual hard disk's name prefix:  test
+ Looking up the VM "stg-ap01"
+ Capturing the virtual machine "stg-ap01"
   ・
   ・
(省略)
   ・
   ・
info:    vm capture command OK

9.キャプチャ確認
元にした仮想マシン作成時に指定したストレージアカウントをポータルから選択してください。
「概要」- 「BLOBストレージ」を見ると、キャプチャを取得した時間にsystemというイメージが取得されていることを確認できます。
クリックして、下の階層に進むと、VHDファイルが確認できます。

image

キャプチャイメージを利用した仮想マシンの作成は別投稿します。
※ 近々UPします。

参考URL

1
0
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
1
0