Skip to content

SmartOS上更新虚拟机配置

这里以一个SmartOS zone更新IP及网关为例。

用到的命令是vmadm update UUID < json.file。相关配置都需要以json的格式给出,这个我有点不习惯。或许还有其它方式,不过我不了解。

我对json不熟悉,也懒得写这样的一个文件,我首先用vmadm get UUID导出zone的现有配置:

{
  "zonename": "a5be5af8-e67f-4f29-cdff-b62948ba28f5",
  "autoboot": false,
  "brand": "joyent",
  "limit_priv": "default",
  "v": 1,
  "create_timestamp": "2016-05-02T13:40:46.078Z",
  "image_uuid": "163cd9fe-0c90-11e6-bd05-afd50e5961b6",
  "cpu_shares": 100,
  "max_lwps": 2000,
  "max_msg_ids": 4096,
  "max_sem_ids": 4096,
  "max_shm_ids": 4096,
  "max_shm_memory": 512,
  "zfs_io_priority": 100,
  "max_physical_memory": 512,
  "max_locked_memory": 512,
  "max_swap": 512,
  "billing_id": "00000000-0000-0000-0000-000000000000",
  "owner_uuid": "00000000-0000-0000-0000-000000000000",
  "tmpfs": 512,
  "hostname": "test01",
  "dns_domain": "local",
  "resolvers": [
    "112.4.0.55",
    "208.67.220.220"
  ],
  "alias": "test01",
  "nics": [
    {
      "interface": "net0",
      "mac": "62:4c:b9:8d:74:8f",
      "nic_tag": "admin",
      "gateway": "192.168.2.1",
      "gateways": [
        "192.168.2.1"
      ],
      "netmask": "255.255.255.0",
      "ip": "192.168.2.12",
      "ips": [
        "192.168.2.12/24"
      ],
      "primary": true
    }
  ],
  "uuid": "a5be5af8-e67f-4f29-cdff-b62948ba28f5",
  "zone_state": "installed",
  "zonepath": "/zones/a5be5af8-e67f-4f29-cdff-b62948ba28f5",
  "zoneid": null,
  "last_modified": "2016-05-04T13:59:13.000Z",
  "exit_status": 0,
  "exit_timestamp": "2016-05-04T13:56:55.619Z",
  "firewall_enabled": false,
  "server_uuid": "0ccd4115-c6b1-4b70-86d5-a8616bc2853b",
  "platform_buildstamp": "20160428T170316Z",
  "state": "stopped",
  "customer_metadata": {},
  "internal_metadata": {},
  "routes": {},
  "tags": {},
  "quota": 20,
  "zfs_root_recsize": 131072,
  "zfs_filesystem": "zones/a5be5af8-e67f-4f29-cdff-b62948ba28f5",
  "zpool": "zones",
  "snapshots": []
}

然后提取出和IP及网关相关的部分,并写入到一个名为newnic.json的文件:

[root@sunflower ~]# vmadm get a5be5af8-e67f-4f29-cdff-b62948ba28f5 |sed -n '29,45p' | tee newnic.json
  "nics": [
    {
      "interface": "net0",
      "mac": "62:4c:b9:8d:74:8f",
      "nic_tag": "admin",
      "gateway": "192.168.2.1",
      "gateways": [
        "192.168.2.1"
      ],
      "netmask": "255.255.255.0",
      "ip": "192.168.2.12",
      "ips": [
        "192.168.2.12/24"
      ],
      "primary": true
    }
  ],

编辑此文件,将最后一行的逗号去掉,将第一行的”nics”改为“update_nics”(重要),把IP地址和网关地址改成自己需要的IP,然后将所有内容放在花括号里面。如下:

{
  "update_nics": [
    {
      "interface": "net0",
      "mac": "62:4c:b9:8d:74:8f",
      "nic_tag": "admin",
      "gateway": "192.168.3.1",
      "gateways": [
        "192.168.3.1"
      ],
      "netmask": "255.255.255.0",
      "ip": "192.168.3.12",
      "ips": [
        "192.168.3.12/24"
      ],
      "primary": true
    }
  ]
}

然后使用vmadm update命令更新此虚拟机的配置:

[root@sunflower ~]# vmadm update a5be5af8-e67f-4f29-cdff-b62948ba28f5 < newnic.json 
Successfully updated VM a5be5af8-e67f-4f29-cdff-b62948ba28f5

最后,使用vmadm get UUID命令来验证。

PS:

vmadm的手册里有下面的例子。但是我用vmadm update UUID ip=newip的方式无法成功修改。我的猜测是如果配置项是在一个list或dict中,则只能使用json格式。

Example 7: Set the quota to 40G for VM 54f1cc77-68f1-42ab-acac-5c4f64f5d6e0

vmadm update 54f1cc77-68f1-42ab-acac-5c4f64f5d6e0 quota=40

Avatar

专业Linux/Unix/Windows系统管理员,开源技术爱好者。对操作系统底层技术,TCP/IP协议栈以及信息系统安全有强烈兴趣。电脑技术之外,则喜欢书法,古典诗词,数码摄影和背包行。

Sidebar