Linux7/centos7 disk partitioning, formatting, and LVM management

Source: Internet
Author: User
Tags uuid

RHEL7 how to partition and format disks and how to configure LVM, unlike previous versions of Rhel, can be managed with disk tools (running in the graphical desktop) or command tools (such as Fdisk, Gdisk, parted). Fdisk can be configured in MBR format, gdisk configuration GPT format, parted can choose their own.
Traditional hard disk partitions are MBR format, MBR partition is located in 0 sectors, he altogether 512 bytes, the first 446 bytes is the Grub boot program, this will learn later, the middle 64 bytes is the partition table, each partition needs 16 bytes, so the primary and extended partitions can only have 4 partitions. More than 4 partitions can only be represented from the extended partition and the logical partition is set. The size of each partition cannot exceed 2T. The last 2 bytes of the MBR are the end symbols
GPT format, breaking the limitations of the MBR, can be set up to 128 partitions, the size of the partition varies depending on the operating system, but all break through the limitations of 2T space. Supports volume sizes up to 18EB (1EB=1024PB,1PB=1024TB), allows for redundancy of primary and backup disk partition tables, and supports unique disk and partition IDs (GUIDs).
Unlike MBR partitioned disks, GPT partition information is in the partition, not the main boot sector, as in MBR. To protect GPT from MBR-class disk management software, GPT establishes a protected partition (protective MBR) MBR partition table in the main boot sector, which is identified as 0xEE, and the size of this protected partition is 128mb,mac OS under Windows Under x 200MB, called GPT Protected Partition in Window Disk Manager, allows MBR class Disk management software to treat GPT as an unknown format partition instead of mistakenly as an unpartitioned disk
In the MBR hard disk, the partition information is stored directly in the master boot Record (MBR) (the master boot record also stores the system's boot program). However, in GPT hard disks, the location information of the partitioned table is stored in the GPT header. For compatibility reasons, however, the first sector of the hard disk is still used as an MBR and then the GPT header.
The structure of the GPT is as follows:

First look at the current drive information

You can view the current partition in the/proc/partitions file.

First try the MBR format partition, the FDISK option is as follows
Enter N to create a new MBR partition, and then enter p to display the current partition state
Repeat Operation N to add additional partitions.
Note: MBR format disks can create up to 4 primary partitions or 3 primary partitions in one extended partition, and several logical partitions can be created in the extended partition.
Note that the ID represents the purpose of the disk and can be changed by T
Look at the partition record.

Gdisk and Fdisk are very similar

You can see 128 partitions when creating a new partition
Parted, more flexible than the first two, can set MBR or GPT format and partition yourself
The Mklabel Msdos can be set to MBR format, which can then be partitioned by Mkpart

Msdos is set to MBR format, GPT is set to GPT format
Primary represents the primary partition, extended represents the extended partition, and logical represents the logical partition.
Set number flag State is used to set the purpose of partitioning, Flag:boot, LVM, and raid. The state:on/off means to turn on or off.
Parted tool does not need to be saved after the completion of the area, enter Q exit.
After partitioning the partition, you need to format it to use it. File system can be formatted via Mkfs/mkswap
#mkfs. xfs/dev/partition device name or #mkfs-t xfs/dev/partition device name
Can modify fstab to implement automatic loading

Test whether it can be mounted automatically

Viewing a mounted device via df-h
-T option to display the device's File system class
Some of the mount point path is longer, automatically divided into 2 lines display, you can force a line display
As the process has PID, the user has the UID, each file system also has its own ID, called the UUID, but not every partition has, if a partition does not have a file system, then this partition is no UUID.
Can be viewed by BLKID (block ID). Note that the UUID is marked by the file system, not the partition.
The benefit of the UUID is that the system can be mounted by the unique value of the UUID,
This avoids the dislocation caused by deleting the hard disk, Sda6 becomes sda5 and so on.

We can manually change the UUID of the file system via Xfs_admin-u.

Attached: In the directory can be ls-ld to view the properties of the directory, Ls-la view the content of the properties, but-ld display the directory size only 4 K, this is only the size of the directory itself, to see the contents and the entire size of its content, can be viewed by Du, if only to see the final results, Use-S (summary) on the line

Let's look at how the swap partition is created manually. Swap similar to Windows virtual memory/page file, when memory is low, the data is saved in swap.
There are two ways to use:

The first one uses a separate partition as the swap.

Create a partition (for example:/DEV/SDB3) and change the partition ID to 82
Execute the PARTX–A/DEV/SDB command to make the partition changes effective

Create swap file system on a partition

modifying fstab for automatic loading

The second way is to create a block of files that occupies space as swap use

For ordinary partitions, the scale is not high, once the partition format is complete, it is difficult to flexibly increase or reduce the size of the partition. To solve this problem, you can use LVM (logical volumes). The basic process is to initialize the physical disk or partition as a physical volume (PV), then add PV to the VG (volume group), and finally divide the logical partition (LVM) on the VG, which can be formatted and mounted as normal partitions.

Create a prepared disk or partition with PV

Can perform Pvdisplay view PV details, pvremove remove PV
After you create the PV, you need to create the VG and then add the PV to the VG

You can view the specific information through the Vgdisplay, note that the size of the PE is 4M, this is the smallest calculation unit of increase or decrease

Note: When you create a VG: The –s option is used to specify the size of the PE block (physical expansion unit) at creation time, which is 4M by default.
such as: # Vgcreate volgroup03-s 8m/dev/sdb[12])
We can continue to add new partitions to the VG.

If the SDB3 is not converted to PV, but added directly to the VG, but once added he automatically initialized to PV.
Can add of course also can reduce PV. #vgreduce VG00/DEV/SDB3

The VG is ready to create the LVM.

Note that his size is actually 112M, because the size of the PE is 4M, this 4M is the smallest unit, can not break open, so 28 PE is 112M

Note: Large L can specify the size directly, small l is the value of the specified number of PE

You can also set a percentage of the remaining space

Delete a logical volume #lvremove/DEV/VG00/LV01

For logical volumes that have been created, they can be formatted and mounted as normal partitions

Modify the/etc/fstab file to automatically mount the boot.

To extend a logical volume by 300M, first make sure that the volume group has more than 300M of free space.

Executing lvextend extended Logical volume size

Note that the file system of the logical volume is still 109M unchanged, and we also need to populate the file system with whitespace.
RHEL7 can use XFS_GROWFS to expand the Xfs file system, or you can use the RESIZE2FS to process the device directly.

Note that the XFS system only grows and cannot be reduced! So if you need to reduce LVM, partitions can only use EXT4.

Execute DF to view the extended file system

Logical Volume Snapshot
LVM provides a wonderful device, it is snaphot. Allows an administrator to create a new block appliance that provides a precise copy of the logical volume at a point in time, Snapshot provides a static view of the original volume LVM snapshots are logged by logging the file system to a snapshot partition, so when you create a snapshot partition, you do not need to use the same size partition as the partition you are creating the snapshot for. The amount of space required depends on the use of the snapshot, so there is no way to set this size. If the size of the snapshot equals the size of the original volume then the snapshot is always available.
A snapshot is a special logical volume that can only be taken as a snapshot of a logical volume. Logical volume snapshots and logical volumes that require snapshots must be in the same volume group
Now that we have a logical volume/devg00/lv00 in our system, we use Lvdisplay to check this logical volume

As you can see, the size of this logical volume/dev/vg00/lv00 is 309M. We will mount this logical volume/dev/vg00/lv00 under/data. Copy some data into the/data. Easy to do the test

Now we're going to take a snapshot of the logical volume/dev/vg00/lv00.

Execute lvscan to view the created logical volumes

You can see that/dev/vg00/lv00 is the original logical volume, and/dev/vg00/lvsp00 is a snapshot

Execute lvdisplay or LVS command to view logical information

You can see that the logical volume snapshot was created successfully,

Note: After this snapshot volume is built, it does not need to be formatted or mounted. The error message that will appear when formatting or mounting.

Impersonation deletes data from the original logical volume

How do I recover data from my original logical volume? There are two ways to recover deleted data

The first way is to dismount the original logical volume Mount #umount/dev/vg00/lv00

You can then mount a logical volume snapshot #mount/dev/vg00/lvsp00/data to access the data normally.

Mode two can re-write the contents of the snapshot back to the original LVM by Lvconvert

Unmount the original logical volume to mount #umount/dev/vg00/lv00 first

Execution Lvconvert merges the data of the snapshot into the original logical volume #lvconvert--merge/dev/vg00/lvsp00

Finally mount the original logical volume to see if the data was successfully restored

Note: When we delete the data inside the original logical volume, the data in the logical volume snapshot is still there, so you can recover the data with a snapshot. And when we add data to the logical volume, the snapshot is not changed, and there is no such file. Because snapshots only back up the moment of the logical volume at that time.

Using SSM (System Storage Manager) for logical management

Logical Volume Manager (LVM) is an extremely flexible disk management tool that allows users to create logical disk volumes from multiple physical hard drives and resize them with no downtime at all. The latest version of Centos/rhel 7 is now available with the System Storage Manager (also known as SSM), a unified command-line interface developed by Red Hat to manage a wide variety of storage devices. Currently, there are three types of volume management backend available for SSM: LVM, Btrfs, and crypt

To prepare the SSM, on CENTOS/RHEL7, you need to install the System Storage Manager first. can be installed via RPM or Yum tool

Let's start by examining information about available hard drives and LVM volumes. The following command displays information about existing disk storage devices, storage pools, LVM volumes, and storage snapshots.

#ssm List

In this example, there are two physical devices ("/DEV/SDA" and "/dev/sdb"), two storage pools ("Rhel and Vg00"), and two LVM volumes created in storage pool Rhel ("Dev/rhel/root" and "/dev/rhel/swap") , an LVM volume (/dev/vg00/lv00) created in the storage pool vg00.

Here's how to create and manage logical volumes and logical volume snapshots through SSM

At least one new disk is added, and the SSM command displays information on existing disk storage devices, storage pools, LVM volumes

You can see that there are two free disks (SDC, SDD)

Create a new LVM pool/volume

In this example, you might want to look at how to create a new storage pool and a new LVM volume on a physical disk drive. With traditional LVM tools, the whole process is quite complex, requiring partitioning, creating physical volumes, volume groups, logical volumes, and finally file systems. However, if you use SSM, the whole process is an overnight event!

The purpose of this command is to create a storage pool named Mypool, create a 500MB size LVM volume named Lv01 in the storage pool, format the volume with the XFS file system, and mount it to/mnt/test.

Verifying the results of SSM creation

Or execute the SSM list

Adding a Physical Disk (SDD) to an LVM pool

When a new device is added to the storage pool, the storage pool expands automatically, depending on the size of the device. Check the size of the storage pool named CentOS to perform the SSM list view

Next, we'll expand the existing LVM volumes

Expand the LVM volume to increase the size of the/DEV/MYPOOL/LV01 volume by 300MB.

If you have additional space in the storage pool, you can expand the existing disk volumes in the storage pool. To do this, use the Resize option for the SSM command

Execute SSM List view enlarged logical volume

You can see that the logical volume expands to 800M, which adds 300M to the original base, but the file system size (Fs size) has not changed and is still the original size.

In order for the file system to recognize the increased volume size, you need to "expand" the existing file system itself. There are different tools available to expand the existing file system, depending on which file system you use. For example, there are resize2fs for EXT2/EXT3/EXT4, Xfs_growfs for XFS, and Btrfs for Btrfs.

In this example, we use the CentOS 7,xfs file system to create it by default. Thus, we use XFS_GROWFS to expand the existing XFS file system.

After expanding the XFS file system, view the results

or perform #df-ht

You can see that the LVM extension was successful

Logical Volume Snapshot

Generating snapshots of existing LVM volumes (such as/DEV/MYPOOL/LV01)

Once the snapshot is generated, it is stored as a special snapshot volume, storing all the data from the original volume when the snapshot was generated

Each time the data changes in the original LVM, you can manually execute the SSM snapshot to generate the snapshot

When the original LVM data is corrupted, it can be recovered with a snapshot

The first is to dismount the original logical volume from the Mount #umount/dev/vg00/lv00

You can then mount a logical volume snapshot #mount/dev/vg00/lvsp00/data to access the data normally.

Mode two can re-write the contents of the snapshot back to the original LVM by Lvconvert

Unmount the original logical volume to mount #umount/dev/vg00/lv00 first

Execution Lvconvert merges the data of the snapshot into the original logical volume #lvconvert--merge/dev/vg00/lvsp00

Finally mount the original logical volume to see if the data was successfully restored

For more information about SSM, refer to SSM's help hand page

Example: Remove LVM volume #ssm remove <volume>

Delete a storage pool #ssm remove <pool-name>

If you are interested in this, please scan the QR code below for free for more details

Linux7/centos7 disk partitioning, formatting, and LVM management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.