Linux磁盘管理

磁盘管理


一、 MBR与GPT

在使用新磁盘之前,你必须对其进行分区。MBR(Master Boot Record)和GPT(GUID Partition Table)是在磁盘上存储分区信息的两种不同方式。这些分区信息包含了分区从哪里开始的信息,这样操作系统才知道哪个扇区是属于哪个分区的,以及哪个分区是可以启动的。在磁盘上创建分区时,你必须在MBR和GPT之间做出选择

MBR是Master Boot Record的简称,也就是主引导记录,是位于磁盘最前边的一段引导(Loader)代码,主要用来引导操作系统的加载与启动

特点:
1.MBR支持最大2TB磁盘,它无法处理大于2TB容量的磁盘
2.只支持最多4个主分区。若想要更多分区,需要创建扩展分区,并在其中创建逻辑分区

[root@yy ~]# fdisk -l /dev/sda | grep type
Disklabel type: dos            //dos代表MBR格式

GPT磁盘是指使用GUID分区表的磁盘,GUID磁盘分区表(GUID Partition Table,缩写:GPT)其含义为“全局唯一标识磁盘分区表”,是一个实体硬盘的分区表的结构布局的标准

特点:
1.GPT对磁盘大小没有限制
2.最多可以创建128个分区

[root@yy ~]# fdisk -l /dev/sdb |grep type    //gpt代表GPT格式
Disklabel type: gpt

注意:
MBR与GPT之间互相转换会导致数据丢失

二、Linux中磁盘命名方式

Linux中磁盘的命名方式与磁盘的接口有关,规则如下:

  • 传统IDE接口硬盘:/dev/hd[a-z]
  • SCISI接口硬盘:/dev/sd[a-z]
  • 虚拟化硬盘:/dev/vd[a-z]

在设备名称的定义规则如下, 其他的分区可以以此类推
系统的第一块SCSI接口的硬盘名称为/dev/sda
系统的第二块SCSI接口的硬盘名称为/dev/sdb
系统中分区由数字编号表示, 1-4留给主分区使用和扩展分区, 逻辑分区从5开始

三、磁盘容量检查

df    //查看已挂载的磁盘容量,不加参数以k为单位
-  -i   //查看inode使用情况
-  -h   //以G或者T或者M人性化方式显示
-  -T   //查看文件类型

实例:
[root@yy ~]# df -i
Filesystem            Inodes IUsed    IFree IUse% Mounted on
devtmpfs              470071   412   469659    1% /dev
tmpfs                 474825     1   474824    1% /dev/shm
tmpfs                 474825   665   474160    1% /run
tmpfs                 474825    17   474808    1% /sys/fs/cgroup
/dev/mapper/cl-root 33488896 51822 33437074    1% /
/dev/sda1             524288   310   523978    1% /boot
/dev/mapper/cl-home 16349184     3 16349181    1% /home
tmpfs                 474825     5   474820    1% /run/user/0

[root@yy ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  1.8G     0  1.8G   0% /dev
tmpfs               tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs               tmpfs     1.9G  8.9M  1.9G   1% /run
tmpfs               tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        64G  2.4G   62G   4% /
/dev/sda1           xfs      1014M  214M  801M  22% /boot
/dev/mapper/cl-home xfs        32G  255M   31G   1% /home
tmpfs               tmpfs     371M     0  371M   0% /run/user/0

使用lsblk查看分区情况:
[root@yy ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cl-root 253:0    0 63.9G  0 lvm  /
  ├─cl-swap 253:1    0    4G  0 lvm  [SWAP]
  └─cl-home 253:2    0 31.2G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1 10.1G  0 rom  

du      //查看目录或者文件的容量,不加参数以k为单位:
     -s    //列出总和
     -h    //人性化显示容量信息

[root@yy ~]# du -sh anaconda-ks.cfg 
4.0K	anaconda-ks.cfg

四、磁盘分区

分区工具有fdisk和gdisk,当硬盘小于2T的时候我们应该用fdisk来分区,而当硬盘大于2T的时候则应用gdisk来进行分区

1、fdisk分区

fdisk //磁盘分区工具,默认采用MBR分区表
-l //列出系统中所有磁盘

[root@yy ~]# fdisk /dev/sdc    //对/dev/sdb进行分区

Welcome to fdisk (util-linux 2.32.1).
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.
Created a new DOS disklabel with disk identifier 0xc2cfdc02.

Command (m for help): m    //获取帮助
Command action
   a   toggle a bootable flag   //切换分区启动标记
   b   edit bsd disklabel     //编辑sdb磁盘标签
   c   toggle the dos compatibility flag    //切换dos兼容模式
   d   delete a partition     //删除分区
   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 //创建新的Sun磁盘标签
   t   change a partition's system id   //修改分区ID,可以通过l查看id
   u   change display/entry units       //修改容量单位,磁柱或扇区
   v   verify the partition table       //检验分区表
   w   write table to disk and exit     //保存退出
   x   extra functionality (experts only)   //拓展功能

//创建主分区
Command (m for help): n    //新建分区
Partition type             
   p   primary (0 primary, 0 extended, 4 free)        //主分区
   e   extended (container for logical partitions)    //扩展分区
Select (default p):      //指定主分区或扩展分区,默认为主分区,回车即可

Using default response p.
Partition number (1-4, default 1):      //分区序号
First sector (2048-10485759, default 2048):    //起始扇区位置,回车默认即可 
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +512M   //指定分区的大小

Created a new partition 1 of type 'Linux' and of size 512 MiB.

Command (m for help): p    //打印分区信息
Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0xc2cfdc02

Device     Boot Start     End Sectors  Size Id Type
/dev/sdc1        2048 1050623 1048576  512M 83 Linux

Command (m for help): w    //保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@yy ~]# partprobe /dev/sdc    //刷新硬盘空间,让刚才的操作立即生效
[root@yy ~]# lsblk     //查看磁盘使用情况
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cl-root 253:0    0 63.9G  0 lvm  /
  ├─cl-swap 253:1    0    4G  0 lvm  [SWAP]
  └─cl-home 253:2    0 31.2G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
sdc           8:32   0    5G  0 disk 
└─sdc1        8:33   0  512M  0 part 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1 10.1G  0 rom 
2.gdisk分区
//安装gdisk工具
[root@yy ~]# dnf -y install gdisk


[root@yy ~]# gdisk /dev/sdb    //给第二块磁盘分区,格式为GPT
GPT fdisk (gdisk) version 1.0.3

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

Creating new GPT entries.

Command (? for help): n     //新建分区
Partition number (1-128, default 1): 
First sector (34-10485726, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-10485726, default = 10485726) or {+-}size{KMGTP}: +512M    //指定分区大小为512M
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): p   //打印分区信息
Disk /dev/sdb: 10485760 sectors, 5.0 GiB
Model: VMware Virtual S
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): C7EA5F68-1016-4492-9926-9267EDAD56CF
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 10485726
Partitions will be aligned on 2048-sector boundaries
Total free space is 9437117 sectors (4.5 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1050623   512.0 MiB   8300  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.

[root@yy ~]# partprobe /dev/sdb    
[root@yy ~]# lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  100G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   99G  0 part 
  ├─cl-root 253:0    0 63.9G  0 lvm  /
  ├─cl-swap 253:1    0    4G  0 lvm  [SWAP]
  └─cl-home 253:2    0 31.2G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
└─sdb1        8:17   0  512M  0 part 
sdc           8:32   0    5G  0 disk 
└─sdc1        8:33   0  512M  0 part 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sr0          11:0    1 10.1G  0 rom  

五、磁盘格式化

使用mkfs命令格式化磁盘,创建文件系统

//mkfs常用的选项有
    -b  //设定数据区块占用空间大小,目前支持1024、2048、4096 bytes每个块。  
    -t  //用来指定什么类型的文件系统,可以是ext3,ext4, xfs
    -i  //设定inode的大小
    -N  //设定inode数量,防止Inode数量不够导致磁盘不足
    -L  //预设该分区的标签label

[root@yy ~]# mkfs.xfs /dev/sdb1    //格式化/dev/sdb1分区为xfs文件系统格式
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=32768 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=131072, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=1368, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@yy ~]# blkid /dev/sdb1   //查看该分区的UUID,文件系统
/dev/sdb1: UUID="799936a2-586b-4043-81b3-b2b76812a5db" BLOCK_SIZE="512" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="c6f32c93-4cda-42be-9065-a853b9140940"

