# OpenStack

OpenStack

# Update a port in Neutron

- ##### To update a port in neutron you can do the following:

```shell
neutron port-update --fixed-ip subnet_id=SubNetID,ip_address=X.X.X.X --fixed-ip subnet_id=SubNetID,ip_address=X.X.X.X <PORT_ID>
```

\* Don't forget to input the already present IPs in the command or they will be overwritten

- ##### To update a port with alot of IPs (shortcut):

```shell
neutron port-update PORT_UUID \
--fixed-ip=subnet_id=SUBNETID,ip_address=192.168.1.{164,165,{167..190}}\  
--fixed-ip=subnet_id=SUBNETID,ip_address=1.1.1.{164..190} \
--fixed-ip=subnet_id=SUBNETID,ip_address=10.0.0.{4..30} \  
--fixed-ip=subnet_id=SUBNETID,ip_address=172.16.1.{68..94}  
```

<p class="callout info">**Explanation:** *Line 1. replace PORT\_UUID by the port ID Line 2. Will add 192.168.1.164, 192.168.1.165, 192.168.1.167 - 192.168.1.190 (We are basically skipping 192.168.1.166) Line 3. Will add 1.1.1.164 - 1.1.1.190  
  
Line 4. Will add 10.0.0..4 - 10.0.0.30 Line 5. Will add 172.16.1.68 - 172.16.1.94*</p>

# Openstack commands

#####  

##### Nova:

- **List of instances**

```shell
openstack server list
nova list
```

- **Details on a specific instance**

```shell
openstack server show <server id>
nova show <server id>
```

- **Rebuild an instance**

```shell
openstack server rebuild --image <image-id> <server-id>
nova rebuild <server-id> <image-id>
```

- **Rebuild an instance with metadata**

```shell
openstack server set --property <meta> <server-id>
nova meta <server-id> set <meta>
nova rebuild --meta <meta> <server-id> <image-id>
```

- **Attach a port to an instance**

```shell
nova interface-attach --port-id <port id> <instance id>
```

- **Create a instance from a availability zone**

```shell
nova boot --availability-zone nova:metal01:<ironic-id> --flavor <flavor-id> --image <img-id> --nic net-id=<network id>,v4-fixed-ip=<ip-address> <name>
openstack server create --availability-zone nova:metal01:<ironic-id> --flavor <flavor-id> --image <img-id> --nic net-id=<network id>,v4-fixed-ip=<ip-address> <name>
```

- **Create a instance with meta property, sec group, fixed ip, ssh-key**

```
openstack server create --image <image id> --flavor <flavor id> --nic net-id=<network id>,v4-fixed-ip=<ip-address> --property <meta-data> --security-group <security group id> --key-name <keypair name> vmname
```

- **Create a instance**

```shell
openstack server create --image <image id> --flavor <flavor id> vmname
```

- **Remove error state from instance**

```shell
nova reset-state --active <server-id>
```

- **Change the flavour (resize)**

```shell
openstack server resize --flavor <flavor> <ID>
```

##### Neutron:

- **List of subnets and network ID**

```shell
openstack network list
neutron net-list
```

- **List of subnets, their IDs and allocation pools:**

```shell
openstack subnet list
neutron subnet-list
```

- <span style="color:#808080;">**List ports**</span>

```shell
openstack port list
neutron port-list
```

- <span style="color:#808080;">**Create port**</span>

```shell
openstack port create --network <network id> --fixed-ip subnet=<subnet id>,ip-address=<ip-address>
neutron port-create --fixed-ip subnet_id=<subnet id>,ip_address=<X.X.X.X> <network id> <name>
```

- <span style="color:#808080;">**Update or Set/Unset port**</span>

```shell
neutron port-update --fixed-ip subnet_id=<subnet id>,ip_address=<ip-address> --fixed-ip subnet_id=<subnet id>,ip_address=<X.X.X.X> <port id>
openstack port set --fixed-ip subnet=<subnet>,ip-address=<ip-address> <port id>
openstack port unset --fixed-ip subnet=<subnet>,ip-address=<ip-address> <port id>
```


- <span style="color:#808080;">**Get details on a port**</span>

```shell
openstack port show <port id>
neutron port-show <port id>
```

- Allowed address pairs - (Allows one port to add additional IP/MAC address pairs on that port to allow traffic that matches those specified values.)

