Friday, July 15, 2016

OpenStack(kilo): Dynamically Adding Nic Card to VM

nicadding

OpenStack(kilo): Adding Nic Card to VM

Dynamically adding a Nic to VM without reboot.

Initially, In the VM, we have eth0 only.

$ip link list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether fa:16:3e:2e:50:12 brd ff:ff:ff:ff:ff:ff

Attach Nic Card to VM

Command Line

If you are using DevStack, you can use the command source openrc admin demo. So that you can use OpenStack command line.

To add Nic Card to VM, we can use the following commands.

neutron port-create private
neutron port-list
nova interface-attach --port-id <portid> <instanceName>
nova interface-detach <instanceName> <portid>

You then can login to VM, and check nic card with ip link list command.

It's very simple, really. And now we can discuss it more detailed.

Detailed Command with debug mode

The most important thing is to study how to call rest-api. You can use DEBUG mode.

stack@kiloceilo:~/devstack$ neutron port-create private --debug
DEBUG: keystoneclient.session REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:5000/v2.0 -H "Accept: application/json" -H "User-Agent: python-keystoneclient"
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session RESP: [200] content-length: 340 vary: X-Auth-Token server: Apache/2.4.7 (Ubuntu) date: Fri, 15 Jul 2016 08:13:28 GMT content-type: application/json x-openstack-request-id: req-86f058df-70a5-4b45-90f2-0d4ce227548f
RESP BODY: {"version": {"status": "stable", "updated": "2014-04-17T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}], "id": "v2.0", "links": [{"href": "http://172.16.235.128:5000/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/", "type": "text/html", "rel": "describedby"}]}}

DEBUG: stevedore.extension found extension EntryPoint.parse('table = cliff.formatters.table:TableFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('shell = cliff.formatters.shell:ShellFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('value = cliff.formatters.value:ValueFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('yaml = clifftablib.formatters:YamlFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('json = clifftablib.formatters:JsonFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('html = clifftablib.formatters:HtmlFormatter')
DEBUG: neutronclient.neutron.v2_0.port.CreatePort get_data(Namespace(admin_state=True, columns=[], device_id=None, device_owner=None, extra_dhcp_opts=[], fixed_ip=None, formatter='table', mac_address=None, max_width=0, name=None, network_id=u'private', no_security_groups=False, prefix='', request_format='json', security_groups=[], tenant_id=None, variables=[], vnic_type=None))
DEBUG: keystoneclient.auth.identity.v2 Making authentication request to http://172.16.235.128:5000/v2.0/tokens
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:9696/v2.0/networks.json?fields=id&name=private -H "User-Agent: python-neutronclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}7c01e319a42caeda349739980976abf13be0c5f0"
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session RESP: [200] date: Fri, 15 Jul 2016 08:13:28 GMT content-length: 62 content-type: application/json x-openstack-request-id: req-5a6e63a4-ae1b-45d4-8b89-395136efb81e
RESP BODY: {"networks": [{"id": "2b696172-5599-4663-85bd-42de265e56df"}]}

DEBUG: keystoneclient.session REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X POST http://172.16.235.128:9696/v2.0/ports.json -H "User-Agent: python-neutronclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}7c01e319a42caeda349739980976abf13be0c5f0" -d '{"port": {"network_id": "2b696172-5599-4663-85bd-42de265e56df", "admin_state_up": true}}'
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session RESP: [201] date: Fri, 15 Jul 2016 08:13:28 GMT content-length: 609 content-type: application/json x-openstack-request-id: req-2f23d4dc-a2d1-402a-9580-4b7cfa89c100
RESP BODY: {"port": {"status": "DOWN", "binding:host_id": "", "allowed_address_pairs": [], "device_owner": "", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.149"}], "id": "1db9fa8e-d376-4034-87ea-7c6620daefcd", "security_groups": ["ef5a81ce-2404-4b8f-8fe2-39af5823f8c9"], "device_id": "", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:b2:68:b1"}}

Created a new port:
+-----------------------+-----------------------------------------------------------------------------------+
| Field                 | Value                                                                             |
+-----------------------+-----------------------------------------------------------------------------------+
| admin_state_up        | True                                                                              |
| allowed_address_pairs |                                                                                   |
| binding:host_id       |                                                                                   |
| binding:profile       | {}                                                                                |
| binding:vif_details   | {}                                                                                |
| binding:vif_type      | unbound                                                                           |
| binding:vnic_type     | normal                                                                            |
| device_id             |                                                                                   |
| device_owner          |                                                                                   |
| fixed_ips             | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.149"} |
| id                    | 1db9fa8e-d376-4034-87ea-7c6620daefcd                                              |
| mac_address           | fa:16:3e:b2:68:b1                                                                 |
| name                  |                                                                                   |
| network_id            | 2b696172-5599-4663-85bd-42de265e56df                                              |
| security_groups       | ef5a81ce-2404-4b8f-8fe2-39af5823f8c9                                              |
| status                | DOWN                                                                              |
| tenant_id             | e07408c60a9e44b6b38161aaa3f776ae                                                  |
+-----------------------+-----------------------------------------------------------------------------------+

List all port to get information.

stack@kiloceilo:~/devstack$ neutron port-list --debug
DEBUG: keystoneclient.session REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:5000/v2.0 -H "Accept: application/json" -H "User-Agent: python-keystoneclient"
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session RESP: [200] content-length: 340 vary: X-Auth-Token server: Apache/2.4.7 (Ubuntu) date: Fri, 15 Jul 2016 08:15:48 GMT content-type: application/json x-openstack-request-id: req-3087bcbd-a113-490c-87c8-f134d2c68ef0
RESP BODY: {"version": {"status": "stable", "updated": "2014-04-17T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}], "id": "v2.0", "links": [{"href": "http://172.16.235.128:5000/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/", "type": "text/html", "rel": "describedby"}]}}

DEBUG: stevedore.extension found extension EntryPoint.parse('table = cliff.formatters.table:TableFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('csv = cliff.formatters.commaseparated:CSVLister')
DEBUG: stevedore.extension found extension EntryPoint.parse('yaml = clifftablib.formatters:YamlFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('json = clifftablib.formatters:JsonFormatter')
DEBUG: stevedore.extension found extension EntryPoint.parse('html = clifftablib.formatters:HtmlFormatter')
DEBUG: neutronclient.neutron.v2_0.port.ListPort get_data(Namespace(columns=[], fields=[], formatter='table', max_width=0, page_size=None, quote_mode='nonnumeric', request_format='json', show_details=False, sort_dir=[], sort_key=[]))
DEBUG: keystoneclient.auth.identity.v2 Making authentication request to http://172.16.235.128:5000/v2.0/tokens
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:9696/v2.0/ports.json -H "User-Agent: python-neutronclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}94a754e9f1e835cbe08f80379281c74f14bf7727"
DEBUG: urllib3.util.retry Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG: keystoneclient.session RESP: [200] date: Fri, 15 Jul 2016 08:15:48 GMT content-length: 10386 content-type: application/json x-openstack-request-id: req-fd17bd6b-3444-45e6-9aea-3a6a566ec675
RESP BODY: {"ports": [{"status": "ACTIVE", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:router_gateway", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.3"}], "id": "0155e3e5-1139-46c1-97a6-54baabf9615e", "security_groups": [], "device_id": "f8cfe243-5908-4003-a776-59ce891e5d80", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:be:57:b1"}, {"status": "ACTIVE", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:dhcp", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.2"}], "id": "01ca3998-19ae-4b9c-b9bc-1a7e53dff980", "security_groups": [], "device_id": "dhcp66142bd9-a6da-5b33-9b98-3219a929d3f5-2b696172-5599-4663-85bd-42de265e56df", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:7b:c6:a5"}, {"status": "ACTIVE", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:router_interface", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.1"}], "id": "19bae3c4-0b9e-43da-bf05-bd8c84066a10", "security_groups": [], "device_id": "f8cfe243-5908-4003-a776-59ce891e5d80", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:fe:cc:77"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.8"}], "id": "1c640b7a-2622-43b3-b146-d874ab30946d", "security_groups": [], "device_id": "39bc1b07-3e2b-47e2-a5aa-31744f0f063c", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:43:e0:a2"}, {"status": "DOWN", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.149"}], "id": "1db9fa8e-d376-4034-87ea-7c6620daefcd", "security_groups": ["ef5a81ce-2404-4b8f-8fe2-39af5823f8c9"], "device_id": "", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:b2:68:b1"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.13"}], "id": "1deab452-8d32-4082-8b8b-3a6a94f2bece", "security_groups": [], "device_id": "b1f40376-8f75-4102-8aab-1d3971da6193", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:67:70:d8"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.14"}], "id": "2041e300-28f9-49c6-9e2c-323f46c24412", "security_groups": [], "device_id": "d85329c0-7fcf-43e8-b744-f28f119a9adb", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:3f:61:87"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.9"}], "id": "3df4b7d0-7c48-4b74-9918-2edd43ec6479", "security_groups": [], "device_id": "fbd8f36f-47cb-4da9-a524-a2b24e37a6b5", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:8f:db:08"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.12"}], "id": "4fb9d343-ae17-490d-9d2c-2d96259958f9", "security_groups": [], "device_id": "20c3e9de-85df-4249-9866-51b4c4f288e1", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:05:eb:dd"}, {"status": "ACTIVE", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "compute:nova", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.147"}], "id": "66f42774-c2cc-4b46-9589-30df1e19b496", "security_groups": ["ef5a81ce-2404-4b8f-8fe2-39af5823f8c9"], "device_id": "e4f6117e-27b3-487c-b487-2e34da1021f5", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:2e:50:12"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.7"}], "id": "9f566f04-3f01-4d29-a63b-a75f69fcf89b", "security_groups": [], "device_id": "429d33cf-b712-4814-9833-03366e3696ae", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:48:56:cf"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.11"}], "id": "a22ef65a-cd25-47bb-a7d4-bb126367768f", "security_groups": [], "device_id": "47414206-5cb5-488b-8876-ac7ae3552fdb", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:b5:d1:bb"}, {"status": "ACTIVE", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "compute:probe", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.4"}], "id": "ad1f4818-b11d-4898-8258-c23ea4dc65dd", "security_groups": ["0bd4135f-9555-4a34-a1c5-f3e4556c90bd"], "device_id": "kiloceilo", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "89a01dccec8c4228b679f347b10b79ab", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:31:bc:61"}, {"status": "ACTIVE", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "compute:probe", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.3"}], "id": "b8ce74b5-cbbb-4630-a779-9da79fa78fe1", "security_groups": ["ef5a81ce-2404-4b8f-8fe2-39af5823f8c9"], "device_id": "kiloceilo", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:b8:be:a1"}, {"status": "N/A", "binding:host_id": "", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "network:floatingip", "binding:profile": {}, "fixed_ips": [{"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.10"}], "id": "c2c90b0a-4781-46f6-9a94-d06eb74e6244", "security_groups": [], "device_id": "4acb3f1d-bed1-4864-b465-62071222b654", "name": "", "admin_state_up": true, "network_id": "3e2df9ef-8dcc-4763-9ee9-fa811db91d15", "tenant_id": "", "binding:vif_details": {}, "binding:vnic_type": "normal", "binding:vif_type": "unbound", "mac_address": "fa:16:3e:aa:be:81"}, {"status": "DOWN", "binding:host_id": "kiloceilo", "allowed_address_pairs": [], "extra_dhcp_opts": [], "device_owner": "", "binding:profile": {}, "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.148"}], "id": "d22191ab-895a-44e0-a641-5117e1c63505", "security_groups": ["ef5a81ce-2404-4b8f-8fe2-39af5823f8c9"], "device_id": "", "name": "", "admin_state_up": true, "network_id": "2b696172-5599-4663-85bd-42de265e56df", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "binding:vif_details": {"port_filter": true, "ovs_hybrid_plug": true}, "binding:vnic_type": "normal", "binding:vif_type": "ovs", "mac_address": "fa:16:3e:0a:24:0c"}]}

+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+
| id                                   | name | mac_address       | fixed_ips                                                                            |
+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+
| 0155e3e5-1139-46c1-97a6-54baabf9615e |      | fa:16:3e:be:57:b1 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.3"}  |
| 01ca3998-19ae-4b9c-b9bc-1a7e53dff980 |      | fa:16:3e:7b:c6:a5 | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.2"}      |
| 19bae3c4-0b9e-43da-bf05-bd8c84066a10 |      | fa:16:3e:fe:cc:77 | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.1"}      |
| 1c640b7a-2622-43b3-b146-d874ab30946d |      | fa:16:3e:43:e0:a2 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.8"}  |
| 1db9fa8e-d376-4034-87ea-7c6620daefcd |      | fa:16:3e:b2:68:b1 | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.149"}    |
| 1deab452-8d32-4082-8b8b-3a6a94f2bece |      | fa:16:3e:67:70:d8 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.13"} |
| 2041e300-28f9-49c6-9e2c-323f46c24412 |      | fa:16:3e:3f:61:87 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.14"} |
| 3df4b7d0-7c48-4b74-9918-2edd43ec6479 |      | fa:16:3e:8f:db:08 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.9"}  |
| 4fb9d343-ae17-490d-9d2c-2d96259958f9 |      | fa:16:3e:05:eb:dd | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.12"} |
| 66f42774-c2cc-4b46-9589-30df1e19b496 |      | fa:16:3e:2e:50:12 | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.147"}    |
| 9f566f04-3f01-4d29-a63b-a75f69fcf89b |      | fa:16:3e:48:56:cf | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.7"}  |
| a22ef65a-cd25-47bb-a7d4-bb126367768f |      | fa:16:3e:b5:d1:bb | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.11"} |
| ad1f4818-b11d-4898-8258-c23ea4dc65dd |      | fa:16:3e:31:bc:61 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.4"}  |
| b8ce74b5-cbbb-4630-a779-9da79fa78fe1 |      | fa:16:3e:b8:be:a1 | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.3"}      |
| c2c90b0a-4781-46f6-9a94-d06eb74e6244 |      | fa:16:3e:aa:be:81 | {"subnet_id": "4f10c5ce-7cf3-481f-af7c-e7ff80a700dc", "ip_address": "172.16.235.10"} |
| d22191ab-895a-44e0-a641-5117e1c63505 |      | fa:16:3e:0a:24:0c | {"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.148"}    |
+--------------------------------------+------+-------------------+--------------------------------------------------------------------------------------+

Now Attach OVS port to VM.

stack@kiloceilo:~/devstack$ nova --debug interface-attach --port-id d22191ab-895a-44e0-a641-5117e1c63505 test1
DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:5000/v2.0 -H "Accept: application/json" -H "User-Agent: python-keystoneclient"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
INFO (connectionpool:203) Starting new HTTP connection (1): 172.16.235.128
DEBUG (connectionpool:383) "GET /v2.0 HTTP/1.1" 200 340
DEBUG (session:226) RESP: [200] content-length: 340 vary: X-Auth-Token server: Apache/2.4.7 (Ubuntu) date: Fri, 15 Jul 2016 08:18:14 GMT content-type: application/json x-openstack-request-id: req-3f0026bc-97b9-4b54-9381-e04c81fe953e
RESP BODY: {"version": {"status": "stable", "updated": "2014-04-17T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}], "id": "v2.0", "links": [{"href": "http://172.16.235.128:5000/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/", "type": "text/html", "rel": "describedby"}]}}

DEBUG (v2:76) Making authentication request to http://172.16.235.128:5000/v2.0/tokens
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG (connectionpool:383) "POST /v2.0/tokens HTTP/1.1" 200 2694
DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers?name=test1 -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}09fcc1a6bf4799dc4a65c96a7fced53d2998c268"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
INFO (connectionpool:203) Starting new HTTP connection (1): 172.16.235.128
DEBUG (connectionpool:383) "GET /v2/e07408c60a9e44b6b38161aaa3f776ae/servers?name=test1 HTTP/1.1" 200 362
DEBUG (session:226) RESP: [200] date: Fri, 15 Jul 2016 08:18:14 GMT content-length: 362 content-type: application/json x-compute-request-id: req-58da8547-07bd-4655-9557-4d5ea73b7c29
RESP BODY: {"servers": [{"id": "e4f6117e-27b3-487c-b487-2e34da1021f5", "links": [{"href": "http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "self"}, {"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "bookmark"}], "name": "test1"}]}

DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5 -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}09fcc1a6bf4799dc4a65c96a7fced53d2998c268"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG (connectionpool:383) "GET /v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5 HTTP/1.1" 200 1806
DEBUG (session:226) RESP: [200] date: Fri, 15 Jul 2016 08:18:14 GMT content-length: 1806 content-type: application/json x-compute-request-id: req-7dd7fb8a-58ea-4e7c-ac21-ccaf814f2fbc
RESP BODY: {"server": {"status": "ACTIVE", "updated": "2016-07-15T07:58:59Z", "hostId": "66b3d5bcf0238e5c02f41ceae66e09a90624594b499b5ef2a52470de", "OS-EXT-SRV-ATTR:host": "kiloceilo", "addresses": {"private": [{"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:2e:50:12", "version": 4, "addr": "10.0.0.147", "OS-EXT-IPS:type": "fixed"}, {"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:2e:50:12", "version": 4, "addr": "172.16.235.14", "OS-EXT-IPS:type": "floating"}]}, "links": [{"href": "http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "self"}, {"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "bookmark"}], "key_name": null, "image": {"id": "def244bc-7291-45cb-b336-005e90b61d00", "links": [{"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/images/def244bc-7291-45cb-b336-005e90b61d00", "rel": "bookmark"}]}, "OS-EXT-STS:task_state": null, "OS-EXT-STS:vm_state": "active", "OS-EXT-SRV-ATTR:instance_name": "instance-00000036", "OS-SRV-USG:launched_at": "2016-07-15T07:58:59.000000", "OS-EXT-SRV-ATTR:hypervisor_hostname": "kiloceilo", "flavor": {"id": "1", "links": [{"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/flavors/1", "rel": "bookmark"}]}, "id": "e4f6117e-27b3-487c-b487-2e34da1021f5", "security_groups": [{"name": "default"}], "OS-SRV-USG:terminated_at": null, "OS-EXT-AZ:availability_zone": "nova", "user_id": "606dca546499433a915459e2db358e14", "name": "test1", "created": "2016-07-15T07:58:51Z", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "OS-DCF:diskConfig": "AUTO", "os-extended-volumes:volumes_attached": [], "accessIPv4": "", "accessIPv6": "", "progress": 0, "OS-EXT-STS:power_state": 1, "config_drive": "True", "metadata": {}}}

DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X POST http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5/os-interface -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}09fcc1a6bf4799dc4a65c96a7fced53d2998c268" -d '{"interfaceAttachment": {"port_id": "d22191ab-895a-44e0-a641-5117e1c63505"}}'
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG (connectionpool:383) "POST /v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5/os-interface HTTP/1.1" 200 279
DEBUG (session:226) RESP: [200] date: Fri, 15 Jul 2016 08:18:15 GMT content-length: 279 content-type: application/json x-compute-request-id: req-f26cafe1-f5da-4c3c-8907-58ce8471702b
RESP BODY: {"interfaceAttachment": {"port_state": "DOWN", "fixed_ips": [{"subnet_id": "ccdb4f67-568b-47a0-b652-c1d8b8d66135", "ip_address": "10.0.0.148"}], "port_id": "d22191ab-895a-44e0-a641-5117e1c63505", "net_id": "2b696172-5599-4663-85bd-42de265e56df", "mac_addr": "fa:16:3e:0a:24:0c"}}

Result

Now, In the VM, we have eth1.

$ ip link list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether fa:16:3e:2e:50:12 brd ff:ff:ff:ff:ff:ff
4: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
    link/ether fa:16:3e:0a:24:0c brd ff:ff:ff:ff:ff:ff

Detach Nic Card From VM

