I recently ran out of disk space on a server running hyper-V to capture data (Ubuntu 16.04). I didn’t put data into a separate partition, so I had to make the root directory bigger. I have never dealt with such a problem before. I searched a lot of information online. Now I record and share the development method. You are advised to back up data before performing operations

Step overview

  1. Example Expand the physical drive
  2. Determine the disk to be operated
  3. Extended Physical extended partition where the LVM logical partition resides
  4. Added LVM logical partitions
  5. The new partition is merged into the corresponding Volumn Group
  6. Updating the file system

Example Expand the physical drive

Shut down the VM and perform operations in VM management.

Determine the disk to be operated

Check the disk usage and run the command

root@vm003:~# df -h
Filesystem               Size  Used Avail Use% Mounted on
udev                     3.9G     0  3.9G   0% /dev
tmpfs                    798M  8.6M  789M   2% /run
/dev/mapper/Ubuntu-root   94G   88G  1.9G  98% /
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    5.0M     0  5.0M   0% /run/lock
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1                472M  382M   66M  86% /boot
tmpfs                    100K     0  100K   0% /run/lxcfs/controllers
tmpfs                    798M     0  798M   0% /run/user/0
Copy the code

Although we have adjusted the physical disk to 300GB, the root directory is still 100GB, 98% used

Run the command

root@vm003:~# fdisk -l
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048    999423    997376  487M 83 Linux
/dev/sda2       1001470 209713151 208711682 99.5G  5 Extended
/dev/sda5       1001472 209713151 208711680 99.5G 8e Linux LVM

Partition 2 does not start on physical sector boundary.


Disk /dev/mapper/Ubuntu-root: 95.5 GiB, 102563315712 bytes, 200318976 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/Ubuntu-swap_1: 4 GiB, 4294967296 bytes, 8388608 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Copy the code

You can see that /dev/sda has indeed been adjusted to 300GiB, but the system has not used it yet. We also know that we need to do /dev/sda

Expand the physical partition where the LVM logical partition resides

Run the command

root@vm003:~# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print # create partitionModel: Msft Virtual Disk (scsi) Disk /dev/sda: 322GB Sector size (logical/physical): 512B/4096B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 512MB 511MB primary ext2 boot 2 513MB 107GB 107GB extended 5 513MB  107GB 107GB logical lvm (parted) resizepart 2Resize the Sda2 partitionEnd? [107GB]? 0# Direct fill
(parted) print # Check againModel: Msft Virtual Disk (scsi) Disk /dev/sda: 322GB Sector size (logical/physical): 512B/4096B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 512MB 511MB primary ext2 boot 2 513MB 322GB 322GB extended 5 513MB  107GB 107GB logical lvm (parted) q# Complete exit
Information: You may need to update /etc/fstab.
Copy the code

Now we have extended /dev/sda2

Added LVM logical partitions

Run the command

root@vm003:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p Check the current partition status
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device     Boot   Start       End   Sectors   Size Id Type
/dev/sda1  *       2048    999423    997376   487M 83 Linux
/dev/sda2       1001470 629145599 628144130 299.5G  5 Extended
/dev/sda5       1001472 209713151 208711680  99.5G 8e Linux LVM

Partition 2 does not start on physical sector boundary.

Command (m for help): n Select a logical partition and enter the starting and ending points as required. The default value is to fill the entire disk

All space for primary partitions is in use.
Adding logical partition 6
First sector (209715200-629145599, default 209715200):
Last sector, +sectors or +size{K,M,G,T,P} (209715200-629145599, default 629145599):

Created a new partition 6 of type 'Linux' and of size 200 GiB.

Command (m for help): p # check the new partition
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048    999423    997376   487M 83 Linux
/dev/sda2         1001470 629145599 628144130 299.5G  5 Extended
/dev/sda5         1001472 209713151 208711680  99.5G 8e Linux LVM
/dev/sda6       209715200 629145599 419430400   200G 83 Linux

Partition 2 does not start on physical sector boundary.

Command (m for help): t Change the partition type to Linux LVM
Partition number (1,2,5,6, default 6): 6 # sda6
Partition type (type L to list all types): 8e # Id code of type LVM

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p Check the partition again
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa88f1366

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048    999423    997376   487M 83 Linux
/dev/sda2         1001470 629145599 628144130 299.5G  5 Extended
/dev/sda5         1001472 209713151 208711680  99.5G 8e Linux LVM
/dev/sda6       209715200 629145599 419430400   200G 8e Linux LVM

Partition 2 does not start on physical sector boundary.

Command (m for help): wq Make sure there are no problems, save and exit

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).
Copy the code

The new partition is merged into the corresponding Volumn Group

Run the command

root@vm003:~# vgdisplay
  --- Volume group ---
  VG Name               Ubuntu
  System ID
  Format                lvm2
Copy the code

It can be seen that the VG Name we want to operate is Ubuntu. And then run the command

root@vm003:~# vgextend Ubuntu /dev/sda6 # /dev/sda6 is the newly added LVM partition
  Device /dev/sda6 not found (or ignored by filtering).
  Unable to add physical volume '/dev/sda6' to volume group 'Ubuntu'.
Copy the code

The /dev/sda6 device is not found.

root@vm003:~# reboot
Copy the code

Restart and run the command again

root@vm003:~# vgextend Ubuntu /dev/sda6 # /dev/sda6 is the newly added LVM partition
  Physical volume "/dev/sda6" successfully created
  Volume group "Ubuntu" successfully extended
Copy the code

Check the status of Volumn Group again and run the command

root@vm003:~# vgs
  VG     #PV #LV #SN Attr VSize VFreeUbuntu 2 20 wz-- n-299.52G 200.00gCopy the code

It did. Then run

root@vm003:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/Ubuntu/root
  LV Name                root
  VG Name                Ubuntu
Copy the code

We know that the LV Path of Ubuntu VG is /dev/ubuntu/root. Note that. Then run

root@vm003:~# lvresize -l +100%FREE /dev/ubuntu/root # /dev/ubuntu/root
  Size of logical volume Ubuntu/root changed from 95.52 GiB (24453 extents) to 295.52 GiB (75652 extents).
  Logical volume root successfully resized.
Copy the code

There we go.

Warning: If the following message is displayed during the operation, the total size of the logic volumn is not correct. Resize does not increase the space, but decreases the space. If the operation continues, the data will be lost. Stop immediately! Press n to cancel.

WARNING: Reducing active and open logical volume to 32.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce root? [y/n]*

Updating the file system

Finally, run the command

root@vm003:~# resize2fs -p /dev/mapper/ubuntu-root # /dev/mapper/ubuntu-rootFilesystem at /dev/mapper/Ubuntu-root is mounted on /; on-line resizing required old_desc_blocks = 6, new_desc_blocks = 19 The filesystem on /dev/mapper/Ubuntu-root is now 77467648 (4k) blocks long.Copy the code

This process may take a few minutes, so just be patient. Then run the df command to view disk usage

root@vm003:~# df -h
Filesystem               Size  Used Avail Use% Mounted on
udev                     3.9G     0  3.9G   0% /dev
tmpfs                    798M  8.6M  789M   2% /run
/dev/mapper/Ubuntu-root  291G   88G  191G  32% /
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    5.0M     0  5.0M   0% /run/lock
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1                472M  382M   66M  86% /boot
tmpfs                    100K     0  100K   0% /run/lxcfs/controllers
tmpfs                    798M     0  798M   0% /run/user/0
Copy the code

And you’re done!

reference

http://luqitao.github.io/2015/12/08/Resize-Image-Rootpartition/