磁盘分区

磁盘分区是使用分区编辑器在磁盘上划分几个逻辑部分,碟片一旦划分成数个分区,不同的类的目录和文件可以存储进不同的分区。
主分区和推展分区可以有4个
拓展分区最多只能有一个
逻辑分区是拓展分区持续切割出来的分区
能够被格式化后作为数据访问的分区为主分区和逻辑分区,扩展分区不能格式化

格式化

格式化又称逻辑格式化,它是指根据用户选定的文件类型,在磁盘的特定区域写入特定的数据,在分区中划出一片用于存放文件分配表,目录表等用于文件管理的磁盘空间。
Linux——磁盘管理 - 图1

挂载

挂载就是利用一个目录当成进入点,将磁盘分区的数据放置在该目录下,也就是进入该目录就可以读取分区的意思,这个操作我们称之为挂载,那个进入点的目录我们称为挂载点

df

查看挂载磁盘总容量,使用容量,剩余容量,默认当时是kb

查看磁盘分区的使用状况
-l 仅显示本地磁盘(默认)
-a 显示所有文件系统的磁盘使用情况,包含比如/proc/(查看包括0字节文件)
-h 以1024进制计算最合适的单位显示磁盘容量
-H 以1000进制计算最合适的单位显示磁盘容量
-T 显示磁盘分区类型(各个分区使用的文件系统)
-t 显示指定类型文件系统的磁盘分区 例如:df -lhT -t ext4
-x 不显示指定类型文件系统的磁盘分区 例如:df -lhT -x ext4

  1. # df
  2. 文件系统 1K-块 已用 可用 已用% 挂载点
  3. /dev/mapper/centos-root 17811456 1117708 16693748 7% /
  4. devtmpfs 485820 0 485820 0% /dev
  5. tmpfs 497948 0 497948 0% /dev/shm
  6. tmpfs 497948 7860 490088 2% /run
  7. tmpfs 497948 0 497948 0% /sys/fs/cgroup
  8. /dev/sda1 1038336 135296 903040 14% /boot
  9. tmpfs 99592 0 99592 0% /run/user/1000
  10. tmpfs 99592 0 99592 0% /run/user/0

人性化的显示磁盘空间

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G  4.0G   34G  11% /
devtmpfs        909M     0  909M   0% /dev
tmpfs           920M     0  920M   0% /dev/shm
tmpfs           920M  448K  919M   1% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
tmpfs           184M     0  184M   0% /run/user/0

fdisk

查找已经安装并且未格式化的磁盘 使用命令 fdisk -l 列出所有的磁盘,使用命令的例子以及结果如下:

fdisk -l

eg

# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c8e4e

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

分区

$fdisk -l

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

$ fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xeedd314c.

Command (m for help): m

输入n

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

输入p

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): w

输入w保存

Command (m for help): w
The partition table has been altered!

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

查看支持的文件系统

ls -l /lib/modules/$(uname -r)/kernel/fs
total 20
-rw-r--r--. 1 root root 5992 Nov 17  2020 binfmt_misc.ko.xz
drwxr-xr-x. 2 root root   25 Nov 30  2020 btrfs
drwxr-xr-x. 2 root root   30 Nov 30  2020 cachefiles
drwxr-xr-x. 2 root root   24 Nov 30  2020 ceph
drwxr-xr-x. 2 root root   24 Nov 30  2020 cifs
drwxr-xr-x. 2 root root   26 Nov 30  2020 cramfs
drwxr-xr-x. 2 root root   23 Nov 30  2020 dlm
drwxr-xr-x. 2 root root   26 Nov 30  2020 exofs
drwxr-xr-x. 2 root root   24 Nov 30  2020 ext4
drwxr-xr-x. 2 root root   60 Nov 30  2020 fat
drwxr-xr-x. 2 root root   27 Nov 30  2020 fscache
drwxr-xr-x. 2 root root   42 Nov 30  2020 fuse
drwxr-xr-x. 2 root root   24 Nov 30  2020 gfs2
drwxr-xr-x. 2 root root   25 Nov 30  2020 isofs
drwxr-xr-x. 2 root root   24 Nov 30  2020 jbd2
drwxr-xr-x. 2 root root   25 Nov 30  2020 lockd
-rw-r--r--. 1 root root 5224 Nov 17  2020 mbcache.ko.xz
drwxr-xr-x. 6 root root  137 Nov 30  2020 nfs
drwxr-xr-x. 2 root root   46 Nov 30  2020 nfs_common
drwxr-xr-x. 2 root root   24 Nov 30  2020 nfsd
drwxr-xr-x. 2 root root 4096 Nov 30  2020 nls
drwxr-xr-x. 2 root root   27 Nov 30  2020 overlayfs
drwxr-xr-x. 2 root root   27 Nov 30  2020 pstore
drwxr-xr-x. 2 root root   28 Nov 30  2020 squashfs
drwxr-xr-x. 2 root root   23 Nov 30  2020 udf
drwxr-xr-x. 2 root root   23 Nov 30  2020 xfs

格式化磁盘

mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

挂载目录

# mkdir /sdb1
# mount /dev/sdb1 /sdb1

查看挂载结果

# df -Th

开启挂载

[root@k8s-master data1]# echo "/dev/sdb1 /sdb1 xfs defaults  0 0" >> /etc/fstab
[root@k8s-master data1]# cat /etc/fstab

du

统计磁盘上的目录或文件大小
-b 以byte为单位统计文件
-k 以KB为单位统计文件
-m 以MB为单位统计文件
-h 按照1024进制以最适合的单位统计文件
-H 按照1000进制以最适合的单位统计文件

du -h --max-depth=1

parted

fdisk 【只支持MSDOS分区布局】
parted 【支持MSDOS、GPT分区布局】
分区有三个步骤:
第一个步骤就是用分区工具进行分区
第二个步骤就是创建文件系统(也就是格式化)因为只有创建文件系统了,才能往里面存取数据。
第三个步骤就是挂载,光分完区不对其进行挂载的话照样是白扯。

[root@k8s-master ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mktable                                                          
New disk label type? gpt                                                  
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes                                                               
(parted) mkpart                                                           
Partition name?  []? primary                                              
File system type?  [ext2]? xfs                                            
Start? 0                                                                  
End? 100%                                                                 
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i                                                          
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  10.7GB  10.7GB               primary

(parted) q                                                                
Information: You may need to update /etc/fstab.

gdisk

# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): d
Using 1

Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 5D8297E1-CBEE-4E03-9575-872D7A48595A
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2-sector boundaries
Total free space is 20971453 sectors (10.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-20971486, default = 34) or {+-}size{KMGTP}: 
Last sector (34-20971486, default = 20971486) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

参考

https://blog.csdn.net/wudinaniya/article/details/82850798
https://www.cnblogs.com/shangpolu/p/8177481.html