2021-11-13 11:41:32 +00:00
---
title: Resize an existing virtual disk
description:
published: true
2024-07-13 13:34:01 +00:00
date: 2024-07-13T13:33:59.560Z
2021-11-13 11:41:32 +00:00
tags:
editor: markdown
dateCreated: 2021-11-13T11:41:29.087Z
---
2021-11-13 17:49:14 +00:00
# Resize a disk
2022-01-25 14:07:20 +00:00
## Background
2021-11-13 17:49:14 +00:00
2022-01-25 14:07:20 +00:00
A virtual machine's disk may have to be resized, typically due to lack of space. This page explains how to do so.
2021-11-13 17:49:14 +00:00
2024-07-13 13:11:10 +00:00
## Usage for Linux guests
2021-11-13 17:49:14 +00:00
2024-07-13 13:11:10 +00:00
> In-place expansion is not supported. A new disk of the desired size has to be created.
2021-11-13 17:49:14 +00:00
{.is-info}
2024-07-13 13:34:01 +00:00
* Navigate to the location that contains the existing image
2021-11-13 17:49:14 +00:00
```
2022-01-25 14:07:20 +00:00
cd /var/lib/libvirt/images
2021-11-13 17:49:14 +00:00
```
2024-07-13 13:11:10 +00:00
* Create a new blank disk image of the desired size
2021-11-13 17:49:14 +00:00
2024-07-13 13:11:10 +00:00
Use the following command to create a disk of 20 GB called `guest_20G.img` .
2021-11-13 17:49:14 +00:00
```
2024-07-13 13:11:10 +00:00
qemu-img create -f raw guest_20G.img 20G
2021-11-13 17:49:14 +00:00
```
2024-07-13 13:34:01 +00:00
* Identify the filesystem layout of the existing disk `guest.img`
2024-07-13 13:11:10 +00:00
2024-07-13 13:34:01 +00:00
```
# virt-filesystems -a -l -h guest.img
Name Type VFS Label Size Parent
/dev/vda1 filesystem vfat EFI 133935104 -
/dev/vda2 filesystem ext4 boot 366869504 -
/dev/vda3 filesystem ext4 root 9933475840 -
```
2024-07-13 13:11:10 +00:00
2024-07-13 13:34:01 +00:00
One can tell that the root partition is located under `/dev/vda3` . This is the one that will need to be expanded.
2024-07-13 13:11:10 +00:00
2024-07-13 13:34:01 +00:00
* Copy the formet old data to the new disk and expand the root partition of the said disk
2021-11-13 17:49:14 +00:00
2024-07-13 13:34:01 +00:00
> This command is cabable of expanding different kinds of filesystems, including `ext4` and `btrfs`
{.is-info}
2021-11-13 17:49:14 +00:00
```
2024-07-13 13:34:01 +00:00
# virt-resize --expand /dev/vda3 guest.img guest_20G.img
2022-01-25 14:07:20 +00:00
```
2021-11-13 17:49:14 +00:00
2024-07-13 13:34:01 +00:00
* Review the changes
2022-01-25 14:07:20 +00:00
```
2024-07-13 13:34:01 +00:00
[ 0.0] Examining guest_20G.img
2021-11-13 17:49:14 +00:00
**********
Summary of changes:
/dev/vda1: This partition will be left alone.
/dev/vda2: This partition will be left alone.
2024-07-13 13:34:01 +00:00
/dev/vda3: This partition will be resized from 10G to 20G. The
2021-11-13 17:49:14 +00:00
filesystem ext4 on /dev/vda3 will be expanded using the ‘ resize2fs’
method.
**********
2024-07-13 13:34:01 +00:00
[ 2.1] Setting up initial partition table on guest_20G.img
2021-11-13 17:49:14 +00:00
[ 12.9] Copying /dev/vda1
[ 13.1] Copying /dev/vda2
[ 13.4] Copying /dev/vda3
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 38.3] Expanding /dev/vda3 using the ‘ resize2fs’ method
Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
```
2024-07-13 13:34:01 +00:00
* Switch to the new disk for your virtual machine
Now that the new disk has been created, it can be used in the virtual machine.
```
# virsh edit guest
```
Locate the source line for the existing disk `guest.img` :
```
[...]
< disk type = 'file' device = 'disk' >
< driver name = 'qemu' type = 'raw' cache = 'writeback' discard = 'unmap' / >
< source file = '/var/lib/libvirt/images/guest.img' / >
< target dev = 'vda' bus = 'virtio' / >
< address type = 'pci' domain = '0x0000' bus = '0x04' slot = '0x00' function = '0x0' / >
< / disk >
[...]
```
Edit the said line so that it points to the new disk `guest-20G.img` :
```
[...]
< disk type = 'file' device = 'disk' >
< driver name = 'qemu' type = 'raw' cache = 'writeback' discard = 'unmap' / >
< source file = '/var/lib/libvirt/images/guest-20G.img' / >
< target dev = 'vda' bus = 'virtio' / >
< address type = 'pci' domain = '0x0000' bus = '0x04' slot = '0x00' function = '0x0' / >
< / disk >
[...]
```
2021-11-13 17:49:14 +00:00
2024-07-13 13:34:01 +00:00
Start the virtual machine and ensure that it is working properly. If it does, the former disk could be removed.
2022-01-25 14:07:20 +00:00
## Resources
As per the software description : "*qemu-img allows you to create, convert and modify images offline. It can handle all image formats supported by QEMU.*"
2024-07-13 13:34:01 +00:00
* Installation
2022-01-25 14:07:20 +00:00
On Fedora-related distributions, `virt-resize` is provided by the `guestfs-tools` package :
```
# dnf install guestfs-tools
```
2022-01-25 14:42:56 +00:00
2022-01-31 13:20:53 +00:00
---
2023-05-27 19:34:15 +00:00
*[**Go to parent page**](https://wiki.phyllo.me/)*