# Migrate VM Storage from Host-Local Storage to Persistent Volume

This guide provides step-by-step instructions for migrating a virtual machine storage from host-local storage to a persistent block storage volume in <code class="expression">space.vars.product\_name</code>.

## Prerequisites

* The VM for which the storage is being migrated must be in `stopped` state before the migration.
* Ensure that the target volume storage backend is configured and available.
* The `OpenStack` CLI must be installed and configured.
* Sufficient storage capacity in the block storage backend.

## Steps to Migrate VM Storage

### Step 1: Identify the VM and its Local Disk

List all VMs to find the target VM's ID:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server list
```

{% endtab %}
{% endtabs %}

Get the details of the specific VM:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server show <vm_id>
```

{% endtab %}
{% endtabs %}

The disk location will be under the `OS-EXT-SRV-ATTR:root_device` name field, typically `/dev/vda` or similar.

### Step 2: Create a New Volume

Create a new volume of the required size to migrate the local disk.

{% tabs %}
{% tab title="Bash" %}

```bash
openstack volume create --size <size_in_GB> --bootable --availability-zone <az> <new_volume_name>
```

{% endtab %}
{% endtabs %}

Verify the volume creation:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack volume show <new_volume_id>
```

{% endtab %}
{% endtabs %}

### Step 3: Convert the Local Disk to an Image

Stop the VM if it's running:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server stop <vm_id>
```

{% endtab %}
{% endtabs %}

Create an image from the local storage disk of the VM:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack image create --disk-format qcow2 --container-format bare --file /var/lib/nova/instances/<vm_id>/disk <image_name>
```

{% endtab %}
{% endtabs %}

Verify that the image is successfully created:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack image list | grep <image_name>
```

{% endtab %}
{% endtabs %}

### Step 4: Create a New Bootable Volume from the Image

Use the newly created image to create a bootable volume:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack volume create --size <size_in_GB> --image <image_name> --bootable <new_volume_name>
```

{% endtab %}
{% endtabs %}

Ensure the volume is `available` before proceeding:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack volume show <new_volume_id>
```

{% endtab %}
{% endtabs %}

### Step 5: Detach Local Storage and Attach New Volume

Detach the local storage disk.

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server stop <vm_id>
openstack server volume detach <vm_id> <old_volume_id>
```

{% endtab %}
{% endtabs %}

Attach the newly created volume.

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server volume attach --device /dev/vda <vm_id> <new_volume_id>
```

{% endtab %}
{% endtabs %}

### Step 6: Start the VM

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server start <vm_id>
```

{% endtab %}
{% endtabs %}

Verify that the VM is running and using the new volume:

{% tabs %}
{% tab title="Bash" %}

```bash
openstack server show <vm_id>
```

{% endtab %}
{% endtabs %}

### Verification

* Confirm that the VM boots from the new volume.
* Confirm the new volume shows `in-use`.
* Check that the local storage is no longer in use.
* Connect to the VM and verify disk configuration

{% tabs %}
{% tab title="Bash" %}

```bash
ssh <username>@<vm_ip>
sudo lsblk
df -h
```

{% endtab %}
{% endtabs %}
