18.Linux磁盘管理 - Go语言中文社区

18.Linux磁盘管理


1.磁盘分区工具fdisk

1. 添加一块小于2TB的磁盘进行使用,步骤如下:

  • 给虚拟机添加一块新的硬盘
  • 使用fdisk进行分区
  • 使用mkfs进行格式化
  • 使用mount进行挂载
    PS: 生产分区建议,如无特殊需求直接使用整个磁盘即可,无需分区。
    PS: 学习分区建议: 1P+1E(3L) 2P+1E(2L) 3P+1E(1L) (仅适用于练习)
[root@yinwucheng ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   40G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    1G  0 part [SWAP]
└─sda3   8:3    0   38G  0 part /
sdb      8:16   0  300G  0 disk 
sdc      8:32   0  300G  0 disk 
sr0     11:0    1  4.3G  0 rom  
[root@yinwucheng ~]# 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 0x271d3013.

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 partitions 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)   #拓展功能

1.创建主分区

   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):      #默认创建第一个主分区
First sector (2048-629145599, default 2048):     #默认扇区-->回车即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-629145599, default 629145599): +150G    #分配150G
Partition 1 of type Linux and of size 150 GiB is set

2.fdisk创建扩展分区

Command (m for help): n     #新建分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e    #创建扩展分区
Partition number (2-4, default 2): 
First sector (314574848-629145599, default 314574848): 
Using default value 314574848
Last sector, +sectors or +size{K,M,G} (314574848-629145599, default 629145599):    #剩余容量都给到扩展分区
Using default value 629145599
Partition 2 of type Extended and of size 150 GiB is set

3.fdisk创建逻辑分区

Command (m for help): n   #新建分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l     #创建逻辑分区
Adding logical partition 5
First sector (314576896-629145599, default 314576896): 
Using default value 314576896
Last sector, +sectors or +size{K,M,G} (314576896-629145599, default 629145599): +50G     #分配50G容量
Partition 5 of type Linux and of size 50 GiB is set

4.fdisk查看分区情况,并保存

Command (m for help): p    #查看创建的分区

Disk /dev/sdb: 322.1 GB, 322122547200 bytes, 629145600 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
Disk label type: dos
Disk identifier: 0x271d3013

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   314574847   157286400   83  Linux
/dev/sdb2       314574848   629145599   157285376    5  Extended
/dev/sdb5       314576896   419434495    52428800   83  Linux

#保存分区信息
Command (m for help): w     
The partition table has been altered!

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

#检查磁盘是否是MBR格式
[root@yinwucheng ~]# fdisk /dev/sdb -l |grep type   
Disk label type: dos

#安装parted, 刷新内核立即生效,无需重启
[root@yinwucheng ~]# yum install parted -y 
[root@yinwucheng ~]# partprobe /dev/sdb

5.格式化磁盘
mkfs格式化磁盘,实质创建文件系统,文件系统类似于将房子装修成3室一厅,还是2室一厅。

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

#1.格式化整个磁盘
[root@yinwucheng ~]# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
19660800 inodes, 78643200 blocks
3932160 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2227175424
2400 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, 71663616

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

#2.格式化磁盘的某个分区
[root@yinwucheng ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=9830400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=39321600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=19200, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

6.使用mount挂载并使用
如果需要使用该磁盘的空间,需要准备一个空的目录作为挂载点,与该设备进行关联。

挂载磁盘并查看

7.磁盘的基本分区Gdisk

前面我们已经了解到fdisk分区,但fdisk不支持给高于2TB的磁盘进行分区。如果有单块盘高于2TB,建议使用Gdisk进行分区。

1.使用gdisk进行磁盘分区

#1.安装gdisk分区工具
[root@yinwucheng ~]# yum install gdisk -y

#2.创建一个新分区,大小500MB
[root@yinwucheng ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10

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-629145566, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-629145566, default = 629145566) or {+-}size{KMGTP}: +500M
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/sdc: 629145600 sectors, 300.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0FE3F4A5-FBC9-4515-8C25-F24BB0553830
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 629145566
Partitions will be aligned on 2048-sector boundaries
Total free space is 628121533 sectors (299.5 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         1026047   500.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/sdc.
The operation has completed successfully.

#3.创建完成后,可以尝试检查磁盘是否为gpt格式
[root@yinwucheng ~]# fdisk /dev/sdc -l |grep type
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk label type: gpt

#4.安装parted, 刷新内核立即生效,无需重启
[root@yinwucheng ~]# yum install parted -y 
[root@yinwucheng ~]# partprobe /dev/sdc

2.使用mkfs进行格式化磁盘。前面已经介绍过,此处不反复介绍。

[root@yinwucheng ~]# mkfs.xfs /dev/sdc1

3.使用mount命令将某个目录挂载该分区,进行使用。

[root@yinwucheng ~]# mkdir /data2
[root@yinwucheng ~]# mount /dev/sdc1 /data2

4.磁盘挂载方式Mount
前面我们已经提到过,如果需要使用磁盘的空间,需要准备一个空的目录作为挂载点,与该设备进行关联。mount主要是为文件系统指定一个访问入口。
PS: 类似我的商场没有门,那么就无法进入购买商品,此时通过mount命令可以创建一个入口。给超市安装一个门。如图:

1.通过mount进行挂载,但重启将会失效。我们称为临时生效。

 选项:
      -t指定文件系统挂载分区
      -a 挂载/etc/fstab中的配置文件
      -o 指定挂载参数
挂载/dev/sdb1至db1目录
[root@yinwucheng ~]# mkdir /db1
[root@yinwucheng ~]# mount -t xfs /dev/sdb1 /db1/ 

2.挂载的磁盘,如果不想使用可以使用umount进行卸载。

#选项: -l 强制卸载

#1.卸载目录方式
[root@yinwucheng ~]# umount /db1/

#2.卸载设备方式
[root@yinwucheng ~]# umount /dev/sdb1

3.umount不能卸载的情况
在这里插入图片描述
4.如果需要实现永久挂载则需要将挂载信息写入/etc/fstab配置文件中实现。

#1.使用blkid命令获取各设备的UUID
[root@yinwucheng ~]# blkid |grep 'sdb1'
/dev/sdb1: UUID="01353c25-0bde-49b6-a026-f805fc0de265" TYPE="xfs" 

#2.使用UUID挂载磁盘sdb1分区至于db1, 测试挂载
[root@yinwucheng ~]# mount UUID="01353c25-0bde-49b6-a026-f805fc0de265" /db1

#3.写入/etc/fstab中,实现开机自动挂载
[root@yinwucheng ~]# vim /etc/fstab    
UUID=01353c25-0bde-49b6-a026-f805fc0de265 /db1 xfs  defaults 0  0

#4.加载fstab配置文件, 同时检测语法是否有错误
[root@yinwucheng ~]# mount -a

5./etc/fstab配置文件编写格式

要挂载的设备 挂载点(入口) 文件系统类型 挂载参数 是否备份 是否检查
/dev/sdb1 /db1 xfs defaults 0 0

第四列:挂载参数。挂载参数有很多,在这块我们了解即可,不必深究

参数 含义
async/sync 是否为同步方式运行。默认async
user/nouser 是否允许普通用户使用mount命令挂载。默认nouser
exec/noexe 是否允许可执行文件执行。默认exec
suid/nosuid 是否允许存在suid属性的文件。默认suid
auto/noauto 执行mount -a 命令时,此文件系统是否被主动挂载。默认auto
rw/ro 是否以只读或者读写模式进行挂载。默认rw
default 具有rw,suid,dev,exec,auto,nouser,async等默认参数的设定

第五列:是否进行备份。通常这个参数的值为0或者1

选项 含义
0 代表不做备份
1 代表要每天进行备份操作
2 代表不定日期的进行备份操作

第六列:是否检验扇区:开机的过程中,系统默认会以fsck检验我们系统是否为完整

选项 含义
0 不要检验磁盘是否有坏道
1 检验
2 校验 (当1级别检验完成之后进行2级别检验)

6.虚拟磁盘介绍SWAP
Swap分区在系统的物理内存不够时,将硬盘空间中的一部分空间释放出来,以供当前运行的程序使用。
PS: 当物理内存不够时会随机kill占用内存的进程,从而产生oom,临时使用swap可以解决。

1.创建分区,并格式化为swap分区。

[root@yinwucheng ~]# fdisk /dev/sdc   ##大小分1G
[root@yinwucheng ~]# mkswap /dev/sdc2   ##格式化为swap
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=f31cae8d-7bc8-4950-985f-94682b1a3718

2.查看当前swap分区大小,然后进行扩展和缩小

[root@yinwucheng ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3931         109        3489          11         332        3574
Swap:          1023           0        1023

#1.扩展swap分区大小
[root@yinwucheng ~]# swapon /dev/sdc2
[root@yinwucheng ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3931         110        3488          11         332        3574
Swap:          2047           0        2047

[root@yinwucheng ~]# swapon -a     ##代表激活所有的swap

#2.缩小swap分区大小
[root@yinwucheng ~]# swapoff /dev/sdc2
[root@yinwucheng ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3931         110        3489          11         332        3574
Swap:          1023           0        1023
[root@yinwucheng ~]# swapoff -a    ##代表关闭所有的swap

3.检查当前swap分区有哪些设备

[root@yinwucheng ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                               partition   1048572 0   -2
/dev/sdc2                               partition   1048572 0   -3

4.如果磁盘没有过多的分区可用,也可以通过文件增加SWAP空间,本质上还是磁盘

[root@yinwucheng ~]# dd if=/dev/zero of=/opt/swap_file bs=1M count=500
[root@yinwucheng ~]# chmod 0600 /opt/swap_file
[root@yinwucheng ~]# mkswap -f /opt/swap_file 
[root@yinwucheng ~]# swapon /opt/swap_file
[root@yinwucheng ~]# free -m

RAID 0 1 5

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/yinwu/p/11390116.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-09 10:30:32
  • 阅读 ( 864 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