# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.platform9.com/private-cloud-director/tutorials/local-storage-to-volume.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
