KVM Virtual Machine Hostname 変更方法

## 方法
1. virsh edit 使わない場合
2. virsh edit 使う場合

## 初期状態

```
# virsh list
 Id    Name                           State
----------------------------------------------------
 12    vm01                           running
```

実際のファイル

* XML : /etc/libvirt/qemu/vm01.xml
* IMAGE : /var/lib/libvirt/images/vm01.qcow2

## Shutdown Domain

```
# virsh shutdown vm01
Domain vm01 is being shutdown
```

shutdown 出来なかったら、destroy で強制停止

```
# virsh destroy vm01
Domain vm01 destroyed
```

```
# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     vm01                           shut off
```

## 1. virsh edit 使わない場合

#### 1.1. XML 取得

    # virsh dumpxml vm01 > vm01.xml

#### 1.2. XML 修正

    # cp vm01.xml vm01.xml.org
    # vi vm01.xml
    # diff vm01.xml.org vm01.xml
    2c2
    <   <name>vm01</name>
    ---
    >   <name>vm02</name>

#### 1.3. 停止状態の古い Domain を削除

    # virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     vm01                           shut off

    # virsh undefine vm01
    Domain vm01 has been undefined

    # virsh list --all
     Id    Name                           State
    ----------------------------------------------------

#### 1.4. XML ファイルからリネームした Domain を登録

    # virsh define ./vm01.xml
    Domain vm02 defined from ./vm01.xml

    # virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     -     vm02                           shut off

#### 1.5. Domain 起動

    # virsh start vm02
    Domain vm02 started

    # virsh list
     Id    Name                           State
    ----------------------------------------------------
     14    vm02                           running


## 2. virsh edit 使う場合

#### 2.0. `virsh edit` で **name** だけ変更しても、 uuid が同じなので変更出来ない

```
# virsh edit vm01
error: operation failed: domain 'vm01' is already defined with uuid 23bcf945-288d-460a-9953-4797022d45c4
Failed. Try again? [y,n,f,?]:
```

#### 2.1. 現在の UUID 確認、新しい UUID 取得

現在の UUID 確認

```
# virsh dominfo vm01 | grep UUID
UUID:           23bcf945-288d-460a-9953-4797022d45c4
```

新しい UUID 取得

```
# uuidgen
466df1ab-c0af-41a4-86d0-04a657042593
```

#### 2.2. あとで Diff る為に XML 取得

```
# virsh dumpxml vm01 > vm01.xml
```

#### 2.3. virsh edit

name と uuid を修正

```
# virsh edit vm01
Domain vm02 XML configuration edited.
```

新しい Domain (vm02) が作成される

```
# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     vm01                           shut off
 -     vm02                           shut off
```

#### 2.4. Diff

* vm01: 変更前
* vm02: 変更後

```
# virsh dumpxml vm02 > vm02.xml
```

```
# diff vm01.xml vm02.xml
2,3c2,3
<   <name>vm01</name>
<   <uuid>23bcf945-288d-460a-9953-4797022d45c4</uuid>
---
>   <name>vm02</name>
>   <uuid>466df1ab-c0af-41a4-86d0-04a657042593</uuid>
```

#### 2.5. 停止状態の古い Domain を削除

```
# virsh undefine vm01
Domain vm01 has been undefined

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     vm02                           shut off
```

#### 2.6. Domain 起動

```
# virsh start vm02
Domain vm02 started

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 15    vm02                           running
```

## Environment

```
# virsh --version
1.2.8

# libvirtd --version
libvirtd (libvirt) 1.2.8
```