Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic commands of Linux disk and file system?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Linux disk and file system basic command refers to which, many novices are not very clear, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Here are the commands related to disk management. The data that needs to be stored persistently in the computer is generally stored in the auxiliary memory such as the hard disk. The hard disk generally has a large capacity, and in order to facilitate management and use, the hard disk can be divided into one or more logical disks, called partitions; in order to organize the files in the partitions into a form that the operating system can handle, the partition needs to be formatted (create a file system) In linux, formatted partitions must also be mounted (which can be simply understood as associating the partition to a known directory in the linux directory tree) before it can be used.

1. Df displays disk space usage of the file system

[root@centos7 temp] # df-h file system capacity used used mount point / dev/mapper/centos-root 49G 18g 31g 36% / devtmpfs 3.9g 03.9g 0% / devtmpfs 3.9g 03.9g 0% / dev/shm tmpfs 3.9G 367M 3.5G 10% / run tmpfs 3.9G 0 3.9G 0% / sys/fs/cgroup / dev/sda1 497M 125M 373M 26% / boot / dev/mapper/centos-home 24G 4.0G 20G 17% / home tmpfs 783M 0783M 0% / run/user/0

The option-h functions to convert the display units of numbers (default is KB).

Showing the information file system column with the word tmpfs below is the virtual memory file system (not expanded here).

The mount point of the file system / dev/mapper/centos-root is / (root directory), which is commonly referred to as the root partition (or root file system); / dev/sda1 (boot partition) holds the kernel image and some auxiliary files needed for startup; in addition, the user's home directory is partitioned separately (/ dev/mapper/centos-home).

There is also a special partition that can be done in linux: swap partition (swap partition). The purpose is that when the physical memory of the system is insufficient, some data in the physical memory that is not used temporarily will be exchanged to the swap partition, and when the data is needed, it will be swapped back from the swap space to the memory space. Swap breaks through the limitation of physical memory functionally, so that the program can manipulate more space than the actual physical memory. However, because the speed of the hard disk is much lower than the memory, swap can only be used as an auxiliary to the physical memory. Typically, the size of the swap space is 1 to 2 times the actual physical memory. Use the command free to view the size of the swap space.

Option-I displays inode information

[root@centos7 tmp] # df-I file system Inode used (I) available (I) used% mount point / dev/mapper/centos-root 50425856 78822 50347034 1% / devtmpfs 998721 391 998330 1% / devtmpfs 1001340 1 1001339 1% / dev/shm tmpfs 1001340 490 1000850 1% / run tmpfs 1001340 13 1001327 1% / sys/fs/cgroup / dev/sda1 512000 330 511670 1% / boot / dev/mapper/centos-home 24621056 190391 244306651% / home tmpfs 1001340 1 1001339 1% / run/user/0

The numbers shown here are the usage of the number of inode in the file system.

2. Fdisk disk Partition tool

Fdisk [options] [device...]

Option-l indicates that the partition table is listed

[root@centos7 tmp] # fdisk-l / dev/sda disk / dev/sda:85.9 GB, 85899345920 bytes 167772160 sector Units = sector of 1 * 512 = 512 bytes sector size (logical / physical): 512byte / 512byte bytes size (min / *): 512byte / 512byte disk label type: dos disk identifier: 0x0001abbc device Boot Start End Blocks Id System / dev/sda1 * 2048 1026047 512000 83 Linux / dev/sda2 1026048 167772159 83373056 8e Linux LVM [root@centos7 tmp] #

The current mechanical hard disk contains one or more platter fixed on the spindle (spindle), which is composed of hard magnetic alloy materials. Each disk has two surfaces, each surface contains a large number of sectors (sector), the sector is a size of 512 byte blocks, these blocks are evenly distributed on the concentric circles of the disk, these concentric circles are called track. Thousands of tracks are as wide as the diameter of human hair.

The hard disk uses a magnetic head fixed at the top of the magnetic arm (disk arm) (both the upper and lower sides of the disk head) to read and write data in the disk. When the hard disk is not working, the magnetic head stays in the start-stop area (the area near the spindle on the disk); outside the start-stop area is the data area, and the outermost track of the disk is called track 0; after the hard disk starts, the disk rotates at a high speed around the spindle, and the air flow generated by the rotation of the disk is strong enough to lift the head and maintain a small distance from the surface of the disk (about 1/1000 of the diameter of human hair). By swinging the magnetic arm, the magnetic head can be moved above any track.

Schematic diagram of single track:

The current speed of the hard drive is about 7200 rpm to 15000 rpm. Assuming that the speed of the hard drive is 10000 revolutions per minute, it means that the time it takes to turn around is 6ms.

The same track on all disks forms a cylinder, which is usually called Cylinder. When the system stores data on disk, it is carried out in the way of cylinder, head and sector, that is, the top 0 head, the outermost 0 track * * sectors begin to write, and after one track is filled, the next head on the same cylinder continues to write. The same cylinder is fully written before it is pushed to the next cylinder of the inner layer.

In the fdisk command, device is usually / dev/hda, / dev/hdb.... (the hard disk device name of the IDE interface type) or / dev/sda, / dev/sdb.... (the hard disk device name of the SCSI interface type), indicating the entire hard disk. If the hard disk is partitioned, a number is appended to the device name to indicate the partition of the device. Such as / dev/sda1 and / dev/sda2 in the example above

Hard disk head access data is in 512bytes (Start and End columns in the example above), but operating system access data is in blocks (Block) (Note: the size of Block here is different from the Blocks column in the output of the fdisk command, the size of the Blocks column in the fdisk command output is 1024 bytes) Sectors are hardware-level and Block are file-system-level, which means that the size and number of block are determined only when the file system is created (formatted). The size of a block is n times the size of a sector size 2, and in this case the default size of the file system Block is 4096 bytes (other values can be specified when formatted).

We added three new hard drives (each 200GB) to the 252 machine.

[root@idc-v-71252] # ls-l / dev/sd [a murd] * brw-rw---- 1 root disk 8, December 13 09:49 / dev/sda brw-rw---- 1 root disk 8, December 13 09:49 / dev/sda1 brw-rw---- 1 root disk 8, December 13 09:49 / dev/sda2 brw-rw---- 1 root disk 8, 16 December 13 09:49 / dev/sdb brw-rw---- 1 root disk 8 32 December 13 09:49 / dev/sdc brw-rw---- 1 root disk 8, 48 December 13 09:49 / dev/sdd # see here in addition to the original partitioned sda With the addition of devices sdb, sdc, sdd # the fifth column here consists of two numbers separated by commas, which are the identification numbers used by the kernel to identify specific devices.

Let's use the fdisk command to partition the new disk

[root@idc-v-71252 ~] # fdisk / dev/sdb Welcome to fdisk (util-linux 2.23.2). The changes remain in memory until you decide to write them to disk. Think twice before using the write command. Device does not contain a recognized partition table uses the disk identifier 0xc41dfd92 to create a new DOS disk label. Command (enter m for help):

Enter m at the prompt to get help (lists the commands that can be used after the prompt and their explanations)

Command (enter m for help): the m command operates a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition g create a new empty GPT partition table G create an IRIX (SGI) partition table l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel T change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only) command (enter m for help):

The command n means to create a new partition

Command (enter m for help): n Partition type: P primary (0 primary, 0 extended, 4 free) e extended Select (default p):

There are two options here, p for primary partition (primary) and e for extended partition (extended). The default is primary partition.

After each hard disk is partitioned, the area located in the 0 head 0 cylinder 1 sector is a special area called MBR (Main Boot Record master boot record area), in which the first 446 bytes is Bootloader (boot loader), the next 64 bytes is DPT (Disk Partition Table hard disk partition table), and * two bytes of Magic Number (hard disk valid flag).

The partition of this hard disk is recorded in DPT. Because of its size limitation, the partition table can only contain four records, which can be one to four primary partitions or one extended partition and one to three primary partitions. The extended partition can be further partitioned, which is called logical partition.

We choose the default primary partition:

Select (default p): Using default response p partition number (1-4, default 1): start sector (2048-419430399, default is 2048): the default value of 2048 Last sector will be used, + sector or + size {KMagne Mpeng} (2048-419430399, default is 419430399): + 100g partition 1 has been set to Linux type and size is set to 100GiB command (enter m for help):

At each step, there is a prompt for sectors that can be used starting from 2048 (the front sector includes MBR for other purposes), and the partition end sector can be specified as a sector number or in a format such as + size. Here we specify that the partition size is 100g

Use the p command to print partition information:

Command (enter m for help): P disk / dev/sdb:214.7 GB, 214748364800 bytes 419430400 sector Units = sector of 1 * 512 = 512 bytes sector size (logical / physical): 512byte / 512byte bytes size (min / *): 512byte / 512byte disk label type: dos disk identifier: 0xc41dfd92 device Boot Start End Blocks Id System / dev/sdb1 2048 209717247 104857600 83 Linux command (enter m for help):

Note the difference shown here. If there is a * flag in the Boot column, it means that the partition is a boot partition. The Id column represents the partition type, and you can use the command l to list all supported types, where 82 represents the linux default partition type and 8e represents linux lvm (later).

Then we save the information:

Command (enter m for help): w The partition table has been altered! Calling ioctl () to re-read partition table. Synchronizing disks. [root@idc-v-71252 ~] #

3. Mkfs creates a file system

Option-t can specify the file system type (including ext3 ext4 btrfs xfs reiserfs, etc.)

[root@idc-v-71252 ~] # mkfs-t ext4 / dev/sdb1 # or execute mkfs.ext4 / dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) File system tag = OS type: Linux Block size = 4096 (log=2) Block size = 4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 6553600 inodes, 26214400 blocks 1310720 blocks (5.00%) reserved for the super user * data blocks = 0 Maximum filesystem blocks=2174746624 800 block groups 32768 blocks per group 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,4096000, 7962624,11239424,20480000,23887872 Allocating group tables: finish writing to inode table: finish Creating journal (32768 blocks): complete Writing superblocks and filesystem accounting information: complete [root@idc-v-71252 ~] #

In this way, we have formatted the partition into an ext4 file system, and the output shows information such as the number of inode and block.

4. Mount mounts the file system

Mount the formatted file system to / root/temp/tmp

[root@idc-v-71252 tmp] # mount / dev/sdb1 / root/temp/tmp [root@idc-v-71252 tmp] # df-h File system capacity available available mount point / dev/mapper/centos-root 49G 14G 35G 28% / devtmpfs 3.9G 03.9G 0% / devtmpfs 3 .9G 0 3.9G 0% / dev/shm tmpfs 3.9G 8.5m 3.9G 1% / run tmpfs 3.9G 03.9G 0% / sys/fs/cgroup / dev/sda1 497M 170M 328M 35% / boot / dev/mapper/centos-home 24G 16G 7.6G 68% / home tmpfs 799m 0799m 0% / run/user/0 / dev/sdb1 99G 61m 94G 1% / root/temp/tmp [root@idc-v-71252 tmp] #

You can see that the new partition is ready for use. When formatting, the system will reserve a certain amount of space on the disk (5% here) for other purposes. You can use the command dumpe2fs / dev/sdb1 2 > / dev/null | grep 'Reserved block count' to view the number of reserved blocks.

This mounted partition is only temporarily valid and will not be automatically mounted when the system is rebooted. If you need * to take effect, you can write partition information to the partition configuration file / etc/fstab

[root@idc-v-71252 ~] # cat / etc/fstab # # / etc/fstab # Created by anaconda on Fri Jan 15 00:59:59 2016 # # Accessible filesystems, by reference, are maintained under'/ dev/disk' # See man pages fstab (5), findfs (8) Mount (8) and/or blkid (8) for more info # / dev/mapper/centos-root / xfs defaults 0 UUID=10205c20-bd44-4991-8c84-7b38db63a581 / boot xfs defaults 0 / dev/mapper/centos-home / home xfs defaults 0 0 / dev/mapper/centos-swap defaults 0 0

The original partition and its mount information are recorded in this file. The behavior comment lines starting with # are divided into six columns:

* * column represents the file system

The second column is the mount point

The third list is the file system type

Fourth column option

The fifth column indicates whether to use the dump tool to back up the file system. 0 means no backup.

The sixth column indicates whether to use the fsck tool to check the file system regularly, while 0 indicates no check.

When the following information is appended to the file, the new partition will also be automatically mounted when the system is restarted:

/ dev/sdb1 / root/temp/tmp ext4 defaults 0 0

When mounting using the mount command, you can use the option-o options to specify the mount option (fourth column in / etc/fstab)

If you re-mount a new mounted partition in a read-only manner:

[root@idc-v-71252 home] # mount-o remount,ro / dev/sdb1 [root@idc-v-71252 home] # cd / root/temp/tmp [root@idc-v-71252 tmp] # touch 1 touch: unable to create "1": read-only file system [root@idc-v-71252 tmp] #

At this point, an error is displayed when creating a file in the directory / root/temp/tmp: read-only file system

[root@idc-v-71252 tmp] # mount-o remount,rw / dev/sdb1 [root@idc-v-71252 tmp] # touch 2 [root@idc-v-71252 tmp] # ls 2 lost+found [root@idc-v-71252 tmp] # File can be created after re-mounting read-write

Defaults in the configuration file refers to the options: rw, suid, dev, exec, auto, nouser, and async. For their meaning, please refer to mount's man manual.

Option-a means to read all records in the configuration file and remount them

Option-B or-- bind can mount one directory to another

[root@idc-v-71252 tmp] # ls-l / opt/ Total usage 0 [root@idc-v-71252 tmp] # [root@idc-v-71252 tmp] # mount-bind / root/temp/tmp / opt [root@idc-v-71252 tmp] # ls / opt-l Total usage 16-rw-r--r-- 1 root root 0 December 13 14:44 2 drwx- 2 root root 16384 December 13 12:54 lost+found [root@idc-v-71252 tmp] #

The directory mounted in this way cannot be seen using the df command, but can be viewed using the mount command

[root@idc-v-71252 tmp] # mount | grep / dev/sdb1 / dev/sdb1 on / root/temp/tmp type ext4 (rw,relatime,data=ordered) / dev/sdb1 on / opt type ext4 (rw,relatime,data=ordered)

The option-t indicates the specified file system type, such as mounting a CD:

[root@centos7 tmp] # mount-t iso9660 / dev/cdrom / mnt mount: / dev/sr0 write protection. [root@centos7 tmp] # # or NFS file system (x.x.x.x is the IP address of the NFS server) mount-t nfs x.x.x.x:/src_dir / path/to/local/dest_dir will be mounted read-only

5. Umount unmounts the file system

When unmounting, you can specify either the device name or the mount point. When a process in the file system is using a file, the unmount will report an error:

[root@idc-v-71252 ~] # umount / root/temp/tmp umount: / root/temp/tmp: target busy. (in some cases, useful information about processes using the device can be found through lsof (8) or fuser (1).) [root@idc-v-71252 ~] #

At this point, you can use lsof or fuser to find the process (see here), stop the process and then uninstall it.

If you are uninstalling the CD, you can also use the eject command.

[root@centos7 tmp] # eject

6. Fsck checks and repairs the file system

You can use the fsck command to check whether the partition is normal, which needs to be checked in the state of uninstallation.

[root@idc-v-71252 temp] # umount / dev/sdb1 [root@idc-v-71252 temp] # fsck / dev/sdb1 fsck, from util-linux 2.23.2 e2fsck 1.42.9 (28-Dec-2013) / dev/sdb1: clean, 12 dev/sdb1 6553600 files, 459544 files 26214400 blocks

When you execute the command directly, if damage is detected, there will be an interactive prompt to ask whether to repair the bad block.

Option-a means to fix directly without asking

Option-y indicates that you always enter yes for interactive queries

7. Mkswap creates a swap partition

The swap partition of linux can be done with disk partition or file, and the swap of the current system uses partition. Here is an example of using a file to create a swap partition

First use the command dd to generate a file with a size of 8G

[root@idc-v-71252 tmp] # dd if=/dev/zero of=swapfile bs=1024K count=8192 recorded 8192 zero read in recorded 8192 zero write out 8589934592 bytes (8.6 GB) copied, 35.1683 seconds 244 MB/ seconds [root@idc-v-71252 tmp] # # command creates a file under the current directory swapfile # if represents a specified read file or device # of represents a specified write file or device # bs represents the size of a read or write # count indicates the number of reads or writes [root@idc-v-71252 tmp] # du-sh swapfile 8.0g swapfile

Create swap Partition

[root@idc-v-71252 tmp] # mkswap swapfile is setting swap space version 1, size = 8388604 KiB no label, UUID=84fbe922-9444-482b-aa55-631ce72161c0

8. Swapon/swapoff enable / disable swap files or devices

[root@idc-v-71252 tmp] # swapon swapfile swapon: / root/temp/tmp/swapfile: unsafe permission 0644. It is recommended to use 0600. [root@idc-v-71252 tmp] # free-m total used free shared buff/cache available Mem: 7983 11553 8 7813 7794 Swap: 16255 16255 # see here that the swap partition has been expanded [root@idc-v-71252 tmp] # swapoff swapfile [root@idc-v- 71252 tmp] # free-m total used free shared buff/cache available Mem: 7983 10959 8 7813 7800 Swap: 8063 0 8063

9. Parted disk Partition tool

The partition table in MBR mentioned above does not support partitions larger than 2TB. In order to solve this limitation and other shortcomings of MBR, GTP (Global unique Identification Partition Table GUID Partition Table) appears, which is a standard for the structural layout of disk partition tables and is a part of the UEFI (Unified Extensible firmware Interface) standard. You need to use the command parted to partition GTP-enabled partitions (compatible with MBR partitions).

When you use the command parted directly, you will enter the interactive interface

[root@idc-v-71252 ~] # parted / dev/sdb GNU Parted 3.1 use / dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)

You can enter help at the prompt to display a list of available commands (commands can be abbreviated)

The command print (p) indicates that the partition table is printed

(parted) p Model: VMware Virtual disk (scsi) Disk / dev/sdb: 215GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flag 1 1049kB 107GB 107GB primary ext4 (parted)

The command quit means to exit the interface

Option-s indicates non-interactive mode, where the command is written after

[root@idc-v-71252 ~] # parted-s / dev/sdb print Model: VMware Virtual disk (scsi) Disk / dev/sdb: 215GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flag 1 1049kB 107GB 107GB primary ext4 [root@idc-v-71252 ~] # fdisk-1 / dev/sdb1 disk / dev/sdb1:107.4 GB, 107374182400 bytes 209715200 sector Units = sector of 1 * 512 = 512 bytes sector size (logical / physical): 512 bytes / 512 bytes I root@idc-v-71252 O size (minimum / * *): 512 bytes / 512 bytes [root@idc-v-71252 ~] #

The msdos after Partition Table is represented as a MBR partition, and the reason why the sdb1 partition size appears as 107G instead of 100G in both commands is because 1000 bytes is used as the 1KB count in the calculation.

You cannot do a GTP partition on a hard disk that has already made a MBR partition. Redoing it will cause the original partition to be formatted.

Here, do the GTP partition on the new disk / dev/sdc:

[root@idc-v-71252 ~] # parted / dev/sdc GNU Parted 3.1 use / dev/sdc Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)

Note that the interaction mode is different from the fdisk command. As soon as the parted command is confirmed by pressing enter, the command is executed immediately, and the changes to the disk take effect immediately.

The command mklabel specifies the partition format (msdos or gtp). If the format is unknown, an error will be reported when using the print command: error: / dev/sdc: unrecognised disk label

(parted) mklabel gpt

The command mkpart means to create a new partition, followed by the partition type (primary or extended partition), the file system type (ext4, etc., which can be omitted), the start point, and the end point.

(parted) mkpart primary 0KB 100GB warning: You requested a partition from 0.00B to 100GB (sectors 0.. 195312500). The closest location we can manage is 17.4kB to 100GB (sectors 34.. 195312500). Is this still acceptable to you? Yes / Yes/ / No? Yes warning: The resulting partition is not properly aligned for best performance. Ignore / Ignore/ abandon / Cancel? Ignore (parted) p Model: VMware Virtual disk (scsi) Disk / dev/sdc: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flag 1 17.4kB 100GB 100GB primary

The command rm deletes the partition, followed by the partition number

(parted) rm 1 (parted) p Model: VMware Virtual disk (scsi) Disk / dev/sdc: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flag (parted)

Let's continue using non-interactive mode

[root@idc-v-71252] # parted-s / dev/sdc mkpart primary ext4 18KB 100GB warning: The resulting partition is not properly aligned for best performance. [root@idc-v-71252 ~] # parted-s / dev/sdc print Model: VMware Virtual disk (scsi) Disk / dev/sdc: 215GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flag 1 17.9kB 100GB 100GB primary

Here 1000 bytes is used as the 1KB count to format and mount (partial output is brief)

[root@idc-v-71252 temp] # mkfs.ext4 / dev/sdc1 [root@idc-v-71252 temp] # mount / dev/sdc1 / root/temp/tmp_1 [root@idc-v-71252 temp] # df-h | grep ^ / dev/ dev/mapper/centos-root 49G 22G 27G 44% / / dev/sda1 497M 170M 328M 35% / boot / dev/mapper/centos-home 24G 16G 7.6G 68% / home / dev/sdb1 99G 61m 94G 1% / root/temp/tmp / dev/sdc1 92G 61m 87G 1% / root/temp/tmp_1

* use parted to create an extended MBR partition with the following command:

Parted-s / dev/sdd mklabel msdos parted-s / dev/sdd mkpart extended 100GB 100% parted-s / dev/sdd mkpart logical 100GB 200GB

The results are as follows:

[root@idc-v-71252 temp] # parted-s / dev/sdd print Model: VMware Virtual disk (scsi) Disk / dev/sdd: 215GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flag 1 100GB 215GB 115GB extended lba 5 100GB 200GB 100GB logical [root@idc-v-71252 temp] # fdisk-1 / dev/sdd disk / dev/sdd:214.7 GB, 214748364800 bytes 419430400 sector Units = sector of 1 * 512 = 512 bytes sector size (logical / physical): 512byte / 512byte size (min / *): 512byte / 512byte disk label type: dos disk identifier: 0x0006d495 device Boot Start End Blocks Id System / dev/sdd1 195311616 419430399 112059392 f W95 Ext'd (LBA) / dev/sdd5 195313664390625279765580883 Linux

Format and mount (omit part of the output)

[root@idc-v-71252 temp] # mkfs.ext4 / dev/sdd5 [root@idc-v-71252 temp] # mount / dev/sdd5 / root/temp/tmp_2 [root@idc-v-71252 temp] # df-h | grep ^ / dev/ dev/mapper/centos-root 49G 22G 27G 44% / / dev/sda1 497M 170M 328M 35% / boot / dev/mapper/centos-home 24G 16G 7.6G 68% / home / dev/sdb1 99G 61m 94G 1% / root/temp/tmp / dev/sdc1 92G 61m 87G 1% / root/temp/tmp_1 / dev/sdd5 92G 61m 87G 1% / root/temp/tmp_2

These new partitions can be written to the configuration file / etc/fstab to automatically mount after restart.

LVM logical volume management

LVM is a mechanism for managing disk partitions in linux environment, which makes it more convenient for system administrators to allocate storage space for applications and users.

Terminology

Physical storage media (The physical media): refers to the storage devices of the system, such as partitions / dev/sdb1, / dev/sdc1, / dev/sdd5 created above

Physical volumes (PV: Physical Volume): equivalent to physical storage media, but with LVM-related management parameters added.

Volume group (VG: Volume Group): consists of one or more physical volumes.

Logical volumes (LV: Logical Volume): logical partitions (file systems) based on volume groups.

PE (physical extent): each physical volume is divided into basic units called PE, and PE with a unique number is the smallest unit that can be addressed by LVM. The size of PE is configurable and defaults to 4MB.

LE (logical extent): logical volumes are also divided into basic addressable units called LE. In the same volume group, the size of LE and PE are the same and correspond one to one.

Steps

1. Create a partition

You can use fdisk or parted for partitioning, and the only difference from the previous example is that the partition type is 8e. Here, make the remaining space of the three new hard drives into LVM partitions in parted mode (just one example, the rest is brief):

Parted-s / dev/sdb mkpart primary 107GB 100% parted-s / dev/sdb toggle 2 lvm # means that the second partition is defined as lvm type (8e)

2. Create PV

[root@idc-v-71252 ~] # pvcreate / dev/sd [bcd] 2 Physical volume "/ dev/sdb2" successfully created Physical volume "/ dev/sdc2" successfully created Physical volume "/ dev/sdd2" successfully created [root@idc-v-71252 ~] # View [root@idc-v-71252 ~] # pvscan PV / dev/sda2 VG centos lvm2 [79.51 GiB / 64.00 MiB free] PV / dev/sdb2 lvm2 [100 . 00 GiB] PV / dev/sdc2 lvm2 [106.87 GiB] PV / dev/sdd2 lvm2 [93.13 GiB] Total: 4 [379.50 GiB] / in use: 1 [79.51 GiB] / in no VG: 3 [300.00 GiB]

3. Create VG

[root@idc-v-71252 ~] # vgcreate-s 8m test_lvm / dev/sd [bcd] 2 Volume group "test_lvm" successfully created # here use the option-s to specify the PE size as 8m, and the volume group is named test_lvm # View [root@idc-v-71252 ~] # vgscan Reading all physical volumes. This may take a while... Found volume group "centos" using metadata type lvm2 Found volume group "test_lvm" using metadata type lvm2

4. Create LV

[root@idc-v-71252] # lvcreate-n test_1-L 50G test_lvm Logical volume "test_1" created. [root@idc-v-71252 ~] # # option-n specify LV name specify size for test_1,-L You can also specify the number of LE with the option-l # see [root@idc-v-71252 ~] # lvscan ACTIVE'/ dev/centos/swap' [7.88 GiB] inherit ACTIVE'/ dev/centos/home' [23.48 GiB] inherit ACTIVE'/ dev/centos/root' [48.09 GiB] inherit ACTIVE'/ dev/test_lvm/test _ 1' [50.00 GiB] inherit [root@idc-v-71252 ~] #

5. Formatting and mounting

