content
Linux Disk Partitioning and Mounting
Step 1: Create a new hard drive
Step 2: Partition the virtual machine hard disk
Step 3: Format the virtual machine hard disk partition
Step 4: Mount the disk to the newdisk directory in the root directory
Step 5: Realize permanent mount (ie restart does not disappear)
du -h queries the disk usage of the specified directory
Linux Disk Partitioning and Mounting
linux partition
Principle introduction
1. For Linux, wulun has several partitions, which directory to use. In the final analysis, there is only one root directory, an independent and unique file structure. Each partition in Linux is used to form part of the entire file system.
2. Linux adopts a processing method called "loading". Its entire file system contains a complete set of files and directories, and associates a partition with a directory. This is a partition to be loaded. Make its storage space in a directory.
Partition and file relationship diagram:
Hard drive description
1. Linux hard disk is divided into IDE hard disk and SCSI hard disk. At present, it is basically SCSI hard disk.
2. For IDE hard disks , the drive identifier is "hdx~", where "hd" indicates the type of the device where the partition is located, which here refers to IDE hard disks. "x" is the disk number (a is the basic disk, b is the basic slave disk, c is the auxiliary master disk, d is the auxiliary slave disk), "~" represents the partition, the first four partitions are represented by numbers 1 to 4, which are Primary or extended partitions, starting from 5 are logical partitions. For example, hda3 is represented as the third primary or extended partition on the first IDE hard disk, and hdb2 is represented as the second primary or extended partition on the second IDE hard disk.
3. For SCSI hard disks , it is marked as "sdx~", SCSI hard disks use "sd" to indicate the type of the device where the partition is located, and the rest are the same as IDE hard disks (x can be abcd, corresponding to the first, second, 3, 4 hard drives).
View all device mounts
Command: lsblk or lsblk -f
[root@kongchao03 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1023M 0 part /boot
├─sda2 8:2 0 17G 0 part /
└─sda3 8:3 0 2G 0 part [SWAP]
sr0 11:0 1 729.9M 0 rom /run/media/root/20210907_143734
[root@kongchao03 ~]#
Here sda1, 2, and 3 represent the first partition and the second partition of the first hard disk respectively...
Mount case
Step 1: Create a new hard drive
In the virtual machine menu, set to add a hard disk, restart after completion to take effect
Use the lsblk command to view
Step 2: Partition the virtual machine hard disk
Partition command: fdisk /dev/sdb
start partitioning sdb
m show a list of commands
p shows disk partitions the same as fdisk -l
n add a partition
d delete partition
w write and exit
Note: After starting the partition, enter n, add a new partition, and then select p, the partition type is the main partition. Enter twice by default to leave all the space, and finally enter w to write to the partition and exit, if you do not save and exit, enter q
[root@kongchao03 ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xdf03b737 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):m
命令操作
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)
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-2097151,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-2097151,默认为 2097151):
将使用默认值 2097151
分区 1 已设置为 Linux 类型,大小设为 1023 MiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@kongchao03 ~]#
Step 3: Format the virtual machine hard disk partition
Format the disk, the UUID will be assigned after formatting
Formatting command: mkfs -t ext4 /dev/sdb1
where ext4 is the partition type
mkfs -t ext4 /dev/sdb1
lsblk -f
Step 4: Mount the disk to the newdisk directory in the root directory
(You can also go to other directories)
mount: associate a partition with a directory,
mount mount
Mount syntax: mount device name mount directory (mount directory is arbitrary)
[root@kongchao03 ~]# cd /
[root@kongchao03 /]# mkdir newdisk
[root@kongchao03 /]# mount /dev/sdb1 /newdisk
[root@kongchao03 /]# lsblk -f
cd enters the mount point and creates a file in it, in fact, a new file is created on the partition
The storage under the Linux root is just a directory, the real data is still stored in the partition on the hard disk
umount unmount
grammar:
umount mount point
or umount device name
umount /dev/sdb1
lsblk -f
Mounting with the command line (mount /dev...) will fail after restarting, that is, the mount point will disappear after restarting
Step 5: Realize permanent mount (ie restart does not disappear)
The essence of permanent mount is to automatically mount
Permanent mount: mount by modifying / etc/fstab
After the addition is complete, execute mount -a or reboot to take effect
[root@kongchao03 /]# vim /etc/fstab
[root@kongchao03 /]# mount -a
df -h query disk usage
[root@kongchao03 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda2 17G 5.8G 11G 37% /
devtmpfs 978M 0 978M 0% /dev
tmpfs 993M 0 993M 0% /dev/shm
tmpfs 993M 9.0M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sdb1 991M 2.6M 922M 1% /newdisk
/dev/sda1 991M 128M 797M 14% /boot
tmpfs 199M 20K 199M 1% /run/user/0
/dev/sr0 730M 730M 0 100% /run/media/root/20210907_143734
[root@kongchao03 ~]#
du -h queries the disk usage of the specified directory
Command: du [parameter] /directory ( queries the disk usage of the specified directory, the default is the current directory)
Parameter options:
parameter | meaning |
-s | Summary of the size of the specified directory |
-h | with unit of measure |
-a | Include files |
--max-depth=1 | subdirectory depth |
-c | Add summary values while listing details |
Parameters can be used in combination
Example: Query the disk usage of the /opt directory with a depth of 1
[root@kongchao03 /]# cd /opt
[root@kongchao03 opt]# ls
rh
[root@kongchao03 opt]# du -h --max-depth=1 /opt
4.0K /opt/rh
8.0K /opt
[root@kongchao03 opt]#
The parameters are combined to use:
[root@kongchao03 opt]# du -hac --max-depth=1 /opt
4.0K /opt/rh
8.0K /opt
8.0K 总用量