Extending the root directory partition of ubuntu virtual machine under VMware

Keywords: Ubuntu sudo Linux Vmware

Links to the original text: http://t.cn/RzCP9E2
Translation source links: http://imcczy.com/how-to-expand-the-root-filesystem-in-vmware.html

Reproduction Notes:

Below this article is the operation of requiring partition. The operation of partition is different from that of normal system partition. It is necessary to specify the starting point of sectors manually. The calculation method is given here. In fact, it is also prompted when the command is issued.
A hint like this
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Units: sectors of 1 * 512 = 512 bytes
sectors are the total sector size of your hard disk's current partition
You can first calculate the size of the partition you intend to allocate, for example, the root partition needs 14G swap and 1G
1024*14=14336bytes(1.4mb) *1024*1024 = 15032385536bytes(14G)
15032385536/512 = 29360128 sectors concluded that 14G is from the beginning to the end of this sector.

Age is long, try cautiously

Virtual machines are not enough to use space. An article by google tried it out and it was useful. With the author's consent, it was translated. Some irrelevant things have been deleted.

Note:

When the author enters the command, he uses "sudo bash" and "sudo" or "su".

Renote:

When it comes to deleting disk partition tables, it's better to back up the whole virtual machine first!

Check the file system:

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:~# 

Check the disk partition table:

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:~# 

Remember the swap partition size shown above (the number of Blocks), here is 1046528. If the swap partition and root directory are not on a disk (such as / dev/sdb), don't remember. In this article, swap partitions are in / dev/sda and need to be re-partitioned.

Close linux first:

root@ubuntu:~# shutdown -h now

Select extensions under virtual machine settings, hard disks, and utilities. Expand the maximum disk size of the virtual machine. Set the disk to 15G and restart.

To reset the partition table, delete all old partitions and shut down the swap of the system:

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 steps will delete / dev/sda1 and / dev/sda2. Be sure to remember the starting position of the partition table. Here is 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): 

Don't quit fdisk, then create a new partition

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
//Note that remember to create swap partitions, size is not wrong, here is1046528(31457279-30410571=1046528),That's to leave a part. block Used to create swap. modify sda2 The partition type is82,That is, swap partition.

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:~# 

Restart the virtual machine:

root@ubuntu:~# shutdown -r now

Exchange partition mounting requires a UUID identifier. Creating a new swap partition does not match the old UUID, resulting in a reboot that cannot mount swap partitions. There are two main solutions: write a new UUID in / etc/fstab, or directly overlay the old UUID on the new partition, and choose the latter. The awk command is used to display the old UUID, and the dd command ensures that the partition has no data.

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:~#

Finally, adjust the partition size:

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:~# 

Posted by kwilameiya on Mon, 25 Mar 2019 09:18:28 -0700