# format here. Formatting is not required after partitioning. # here we format it into xfs format [root@idc-v-71252 ~] # mkfs.xfs / dev/test_lvm/test_1 meta-data=/dev/test_lvm/test_1 isize=256 agcount=4, agsize=3276800 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data= bsize=4096 blocks=13107200 Imaxpct=25 = sunit=0 swidth=0 blks naming = version 2 bsize=4096 ascii-ci=0 ftype=0 log = internal log bsize=4096 blocks=6400, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime = none extsz=4096 blocks=0 Rtextents=0 [root@idc-v-71252 ~] # mount / dev/test_lvm/test_1 / root/temp/test_1 [root@idc-v-71252 ~] # df-h | grep ^ / dev/ dev/mapper/centos-root 49G 22G 27G 44% / / dev/sda1 497M 170M 328M 35% / boot / dev/mapper/centos-home 24G 16G 7.6G 68% / home / dev/sdb1 99G 61m 94G 1% / root/temp/tmp / dev/sdc1 92G 61m 87G 1% / root/temp/tmp_1 / dev/sdd5 92G 61m 87G 1% / root/temp/tmp_2 / dev/mapper/test_lvm-test_1 50G 33m 50G 1% / root/temp/test_1

The reason why the file system appears as / dev/mapper/.... here Because the kernel uses the Mapper Device mechanism to map devices:

[root@idc-v-71252] # ls-l / dev/mapper/test_lvm-test_1 lrwxrwxrwx 1 root root 7 December 14 09:58 / dev/mapper/test_lvm-test_1->.. / dm-3 [root@idc-v-71252 ~] # ls-l / dev/test_lvm/test_1 lrwxrwxrwx 1 root root 7 December 14 09:58 / dev/test_lvm/test_1->.. / dm-3

Actually / dev/test_lvm/test_1 and / dev/mapper/test_lvm-test_1 point to the same device / dev/dm-3 (you can write either in the configuration file / etc/fstab), so I won't expand on the mapping mechanism in detail here.

Command

In addition to several create and view commands mentioned in the previous example, LVM has a series of commands that start with pv/vg/lv and mostly add, delete, expand, shrink, view, change, and so on.

Create command

Pvcreate vgcreate lvcreate

There are three types of viewing commands, with different emphasis or level of detail:

Pvs pvscan pvdisplay vgs vgscan vgdisplay lvs lvscan lvdisplay

Change the properties (change the properties of the objects at this level respectively)

Pvchange vgchange lvchange

Expand capacity

Vgextend lvextend

Example of LV expansion (note that the kernel may not support online expansion of some file systems, so you need to unmount the file system first):

[root@idc-v-71252 dev] # lvextend-L + 10G / dev/test_lvm/test_1 Size of logical volume test_lvm/test_1 changed from 50.00 GiB (6400 extents) to 60.00 GiB (7680 extents). Logical volume test_1 successfully resized. [root@idc-v-71252 ~] # df-h / dev/mapper/test_lvm-test_1 file system capacity available available mount point / dev/mapper/test_lvm-test_1 50G 33m 50G 1% / root/temp/test_1 # the expansion has not taken effect at this time Online expansion of xfs file system using xfs_growfs [root@idc-v-71252 dev] # xfs_growfs / dev/test_lvm/test_1 meta-data=/dev/mapper/test_lvm-test_1 isize=256 agcount=4, agsize=3276800 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data= bsize=4096 blocks=13107200 Imaxpct=25 = sunit=0 swidth=0 blks naming = version 2 bsize=4096 ascii-ci=0 ftype=0 log = internal bsize=4096 blocks=6400, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime = none extsz=4096 blocks=0 Rtextents=0 data blocks changed from 13107200 to 15728640 [root@idc-v-71252 ~] # df-h / dev/mapper/test_lvm-test_1 File system capacity available available mount point / dev/mapper/test_lvm-test_1 60G 33m 60G 1% / root/temp/test_1

The file system of ext series needs to be expanded online by using the command resize2fs.

Reduce (use with caution)

Vgreduce lvreduce

Rename

Vgrename lvrename

There are some other commands that are no longer listed here. For their usage, please refer to the relevant manual.

This paper briefly introduces the management commands related to disk and LVM. In addition, there is a middle layer between physical disk and disk partition: RAID (Independent redundant disk Array), which provides disk-level data redundancy. Raid cards (hardware) are generally available on the current server.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report