VMware ubuntu extension of disk space method

  • 2020-06-07 05:42:05
  • OfStack

Recently due to the virtual machine under the installation of too much software, from time to time pop-up disk space problem. I looked up a lot of information, but there was no good solution.

A friend sent a link, or foreigners have a conscience. Translated here, according to my actual requirements for the operation, to achieve the desired purpose.

In case of 10,000, the virtual machine is backed up before operation.

The default disk size of 20G is also used because the installation was not partitioned by itself. It turned out to be a tragedy.

Use the ES10en-ES11en command to see how it works


@ubuntu:~$ df -h 
Filesystem   Size Used Avail Use% Mounted on 
/dev/sda1    19G  17G 1020M 95% / 
none      4.0K   0 4.0K  0% /sys/fs/cgroup 
udev      745M 4.0K 745M  1% /dev 
tmpfs      151M 1.3M 150M  1% /run 
none      5.0M   0 5.0M  0% /run/lock 
none      755M 220K 755M  1% /run/shm 
none      100M  48K 100M  1% /run/user 

Use the root permission fdisk-ES17en to view

In the virtual machine Settings -- > Hard drive - > Extension option, select the capacity you want to extend to, in this case From 20G to 40G.

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


Disk /dev/sda: 42.9 GB, 42949672960 bytes 
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000ea50f 
  Device Boot   Start     End   Blocks  Id System 
/dev/sda1  *    2048  39845887  19921920  83 Linux 
/dev/sda2    39847934  41940991   1046529  5 Extended 
/dev/sda5    39847936  41940991   1046528  82 Linux swap / Solaris 

Specific train of thought is to partition first delete, undertake partition afresh next. Note that the information viewed here using ES29en-ES30en is recommended to be recorded and used later for repartitioning.

View using the ES33en-ES34en command


bay@ubuntu:~$ free -m 
       total    used    free   shared  buffers   cached 
Mem:     1508    1065    443     5     74    419 
-/+ buffers/cache:    572    936 
Swap:     1021     0    1021 

We can see the size of the swap partition, first turn off the swap partition using the command


root@ubuntu:# swapoff -a 
root@ubuntu:# free -m 
       total    used    free   shared  buffers   cached 
Mem:     1508    1075    433     5     73    419 
-/+ buffers/cache:    581    926 
Swap:   0     0     0 

At this point, you can see that the partition size of swap is already 0.

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


root@ubuntu:# fdisk /dev/sda 
Command (m for help): p 
Disk /dev/sda: 42.9 GB, 42949672960 bytes 
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000ea50f 
  Device Boot   Start     End   Blocks  Id System 
/dev/sda1  *    2048  39845887  19921920  83 Linux 
/dev/sda2    39847934  41940991   1046529  5 Extended 
/dev/sda5    39847936  41940991   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: 42.9 GB, 42949672960 bytes 
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000ea50f 
  Device Boot   Start     End   Blocks  Id System 

After deleting the old partition, you can start creating a new partition. Note that this is where the saved information comes in handy.


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

Note that this is a time when someone might be wondering how to set this size.

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

Then do the partitioning:


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): 2 
First sector (82839553-83886079, default 82839553):  
Using default value 82839553 
Last sector, +sectors or +size{K,M,G} (82839553-83886079, default 83886079):  
Using default value 83886079 
Command (m for help): p 
Disk /dev/sda: 42.9 GB, 42949672960 bytes 
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000ea50f 
  Device Boot   Start     End   Blocks  Id System 
/dev/sda1      2048  82839552  41418752+ 83 Linux 
/dev/sda2    82839553  83886079   523263+ 83 Linux 

At this point, the partition size is correct, but ID is still the same value. Here we change ID for the swap partition to 82.


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) 

Save using w


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. 

Restart the virtual machine.

The swap partition is mounted using UUID. After the new partition is created, UUID will not match and there will be no swap partition available after reboot. There are two ways to do this

1) Use the new UUID in /etc/fstab

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

Check the UUID:


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

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


Related articles: