硬盘的介绍和Linux磁盘的管理

  • SAS-SATA-SSD-SCSI-IDE硬盘讲解
  • 磁盘分区工具和挂载
  • 磁盘分区免交互方法
  • 扩展swap分区

SAS-SATA-SSD-SCSI-IDE硬盘讲解

常见的磁盘类型:
SAS硬盘;SATA硬盘;SCSI硬盘;SSD硬盘;IDE硬盘

SAS硬盘:

  • SAS(串行连接 SCSI 接口)
  • SAS(Serial Attached SCSI),串行连接 SCSI接口,串行连接小型计算机系统接口。
  • SAS 是新一代的 SCSI 技术,和现在流行的 SerialATA(SATA)硬盘相同,都是采用串行技术以获得 更高的传输速度,并通过缩 短连结线改善内部空间等。
  • SAS 的接口技术可以向下兼容SATA。

磁盘尺寸:

  • 3.5 英寸设计
  • 2.5 英寸设计

此前主流的桌面磁盘和服务器磁盘都是采用 3.5 英寸设计,而 SAS 硬盘除了具有传统的 3.5 英寸规
栺之外,还采用了 2.5 英寸的缩小版,这样可以在机架式服务器有限的空间内安装更多的磁盘以扩充存储
系统的容量,也能够为其他配件腾出更大的空间,以便通风散热,在 2U 高度内使用 8 个 2.5 英寸的 SAS
硬盘位已经成为大多数服务器厂商的选择。

当下流行的磁盘种类
常见硬盘品牌:希捷 西数 日立 HP DELL EMC IBM
硬盘分几种?
A. 从工作原理来说:
固态:价栺相对贵,导命长,读取速度
机械:怕摔、怕磁,(单位换下来的坏盘会做消磁处理),读取速度—》磁道寺址时间,潜伏时间
B. 从硬盘的接口来说
STAT:用在低端服务器多
SAS、SCSI:用在中高服务器
PCIE M.2
对 LINUX 来说,在内核中,不同的接口对应有不同的命名方式:
操作系统 IDE STAT|SCSI SAS
RHEL5 /dev/hda /dev/sda /dev/sda
RHEL6 /dev/sda /dev/sda /dev/sda
KVM /dev/vda

磁盘的分区和挂载

1、硬盘分区符的认识
MBR 概述:全称为 Master Boot Record,即硬盘的主引寻记录。
硬盘的 0 柱面、0 磁头、1 扇区称为主引寻扇区(也叫主引寻记录 MBR)。它由三个部分组成,主引导程序、硬盘分区表 DPT(Disk Partition table)和分区有效标志(55AA)。在总共 512 字节的主引导扇区里主引导程序(boot loader)占 446 个字节,第二部分是 Partition table 区(分区表),即 DPT,占 64 个字节,硬盘中分区有多少以及每一分区的大小都记在其中。第三部分是 magic number,占 2 个字节,固定为 55AA。
分区编号:主分区 1-4 ,逻辑分区 5……
LINUX 规定:逻辑分区必须建立在扩展分区之上,而不是建立在主分区上
分区作用:
主分区:主要是用来启动操作系统的,它主要放的是操作系统的启动或引导程序,/boot 分区最好放在主分区上
扩展分区不能使用的,它只是做为逻辑分区的容器存在的;我们真正存放数据的是主分区和逻辑分区,大量数据都放在逻辑分区中
如果你用的是 GPT 的分区方式,那么它没有限制主分区个数

2、使用fdisk管理分区
fdisk:磁盘翻去,是Linux发行版本中最常用的分区工具
常用选项:

  • d:删除已创建的分区
  • l:查看硬盘分区表
  • n:创建分区
  • p:打印分区表
  • w:保存创建的分区表
  • q:退出,不保存
  • m:帮助菜单
  • t:更改分区的系统类型
  • v:验证分区表

例1:添加一块磁盘,进行分区
因为在生产环境中,我们一般添加硬盘都是热插拔形式的,不能重启服务器来识别硬盘,所有需要使用到如下命令扫描硬盘:

[root@xuegod140 ~]# echo “- - -” >> /sys/class/scsi_host/host0/scan

上述命令可以将在不重启服务器的基础上,将新增加的硬盘扫描出来,因为scsi_host目录下有很多个host目录,所以我们需要一个个扫描,知道将增加的硬盘扫描出来为止。

面交互的扫描硬盘的两种命令:

[root@xuegod140 ~]# for i in /sys/class/scsi_host/host*/scan; do echo “- - -” > $i; done

磁盘分区后,有的分区因为没有加载到内存中,需要使用命令刷下后,才可以显示:

[root@xuegod140 ~]# partprobe -a /dev/sdb #或者下面的命令
[root@xuegod140 ~]# partx -a /dev/sdb

GPT格式硬盘转换成MBR格式硬盘命令:

[root@xuegod140 ~]# parted -s /dev/sdb mklabel msdos

MBR格式硬盘转换成GPT硬盘命令:

[root@xuegod140 ~]# parted -s /dev/sdb mklabel gpt

例1:使用fdisk命令将磁盘进行分区,默认是MBR格式分区

[root@xuegod140 ~]# 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.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p #p创建主分区
Partition number (1-4, default 1): #默认1号分区,回车
First sector (2048-41943039, default 2048): @默认扇区开始,回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +300M #指定大小为300M
Partition 1 of type Linux and of size 300 MiB is set
Command (m for help): w # w保存分区表
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@xuegod140 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 10G 0 part /
└─sda3 8:3 0 4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 300M 0 part
sdc 8:32 0 20G 0 disk
sr0 11:0 1 4.2G 0 rom /mnt

注释说明:
MBR格式的分区最多可以创建4个主分区;也可以创建3个主分区,一个扩展分区,然后在扩展分区的基础上,最多可以创建11个逻辑分区

例2:创建一个扩展分区

[root@xuegod140 ~]# 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.
Command (m for help): p #打印分区表
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
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 (616448-41943039, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-41943039, default 41943039): +10G #指定大小10G
Partition 2 of type Extended and of size 10 GiB is set
Command (m for help): w #保存分区表
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended

例3:在扩展分区的基础上,创建一个逻辑分区

[root@xuegod140 ~]# 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.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended
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 (618496-21587967, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-21587967, default 21587967): +300M
Partition 5 of type Linux and of size 300 MiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended
/dev/sdb5 618496 1232895 307200 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

例4:使用gdisk命令,将磁盘进行分区,分区格式是GPT格式
gdisk 主要是用来划分容量大于 2T 的硬盘,大于 2T fdisk 搞不定
两种类型的分区表:GPT 和 MBR ; MBR 不支持 4T 以上
GPT 分区:GPT,全局唯一标识分区表(GUID Partition Table),它使用 128 位 GUID 来唯一标识每个磁盘和分区,与MBR 存在单一故障点不同,GPT 提供分区表信息的冗余,一个在磁盘头部一个在磁盘尾部;它通过 CRC校验和来检测 GPT 头和分区表中的错误与损坏;默认一个硬盘支持 128 个分区

[root@xuegod140 ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.6
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): #最大可以创建128个,默认从1开始
First sector (34-41943006, default = 2048) or {±}size{KMGTP}: #从默认扇区开始
Last sector (616448-41943006, default = 41943006) or {±}size{KMGTP}: +300M #指定大小300M
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: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): A0D74468-9C24-4727-ADF0-A9FFA3468988
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 41328573 sectors (19.7 GiB)
Number Start (sector) End (sector) Size Code Name
1 616448 1230847 300.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 #输入y进行确认
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.

例5:将创建的分区表,删除

[root@xuegod140 ~]# 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.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended
/dev/sdb5 618496 1232895 307200 83 Linux
Command (m for help): d #删除分区表
Partition number (1,2,5, default 5): 5 #指定需要删除分区的分区号
Partition 5 is deleted
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended
Command (m for help): w #保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

3、磁盘分区后,进行格式、挂载
磁盘挂载有两种方式:
临时挂载和永久挂载
永久挂载里面可以直接针对设备的名称,也可以针对设备的UUID挂载,我们一般最好使用设备的UUID挂载,这样如果设备热插拔之后,设备名称会变更,但是UUID不会变动。(格式化分区除外)
例1:对创建的分区进行格式

[root@xuegod140 ~]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (swap).
mkfs.xfs: Use the -f option to force overwrite.
[root@xuegod140 ~]# mkfs.xfs -f /dev/sdb1 #如果不是新硬盘,需要加-f参数,强制格式化
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

例2:创建挂载点,挂载分区

[root@xuegod140 ~]# mkdir /sdb1
[root@xuegod140 ~]# mount /dev/sdb1 /sdb1/
[root@xuegod140 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 10G 0 part /
└─sda3 8:3 0 4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 300M 0 part /sdb1
└─sdb2 8:18 0 1K 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 300M 0 part
sr0 11:0 1 4.2G 0 rom /mnt

上面的挂载方式属于临时挂载,重启后挂在无效

例3:将挂在内容写入到配置文件,开机自动挂载

[root@xuegod140 mnt]# blkid #查看设备的UUID值
/dev/sr0: UUID=“2018-05-03-20-55-23-00” LABEL=“CentOS 7 x86_64” TYPE=“iso9660” PTTYPE=“dos”
/dev/sdb1: UUID=“633c9e4e-04d9-424e-930b-5d01255706c4” TYPE=“xfs”
/dev/sda1: UUID=“d7fee807-b89e-4a72-8a36-b75f0890c843” TYPE=“xfs”
/dev/sda2: UUID=“2ea34aff-f003-43fc-b512-07f12d8cc644” TYPE=“xfs”
/dev/sda3: UUID=“5370fb36-3120-464e-a007-27d0b87d174b” TYPE=“swap”
/dev/sdc1: PARTLABEL=“Linux filesystem” PARTUUID=“04f9ff08-385a-483f-953a-38bf1b8b0ba1”
[root@xuegod140 ~]# echo “/dev/sdb1 /sdb1 xfs defaults 0 0” >> /etc/fstab
[root@xuegod140 ~]# mount -a
[root@xuegod140 ~]# tail -1 /etc/fstab
/dev/sdb1 /sdb1 xfs defaults 0 0

例4:使用UUID,挂载分区

[root@xuegod140 ~]# echo “UUID=633c9e4e-04d9-424e-930b-5d01255706c4 /sdb1 xfs 0 0” >> /etc/fstab
[root@xuegod140 ~]# mount -a
[root@xuegod140 ~]# tail -1 /etc/fstab
UUID=633c9e4e-04d9-424e-930b-5d01255706c4 /sdb1 xfs 0 0

例5:卸载分区
我们卸载分区的时候有时候也会出现问题,一般情况下都是因为有人在使用我们的挂载目录,所以导致无法卸载。
可以通过lsof和fuser命令查看是那个用户和进程在使用,直接kill掉。或者强制卸载分区

[root@xuegod140 ~]# cd /mnt
[root@xuegod140 mnt]# umount -a
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@xuegod140 mnt]# lsof /mnt
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 1704 root cwd DIR 11,0 2048 1856 /mnt
lsof 3148 root cwd DIR 11,0 2048 1856 /mnt
lsof 3149 root cwd DIR 11,0 2048 1856 /mnt
[root@xuegod140 mnt]# kill -9 1704 #这里因为是当面终端在占用,所以kill之后,会断开回话

另外一种方法,使用umount强制卸载:

[root@xuegod140 ~]# cd /mnt
[root@xuegod140 mnt]# umount -a
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@xuegod140 mnt]# umount -lf /mnt
[root@xuegod140 mnt]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part
├─sda2 8:2 0 10G 0 part /
└─sda3 8:3 0 4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 300M 0 part
└─sdb2 8:18 0 1K 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 300M 0 part
sr0 11:0 1 4.2G 0 rom

扩展了解:
在这里插入图片描述
在这里插入图片描述

磁盘分区免交互方法

方法一:使用脚本分区

[root@xuegod140 ~]# vim a.sh
[root@xuegod140 ~]# cat a.sh
#!/bin/bash
fdisk /dev/sdb << EOF
n
l
#空格起来,表示默认
+300M
w
EOF
[root@xuegod140 ~]# . a.sh
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.
Command (m for help): Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): Adding logical partition 6
First sector (1234944-21587967, default 1234944): Using default value 1234944
Last sector, +sectors or +size{K,M,G} (1234944-21587967, default 21587967): Partition 6 of type Linux and of size 300 MiB is set
Command (m for help): The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@xuegod140 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 10G 0 part /
└─sda3 8:3 0 4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 300M 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb5 8:21 0 300M 0 part
└─sdb6 8:22 0 300M 0 part #自动创建一个sdb6
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 300M 0 part
sr0 11:0 1 4.2G 0 rom /mnt

方法二:

[root@xuegod140 ~]# echo -e “n\nl\n\n+300M\nw\n” | 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.
Command (m for help): Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): Adding logical partition 7
First sector (1851392-21587967, default 1851392): Using default value 1851392
Last sector, +sectors or +size{K,M,G} (1851392-21587967, default 21587967): Partition 7 of type Linux and of size 300 MiB is set
Command (m for help): The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@xuegod140 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 10G 0 part /
└─sda3 8:3 0 4G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 300M 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb5 8:21 0 300M 0 part
├─sdb6 8:22 0 300M 0 part
└─sdb7 8:23 0 300M 0 part
sdc 8:32 0 20G 0 disk
└─sdc1 8:33 0 300M 0 part
sr0 11:0 1 4.2G 0 rom /mnt

扩展swap分区

这里说明两种方式
方法一:使用磁盘分区扩展swap大小
1)将已经划分好的分区,转为为swap类型

[root@xuegod140 ~]# 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.
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended
/dev/sdb5 618496 1232895 307200 83 Linux
Command (m for help): t #转换格式
Partition number (1,2,5, default 5): 5 #将分号为5的分区转换分区类型
Hex code (type L to list all codes): l #查看支持的列表
Hex code (type L to list all codes): 82 #转换为Linux swap类型
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0000216c
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 83 Linux
/dev/sdb2 616448 21587967 10485760 5 Extended
/dev/sdb5 618496 1232895 307200 82 Linux swap / Solaris
Command (m for help): w #保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

2)格式分区,将分区格式为swap类型

[root@xuegod140 ~]# mkswap /dev/sdb5
Setting up swapspace version 1, size = 307196 KiB
no label, UUID=a1cc60d2-9873-4255-a7ed-70ffeaf1f9f7
[root@xuegod140 ~]# blkid
/dev/sr0: UUID=“2018-05-03-20-55-23-00” LABEL=“CentOS 7 x86_64” TYPE=“iso9660” PTTYPE=“dos”
/dev/sdb1: UUID=“633c9e4e-04d9-424e-930b-5d01255706c4” TYPE=“xfs”
/dev/sda1: UUID=“d7fee807-b89e-4a72-8a36-b75f0890c843” TYPE=“xfs”
/dev/sda2: UUID=“2ea34aff-f003-43fc-b512-07f12d8cc644” TYPE=“xfs”
/dev/sda3: UUID=“5370fb36-3120-464e-a007-27d0b87d174b” TYPE=“swap”
/dev/sdc1: PARTLABEL=“Linux filesystem” PARTUUID=“04f9ff08-385a-483f-953a-38bf1b8b0ba1”
/dev/sdb5: UUID=“a1cc60d2-9873-4255-a7ed-70ffeaf1f9f7” TYPE=“swap”

3)查看swap的大小,并开启swap分区

[root@xuegod140 ~]# free -m
total used free shared buff/cache available
Mem: 3934 391 3190 20 352 3260
Swap: 4095 0 4095
[root@xuegod140 ~]# swapon /dev/sdb5
[root@xuegod140 ~]# free -m
total used free shared buff/cache available
Mem: 3934 391 3190 20 352 3260
Swap: 4395 0 4395

4)关闭swap分区

[root@xuegod140 ~]# swapoff /dev/sdb5
[root@xuegod140 ~]# free -m
total used free shared buff/cache available
Mem: 3934 391 3189 20 352 3259
Swap: 4095 0 4095

方法二:使用dd命令,生成一个300M的空间

[root@xuegod140 ~]# dd if=/dev/zero of=swapfile bs=30M count=10
10+0 records in
10+0 records out
314572800 bytes (315 MB) copied, 3.76789 s, 83.5 MB/s
[root@xuegod140 ~]# ll -h swapfile
-rw-r–r-- 1 root root 300M Apr 10 00:35 swapfile
[root@xuegod140 ~]# chmod 0600 swapfile
[root@xuegod140 ~]# mkswap swapfile
Setting up swapspace version 1, size = 307196 KiB
no label, UUID=e7a6595f-1909-422b-9fd0-32c95e052ff2
[root@xuegod140 ~]# free -m
total used free shared buff/cache available
Mem: 3934 392 2880 20 661 3255
Swap: 4095 0 4095
[root@xuegod140 ~]# swapon /root/swapfile
[root@xuegod140 ~]# swapon /root/swapfile
[root@xuegod140 ~]# free -m
total used free shared buff/cache available
Mem: 3934 391 2881 20 661 3255
Swap: 4395 0 4395
[root@xuegod140 ~]# swapoff /root/swapfile
[root@xuegod140 ~]# free -m
total used free shared buff/cache available
Mem: 3934 391 2881 20 661 3255
Swap: 4095 0 4095

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值