Home  >  Article  >  System Tutorial  >  Linux, the King of Hard Drive Storage – How to Manage Hard Drives Elegantly?

Linux, the King of Hard Drive Storage – How to Manage Hard Drives Elegantly?

PHPz
PHPzforward
2024-02-15 09:30:03903browse

In today's digital era, data storage and management are becoming more and more important. For Linux users, hard disk management is an inevitable topic. However, beginners often get bogged down by the complexity of Linux hard drive management. Don’t worry, this article will teach you how to manage your hard drive elegantly, allowing you to easily control your Linux system.

硬盘存储之王者Linux – 如何优雅地管理硬盘?

Linux mount new hard disk

1. View hard disk information

Order:

fdisk -l
# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes                                         
 #第一块硬盘的信息和分区信息
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003a7b4

  Device Boot      Start        End      Blocks  Id  System
    /dev/vda1  *          1        2611    20970496  83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes                                       
 #第二块硬盘的信息和分区信息
16 heads, 63 sectors/track, 208050 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

2. Create a new hard disk partition

Order:

fdisk /dev/vdb
# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1e694286.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
        switch off the mode (command 'c') and change display units to
        sectors (command 'u').

Command (m for help): n
Command action
  e  extended                               
 #e为创建扩展分区
  p  primary partition (1-4)            
#p为创建逻辑分区
p
Partition number (1-4): 1          
#在这里输入1,就进入划分逻辑分区阶段了;
First cylinder (1-208050, default 1): 1      
#分区的Start 值,这里最好直接按回车,否则可能会造成空间浪费;
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-208050, default 208050): 208050     
 #分区的Over值,我就分一个区
Using default value 208050

Command (m for help): w                            
#最后输入w回车保存退出。
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

fdisk You can use the m command to view the internal commands of the fdisk command;
a: Command specifies the boot partition;
d: Command to delete an existing partition;
l: command displays a list of partition ID numbers;
m: View fdisk command help;
n: command to create a new partition;
p: command displays partition list;
t: Command to modify the type ID number of the partition;
w: The command is to save the modifications to the partition table and let them take effect;

3. Confirm the new partition information

Order:

fdisk -l
# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003a7b4

  Device Boot      Start        End      Blocks  Id  System
    /dev/vda1  *          1        2611    20970496  83  Linux

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x1e694286

  Device Boot      Start        End      Blocks  Id  System
    /dev/vdb1              1      208050  104857168+  83  Linux         
 #刚创建好的分区信息

4. Format partition

Order:

mkfs.ext4 /dev/vdb1
# mkfs.ext4 /dev/vdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=                  
#文件系统标签
OS type: Linux               
 #操作系统类型
Block size=4096 (log=2)          
#块大小
Fragment size=4096 (log=2)      
#分块大小
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214292 blocks
1310714 blocks (5.00%) reserved for the super user
First data block=0         
 #第一个数据块
Maximum filesystem blocks=4294967296
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

Writing inode tables: done                          
#写入inode表
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Linux partition type introduction:
With the application of Linux systems in current business, the weaknesses of Linux file systems have gradually been revealed. Among them, the ext2 file system used by the system by default is a non-log file system. This is a fatal weakness in applications in key industries. The Ext3 file system is directly developed from the Ext2 file system. The current ext3 file system is already very stable and reliable. It is fully compatible with Ext2 file system.
Features of Ext3: high availability, data integrity, data integrity, data conversion, multiple log modes
The same Ext4 is fully compatible with Ext3. Compared with Ext3, Ext4 supports larger storage. Ext3 currently supports a maximum 16TB file system and a maximum 2TB file, while Ext4 supports a 1EB file system and a 16TB file respectively, and That is, Ext3 currently only supports 32,000 subdirectories, while Ext4 supports an unlimited number of subdirectories. Ext4 introduces the popular concept of extents in modern file systems. Each extent is a set of continuous data blocks. The above file is represented as "the file data Saved in the next 25,600 data blocks", which improves a lot of efficiency.

5. Create a mounting directory

Order:

mkdir /data

6. Mount partition

Order:

mount /dev/vdb1 /data

7. Check the hard disk size and mount partition

Order:

df -Th
# df -Th
Filesystem    Type  Size  Used Avail Use% Mounted on
/dev/vda1      ext4    20G  1.1G  18G  6% /
tmpfs          tmpfs  3.9G    0  3.9G  0% /dev/shm
/dev/vdb1      ext4    99G  60M  94G  1% /data         
 #新挂载的分区

8. Configure automatic mounting at boot

Order:

vim /etc/fstab /dev/vdb1 /data ext4 defaults 1 1
# blkid
/dev/vda1: UUID="b7aae0d4-268c-4b60-914a-f3b48e22819c" TYPE="ext4"
/dev/vdb1: UUID="5de835dd-5322-46f0-8728-3d4ae7d83b54" TYPE="ext4"
# cat /etc/fstab
#
# 
/etc/fstab
# Created by anaconda on
 Tue Mar 27 04:51:55 2018
#
# 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
#
UUID=b7aae0d4-268c-4b60-914a-f3b48e22819c      /                      ext4      defaults      1 1
UUID=5de835dd-5322-46f0-8728-3d4ae7d83b54    /data                ext4  defaults    1 1
tmpfs                  /dev/shm                tmpfs  defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                  /sys                    sysfs  defaults        0 0
proc                    /proc                  proc    defaults        0 0

Swap partition

The Swap partition is actually the same as the virtual memory on Windows. When the system's physical memory is not enough, the Swap partition releases a part of the physical memory for use by the currently running program. The freed space may come from some programs that have not operated for a long time. The freed space is temporarily saved in the Swap partition. When those programs are about to run, the saved data is restored from the Swap partition to the memory.

The following introduces two methods of creating swap:
•Create a new disk partition as a swap partition
•Use files as Swap partition

Create a new disk partition as a swap partition

1. Use the fdisk command to partition the disk, add a swap partition, create a new partition, and use the "t" command in fdisk to change the newly added partition id to 82
(Linux swap type)

2. Format the swap partition. The sdb2 here depends on the actual partition device name displayed by the p command after you add it

mkswap /dev/sdb1

3. Start a new swap partition

swapon /dev/sdb1

4. To enable this swap partition automatically when the system starts, you can edit /etc/fstab and add the following line

/dev/sdb1 swap swap defaults 0 0

Use files as Swap partitions

1. Create a file to be used as a swap partition: add a 1GB swap partition. The command is written as follows, where the count is equal to the number of blocks you want (bs*count=file size).

dd if=/dev/zero of=/root/swapfile bs=1M count=1024

2. Format as a swap partition file and establish a swap file system:

mkswap /root/swapfile

3. Use swap partition file:

swapon /root/swapfile

4. To enable the system automatically when it boots up, add a line in the file /etc/fstab:

/root/swapfile swap swap defaults 0 0

In short, the hard disk management tools for Linux are very diverse and powerful. For beginners, basic commands are enough for daily use. For users who want a deeper understanding, there are many advanced methods and techniques waiting for you to explore. I hope this article can be helpful to you and make you more comfortable when using Linux systems.

The above is the detailed content of Linux, the King of Hard Drive Storage – How to Manage Hard Drives Elegantly?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete