Recently I was working on a Linux VM which was running out of disk space and I wanted to increase the available space. I didn’t want to just add another drive and mount it separately but increase the size of the root partition instead.
Disclaimer: The following instructions can easily screw your data if you make a mistake. I was doing this on a VM which I backed up before performing the following actions. If you loose your data because you didn’t perform a backup don’t come and complain.
The VM I was working on is a stock Ubuntu 12.10 Desktop install.
First: Increase the disk size.
In ESXi this is simple, just increase the size of the virtual disk. Now you have a bigger hard drive but you still need to a) increase the partition size and b) resize the filesystem.
Second: Increase the partition size.
You can use fdisk
to change the partition table while running. The stock Ubuntu install has created 3 partitions: one primary (sda1), one extended (sda2) with a single logical partition (sda5) in it. The extended partition is simply used for swap, so I could easily move it without losing any data.
- Delete the primary partition
- Delete the extended partition
- Create a new primary partition starting at the same sector as the original one just with a bigger size (leave some for swap)
- Create a new extended partition with a logical partition in it to hold the swap space
me@ubuntu:~$ sudo fdisk /dev/sda Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 * 2048 192940031 96468992 83 Linux /dev/sda2 192942078 209713151 8385537 5 Extended /dev/sda5 192942080 209713151 8385536 82 Linux swap / Solaris Command (m for help): d Partition number (1-5): 1 Command (m for help): d Partition number (1-5): 2 Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): Using default value 1 First sector (2048-524287999, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-524287999, default 524287999): 507516925 Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): e Partition number (1-4, default 2): 2 First sector (507516926-524287999, default 507516926): Using default value 507516926 Last sector, +sectors or +size{K,M,G} (507516926-524287999, default 524287999): Using default value 524287999 Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux /dev/sda2 507516926 524287999 8385537 5 Extended Command (m for help): n Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (507518974-524287999, default 507518974): Using default value 507518974 Last sector, +sectors or +size{K,M,G} (507518974-524287999, default 524287999): Using default value 524287999 Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux /dev/sda2 507516926 524287999 8385537 5 Extended /dev/sda5 507518974 524287999 8384513 83 Linux Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 82 Changed system type of partition 5 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux /dev/sda2 507516926 524287999 8385537 5 Extended /dev/sda5 507518974 524287999 8384513 82 Linux swap / Solaris Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. me@ubuntu:~$ sudo reboot
I noticed afterwards that I didn’t set the bootable flag but apparently you don’t really need it.
Third: Enlargen the filesystem.
You can do this with resize2fs online on a mounted partition.
me@ubuntu:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 91G 86G 12M 100% / udev 3.9G 4.0K 3.9G 1% /dev tmpfs 1.6G 696K 1.6G 1% /run none 5.0M 0 5.0M 0% /run/lock none 3.9G 144K 3.9G 1% /run/shm none 100M 16K 100M 1% /run/user me@ubuntu:~$ sudo resize2fs /dev/sda1 resize2fs 1.42.5 (29-Jul-2012) Filesystem at /dev/sda1 is mounted on /; on-line resizing required old_desc_blocks = 6, new_desc_blocks = 16 The filesystem on /dev/sda1 is now 63439359 blocks long. me@ubuntu:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 239G 86G 142G 38% / udev 3.9G 12K 3.9G 1% /dev tmpfs 1.6G 696K 1.6G 1% /run none 5.0M 0 5.0M 0% /run/lock none 3.9G 152K 3.9G 1% /run/shm none 100M 36K 100M 1% /run/user
Slight catch: After rebooting the swap space wasn’t active. Turned out you need to run mkswap
, adjust /etc/fstab
to the new UUID and turn the swap on
me@ubuntu:~$ sudo mkswap /dev/sda5 Setting up swapspace version 1, size = 8384508 KiB no label, UUID=141d401a-b49d-4a96-9b85-c130cb0de40a me@ubuntu:~$ sudo swapon --all --verbose swapon on /dev/sda5 swapon: /dev/sda5: found swap signature: version 1, page-size 4, same byte order swapon: /dev/sda5: pagesize=4096, swapsize=8585740288, devsize=8585741312
Edit /etc/fstab
to replace the UUID for the old swap partition with the new one from mkswap
.
Thank you very much, that perfectly worked for me too!
Worked like a charm! Great info, helped me a lot.
Fred de Goede
Thanks for sharing! This is just what I was looking for!
This spared me a lot of time, its a pain to boot from live disk 🙂
This was incredibly helpful. Thank you!
Thanks a lot, this was really helpful as I only had remote access to a Debian server. Surprisingly most other tutorials claim that a live root resize is impossible while it’s clearly not.
This worked for me as well (VM ubuntu 8.04.4LTS) and the logic behind it should make it sound for pretty much every other flavor. Great work OP
Supercool! Has worked like a charm on Ubuntu 12.04LTS. The only thing I changed was to run partprobe instead of rebooting. I had to resize the filesystem of a host computer with mutliple virtualbox instances running on it, so rebooting would have been a bother. However, doing first a partprobe and then the resize2fs did the job, and the system appears to be stable after the change. Thank you very much for sharing this!
Pingback: Build your own CDH5 QuickStart VM with Spark on CentOS | Denny Lee
Thank you, this was a life saver today!
Worked fine for me on Ubuntu 12.0.4 / Virtual box. Many thanks !
Mil gracias brother, de 10 !
Pingback: Cloning .vdi was Successful However Disk Space Did Not Increase
Excellent procedure. Thanks
Thank you!!. It was like a clock work.
Hi team,mine worked half.This is the case,I followed this wonderful Tuto on my Oracle linux box.And I succeed to increase my logical volume but the RESIZE of the file system is taking forever…Any idea ?.
REF: this is what I did :
First : fdisk /dev/sda and follow the wizard but don’t forget to choose 3 for the partition number
I use Hyper-V to increase the size of my virtual Linux box(oracle linux).I just need to increase my root partition mounted
a- vgextend vg_test /dev/sda3
b- vgdisplay vg_test | grep “Free”
c- lvextend -l +100%FREE /dev/vg_test/lv_root — > to check the free space
d- resize2fs /dev/vg_test/lv_root
Any help would be much appreciated ! thanks.
Thanks for sharing, great help.
Great help, thanks for sharing. It worked for me on a Oracle VirtualBox with Debian 7 Wheezy. I used VBoxManage modifyhd to increase the disk size. However, this command will only work on dynamically allocated storage devices and not on fixed sized virtual hard disks.
Thank you so much of sharing this. Worked great with ESXi 6.0 and Debian 8 Jessie.
Thanks a lot!
Thank you, my Plex server has another 20 GB to grow (not the media storage). Ubuntu Server 12.04 on XenServer 6.2 pool iSCSI booting from a Synology DS412+
Tell me your address, I need to send you a cold beer
What @Bat Venci said. Thanks!
Pingback: Unix:What's the easiest way to resize an ext4 partition from the command line? – Unix Questions
Concise and effective. Thank you!
Pingback: Why You Won’t Get N’s Seamless Updates on Your Current Android Device – Global Blog News Robot Feeds
Thanks for sharing, Very Helpful
Great. Thanks for sharing this solution.
Helped me a lot! Thanks!
This is still saving the bacon 3 years later! We just used this recipe to expand the fs of a live production debian server after practising on a clone yesterday. As a bit of a linux noob, the only bit that caused some head scratching was the last bit where we didn’t do the swapon command last, but tried to do the fstab bit last as we were following the commands in the code box…. we worked it out though 🙂
Thanks for sharing this. It’s actually more of a spell than a recipe 🙂
Pingback: Increase disk space on debian VM filesystem with command line – Convergency
All hail OP!!
Thank you! Just what I needed.
This makes my day and much simpler than the other tuts!
Thanks a lot. it worked for me. But i got another scenario here. There is no space left in my hard disk. So now I want to increase the root partition size by adding a new hard disk. Can somebody please help..
Thanks. Very helpful.
Thanks for that, worked straight away. After spending a few hours finding the correct way 😉
Cheers
Dan
Thank-you for your post, 3rd time using them, never an issue!
Sincerely,
Michael
how to create new partition from unallocated space?
Thank you very much. Its helped me to increase the virtual disk of my Ubuntu Server.
WoW in August 2017 still working like a charm. Thanks for shared.
great, works 100%. Thanks.
Another thanks here! Used this to resize a ubuntu 16.0.4 VM running in bhyve on FreeNAS
Pingback: My Docker journey so far - Adrian's Blog
Very useful! Thanks, man!
Pingback: Resize Virtual Disks - Adrian's Blog
Great!!!
Pingback: How to to resize an ext4 partition from the command line?
Pingback: ext4 Partition unter Linux im Betrieb vergrößern - Janpedia