[root@yy ~]# mkfs.ext4 /dev/sdc1  //格式化/dev/sbc1为ext4文件系统格式
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 131072 4k blocks and 32768 inodes
Filesystem UUID: c4ed228b-d0fd-49fb-91b8-681f24ef0b41
Superblock backups stored on blocks: 
	32768, 98304

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

[root@yy ~]# blkid /dev/sdc1    //查看该分区的UUID、文件系统
/dev/sdc1: UUID="c4ed228b-d0fd-49fb-91b8-681f24ef0b41" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c2cfdc02-01"

六、挂载磁盘

在上面的内容中讲到了磁盘的分区和格式化, 那么格式化完了后, 如何使用, 这就涉及到了挂载这块磁盘

挂载分区前需要创建挂载点, 挂载点以目录形式出现
如果往挂载点目录写入数据, 实际上会写入到该分区
挂载点建议是空目录, 不是也不影响挂载分区的使用,但是会隐藏原有的文件

1.临时挂载

命令:mount挂载磁盘,实质为文件系统指定访问入口
mount -t //指定文件系统挂载分区,如ext3,ext4, xfs
mount -a //重新读取/etc/fstab配置文件的所有分区
mount -o //指定挂载参数

[root@yy ~]# mkdir /mnt/sdb1
[root@yy ~]# mount /dev/sdb1 /mnt/sdb1/
[root@yy ~]# df -h /dev/sdb1
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       507M   30M  478M   6% /mnt/sdb1
2.永久挂载
[root@yy ~]# blkid /dev/sdc1
/dev/sdc1: UUID="c4ed228b-d0fd-49fb-91b8-681f24ef0b41" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c2cfdc02-01"
[root@yy ~]# mkdir /mnt/sdc1
[root@yy ~]# vi /etc/fstab 
UUID="c4ed228b-d0fd-49fb-91b8-681f24ef0b41" BLOCK_SIZE="4096" /mnt/sdc1 ext4 defaults 0 0
[root@yy ~]# mount -a
mount: /etc/fstab: parse error at line 16 -- ignored
[root@yy ~]# df -h /dev/sdc1
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.8G     0  1.8G   0% /dev

七、交换分区SWAP

交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。

交换分区一般指定虚拟内存的大小为实际内存的1~1.5倍。如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。

1.swapfile创建
//创建swapfile 
[root@yy ~]# dd if=/dev/zero of=/opt/yy.swap bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 7.59952 s, 70.6 MB/s

//格式化swap分区
[root@yy ~]# mkswap -f /opt/yy.swap 
mkswap: /opt/yy.swap: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=cda63aba-c65f-46f2-9559-78ae298e0a0b

//检测当前swap分区情况
[root@yy ~]# free -m   //没有启动新建的交换分区时,是2G
              total        used        free      shared  buff/cache   available
Mem:           3709         226        2621           8         861        3249
Swap:          4031           0        4031

//开启新建的SWAP分区
[root@yy ~]# chmod 600 /opt/yy.swap //给予一个相对安全的权限
[root@yy ~]# ll /opt/yy.swap 
-rw------- 1 root root 536870912 Jul 17 16:26 /opt/yy.swap
[root@yy ~]# swapon /opt/yy.swap  //启动swap分区
[root@yy ~]# free -m              //发现交换分区增加了512M
              total        used        free      shared  buff/cache   available
Mem:           3709         226        2621           8         861        3249
Swap:          4543           0        4543

//关闭新建的swap分区
[root@yy ~]# swapoff /opt/yy.swap 
[root@yy ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3709         226        2621           8         861        3249
Swap:          4031           0        4031

//开启swap并且永久使用
[root@yy ~]# blkid /opt/yy.swap
/opt/yy.swap: UUID="cda63aba-c65f-46f2-9559-78ae298e0a0b" TYPE="swap"
[root@yy ~]# vi /etc/fstab 
UUID="cda63aba-c65f-46f2-9559-78ae298e0a0b" swap swap defaults 0 0
[root@yy ~]# swapon -a
swapon: /etc/fstab: parse error at line 17 -- ignored
[root@yy ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3709         228        2619           8         861        3247
Swap:          4543           0        4543
2.新增swap分区
[root@yy ~]# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (1050624-10485759, default 1050624): 
Last sector, +sectors or +size{K,M,G,T,P} (1050624-10485759, default 10485759): +512M

Created a new partition 2 of type 'Linux' and of size 512 MiB.

Command (m for help): t
Partition number (1,2, default 2): 
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p
Disk /dev/sdc: 5 GiB, 5368709120 bytes, 10485760 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
Disklabel type: dos
Disk identifier: 0xc2cfdc02

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdc1          2048 1050623 1048576  512M 83 Linux
/dev/sdc2       1050624 2099199 1048576  512M 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@yy ~]# partprobe /dev/sdc
[root@yy ~]# mkswap /dev/sdc2
Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=bb8a15d4-bfdf-4bf7-94bb-a7edb2358f5f
[root@yy ~]# blkid /dev/sdc2
/dev/sdc2: UUID="bb8a15d4-bfdf-4bf7-94bb-a7edb2358f5f" TYPE="swap" PARTUUID="c2cfdc02-02"
[root@yy ~]# vi /etc/fstab 
UUID="bb8a15d4-bfdf-4bf7-94bb-a7edb2358f5f" swap swap defaults 0 0
[root@yy ~]# swapon -a
swapon: /etc/fstab: parse error at line 18 -- ignored
[root@yy ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3709         229        2618           8         861        3246

八、磁盘故障案例

Inode被占满,导致磁盘有可用的剩余空间也无法继续使用

[root@yy ~]# dd if=/dev/zero of=/opt/newdisk bs=1k count=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00114433 s, 916 MB/s
[root@yy ~]# mkfs.ext4 -i 1024 /opt/newdisk
mke2fs 1.45.6 (20-Mar-2020)

Filesystem too small for a journal
Discarding device blocks: done                            
Creating filesystem with 1024 1k blocks and 1024 inodes

Allocating group tables: done                            
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done

[root@yy ~]#  mkdir /data
[root@yy ~]# mount -t ext4 -o loop /opt/newdisk /data/
[root@yy ~]# cd /data
[root@yy data]# touch {1..20000} 
touch: cannot touch '19982': No space left on device
touch: cannot touch '19983': No space left on device
touch: cannot touch '19984': No space left on device
touch: cannot touch '19985': No space left on device
touch: cannot touch '19986': No space left on device
touch: cannot touch '19987': No space left on device
touch: cannot touch '19988': No space left on device
touch: cannot touch '19989': No space left on device
touch: cannot touch '19990': No space left on device
touch: cannot touch '19991': No space left on device
touch: cannot touch '19992': No space left on device
[root@yy data]# df -ih /data
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/loop0       1.0K  1.0K     0  100% /data
[root@yy data]# df -h /data
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      891K   38K  782K   5% /data

ch ‘19989’: No space left on device
touch: cannot touch ‘19990’: No space left on device
touch: cannot touch ‘19991’: No space left on device
touch: cannot touch ‘19992’: No space left on device
[root@yy data]# df -ih /data
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/loop0 1.0K 1.0K 0 100% /data
[root@yy data]# df -h /data
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 891K 38K 782K 5% /data

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值