```
neutron port-update <port-uuid> --allowed-address-pairs type=dict list=true ip_address='0.0.0.0/0'
```

##### <span style="color:#000000;">Glance:</span>

- <span style="color:#808080;">**List images**</span>

```shell
openstack image list
glance image-list
```

- <span style="color:#808080;">**Get details on an image**</span>

```shell
glance image-show <image id>
```

- **Upload a image**

```shell
openstack image create --disk-format qcow2 --container-format bare --public --file ./centos7.qcow2 centos7-image
```

- **Delete**

```shell
openstack image delete <ID>
```

- **Download a image**

```shell
glance image-download <img-id> --file img.qcow2
```

- **Change state**

```shell
cinder reset-state --state in-use <id>
```

##### Other:

- **List flavors**

```shell
openstack flavor list
```

- **List keypairs**

```shell
openstack keypair list
```

- **List Security groups**

```shell
openstack security group list
```

##### **Cinder**

```
openstack volume set --state error <id>
```

**Stop a migraion**

**Remove the active volume from the migration state**

```
cinder reset-state <id> --reset-migration-status
```

**find the volume in the attaching state and remove it**

```
openstack volume list
```

```
cinder reset-state <id> --state error 
```

```
cinder reset-state <id> --reset-migration-status
```

```
openstack volume delete <id>
```

# Adding a LVM Cinder Volume

```shell
mkfs.ext /dev/md0
mkdir /cinder-vol
mount /dev/md0 /cinder-volumes
dd if=/dev/zero of=/cinder-vol/cinder-vol bs=1 count=0 seek=3950G
losetup /dev/loop2 /cinder-vol/cinder-vol
pvcreate /dev/loop2
vgcreate cinder-vol /dev/loop2
```

Double check your work with **pvdisplay** and **vgdisplay**:

> \[root@openstack ~\]# pvdisplay   
> \--- Physical volume ---  
>  PV Name /dev/loop2  
>  VG Name cinder-vol  
>  PV Size &lt;3.86 TiB / not usable 4.00 MiB  
>  Allocatable yes   
>  PE Size 4.00 MiB  
>  Total PE 1011199  
>  Free PE 1011199  
>  Allocated PE 0  
>  PV UUID HwHPNB-48pG-igg4-EU7d-Rucj-vniF-lrYP22  
>   
> \[root@openstack ~\]# vgdisplay   
> \--- Volume group ---  
>  VG Name cinder-vol  
>  System ID   
>  Format lvm2  
>  Metadata Areas 1  
>  Metadata Sequence No 1  
>  VG Access read/write  
>  VG Status resizable  
>  MAX LV 0  
>  Cur LV 0  
>  Open LV 0  
>  Max PV 0  
>  Cur PV 1  
>  Act PV 1  
>  VG Size &lt;3.86 TiB  
>  PE Size 4.00 MiB  
>  Total PE 1011199  
>  Alloc PE / Size 0 / 0   
>  Free PE / Size 1011199 / &lt;3.86 TiB  
>  VG UUID ILuHCP-9822-V1GS-lMu7-eiRu-UD3S-9xqDwY

```shell
vim /etc/cinder/cinder.conf
```

> \[lvm\]  
> volume\_backend\_name=lvm  
> volume\_driver=cinder.volume.drivers.lvm.LVMVolumeDriver  
> iscsi\_ip\_address=192.168.2.57  
> iscsi\_helper=lioadm  
> volume\_group=cinder-vol  
> volumes\_dir=/cinder-vol/cinder-vol

```shell
systemctl restart openstack-cinder-scheduler
systemctl restart openstack-cinder-volume
```

Update /usr/lib/systemd/system/openstack-losetup.service

> ExecStart=/usr/bin/sh -c '/usr/sbin/losetup -j /cinder-vol/cinder-vol | /usr/bin/grep /cinder-vol/cinder-vol || /usr/sbin/losetup -f /cinder-vol/cinder-vol'  
>  ExecStop=/usr/bin/sh -c '/usr/sbin/losetup -j /cinder-vol/cinder-vol | /usr/bin/cut -d : -f 1 | /usr/bin/xargs /usr/sbin/losetup -d'

# OpenStack MySQL command

Force delete snapshot

```
use cinder;
update snapshots set status='deleted', deleted='1' where id ='<id>';
update snapshots set status='deleted', deleted='1' where id = '<id>';
```