A detailed method for dynamic on line expansion of root root partition size

  • 2020-09-28 09:18:37
  • OfStack

preface

This article mainly introduces the dynamic online expansion of root root partition size related content, Shared for your reference and learning, the following words are not all said, to see a detailed introduction.


qemu-img resize yourname.img +10G 

First, increase the partition size with the command above for ES7en-ES8en

LVM

Scenario: The virtual machine's disk, image, has been expanded, or the disk capacity has been increased for the physical machine. Then we want to use the increased capacity without impacting the existing file system (non-grid).

In the actual use of the process, we sometimes need to virtual machine image of the hard disk capacity, for example, 1 we started to create the virtual machine, thinking that 20G disk space is enough, but one time we may have to copy a 10G+ file into the virtual machine, at this time we are silly.

With VMware or VirtualBox's graphical interface or 1 command, we can easily increase the disk size of the virtual machine, but when the disk gets bigger, the system doesn't take advantage of it. So at this point, we need to think about how to make this extra space available to the Linux system in the virtual machine.

Before we do that, let's add a word about disk MBR:

[

The hard disk is divided into two regions, the MBR (primary boot partition) and the data region.

Two important pieces of information are recorded in MBR: the boot program and the disk partition table.

The partition table defines "n disk block is from x cylinder to y cylinder", so every time the system takes n block, only the data between x and y sectors will be read.

Due to the limited capacity of MBR, only four partition records were designed at the time of design. It can be used as four primary partitions, or three primary partitions and one extended partition.

If there are more than four partitions, the system allows to place one more disk partition information in the extra disk space, which is called an extended partition. When the disk is divided into one extended partition, the information of the extended partition in the MBR disk partition table is the location of the other partition table. Therefore, in the extended partition also has to partition logical partition to use.

A maximum of four primary partitions are allowed per hard disk, and the other partitions can only be placed in extended partitions.

]

This makes sense, since the number of primary partitions is limited and the capacity we want to increase is only used for storage, we can add it to the extended partition (extended). (If you are a tuhao and you have 4 primary partitions, and you plan to use one more primary this time, you can skip Part1 and go straight to Part2.)

PART1

We want to add the increased capacity to the extended. To expand the extended partition, this fdisk cannot be used. You need the parted command (if the system does not come with parted, then one is installed from the source) :


parted /dev/xxx

Enter interactive mode and view the help command with help.

1. Some special instructions:

print views the partition table. Pay attention to the partition to be operated on, 'Number', which will be used later. unit changes the default unit used to describe the size of parted (for example 'compact' is' MB').
It is worth noting that if you use units such as MB/GB, the selection of disk sector will be inaccurate. parted will pick the nearest sector for you, but it may not be accurate. For example, if unit is MB, an error of + -500ES89en may occur. If it is GB, then it is possible to have a +-500MB error, which is intolerable. So if you're doing something like 'partition creation', it's recommended to use units like 'MiB' instead of' MB'. 'MiB' will be an exact value, and parted will not look for the nearest cell as it would for 'MB'. resize < minor > < start > < end > From start to end for a partition specifying minor (or Number), where start/end can be xxxMB or negative to indicate how far forward from the end of the disk, such as -0 to the end of the disk.

More order details, please refer to: http: / / www gnu. org software/parted manual/html_chapter/parted_toc html

Practical:

Before operation, the print results are as follows. The existing disks, 62.3G, are allocated to extended 8G only, and more than 50 G are not allocated at all.


Number Start End  Size Type  File system Flags
1  1049kB 256MB 255MB primary ext2   boot
2  257MB 8589MB 8332MB extended
5  257MB 8589MB 8332MB logical    lvm

I hope to use all the more than 50 G to expand extended.

Use the command:


resize 2 257MB -0

In fact, just type in resize 2, enter, and parted will interactively let you fill in the remaining two parameters. -0 represents the end of the disk for that partition.

Now let's look at print again,


Number Start End  Size Type  File system Flags
1  1049kB 256MB 255MB primary ext2   boot
2  257MB 62.3GB 62.0GB extended
5  257MB 8589MB 8332MB logical    lvm

The extended area has been expanded successfully.

The extended block is just "one physical hard disk", and to use the extra space, you need to expand the lv (Number 5).

The expansion of logic volumn depends on the size of the volumn group where it is located. Since logic volumn is derived from volumn group, if volumn group does not change in size, then logic volumn cannot exceed volumn group. So you really have to add space to volumn group.

PART2

To increase the size of volumn group, first use fdisk on extended to create a new partition using the disk space that has just been added but has not yet been allocated. through fdisk <disk_dev_name> In interactive mode, you can view help with the command m. First, type n to create a new partition, then select l to set the new partition as a logical partition, and then set the start and end locations of the partitions (by default, it takes all the space left on the disk, so the default is fine). The partition that is created is numbered 6. You can use the command p to see 1.


 Device Boot  Start   End  Blocks Id System
/dev/vda1 *  2048  499711  248832 83 Linux
/dev/vda2   501758 121634815 60566529 5 Extended
/dev/vda5   501760 16775167  8136704 8e Linux LVM
/dev/vda6  16777216 121634815 52428800 83 Linux

Next, since we are using LVM to manage this new partition, we need to change the management system for the new partition from Linux to Linux LVM. In interactive mode, enter the command t, then select the 6 you just created, and enter 8e (the code name for Linux LVM). Finally, we're going to actually write all of this to the hard disk and type the command w.

So here we go fdisk -l You can already see that /dev/vda6 has been created.

To perform


vgextend <your_vg_name> /dev/vda6 

Add the new partition to volumn group (VG Name is available via vgdisplay).

Now use vgs to check the status of volumn group and find that volumn group has become larger.


 VG   #PV #LV #SN Attr VSize VFree
 jiang51-vg 2 2 0 wz--n- 57.75g 50.03g

And then I'm going to make this logic volumn larger.

Command (the last 'Logic Volumn name' can be found via lvdisplay) :


lvresize -l +100%FREE <Logic Volumn name>
[

Warning: If the following warning appears during the operation, it means that the total size of logic volumn is not correct. Instead of increasing the space, resize is shrinking the space. If the operation continues, the data will be lost. Should 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]

]

Finally, you need to update the file system on logic volumn, otherwise you can see from df that the file system is unaware that logic volumn has become larger.

With the command (file_system_name found by df) :


resize2fs -p <file_system_name>

Thus, the expansion of the extended partition is finally complete.

The LVM


parted /dev/xxx
0

Most important step 1: "Delete current partition, repartition" press d to delete current partition 1, note: do not press w to save after deletion! Create a new partition directly according to n, and then start from the original cylinder, 1 to the final size (the default value is two enter, if the previous partition does not start from the first cylinder, you need to record the starting cylinder of the previous partition), after the new partition operation, save according to w.

conclusion


Related articles: