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

Linux disk and file system management

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

Share

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

Preface to the catalogue of articles

One: disk base

​ 1.1: disk structure

​ 1.1.1: physical structure of hard disk

​ 1.1.2: data structure of hard disk

​ 1.1.3: storage capacity

​ 1.1.4: interface of hard disk

​ 1.2:MBR and disk partition representation

​ 1.2.1:MBR

​ 1.2.2 representation of disk partitions

Types of file systems used in ​ 1.2.3:Linux systems

Two: detect and confirm the new hard drive

​ 2.1:fdisk command-View or manage disk partitions

​ 2.2: use the "n" command

Three: create a file system

​ 3.1:mkfs command

​ 3.2:mkswap command

Four: Mount and unmount the file system

​ 4.1:mount command-- Mount the file system

​ 4.2:umount command-- unmount the file system

​ 4.3: setting up automatic mounting of the file system

​ 4.4:df command-- View disk usage

Preface

Disk refers to the memory that uses magnetic recording technology to store data. Disk is the main storage medium of computer, which can store a large amount of binary data and keep the data from being lost after power loss. The disk used by early computers is floppy disk (soft disk), but now the commonly used disk is hard disk (hard disk, referred to as hard disk).

A file system is a method and data structure used by the operating system to identify files on storage devices (usually disks, but also NAND Flash-based solid state drives) or partitions; that is, methods of organizing files on storage devices.

One: disk base

Hard disk (Hard Disk Driver, referred to as HDD) is one of the commonly used storage devices for computers.

1.1: disk structure

1.1.1: physical structure of hard disk

Disk: a hard disk has multiple disks, each with 2 sides

Magnetic head: one magnetic head per side

1.1.2: data structure of hard disk

Sectors: the disk is divided into multiple sectors, each of which stores 512 bytes of data

PS: the first sector of a hard disk, called the boot sector

Track: concentric circles with different radii of the same disk (when the disk rotates, if the head is kept in one position, each head will draw a circular track on the surface of the disk, which is called Track).

Cylinder: a cylindrical surface consisting of the same radius of different plates (in a group of multiple disks, a cylindrical surface (Cylinder) consisting of faces of different plates but multiple tracks in the same radius circle)

PS: solid state disk: electronic chip storage, fast, but data loss can not be recovered

Mechanical hard disk: track storage, average speed, data loss has a chance of recovery

1.1.3: storage capacity

Hard disk storage capacity = number of heads x tracks (cylinders) x number of sectors x bytes per sector

Cylinders / heads / sectors can be used to determine the unique location of each area on the disk

Use fdisk-l to view partition information

[root@localhost ~] # fdisk-l

Here are the details

Disk / dev/sda:42.9 GB, 42949672960 bytes, 83886080 sectors

* Units = sector of 1 512 = 512 bytes

Sector size (logical / physical): 512 bytes / 512 bytes

Igamo size (minimum / best): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x000a58c6disks *

* device Boot Start End Blocks Id System

/ dev/sda1 2048 12584959 6291456 83 Linux / / this is the boot partition

/ dev/sda2 12584960 54527999 20971520 83 Linux

/ dev/sda3 54528000 62916607 4194304 82 Linux swap / Solaris

/ dev/sda4 62916608 83886079 10484736 5 Extended

/ dev/sda5 62918656 83886079 10483712 83 Linux**

12345678910111213141516

1.1.4: interface to the hard disk

According to the data interface, the hard disk can be divided into the following types (the interface speed is not the actual data transmission speed of the hard disk)

ATA (IDE (parallel port)): it is rarely seen now and is gradually replaced by SATA.

SATA (serial port): the full name is Serial ATA, strong anti-interference, support hot plug hot plug and other functions, high speed, strong error correction ability.

SCSI: the full name is Small Computer System Interface (minicomputer system interface). SCSI hard drives are widely used in workstation-level personal computers and servers. CPU has a low occupancy rate during data transmission, high speed, hot plug and hot plug support, etc.

SAS (Serial Attached SCSI): is a new generation of SCSI technology, which is the same as SATA hard disk, which adopts sequential technology to achieve higher transmission speed and can reach 6Gb/s.

1.2:MBR and disk partition representation

1.2.1:MBR

MBR is the master boot record (Master Boot Record), located in the first physical sector of the hard disk (boot sector)

MBR contains the master bootstrap and partition table of the hard disk. The partition table has 4 partition record areas, each accounting for 16 bytes

MBR has up to four partitions to create logical partitions

1.2.2 representation of disk partitions

The common hard disk can be divided into primary partition, extended partition and logical partition, usually there are only four main partitions, and the extended partition can be regarded as a special main partition type, and the logical partition can be established in the extended partition.

The primary partition is generally used to install the operating system, while the extended partition is mostly used to store file data (the data cannot be stored directly, but on the logical partition).

Hard drives, partitions and other devices are represented as files in Linux

Hard disk: for a hard disk device with IDE interface, it is represented as a file name in the form of "hdX". For hard disk devices with SCSI interfaces, it is represented as a file name in the form of "sdX". Among them, "X" can be an alphabet number, such as "a", "b", "c", "d", and so on. For example: represent the first IDE device (hard drive) in the system as "hda" and the second SCSI device as "sdb"

Partition: when representing a partition, it is based on the file name of the hard disk device, and then add the corresponding numeric sequence number of the partition (primary partition, extended partition, logical partition). For example, the first partition on the first IDE hard drive is represented as "hda1" and the second partition is represented as "hda2". The third partition on the second SCSI hard drive is represented as "sdb3" and the fifth partition is represented as "sdb5"

PS: there are only 4 primary partitions in the hard disk

Therefore, the serial numbers of primary and extended partitions are limited to 1-4.

Extended partition is subdivided into logical partition

The sequence number of the logical partition will always start at 5

Types of file systems used in 1.2.3:Linux systems

The file system (File System) type determines the way and efficiency of storing and reading file data in the partition. When formatting the partition, you need to select the file system type to use.

In Windows operating systems, frequently used file system types include FAT32, NTFS and other formats

The following formats are mainly used in Linux systems

EXT4 file system:

1. A partition that stores file and directory data

two。 High performance journal file system

File system used by default in 3.CentOS 6 system

SWAP, swap file system

1. The swap partition established for the Linux system

two。 Swap partition is equivalent to virtual memory, which can alleviate the problem of insufficient physical memory to some extent.

3. It is generally recommended that the size of the swap partition be set to 1.5-2 times the physical memory.

Other file system types supported by Linux

1.FAT16, FAT32, NTFS

2.EXT4 M JFS …

XFS

1. With the logging feature enabled, you are not afraid of data destruction even if there is an outage, and you can recover the data in a short time according to the log record.

two。 High-performance journaling file system, especially good at handling large files, can support millions of terabytes of storage space

3.CentOS 7 systems use the XFS file system by default

Two: detect and confirm the new hard drive

In the Linux server, when the partition planning of the existing hard disk can not meet the requirements (for example, there is too little space left in the root partition to continue to install new system programs), the partitions in the hard disk need to be replanned and adjusted, and sometimes new hard disk devices need to be added to expand the storage space.

Fdisk disk and partition management tools are needed to achieve the above operations. Fdisk is one of the basic tools that come with most Linux systems.

In the system ID number corresponding to the partition, 83 represents the EXT4 partition in Linux, and 8e represents the LVM logical volume

2.1:fdisk command-View or manage disk partitions

Creating, deleting, and changing partitions in the hard disk device are also done through the fdisk command, as long as the device file of the hard disk is used as a parameter.

Fdisk-l [disk device]

Or

Fdisk [disk device]

* when viewing, the one marked with "" is the boot partition * *

Fdisk / dev/sdb enters the interactive partition management interface

Common instruction

P lists the partitions on the hard disk, and the information is displayed in the same format as the "fdisk-l" command

N create a new partition

D delete partition

T change partition type, convert format

W keep configuration

Q exit

L View the system ID number corresponding to the partition

1234567891011121314

Command example

[root@localhost ~] # fdisk / dev/sda1

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

Create a new DOS disk label using the disk identifier 0x3556a5c2.

Command (enter m for help): M

Command operation

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)

With so many instructions, we can enter instructions to achieve our own requirements.

123456789101112131415161718192021222324252627282930313233

2.2: use the "n" command

Use the "n" command to create partitions, including primary and extended partitions.

Enter "p" at the prompt to select to create a primary partition. Enter "e" to select to create an extended partition. After that, select the partition number, start position, end, or partition size to complete the creation of the new partition.

When selecting the partition number, the sequence number of the primary partition and the extended partition can only be 1-4. The starting position of the partition is generally recognized by fdisk by default, and the end position or size can be in the form of''+ sizeM' or "+ sizeG". For example, "+ 20g" means that the capacity of the partition is set to 20GB.

For example: create partition / dev/sdb1

[root@hostname ~] # fdisk / dev/sdb / / first use the fdisk command tool to enter the interactive partition management interface

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

Create a new DOS disk label using the disk identifier 0x227f39cd.

Command (enter m for help): n / / start creating the partition

Partition type:

P primary (0 primary, 0 extended, 4 free)

E extended

Select (default p): / / enter directly and accept the default p: create primary partition

Using default response p

Zone number (1-4, default 1): / / enter directly, accept the default value of 1, and the number of the main partition is 1

Start sector (2048-41943039, default is 2048): / / enter directly and accept the default sector size of 2048

The default value of 2048 will be used

Last sector, + sector or + size {K _ Magne _ M _ G} (2048-41943039, default is 41943039): / / enter directly to allocate all space to the created partition

The default value of 41943039 will be used

Partition 1 has been set to type Linux and the size is set to 20 GiB

Command (enter m for help): t / / set the ID number corresponding to the partition: 82 means set to swap file system

Partition 1 selected

Hex code (enter L to list all codes): 82

The type of partition "Linux" has been changed to "Linux swap / Solaris"

Command (enter m for help): P / / View the partition

Disk / dev/sdb:21.5 GB, 21474836480 bytes, 41943040 sectors

* Units = sector of 1 512 = 512 bytes

Sector size (logical / physical): 512 bytes / 512 bytes

Igamo size (minimum / best): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x227f39cdidentifier *

Device Boot Start End Blocks Id System

/ dev/sdb1 2048 41943039 20970496 82 Linux swap / Solaris

Command (enter m for help): wq / / Save exit

123456789101112131415161718192021222324252627282930313233343536373839

Three: create a file system

The process of creating a file system is also a process of formatting partitions. In Linux systems, different types of partitions such as XFS, EXT4, FAT can be formatted using the mkfs (Make Filesystem, create file system) command format, while Swap swap partitions can be formatted using the mkswap command.

3.1:mkfs command

In fact, the mkfs command is a front-end tool that can automatically load different programs to create various types of partitions, while the back end includes several tool programs related to the mkfs command, which are located in the / sbin/ directory, such as mkfs.ext4 programs that support EXT4 partition format.

* * [root@hostname ~] # ls / sbin/mkfs*

/ sbin/mkfs / sbin/mkfs.ext2 / sbin/mkfs.fat / sbin/mkfs.vfat

/ sbin/mkfs.btrfs / sbin/mkfs.ext3 / sbin/mkfs.minix / sbin/mkfs.xfs

/ sbin/mkfs.cramfs / sbin/mkfs.ext4 / sbin/mkfs.msdos**

12345

Mkfs command: Make Filesystem, create a file system (formatted)

The mkfs command uses the format

Mkfs-t file system type partition device

twelve

CentOS7 defaults to the xfs file system type, so we often use this command now

-[root@localhost ~] # mkfs.xfs / dev/ volume group name / logical volume name

Create an EXT4 file system

When creating an EXT4 file system, specify the type with the "- t ext4" option and specify the partition device to be formatted

For example: format partition / dev/sdb2 to EXT4 file system

[root@hostname] # mkfs-t ext4 / dev/sda2

one hundred and twenty three

Create a FAT32 file system

Generally speaking, it is not recommended to create or use file system types in Windows in Linux systems, including FAT16, FAT32, etc., except in some special cases, such as Windows system is not available, USB disk system is damaged by pie chart, etc.

To create a FAT32 file system on a Linux system, specify the type with the'- t vfat' option and add the'- F 32 'option to specify the version of the FAT.

For example, format the partition / dev/sdb6 as the FAT32 file system (first add the / dev/sdb6 partition through the fdisk tool and set the ID number to 6)

[root@hostname] # mkfs-t vfat-F 32 / dev/sdb6

Or

[root@hostname] # mkfs.vfat-F 32 / dev/sdb6

Two orders mean the same thing.

12345

3.2:mkswap command

In Linux systems, Swap partitions act like "virtual memory" in Windows systems, which can alleviate the shortage of physical memory to some extent. When the current Linux host runs more services and needs more swap space to support applications, a new swap partition can be added for it.

Using the mkswap command tool, you can create a swap file system on a specified partition. The target partition should first set the ID number to 82. 0 through the fdisk tool.

Make swap: creating a swap file system

The format used by the command

Mkswap Partition device

Cat / proc/meminfo | grep "SwapTotal" / / View the total swap space size

Swapon / dev/sdb1 / / enable switching partition / dev/sdb1

Swapoff / dev/sdb1 / / close swap partition / dev/sdb1

123456

For example: create partition / dev/sdb1 as swap partition (first set the ID number to 82 with the fdisk command tool)

[root@hostname ~] # fdisk / dev/sdb / / first use the fdisk command tool to enter the interactive partition management interface

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

Create a new DOS disk label using the disk identifier 0x227f39cd.

Command (enter m for help): n / / start creating the partition

Partition type:

P primary (0 primary, 0 extended, 4 free)

E extended

Select (default p): / / enter directly and accept the default p: create primary partition

Using default response p

Zone number (1-4, default 1): / / enter directly, accept the default value of 1, and the number of the main partition is 1

Start sector (2048-41943039, default is 2048): / / enter directly and accept the default sector size of 2048

The default value of 2048 will be used

Last sector, + sector or + size {K _ Magne _ M _ G} (2048-41943039, default is 41943039): / / enter directly to allocate all space to the created partition

The default value of 41943039 will be used

Partition 1 has been set to type Linux and the size is set to 20 GiB

Command (enter m for help): t / / set the ID number corresponding to the partition: 82 means set to swap file system

Partition 1 selected

Hex code (enter L to list all codes): 82

The type of partition "Linux" has been changed to "Linux swap / Solaris"

Command (enter m for help): P / / View the partition

Disk / dev/sdb:21.5 GB, 21474836480 bytes, 41943040 sectors

* Units = sector of 1 512 = 512 bytes

Sector size (logical / physical): 512 bytes / 512 bytes

Igamo size (minimum / best): 512 bytes / 512 bytes

Disk label type: dos

Disk identifier: 0x227f39cdidentifier *

Device Boot Start End Blocks Id System

/ dev/sdb1 2048 41943039 20970496 82 Linux swap / Solaris

Command (enter m for help): wq / / Save exit

[root@hostname ~] # mkswap / dev/sdb1 / / create partition / dev/sdb1 as swap partition

Setting swap space version 1, size = 20970492 KiB

No tag, UUID=5736a379-3e6c-4e12-9a5c-78cb6a586cf7

[root@hostname ~] # cat / proc/meminfo | grep "SwapTotal" / / View the total swap space

SwapTotal: 4194300 kB / / used to be more than 4G

[root@hostname ~] # swapon / dev/sdb1 / / enable switching partition / dev/sdb1

[root@hostname ~] # cat / proc/meminfo | grep "SwapTotal" / / check the total swap space again

SwapTotal: 25164792 kB / / now 25G

[root@hostname ~] # swapoff / dev/sdb1 / / close swap partition / dev/sdb1

[root@hostname ~] # cat / proc/meminfo | grep "SwapTotal" / / check the total swap space again

SwapTotal: 4194300 kB / / swap space has become more than 4G.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

Four: Mount and unmount the file system

In Linux system, access to resources in various storage devices (such as reading, saving files, etc.) is carried out through the directory structure. Although the system core can manipulate various devices through "device files", it is necessary for users to add a "mount" process to access resources in storage devices like normal access directories.

Of course, during the installation of the Linux operating system, automatically created or recognized partitions are usually mounted automatically by the system, such as "/" partition, "boot" partition and so on. However, for the newly added hard disk partitions, CDs and other devices, sometimes the administrator is required to mount manually. In fact, the user accesses the file system established after formatting. When mounting a partition, you must set up a directory for it as the mount point (or mount point), through which users can access files and directory data in the device.

4.1:mount command-Mount the file system

Mount the file system, ISO image to the specified folder

Basic format of mount command

Mount displays information about each mounted partition (file system) in the current system, and the most recently mounted file information will be displayed at the end.

Mount [- t file system type] storage device mount point directory

Mount-o loop ISO image file mount point directory

Mount-a mounts everything that can now be mounted.

12345

File system types are usually omitted and are automatically identified by the system

The storage device is the device file name (such as / dev/sdb1, / dev/cdrom) or network resource path of the corresponding partition

The mount point is the directory specified by the user for mount

The device file corresponding to the CD usually uses'/ dev/cdrom', 'which is actually a connection file that connects to the actual CD device' / dev/sr0'. Both names refer to optical disc devices. Because the CD is a read-only storage medium, the system will display a message of 'mounting read-only'' when mounting.

For example, mount the CD device to the / media/cdrom directory

[root@hostname ~] # mount / dev/cdrom / media/cdrom

twelve

The same is true when mounting a Linux partition or USB disk device, just specify the correct device location and mount directory.

For example: Mount the / dev/sdb1 partition to the newly created / ccc directory

[root@hostname ~] # mkdir / ccc

[root@hostname ~] # mount / dev/sdb1 / ccc

one hundred and twenty three

In the Linux system, the USB disk device is simulated as a SCSI device, so it is not significantly different from the partition in the ordinary SCSI hard disk. U disk generally uses FAT16 or FAT32 file system, if you are not sure the location of U disk device files, you can first execute the 'fdisk-l' command to check and confirm.

For example: Mount the USB disk device located in / dev/sdc1 to the newly created / ccc/usbdisk directory

[root@hostname ~] # mkdir / ccc/usbdisk

[root@hostname ~] # mount / dev/sdc1 / ccc/usbdisk

one hundred and twenty three

File systems such as proc,sysfs,tmpfs are temporary file systems needed for Linux to run, and there are no actual hard disk partitions corresponding to them, so they have also become 'pseudo file systems'.

For example, the proc file system actually maps some of the data in memory and CPU registers

In practical work, we may often download some ISO image files of software or application systems from the Internet. If we cannot burn the CD, we need to decompress it before browsing and using the file data in it. If you use the mount mount command, you can browse without unlocking the package and use the data in the ISO image file.

ISO image files are usually treated as a special "loopback" file system, so you need to add the "- o loop" option when mounting.

For example, mount the downloaded DVD CD image file "CentOS-7-x86_64-DVD-1611.iso" of the CentOS system to the / media/mnt directory.

[root@hostname] # mount-o loop CentOS-7-x86_64-DVD-1611.iso / media/mnt

twelve

4.2:umount command-unmount the file system

When unmounting a file system, use the file name suspended in a single directory or the corresponding device as the unmount parameter.

In Linux system, because the same device can be mounted to multiple directories, it is generally recommended to uninstall it through the directory location of the mount point.

Umount command-unmount a mounted file system

Use the basic format of the command

Umount storage device location

Umount mount point directory

one hundred and twenty three

For example, uninstall the previously mounted Linux partition and CD-ROM device by umount command

[root@hostname ~] # umount / ccc / / Unmount the corresponding partition through the mount point directory

[root@hostname ~] # umount / dev/cdrom / / Uninstall the CD through the device file

one hundred and twenty three

4.3: set up automatic mounting of the file system

The / etc/fstab file in the system can be regarded as the configuration file of the mount command, which stores the static mount data of the file system. Every time the Linux system boots, it will automatically read the contents of this file and mount the specified file system automatically.

The default fstab file includes the mount configuration of pseudo file systems such as root partition, / boot partition, swap partition and proc,tmpfs.

In the / etc/fstab file, each line records the mount configuration information for a partition or device, including six fields from left to right, meaning as follows

Field

explain

First field

Partition / device name / device volume label name

Second field

Mount point, the location of the mount point directory of the file system

Third field

File system type, such as EXT4,Swap, etc

Fourth field

Mount parameters (including permissions and features), that is, parameters that can be used after the "- o" option of the mount command. For example: default,rw,ro,noexec represents the default parameter, writable, read-only, and disable the executor.

Fifth field

Indicates whether the file system needs dump backup (dump is a backup tool). If set to 1, it is required. If set to 0, it will be ignored by dump.

Sixth field

This field is used to determine the order in which disk checks are performed when the system boots. 0: no check. 1: priority inspection. 2: secondly, check that the root partition should be set to 1 and the other partition should be set to 2.

By adding the corresponding mount configuration in the "/ etc/fstab" file, you can automatically mount the specified partition after boot.

For example, add the configuration of the auto-mount partition / dev/sdb1

[root@hostname ~] # vi / etc/fstab

... / / omit part of the content.

/ dev/sdb1 / ccc ext4 default 0 0

12345

4.4:df command-View disk usage

The mount command without options and parameters can display the mount of the partition. To understand the disk usage of each file system that has been mounted in the system (such as remaining disk space, etc.), you can use the df command.

The df command uses a file or device as a command parameter, and the more common option is "- h"- T". The "- h" option displays the capacity units that are easier to read, while the "- T" option is used to display the type of the corresponding file system.

For example, execute the "df-hT" command to view the disk usage of each file system mounted on the current system.

[root@hostname ~] # df-hT

File system type capacity used available used mount point

/ dev/sda2 xfs 20G 3.3G 17G 17% /

Devtmpfs devtmpfs 898M 0898M 0% / dev

Tmpfs tmpfs 912M 0912m 0% / dev/shm

Tmpfs tmpfs 912m 9.0m 903m 1% / run

Tmpfs tmpfs 912M 0912m 0% / sys/fs/cgroup

/ dev/sda5 xfs 10G 37m 10G 1% / home

/ dev/sda1 xfs 6.0G 174m 5.9G 3% / boot

Tmpfs tmpfs 183M 12K 183M 1% / run/user/42

Tmpfs tmpfs 183M 0 183M 0% / run/user/0

12345678910111213

-

Copyright notice: this article is the original article of CSDN blogger "Lord Tang Zheng", in accordance with the copyright agreement of CC 4.0BY-SA. Please attach the original source link and this statement to reprint it.

Original link: https://blog.csdn.net/CN_TangZheng/article/details/102866952

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