# virt-resize

`virt-resize` is a tool provided by the **libvirt** project that allows you to resize virtual disk images, such as those in **qcow2** or **raw** formats. It can be used to increase or decrease the size of a virtual machine (VM) disk image while preserving the contents, which is particularly useful for managing virtual machine storage and adjusting the disk size to meet changing needs.

#### Purpose of `virt-resize`

The main purpose of `virt-resize` is to resize virtual machine disk images. This can be used in scenarios where the virtual machine needs more disk space (e.g., resizing a disk to allow for more data), or if you want to reduce the disk size, for example, after cleaning up a VM image.

#### How `virt-resize` Works

`virt-resize` operates by creating a new virtual disk image that is resized according to the parameters you provide. The tool modifies the partition table, filesystem, and other relevant data to accommodate the new size. It supports resizing both the disk and its partition structure, and can also extend the filesystem to use the new available space.

Here’s a breakdown of how `virt-resize` works:

1. **Source Disk Image**: You provide the original disk image that needs to be resized.
2. **Resize Operation**: `virt-resize` resizes the disk image, including modifying the partition table and the filesystem.
3. **Target Disk Image**: A new disk image is created with the new size, and it contains all the original data along with the resized partitions.

#### Basic Syntax of `virt-resize`

```bash
virt-resize [options] source-disk target-disk
```

* **source-disk**: The original virtual disk image that you want to resize.
* **target-disk**: The new disk image that will be created with the resized partition.

#### Common Options

* `-v, --verbose`: Provides detailed output of the resizing process.
* `--expand <partition>`: Expands the specified partition to fill the available space.
* `--shrink <partition>`: Shrinks the specified partition.
* `-f, --format <format>`: Specifies the output disk format (e.g., `qcow2`, `raw`).
* `--resize`: Specify a size for the output disk, either by extending or shrinking.
* `--partitions`: If you want to resize individual partitions, you can specify this option to handle partitions explicitly.
* `--no-make-mbr`: Avoids creating a master boot record (MBR) on the target disk.

#### Example Usage

1. **Resize a Disk Image**:

   To resize a disk image (e.g., `qcow2`) to a new size, use the following command:

   ```bash
   virt-resize /path/to/source-image.qcow2 /path/to/target-image.qcow2
   ```

   This will resize the source image and create a new `target-image.qcow2` with the new size.
2. **Expanding the Disk to a New Size**:

   If you want to expand a virtual disk image to a larger size (e.g., 50 GB), you can use the `--expand` option:

   ```bash
   virt-resize --expand /dev/sda1 /path/to/source-image.qcow2 /path/to/expanded-image.qcow2
   ```

   This will resize the disk and expand partition `/dev/sda1` to fill the new available space.
3. **Shrink the Disk Image**:

   If the virtual disk is too large and you want to shrink it, you can specify a smaller target size for the image. However, this typically requires shrinking partitions and filesystems first:

   ```bash
   virt-resize --shrink /dev/sda1 --size 10G /path/to/source-image.qcow2 /path/to/shrunken-image.qcow2
   ```

   This will shrink the partition `/dev/sda1` and create a new disk image of size 10 GB.
4. **Verbose Output**:

   To see detailed information about the process, you can run `virt-resize` with the `-v` option for verbose output:

   ```bash
   virt-resize -v /path/to/source-image.qcow2 /path/to/resized-image.qcow2
   ```

#### Resize Disk and Filesystem

After resizing a disk image, the filesystem is not automatically expanded to use the new space unless you specify it. The `virt-resize` tool can handle expanding the filesystem along with the disk resizing if the partition is configured to expand.

If you're expanding the disk and need to extend the filesystem on the resized partition, you should specify the partition to expand:

```bash
virt-resize --expand /dev/sda1 /path/to/source-image.qcow2 /path/to/resized-image.qcow2
```

#### Partition Table Modification

`virt-resize` automatically modifies the partition table and adjusts it according to the new disk size. This is important to ensure that the disk layout remains consistent, and that partitions are resized or created correctly. The tool supports **MBR** and **GPT** partition tables.

#### Use Cases for `virt-resize`

1. **Increase Disk Size**: If your virtual machine disk is running out of space, `virt-resize` can help increase the disk size by adding more storage to the disk and expanding the partitions.
2. **Reduce Disk Size**: After cleaning up data or when the disk is too large, `virt-resize` can help shrink the disk to save storage.
3. **VM Migration**: When moving virtual machines between hosts, you may want to resize the disk during the migration to adjust for storage differences.
4. **Optimizing Storage**: Reclaiming unused space from virtual disk images to make them smaller and more efficient.

#### Limitations and Considerations

* **Filesystem Compatibility**: Not all filesystems are supported by `virt-resize`. It works best with ext2/3/4, xfs, and similar filesystems, but care should be taken when working with more exotic or proprietary filesystems.
* **Backup Your Data**: Always back up your disk image before performing resizing operations. While `virt-resize` is designed to preserve the data, there’s always a risk when performing operations that alter disk structure.
* **Space Requirements**: Make sure that the system has enough available disk space to hold both the original disk image and the resized disk during the operation. This is especially critical when expanding the disk.

#### Conclusion

`virt-resize` is a powerful and flexible tool for resizing virtual machine disk images. It allows you to resize both the disk and the partitioning scheme, and can expand or shrink virtual disks as needed. Whether you are optimizing disk usage, preparing for a migration, or increasing disk space for a growing virtual machine, `virt-resize` is a useful utility in the toolkit for managing virtualized environments.
