Ubununtu Expansion of Disk Space under VMware

Keywords: Ubuntu Linux Vmware

Recently, due to too much software installed under the virtual machine, the problem of insufficient disk space pops up from time to time. Looked up a lot of information, there is no good solution.

Friends send a link, or foreigners have conscience. The translation is put here and operated according to my actual needs, which achieves the desired purpose.

Just in case, the virtual machine is backed up before operation.

The default 20G disk size is also used because there is no partition on the installation itself. It turned out to be a tragedy.

Use the df-h command to see the specific usage

  1. @ubuntu:~$ df -h  
  2. Filesystem      Size  Used Avail Use% Mounted on  
  3. /dev/sda1        19G   17G 1020M  95% /  
  4. none            4.0K     0  4.0K   0% /sys/fs/cgroup  
  5. udev            745M  4.0K  745M   1% /dev  
  6. tmpfs           151M  1.3M  150M   1% /run  
  7. none            5.0M     0  5.0M   0% /run/lock  
  8. none            755M  220K  755M   1% /run/shm  
  9. none            100M   48K  100M   1% /run/user  
View with root permission fdisk-l

In the Virtual Machine Settings - > Hard Disk - > Extension Options, select the capacity that needs to be expanded, here I am expanding from 20G to 40G.

When the setup is completed, it is still unusable, but in the extended partition.

  1. Disk /dev/sda: 42.9 GB, 42949672960 bytes  
  2. 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors  
  3. Units = sectors of 1 * 512 = 512 bytes  
  4. Sector size (logical/physical): 512 bytes / 512 bytes  
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  6. Disk identifier: 0x000ea50f  
  7.   
  8.    Device Boot      Start         End      Blocks   Id  System  
  9. /dev/sda1   *        2048    39845887    19921920   83  Linux  
  10. /dev/sda2        39847934    41940991     1046529    5  Extended  
  11. /dev/sda5        39847936    41940991     1046528   82  Linux swap / Solaris  
The specific idea is to delete the partition first, and then re-partition. Note that the information viewed here using fdisk-l is suggested to be recorded and used later when partitioning again.

Use the free -m command to view

  1. bay@ubuntu:~$ free -m  
  2.              total       used       free     shared    buffers     cached  
  3. Mem:          1508       1065        443          5         74        419  
  4. -/+ buffers/cache:        572        936  
  5. Swap:         1021          0       1021  
We can see the size of the swap partition. First, turn off the swap partition and use the command.

  1. root@ubuntu:# swapoff -a  
  2. root@ubuntu:# free -m  
  3.              total       used       free     shared    buffers     cached  
  4. Mem:          1508       1075        433          5         73        419  
  5. -/+ buffers/cache:        581        926  
  6. Swap:            0          0          0  
At this point, you can see that swap's partition size is zero.

The next step is to delete / dev/sda1 and / dev/sda2. Delete partitions using fdisk/dev/sda

  1. root@ubuntu:# fdisk /dev/sda  
  2.   
  3. Command (m for help): p  
  4.   
  5. Disk /dev/sda: 42.9 GB, 42949672960 bytes  
  6. 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors  
  7. Units = sectors of 1 * 512 = 512 bytes  
  8. Sector size (logical/physical): 512 bytes / 512 bytes  
  9. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  10. Disk identifier: 0x000ea50f  
  11.   
  12.    Device Boot      Start         End      Blocks   Id  System  
  13. /dev/sda1   *        2048    39845887    19921920   83  Linux  
  14. /dev/sda2        39847934    41940991     1046529    5  Extended  
  15. /dev/sda5        39847936    41940991     1046528   82  Linux swap / Solaris  
  16.   
  17. Command (m for help): d  
  18. Partition number (1-5): 1  
  19.   
  20. Command (m for help): d  
  21. Partition number (1-5): 2  
  22.   
  23. Command (m for help): p  
  24.   
  25. Disk /dev/sda: 42.9 GB, 42949672960 bytes  
  26. 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors  
  27. Units = sectors of 1 * 512 = 512 bytes  
  28. Sector size (logical/physical): 512 bytes / 512 bytes  
  29. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  30. Disk identifier: 0x000ea50f  
  31.   
  32.    Device Boot      Start         End      Blocks   Id  System  
After you delete the old partition, you begin to create a new partition. Note that this is the time to put the previously saved information to use.

  1. Command (m for help): n  
  2. Partition type:  
  3.    p   primary (0 primary, 0 extended, 4 free)  
  4.    e   extended  
  5. Select (default p): p  
  6. Partition number (1-4, default 1): 1  
  7. First sector (2048-83886079, default 2048):   
  8. Using default value 2048  
  9. Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): 82869552  
Note that this is a time when some people may wonder how to set this size.

This size is the total size minus Swap size: 83886080 - 1046528 = 82839552. Here 1046528 is the size of Swap.

Then the partition is finished:

  1. Command (m for help): n  
  2. Partition type:  
  3.    p   primary (1 primary, 0 extended, 3 free)  
  4.    e   extended  
  5. Select (default p): p  
  6. Partition number (1-4, default 2): 2  
  7. First sector (82839553-83886079, default 82839553):   
  8. Using default value 82839553  
  9. Last sector, +sectors or +size{K,M,G} (82839553-83886079, default 83886079):    
  10. Using default value 83886079  
  11.   
  12. Command (m for help): p  
  13.   
  14. Disk /dev/sda: 42.9 GB, 42949672960 bytes  
  15. 255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectors  
  16. Units = sectors of 1 * 512 = 512 bytes  
  17. Sector size (logical/physical): 512 bytes / 512 bytes  
  18. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  19. Disk identifier: 0x000ea50f  
  20.   
  21.    Device Boot      Start         End      Blocks   Id  System  
  22. /dev/sda1            2048    82839552    41418752+  83  Linux  
  23. /dev/sda2        82839553    83886079      523263+  83  Linux  
At this point, the partition size is correct, but the ID is the same value. Here we change the ID of the swap partition to 82.

  1. Command (m for help): t  
  2. Partition number (1-4): 2  
  3. Hex code (type L to list codes): 82  
  4. Changed system type of partition 2 to 82 (Linux swap / Solaris)  
Save with w

  1. Command (m for help): w  
  2. The partition table has been altered!  
  3.   
  4. Calling ioctl() to re-read partition table.  
  5.   
  6. WARNING: Re-reading the partition table failed with error 16: Device or resource busy.  
  7. The kernel still uses the old table. The new table will be used at  
  8. the next reboot or after you run partprobe(8) or kpartx(8)  
  9. Syncing disks.  
Restart the virtual machine.

The swap partition is mounted using UUID. After creating a new partition, the UUID will not match and no swap partition will be available after reboot. There are two ways to deal with it.

1) Use the new UUID in / etc/fstab

2) Use the old UUID on the new partition. The second method is used here.

View UUID:

  1. root@ubuntu:/home/bay# awk '/swap/ { print $1 }' /etc/fstab  
  2. #  
  3. UUID=990ae69c-2bbe-425f-bb8f-91200f70c9b9  
  1. root@ubuntu:# swapoff -a  
  2. root@ubuntu:# free -m  
  3.              total       used       free     shared    buffers     cached  
  4. Mem:          1508       1171        337          6        191        424  
  5. -/+ buffers/cache:        555        953  
  6. Swap:            0          0          0  
  7. root@ubuntu:# dd if=/dev/zero of=/dev/sda2  
  8. dd: writing to '/dev/sda2': No space left on device  
  9. 1046528+0 records in  
  10. 1046527+0 records out  
  11. 535821824 bytes (536 MB) copied, 1.89975 s, 282 MB/s  
  12. root@ubuntu:# mkswap -U 990ae69c-2bbe-425f-bb8f-91200f70c9b9 /dev/sda2  
  13. Setting up swapspace version 1, size = 523256 KiB  
  14. no label, UUID=990ae69c-2bbe-425f-bb8f-91200f70c9b9  
  15. root@ubuntu:# swapon -a  
  16. root@ubuntu:# free -m  
  17.              total       used       free     shared    buffers     cached  
  18. Mem:          1508        717        790          6          6        164  
  19. -/+ buffers/cache:        546        961  
  20. Swap:          510          0        510  
  21. root@ubuntu:# df -h  
  22. Filesystem      Size  Used Avail Use% Mounted on  
  23. /dev/sda1        19G   17G 1023M  95% /  
  24. none            4.0K     0  4.0K   0% /sys/fs/cgroup  
  25. udev            745M   12K  745M   1% /dev  
  26. tmpfs           151M  1.3M  150M   1% /run  
  27. none            5.0M     0  5.0M   0% /run/lock  
  28. none            755M  152K  755M   1% /run/shm  
  29. none            100M   48K  100M   1% /run/user  
  30. root@ubuntu:# resize2fs /dev/sda1  
  31. resize2fs 1.42.9 (4-Feb-2014)  
  32. Filesystem at /dev/sda1 is mounted on /; on-line resizing required  
  33. old_desc_blocks = 2new_desc_blocks = 3  
  34. The filesystem on /dev/sda1 is now 10354688 blocks long.  
  35.   
  36. root@ubuntu:# df -h  
  37. Filesystem      Size  Used Avail Use% Mounted on  
  38. /dev/sda1        39G   17G   21G  45% /  
  39. none            4.0K     0  4.0K   0% /sys/fs/cgroup  
  40. udev            745M   12K  745M   1% /dev  
  41. tmpfs           151M  1.3M  150M   1% /run  
  42. none            5.0M     0  5.0M   0% /run/lock  
  43. none            755M  152K  755M   1% /run/shm  
  44. none            100M   48K  100M   1% /run/user  

The dd command simply guarantees that there is no data at the beginning of the partition.

Link: https://hexeract.wordpress.com/2012/04/30/how-to-expand-the-root-filesystem-of-a-11-10-ubuntu-running-in-vmware-player/

Posted by CowGuy on Sun, 07 Apr 2019 21:57:31 -0700