Detach Nic Card.

stack@kiloceilo:~/devstack$ nova --debug interface-detach test1 d22191ab-895a-44e0-a641-5117e1c63505
DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:5000/v2.0 -H "Accept: application/json" -H "User-Agent: python-keystoneclient"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
INFO (connectionpool:203) Starting new HTTP connection (1): 172.16.235.128
DEBUG (connectionpool:383) "GET /v2.0 HTTP/1.1" 200 340
DEBUG (session:226) RESP: [200] content-length: 340 vary: X-Auth-Token server: Apache/2.4.7 (Ubuntu) date: Fri, 15 Jul 2016 08:25:12 GMT content-type: application/json x-openstack-request-id: req-60ba77ef-b798-424f-bdb2-76aead8155a6
RESP BODY: {"version": {"status": "stable", "updated": "2014-04-17T00:00:00Z", "media-types": [{"base": "application/json", "type": "application/vnd.openstack.identity-v2.0+json"}], "id": "v2.0", "links": [{"href": "http://172.16.235.128:5000/v2.0/", "rel": "self"}, {"href": "http://docs.openstack.org/", "type": "text/html", "rel": "describedby"}]}}

DEBUG (v2:76) Making authentication request to http://172.16.235.128:5000/v2.0/tokens
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG (connectionpool:383) "POST /v2.0/tokens HTTP/1.1" 200 2694
DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers?name=test1 -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}5b5fc87bd3e95d2fc6ce0e09717b602f5071344f"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
INFO (connectionpool:203) Starting new HTTP connection (1): 172.16.235.128
DEBUG (connectionpool:383) "GET /v2/e07408c60a9e44b6b38161aaa3f776ae/servers?name=test1 HTTP/1.1" 200 362
DEBUG (session:226) RESP: [200] date: Fri, 15 Jul 2016 08:25:12 GMT content-length: 362 content-type: application/json x-compute-request-id: req-ee3d5398-79bb-4c02-88a9-f29177ea7bda
RESP BODY: {"servers": [{"id": "e4f6117e-27b3-487c-b487-2e34da1021f5", "links": [{"href": "http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "self"}, {"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "bookmark"}], "name": "test1"}]}

DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X GET http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5 -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}5b5fc87bd3e95d2fc6ce0e09717b602f5071344f"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG (connectionpool:383) "GET /v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5 HTTP/1.1" 200 1941
DEBUG (session:226) RESP: [200] date: Fri, 15 Jul 2016 08:25:12 GMT content-length: 1941 content-type: application/json x-compute-request-id: req-17707da0-e9bf-4ecc-acd6-8547dda31dbf
RESP BODY: {"server": {"status": "ACTIVE", "updated": "2016-07-15T07:58:59Z", "hostId": "66b3d5bcf0238e5c02f41ceae66e09a90624594b499b5ef2a52470de", "OS-EXT-SRV-ATTR:host": "kiloceilo", "addresses": {"private": [{"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:2e:50:12", "version": 4, "addr": "10.0.0.147", "OS-EXT-IPS:type": "fixed"}, {"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:0a:24:0c", "version": 4, "addr": "10.0.0.148", "OS-EXT-IPS:type": "fixed"}, {"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:2e:50:12", "version": 4, "addr": "172.16.235.14", "OS-EXT-IPS:type": "floating"}]}, "links": [{"href": "http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "self"}, {"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5", "rel": "bookmark"}], "key_name": null, "image": {"id": "def244bc-7291-45cb-b336-005e90b61d00", "links": [{"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/images/def244bc-7291-45cb-b336-005e90b61d00", "rel": "bookmark"}]}, "OS-EXT-STS:task_state": null, "OS-EXT-STS:vm_state": "active", "OS-EXT-SRV-ATTR:instance_name": "instance-00000036", "OS-SRV-USG:launched_at": "2016-07-15T07:58:59.000000", "OS-EXT-SRV-ATTR:hypervisor_hostname": "kiloceilo", "flavor": {"id": "1", "links": [{"href": "http://172.16.235.128:8774/e07408c60a9e44b6b38161aaa3f776ae/flavors/1", "rel": "bookmark"}]}, "id": "e4f6117e-27b3-487c-b487-2e34da1021f5", "security_groups": [{"name": "default"}, {"name": "default"}], "OS-SRV-USG:terminated_at": null, "OS-EXT-AZ:availability_zone": "nova", "user_id": "606dca546499433a915459e2db358e14", "name": "test1", "created": "2016-07-15T07:58:51Z", "tenant_id": "e07408c60a9e44b6b38161aaa3f776ae", "OS-DCF:diskConfig": "AUTO", "os-extended-volumes:volumes_attached": [], "accessIPv4": "", "accessIPv6": "", "progress": 0, "OS-EXT-STS:power_state": 1, "config_drive": "True", "metadata": {}}}

DEBUG (session:196) REQ: curl -g -i --cacert "/opt/stack/data/CA/int-ca/ca-chain.pem" -X DELETE http://172.16.235.128:8774/v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5/os-interface/d22191ab-895a-44e0-a641-5117e1c63505 -H "User-Agent: python-novaclient" -H "Accept: application/json" -H "X-Auth-Token: {SHA1}5b5fc87bd3e95d2fc6ce0e09717b602f5071344f"
DEBUG (retry:155) Converted retries value: 0 -> Retry(total=0, connect=None, read=None, redirect=0)
DEBUG (connectionpool:383) "DELETE /v2/e07408c60a9e44b6b38161aaa3f776ae/servers/e4f6117e-27b3-487c-b487-2e34da1021f5/os-interface/d22191ab-895a-44e0-a641-5117e1c63505 HTTP/1.1" 202 0
DEBUG (session:226) RESP: [202] date: Fri, 15 Jul 2016 08:25:12 GMT content-length: 0 content-type: text/html; charset=UTF-8 x-compute-request-id: req-121a281d-70eb-4541-9b1a-2b737edbef1d

Result

In the VM, eth1 is disappeard.

$ ip link list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether fa:16:3e:2e:50:12 brd ff:ff:ff:ff:ff:ff

Monday, July 4, 2016

SVM演算法於python的使用

svm

前言

在很久以前,我是做演算法的,我记得,我最后一个接触的演算法就是SVM。 但那时只到听过这个演算法而已。 几年后,竟然有机会接触到演算法,竟然还是SVM。这过程真有趣。 以前总是想实现算法,现在python实在太方便了,lib呼叫就什么事情都解决了。

这也是当初我学Golang,最后转回python的原因。python的lib太强大了。

也感谢,聪明又美丽的北一女学妹,给我以下的blog,加速我的学习。

http://yourgene.pixnet.net/blog/post/115169530

SVM用法

from sklearn import svm
X = [[0, 0], [1, 1],[2, 2]]
y = [0, 1, 2]
clf = svm.SVC()
clf.fit(X, y)
print clf.predict([[5., 5.]])

X為多維的training data,y為label。

One-Class SVM

One-Class

http://scikit-learn.org/stable/autoexamples/svm/plotoneclass.html

What's the meaning of nu

http://qiita.com/SE96UoC5AfUt7uY/items/88006646179adf68cb95

One-Class SVM,异常判定。 有的时候,我们有大量资料,我们只想知道,我们现在的资料是否为异常。 我们可以用以下的方式,实现。

from sklearn import svm
X = [[0, 0], [1, 1],[2, 2]]
y = [0, 1, 2]
clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf.fit(X)

print clf.predict([[3, 3]])

return值-1為異常。