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

How to understand the disk and file system management of Linux

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

Share

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

This article mainly introduces "how to understand the disk and file system management of Linux". In the daily operation, I believe many people have doubts about how to understand the disk and file system management of Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to understand the disk and file system management of Linux". Next, please follow the editor to study!

Manage disk and file system

After introducing the basic file and directory management, it is followed by disk and file system management. How do you partition a disk and create a file system?

Add disks and partition, create and mount file systems

1. Confirm the disk command in the system-- fdisk

Fdisk-l [disk device]

The code is as follows:

[root@localhost ~] # fdisk-l = = > View all partitions in the current system (the result shown here is the same as the P command in interactive mode, later)

Disk / dev/hda: 21.4GB, 21474836480 bytes = = > File name and capacity of disk

Heads, 63 sectors/track, 2610 cylinders = = > head, sector and cylinder size

Units = cylinders of 16065 * 512 = 8225280 bytes = > the size of each cylinder

Device Boot Start End Blocks Id System

/ dev/hda1 * 1 13 104391 83 Linux

/ dev/hda2 14 1288 10241437 + 83 Linux

/ dev/hda3 1289 1925 5116702 + 83 Linux

/ dev/hda4 1926 2610 5502262 + 5 Extended

/ dev/hda5 1926 1989 514048 + 82 Linux swap / Solaris

The meaning of the above fields:

Device: device file name of the partition Boot: whether it is a boot partition, that is, the boot area, if so, it is identified by "*"

Start: start cylinder, that is, the start position of the partition on the hard disk End: end cylinder, that is, the end position of the partition on the hard disk

Blocks: the size of the partition in Blocks (blocks). The default block size is 1024 bytes, that is, 1KB

Id: the ID tag number of the partition type, and 8e System for the EXT3 partition 83LVM partition: partition type, that is, the system within the disk partition

2. Plan the partition in the hard disk-fdisk

Fdisk [disk device]

Use this command to enter interactive manipulation, you can misbehave in the following interactive manipulation, but remember to press Q command when you leave, not w

The code is as follows:

# find out the disk where the root directory is located in the system, and check the relevant information in the hard disk

[root@localhost ~] # df / enter interactive manipulation mode. Be careful not to write specific numbers here.

The number of cylinders for this disk is set to 2610.

There is nothing wrong with that, but this is larger than 1024

And could in certain setups cause problems with:

1) software that runs at boot time (e.g.old versions of LILO)

2) booting and partitioning software from other OSs

(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): M deletes a partition

L list known partition types = = > create a logical partition

M print this menu

N add a new partition = = > add a partition

O create a new empty DOS partition table

P print the partition table = > display the partition table on the screen

Q quit without saving changes = = > do not store, just leave the fdisk program

S create a new empty Sun disklabel

T change a partition's system id changes the partition type

U change display/entry units

V verify the partition table

W write table to disk and exit = = > write the previous operation to the partition table, proceed with caution

X extra functionality (experts only)

Common operations include: creating a primary partition, creating an extended partition (using all the remaining space of the hard disk as an extended partition), creating a logical partition, changing the system code of the partition, saving partition table information, and using partprobe to detect hard disk partition changes.

Here are a few examples to test without saving when you leave:

The code is as follows:

Command (m for help): P = > View the disk partition table, which is the same as fdisk-l in terminal command mode

Disk / dev/hda: 21.4 GB, 21474836480 bytes

Heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/hda1 * 1 13 104391 83 Linux

/ dev/hda2 14 1288 10241437 + 83 Linux

/ dev/hda3 1289 1925 5116702 + 83 Linux

/ dev/hda4 1926 2610 5502262 + 5 Extended

/ dev/hda5 1926 1989 514048 + 82 Linux swap / Solaris

Command (m for help): d

Partition number (1-5): 4 = = > Delete the fourth partition

Command (m for help): d = = > Delete the third partition

Partition number (1-4): 3

Command (m for help): P = > when it is displayed again, only the first two partitions are shown below, and the fifth partition is not shown.

Disk / dev/hda: 21.4 GB, 21474836480 bytes

Heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/hda1 * 1 13 104391 83 Linux

/ dev/hda2 14 1288 10241437 + 83 Linux

Command (m for help): n = = > create a partition

Command action

E extended

P primary partition (1-4)

P = = > Select the type of partition created as the primary partition

Partition number (1-4): 4 = > Select No. 4 as the primary partition

First cylinder (1289-2610, default 1289):

Using default value 1289 = = > if you press enter directly on the starting cylinder, you will use the default value.

Last cylinder or + size or + sizeM or + sizeK (1289-2610, default 2610) + 512m

Command (m for help): P = > look at the partition table again and find that there is an extra partition 4 from the newly established partition

Disk / dev/hda: 21.4 GB, 21474836480 bytes

Heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/hda1 * 1 13 104391 83 Linux

/ dev/hda2 14 1288 10241437 + 83 Linux

/ dev/hda4 1289 1351 506047 + 83 Linux

Command (m for help): n = = > create another partition

Command action

E extended

P primary partition (1-4)

E = = > create an extended partition

Selected partition 3 = = > this is the partition actively selected by the system

First cylinder (1352-2610, default 1352):

Using default value 1352

Last cylinder or + size or + sizeM or + sizeK (1352-2610, default 2610):

Using default value 2610

Command (m for help): P

Disk / dev/hda: 21.4 GB, 21474836480 bytes

Heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/hda1 * 1 13 104391 83 Linux

/ dev/hda2 14 1288 10241437 + 83 Linux

/ dev/hda3 1352 2610 10112917 + 5 Extended = = > look at the newly created extended partition

/ dev/hda4 1289 1351 506047 + 83 Linux

Partition table entries are not in disk order

Command (m for help): n = = > build a few more partitions to play with, anyway, you don't have to save them later, hehe

First cylinder (1352-2610, default 1352):

Using default value 1352

Last cylinder or size or sizeM or sizeK (1352-2610, default 2610): + 2084

Value out of range.

Last cylinder or + size or + sizeM or + sizeK (1352-2610, default 2610): + 2084 M

Command (m for help): n = = > did you find that when you finally set up two partitions, you didn't ask us whether to build a primary partition or an extended partition at all? why? Because the partition has exceeded 4, you have no choice but to expand the partition.

First cylinder (1606-2610, default 1606):

Using default value 1606

Last cylinder or + size or + sizeM or + sizeK (1606-2610, default 2610):

Using default value 2610

Command (m for help): P

Disk / dev/hda: 21.4 GB, 21474836480 bytes

Heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/ dev/hda1 * 1 13 104391 83 Linux

/ dev/hda2 14 1288 10241437 + 83 Linux

/ dev/hda3 1352 2610 10112917 + 5 Extended

/ dev/hda4 1289 1351 506047 + 83 Linux

/ dev/hda5 1352 1605 2040223 + 83 Linux = > these are the next two new partitions

/ dev/hda6 1606 2610 8072631 83 Linux

Partition table entries are not in disk order

Command (m for help): Q = = > be sure to press Q here, because I just demonstrated the operation and deleted 2 partitions destructively. If you really want to partition, you should press w to save it.

[root@localhost ~] #

After the disk is partitioned, update the kernel, otherwise you may not see the change after the partition using fdisk-l, and you can only restart it. But use partprobe to force the kernel to find the partition table again.

The code is as follows:

[root@localhost ~] # partprobe [/ dev/hda] format swap partition

Setting up swapspace version 1, size = 526381 kB

[root@localhost ~] # swapon / dev/hda5 = = > enable swap partition

[root@localhost ~] # free | grep-I swap = = > View swap space

Swap: 514040 0 514040

[root@localhost ~] # swapoff / dev/hda5 = = > deactivate swap space

[root@localhost ~] # free | grep-I swap

Swap: 0 0 0

[root@localhost ~] #

After the new hard disk is partitioned, the partition needs to be formatted (that is, to create a file system) and mounted to a specified directory in the Linux system before it can be used to store files, directories, and other data. And the mount point must be a directory, which is the entry into the file system. Therefore, not all files can be used, and you must mount to a directory in a directory tree before you can use the file system.

4. Mount and unmount the file system-mount,umount

(1) mount

Purpose: Mount the file system and ISO image to the specified folder

Format: mount [- t type] storage device mount point directory (the option of "- t type" can usually be omitted, and most Linux systems can automatically recognize the corresponding file system type)

Mount-o loop ISO image file mount point directory

(2) umount

Purpose: unmount a mounted file system

Format: umount storage device location

Umount mount point directory

(3) check the usage of mounted partitions (df-hT)

The code is as follows:

[root@localhost ~] # df-hT = = > View the usage of existing zones that have been hung up

File system type capacity used available used mount point

/ dev/hda2 ext3 9.5G 2.9G 6.2G 32% /

/ dev/hda3 ext3 4.8G 195m 4.3G 5% / home

/ dev/hda1 ext3 99m 11m 83M 12% / boot

Tmpfs tmpfs 506m 0506m 0% / dev/shm

[root@localhost ~] # mount / dev/hda3 / home/jzhou = = > Mount the device / dev/hda3 to the directory / home/jzhou

[root@localhost ~] # df-hT = = > check the mount again

File system type capacity used available used mount point

/ dev/hda2 ext3 9.5G 2.9G 6.2G 32% /

/ dev/hda3 ext3 4.8G 195m 4.3G 5% / home

/ dev/hda1 ext3 99m 11m 83M 12% / boot

Tmpfs tmpfs 506m 0506m 0% / dev/shm

/ dev/hda3 ext3 4.8G 195m 4.3G 5% / home/jzhou = > look, there is an extra mount point

[root@localhost ~] # umount / dev/hda3 = = > Uninstall the mount just now

[root@localhost ~] # df-hT = = > check again. It is gone. It has been uninstalled successfully.

File system type capacity used available used mount point

/ dev/hda2 ext3 9.5G 2.9G 6.2G 32% /

/ dev/hda3 ext3 4.8G 195m 4.3G 5% / home

/ dev/hda1 ext3 99m 11m 83M 12% / boot

Tmpfs tmpfs 506m 0506m 0% / dev/shm

[root@localhost ~] #

In addition, the mounting of the optical drive and the image is only slightly different, and the syntax is actually the same. The CD device is usually / dev/cdrom or / dev/hdc. The image is obviously a compression package in iso format. Add the-o loop option when mounting the image. There are a lot of specific uses on the web.

5. Set up automatic file system mount-modify configuration file

The "/ etc/fstab" file in the system can be regarded as a configuration file for the mount command, and it contains the file system directories that need to be mounted automatically after boot.

Take a look at the contents of this file as above, and the meanings of each field are as follows (the first three fields are important):

First field: device location

Second field: Mount point directory

Third field: file system type

The fourth field: the mount parameter, which is the parameter that can be used after the "- o" option of the mount command

The fifth field: indicates whether the file system needs dump backup (dump is a backup tool). Generally, if set to 1, it will be ignored by dump.

Field 6: this number is used to determine the order in which disk checks are performed at system startup, 0 is not checked, 1 is first, 2 is second. The root partition should be set to 1 and the other partitions to 2

Set up automatic mount example: that is, after each reboot, the mount can be completed automatically.

Enter vim to edit the content, change the mount point of the device proc from / proc to / home/jzhou, and save and exit:

When it starts again, it will be automatically mounted to the appropriate directory. The above is a demonstration, and then I changed it back.

Description:

(1) after the automatic mount record of the corresponding partition is correctly set in the / etc/fstab file, when you mount or unmount the partition manually, you only need to specify any one of the device name and mount point as a parameter. For example, execute "mount / dev/sdb1" or "umount / mailbox" to complete the unmount.

(2) when the mount command does not take any options or parameters, it can display the information of the file system that has been mounted in the current system.

Brief introduction of LVM

Logical Volume Manager, logical volume management

Shielding the underlying disk layout to facilitate dynamic adjustment of disk capacity

(it should be noted that the p / boot partition is used to store boot files and the LVM mechanism cannot be applied)

1. The basic concept of LVM mechanism.

(1) PV (Physical Volume, physical volume)

The entire hard disk, or a normal partition established by tools such as fdisk

Includes many PE (Physical Extent, basic units) with the default 4MB size

(2) VG (Volume Group, volume group)

A whole made up of one or more physical volumes

(3) LV (Logical Volume, logical volume)

A piece of space split from a volume group to establish a file system

Their logical relationship is as follows:

I think you already know why this LVM mechanism exists. Similar to most virtualized things, the core is PV, that is, the real physical volume, which forms a volume group of multiple physical volumes, and then divides these volume groups into volumes, which has nothing to do with PV, which makes good use of capacity and space, and when new physical volumes are added, just add them to the logical volume. This mechanism is very similar to dividing VLAN in the network.

The main management commands of LVM are as follows:

Function

Physical volume management

Volume group management

Logical volume management

Scan scanning

Pvscan

Vgscan

Lvscan

Create establishment

Pvcreate

Vgcreate

Lvcreate

Display display

Pvdisplay

Vgdisplay

Lvdisplay

Remove deletion

Pvremove

Vgremove

Lvremove

Extend extension

Vgextend

Lvextend

Usage of the main command:

Pvcreate device name

Vgcreate volume group name physical volume name 1 physical volume name 2

Lvcreate-L size-n logical volume name volume group name

Lvextend-L + size / dev/ volume group name / logical volume name

Basic steps for creating logical volumes: PV- > VG- > LV- > format-> Mount using the file system

Specific steps: (my virtual machine can not demonstrate, bash does not have this pvcreate command, a brief theoretical explanation)

(1) convert physical volumes

1) use the fdisk command to plan two partitions fdisk-l / dev/hda / dev/hdb | grep "LVM"

2) use the pvcreate command to convert the above partitions to physical volumes

The code is as follows:

Pvcreate / dev/hda1 pvcreate / dev/hdb1

(2) create a volume group

The code is as follows:

Vgcreate vg_test / hda1 / dev/hdb1

(3) create a logical volume

1) use the lvcreate command to create a logical volume lv_test, and set aside 10G space from the volume group vg_test

The code is as follows:

Lvcreate-L 10G-n lv_test vg_test

2) use the mkfs command to create an ext3 file system

The code is as follows:

Mkfs-t ext3 / dev/vg_test/lv_test

(4) format the ext3 file system and mount it to the directory / dirtest

The code is as follows:

Mount / dev/vg_test/lv_test / dirtest

(5) it can also expand the capacity of logical volumes.

1) use the lvextend command to expand the capacity of the logical volume lv_test, and delimit 10G from the volume group vg_test to the logical volume lv_test

The code is as follows:

Lvextend-L + 10G / dev/vg_test/lv_test

2) use the resize2fs command to update the file system size recognized by the system

The code is as follows:

Resize2fs / dev/vg_test/lv_test

At this point, the study on "how to understand the disk and file system management of Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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