How to expand the root filesystem of a 12.04 Ubuntu running inside VMWare Player

Yesterday, I ran into the problem that I was missing enough disk space to upgrade my virtualised Ubuntu from 11.10 to 12.04. At least ‘do-release-upgrade’ told me so prior to having 100% disk full errors.

As I do not know of any magic tricks to cheat ‘do-release-upgrade’ I decided to expand the root file system.

For this article I have used a fresh installation/setup of Ubunutu 12.04 to show the steps required. Although VMWare Player recommends 20GB when installing Ubuntu 12.04, I have reduced this to 10GB. The VMWare Player version used for this demonstration was 4.0.2 build-591240.

For all following steps, Terminal (xterm) is required,

Checking the filesystem setup:

cruz@ubuntu:~$ sudo bash
[sudo] password for cruz: 
root@ubuntu:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       9.0G  2.7G  5.9G  32% /
udev            488M  4.0K  488M   1% /dev
tmpfs           199M  800K  198M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            497M   76K  496M   1% /run/shm
root@ubuntu:~# 

Checking the partition layout:

root@ubuntu:~# fdisk -l /dev/sda

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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: 0x00001dec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    18874367     9436160   83  Linux
/dev/sda2        18876414    20969471     1046529    5  Extended
/dev/sda5        18876416    20969471     1046528   82  Linux swap / Solaris
root@ubuntu:~# 

Make a note of how many blocks the Linux swap is using, in this case it is 1046528. If the Linux swap had been on its own device (/dev/sdb for example) things would have been a lot easier as we would not need to worry about the swap partition at all and could just extend /dev/sda. In this example, the Linux swap is also located in /dev/sda and thus also requires relocation.

At first, shut down the Linux from within

root@ubuntu:~# shutdown -h now

Then expand the hardisk via “Virtual Machine -> Virtual Machine Settings -> Harddisk(SCSI) -> Utilities -> Expand”. For this example, the maximum disk size was set to 15GB. If this step completes successfully, start the virtual machine.

For new partitions to be created/setup up, all old partitions have to be deleted.
To be able to delete the swap partition the swap has to be turned off:

cruz@ubuntu:~$ sudo bash
[sudo] password for cruz: 
root@ubuntu:~# free -m
             total       used       free     shared    buffers     cached
Mem:           992        924         67          0         43        426
-/+ buffers/cache:        454        537
Swap:         1021          0       1021
root@ubuntu:~# swapoff -a
root@ubuntu:~# free -m
             total       used       free     shared    buffers     cached
Mem:           992        924         67          0         43        426
-/+ buffers/cache:        454        537
Swap:            0          0          0
root@ubuntu:~# 

The next step involves deleting both /dev/sda1 and /dev/sda2. It is very important to make a note of the start of the partition, in this example 2048!

root@ubuntu:~# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 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: 0x00001dec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    18874367     9436160   83  Linux
/dev/sda2        18876414    20969471     1046529    5  Extended
/dev/sda5        18876416    20969471     1046528   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): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 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: 0x00001dec

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 

Do not quit fdisk just yet! New partitions have to be created.

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): 1
First sector (2048-31457279, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): 30410751

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 
Using default value 2
First sector (30410752-31457279, default 30410752): 
Using default value 30410752
Last sector, +sectors or +size{K,M,G} (30410752-31457279, default 31457279): 
Using default value 31457279

You might notice, the end of the first primary partition is not 31457279 but 30410751. This number was calculated by subtracting the amount of blocks used by the swap from the block count of the new disk (in our example: 31457279-1046528 = 30410751).

The primary partition is now correct but the swap partition is not. The type of the partition should not be “83” but “82” (marking it as a partition for the Linux swap)

Command (m for help): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders, total 31457280 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: 0x00001dec

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    30410751    15204352   83  Linux
/dev/sda2        30410752    31457279      523264   83  Linux

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 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.
root@ubuntu:~# 

Reboot the virtual machine from within:

root@ubuntu:~# shutdown -r now

The swap partition will be mounted using an UUID identifier. After creating a new partition for swap the UUID will not match and upon reboot, no swap will be available. At this point two possibilites are available: Either enter the new UUID into the /etc/fstab or just “slap” the old UUID onto the new partition. I chose the latter.

For some odd/strange reason, Ubuntu actually finds the swap after reboot, although the UUID does not (yet) match. Testing this on a plain Debian system did not have the same results. My wild guess is that it did somehow find the “chunk” of swap space within the new /dev/sda1 and used that. As this could not have been used by data anyway, no damage should occur but in any case setting up swap properly is the way to go.

The awk command is to show the UUID which is used for mkswap.

The dd is just to make sure that there is no data (usable by the kernel) at the beginning of the partition.

cruz@ubuntu:~$ sudo bash
[sudo] password for cruz: 
root@ubuntu:~#  awk '/swap/ { print $1 }' /etc/fstab
#
UUID=8bb62351-4436-47df-92fe-af2865f03461
root@ubuntu:~# swapoff -a
root@ubuntu:~# free -m
             total       used       free     shared    buffers     cached
Mem:           992        695        296          0         23        325
-/+ buffers/cache:        346        645
Swap:            0          0          0
root@ubuntu:~# dd if=/dev/zero of=/dev/sda2
dd: writing to `/dev/sda2': No space left on device
1046529+0 records in
1046528+0 records out
535822336 bytes (536 MB) copied, 11.9388 s, 44.9 MB/s
root@ubuntu:~# mkswap -U 8bb62351-4436-47df-92fe-af2865f03461 /dev/sda2
Setting up swapspace version 1, size = 523260 KiB
no label, UUID=8bb62351-4436-47df-92fe-af2865f03461
root@ubuntu:~# swapon -a
root@ubuntu:~# free -m
             total       used       free     shared    buffers     cached
Mem:           992        693        298          0         23        325
-/+ buffers/cache:        345        646
Swap:          510          7        503
root@ubuntu:~# 

The final and easiest step is to resize the filesystem :)

root@ubuntu:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       9.0G  2.8G  5.8G  33% /
udev            488M  4.0K  488M   1% /dev
tmpfs           199M  788K  198M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            497M  200K  496M   1% /run/shm
root@ubuntu:~# resize2fs /dev/sda1
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sda1 to 3801088 (4k) blocks.
The filesystem on /dev/sda1 is now 3801088 blocks long.

root@ubuntu:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        15G  2.8G   11G  21% /
udev            488M  4.0K  488M   1% /dev
tmpfs           199M  788K  198M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            497M  200K  496M   1% /run/shm
root@ubuntu:~# 

Happy upgrading :)

Advertisements

88 thoughts on “How to expand the root filesystem of a 12.04 Ubuntu running inside VMWare Player

  1. This was exactly what I was looking for, and you saved me a lot of time. Thanks a lot for taking the time to write it up…

  2. Hello , I followed these steps on my VM setup which is not fresh. Since, I was bit afraid of my data would be deleted which is presend in the sda1 partition while deleting the partition, so i took backup of my VM and performed above steps. But after making the type of the partition for Linux Swap that was “83″ and changed to “82″ . There is a step to Reboot the virtual machine from within: root@ubuntu:~# shutdown -r now . After doing this ubuntu did not restarted instead i am getting “error: unknown filesystem” and “grub rescue>” prompt is coming. Can anyone help me what should be done for this issue ? Thank You in advace.

    • There is too little information to really help you but I will use my psychic powers :)

      My guess is you are using a different Linux than Ubuntu (Debian maybe).
      The key difference between Debian and Ubuntu regarding the usage of fdisk is that on Ubuntu it will show blocks instead of sectors as default behaviour.

      Example Debian:

      playground:~# fdisk -l /dev/sda
      
      Disk /dev/sda: 2147 MB, 2147483648 bytes
      255 heads, 63 sectors/track, 261 cylinders
      Units = cylinders of 16065 * 512 = 8225280 bytes
      Disk identifier: 0x00000000
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1               1         196     1574338+  83  Linux
      /dev/sda2             197         261      522112+  82  Linux swap / Solaris
      
      playground:~# fdisk -lu /dev/sda
      
      Disk /dev/sda: 2147 MB, 2147483648 bytes
      255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors
      Units = sectors of 1 * 512 = 512 bytes
      Disk identifier: 0x00000000
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1              63     3148739     1574338+  83  Linux
      /dev/sda2         3148740     4192964      522112+  82  Linux swap / Solaris
      

      Do you spot the difference? :)

      On Ubuntu, both outputs are the same:

      root@dumpster(~) # fdisk -l /dev/sda
      
      Disk /dev/sda: 16.1 GB, 16106127360 bytes
      255 heads, 63 sectors/track, 1958 cylinders, total 31457280 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: 0x000a1bb9
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1            2048    30472192    15235072+  83  Linux
      /dev/sda2        30472193    31457279      492543+  82  Linux swap / Solaris
      
      root@dumpster(~) # fdisk -lu /dev/sda 
      
      Disk /dev/sda: 16.1 GB, 16106127360 bytes
      255 heads, 63 sectors/track, 1958 cylinders, total 31457280 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: 0x000a1bb9
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sda1            2048    30472192    15235072+  83  Linux
      /dev/sda2        30472193    31457279      492543+  82  Linux swap / Solaris
      

      So in order to fix our problem I suggest you boot your VM using a Linux live CD and then using fdisk -u /dev/sda to delete the first partition and recreate it with a different starting block number (Try 2048 if you do not know) and then try to boot off the VM.

      Another approach would be to restore the VM from your backup and use the “-u” parameter for fdisk whenever fdisk is used in the article when following the steps described.

      I hope this helps.

      • Hello, Thank you for your response.
        Sorry for my minimal details, I am using ubuntu only, but the blocks were not displayed in required units.
        So i used “-u” parameter for fdisk whenever fdisk is used in the article when following the steps described and got the proper units and so my calculation was correct this time.
        Additionally, I did below step before reboot, to set the partition for bootable, since i doubted this might also be the issue which i faced.
        root@ubuntu# fdisk -u /dev/sda
        Command (m for help): a
        Partition number (1-4): 1
        Command (m for help): p
        Device Boot Start End Blocks Id System
        24 /dev/sda1 * 2048 30472192 15235072+ 83 Linux
        25 /dev/sda2 30472193 31457279 492543+ 82 Linux swap / Solaris

        After this reboot was successful and i followed the rest of the steps.
        I now succeeded to resize the partition.
        Thank you very much for the steps.

    • Initially did not solve my “no space left” issue allthough df showed only 23% use.
      At boot i got “The disk drive for UUID= is not ready yet”.
      Another reboot seems to have fixed this.

      • Strange. Do you use multiple disks or a simple setup like shown in the blog posting.
        Can you post your /etc/fstab?

  3. Awesome! Thanks very much for this!!! I used this to expand Ubuntu virtual machine for VMware Fusion 5. The VMware help website is awful! Ditto their tech support. And it’s unbelievable that they make it sound like expanding virtual machine is a breeze when it isn’t!!!

    One thing I did different was not using the calculation 31457279-1046528 = 30410751 because your use of 1046528 blocks resulted in smaller size Linux swap / Solaris (new sda2 or old sda5). Instead I kept the size same by using the numbers for “START” rather than “BLOCKS.” I’m not sure how critical this is. Also can I assume that the old sda2 Extended volume is unnecessary?

    • Indeed, the new swap partition is half the size of the old one. Having 512MB swap space on a virtual machine is (IMO) not a problem. You can always increase RAM if you see your machine using the swap space on a regular basis thus also improving overall performance if your machine (and the services run within).

      The old sda2 extended volume is just the way Ubuntu sets the swap up up if you chose to install without LVM.

      If it had done it the other way around (sda1 swap, sda2 /) it would have been a lot easier to extend “/” without
      having to migrate the swap space to a harddisk of its own…

      That is what you get for not using the recommended LVM when installing…

  4. Sweet. Worked for me. My system is Linux Mint 14 in Vmware though, I guess, the main point is that one can change the partition table from within the operating system which also indicates one should be cautious not to render the system useless by wrongly altering the table. I now understand that by altering the partition table, the actual data on the disk won’t get erased.
    By the way, does the swap partition really need to be large. I have 6GB of ram dedicated to the virtual machine and the linux allocated the same amount of swap swap space which I thought is too large. I shrank it to 800MB. Will that be OK in the long run?

    • Be careful, you can wipe your data by altering the partition! The very important part is that you start the new partition on the same spot. If you don’t and create a new file system on that partition, your data is gone…

      Concerning the swap size, if you have 8GB of RAM I would set swap to something like 4GB but that really depends a lot on what the machine is actually doing. If you are using your VM on an ESX(i) you have detailed resource reports available that can help you to pinpoint the sweet spot for your swapsize. Setting it to 800MB sounds a bit low, does ‘free -m’ show any/a lot of swap usage?

  5. Do you know if it is possible to create the VM with a predefined partition structure. i.e. /boot, /swap, / . this would make expanding easier?

    • Yes, you could use to setup an automated installation but you would still have to provide at least two harddisks if you want to avoid having /swap on the first harddisk.

      The easier solution is just to provide a bigger harddisk in the first place so you do not need to expand / :)

  6. Very helpful. Thank you so much for taking the time to record the steps. Out of curiosity why didn’t you rebuild the Extended system device (originally /dev/sda2)?

    • There was no need to rebuild that. The extended partition was coverning the swap:

        Device Boot      Start         End      Blocks   Id  System
      /dev/sda2        18876414    20969471     1046529    5  Extended
      /dev/sda5        18876416    20969471     1046528   82  Linux swap / Solaris
      

      See the start and end block number. As to why Ubuntu sets up the partition layout is beyond my knowledge. Maybe the Ubuntu people can answer/explain that.

  7. Excellent article – thank you. Noting your comments early on in the piece, I took the opportunity to create another Virtual disk (for /dev/sdb) and recreate the swap file there in case I want to enlarge sda1 again in the future. Worked like a charm thanks to your very clear instructions!

  8. Pingback: Ubuntuda Primary Partition Genişlətmə | proqramcı _gündəliyi
  9. I am a Linux AND VMWare newbie, but I needed to expand my file system, so I cloned my Linux machine for safety, crossed my fingers, printed out the article, and went through it, checking each line off with a red pen as I typed it in… and it worked! Holy cow, it totally worked! Mahalo, brah!

  10. Pingback: LUKKY.US | Extend Volume on Amazon EC2 Instance
    • Thank you for your positive feedback! It is nice to know the article is even useful for users new to Linux!

  11. Pingback: TraccStack » Blog Archive » Resizing a root partition under VMware
    • I’m just not sure exactly what you’re supposed to enter in the initramfs, since it won’t boot.

      • What error message do you see on the screen?
        Do you see a grub error message or does it not boot at all?

        • Originally, it was something like:
          /init: line 352: can’t open /root/dev/console: no such file
          Kernel panic – not syncing: Attempted to kill init!

          in which it let me enter commands, and typing “exit” made it go to some new error and freeze (don’t remember error, because now I can’t get to it)

          Now after trying to reboot ubuntu, I get: error: unknown file system.
          grub rescue>

          • At least grub starts up. But I would make an educated guess that you did not put the start of the partition /dev/sda1 as it was before. Please see the comments about different versions of Linux showing different starting points for the first partition (sectors versus blocks).

            In any case, you need to boot up a Linux LiveCD and use the fdisk from there to edit the /dev/sda1 partition from there. Do not worry about the swap partition, you can fix that later once you get the system up and running again.

    • You can set the old UUID onto the a partition by using tunefs. Example here is /dev/sda1

      blkid /dev/sda1
      grep sda1 /etc/fstab
      # they should not differ, but if they do
      sudo tune2fs /dev/sda1 -U THE-OLD-UUID-STRING-IN-ETC-FSTAB

  12. This is brilliant. Thank you so much for writing this, and I can confirm it works on Ubuntu 12.04 running in VirtualBox as well.

  13. Dude you rock! I tired several other sites for help with this and none worked. I just followed your steps and it worked without any issues.

  14. It worked AGAIN! I tried this in Nov 2013, and had to do it again with a different virtual Linux machine. I had the hardcopy of your instructions from the last time, typed in the URL that was at the footer of the pages, and the same set of instructions came up again. I checked them for changes, got out my red pen, and chugged on through it again (still not knowing what my commands totally meant most of the time). And it worked just as well this time!

    By the way, one of the things I used to do as part of my work was writing spacecraft operating procedures. Your procedure here met all the standards of thoroughness and clarity that I would require from my team: generous use of screen shots, clear, concise notes, etc. Well done!

  15. This was awesome!
    All this steps was followed and it worked as a charm! Dude, thank you so much for taking your time to share this steps. I sort of have an idea what just happened but it worked exactly the way I was hopping it would.

    This steps was done on environment:
    Host: ESXi 5.1
    VM: Ubuntu 14.04 with a single HDD.
    VM was build as a gen template. HDD needed to be resized and VM tailored to do other things…
    Original VM HDD was 4GB, after all steps followed, Ubuntu Linux volume got expanded to 14GB.

    Could you please explain a bit why did you need to look at the free memory using:
    root@ubuntu:~# free -m

    Other then that, this is THE BEST guide I was able to find on the net… clear, down to the point, easy to follow. I can’t say this enough, for some one like me who is not that comfortable with Linux CLI and partitioning schema, this guide was a huge time saver.
    Thank you so very much.

    Please share more of your experience if you could on other linux/vmWare projects if you have a time and chance….
    Thank you once again.

    • I used free to see how big the swap in megabytes is. There is more than one way to do this (using ‘swapon -s’ or ‘cat /proc/swaps’) but for me, using ‘free -m’ was the easiest.

      Nice feedback and I am really glad the article was able to help you even not being comfortable with the CLI on Linux.

      The key to a successful Linux/ESX environment is basically using a very good and stable provisioning system so you can easily and quickly(!) create/destroy virtual machines, set up DHCP and depending on the services you run and try to separate your services/data from the OS platform.

  16. This is one of the best tutorial I’ve seen, and I’ve seen many. It works perfectly on ubuntu 14.04 LTS.
    Thank you!

  17. Thank you very much, excellent tutorial. Just expanded my Ubuntu 14.04.1 VM without using any Ubuntu LiveCD (as suggested by most of the tutorials).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s