free查看内存和swap使用情况,增加、删除、自动挂载swap分区

时间:2022-09-21 10:27:56

free

[root@localhost ~]# free
total used free shared buff/cache available
Mem: 999936 142760 566536 6884 290640 672076
Swap: 2097148 0 2097148
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 976M 139M 553M 6.7M 283M 656M
Swap: 2.0G 0B 2.0G
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。 命令(输入 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 获取帮助):d  注释:删除现有分区给swap分区使用
分区号 (1,2,5,6,默认 6):
分区 6 已删除 命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
添加逻辑分区 6
起始 扇区 (6297600-10485759,默认为 6297600):
将使用默认值 6297600
Last 扇区, +扇区 or +size{K,M,G} (6297600-10485759,默认为 10485759):
将使用默认值 10485759
分区 6 已设置为 Linux 类型,大小设为 2 GiB 命令(输入 m 获取帮助):t  注释:改变分区系统id,swap是82
分区号 (1,2,5,6,默认 6):
Hex 代码(输入 L 列出所有代码):82
已将分区“Linux”的类型更改为“Linux swap / Solaris” 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0c3ac977 设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 10485759 4193280 5 Extended
/dev/sdb5 2101248 6295551 2097152 83 Linux
/dev/sdb6 6297600 10485759 2094080 82 Linux swap / Solaris 令(输入 m 获取帮助):w
▽he partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。
[root@localhost ~]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 999936 140624 560296 6884 299016 674220
Swap: 2097148 0 2097148
[root@localhost ~]# mkswap /dev/sdb6 
mkswap: error: /dev/sdb6 is mounted; will not make swapspace
[root@localhost ~]# umount /dev/sdb6  注释:格式化前需要先卸载分区
[root@localhost ~]# mkswap /dev/sdb6
mkswap: /dev/sdb6: warning: wiping old xfs signature.
正在设置交换空间版本 1,大小 = 2094076 KiB
无标签,UUID=a902382a-8368-48e9-89d8-faa1a965304f
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 999936 140340 560924 6884 298672 674504
Swap: 2097148 0 2097148
[root@localhost ~]# swapon /dev/sdb6  注释:添加格式化后的swap分区
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 999936 141700 559560 6884 298676 673144
Swap: 0 4191224
[root@localhost ~]# swapoff /dev/sdb6  注释:删除swap分区
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 999936 140336 560928 6884 298672 674508
Swap: 0 2097148 [root@localhost ~]# vim /etc/fstab  注释:修改配置文件使用新添加的swap分区在系统每次启动时自动挂载 #
# /etc/fstab
# Created by anaconda on Wed Jul 19 23:22:45 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=70dade4c-e85f-4592-a133-26f1fb359801 / xfs defaults 0 0
UUID=69ac3810-0e34-4475-9f67-d4cf500c7a98 /boot xfs defaults 0 0
UUID=5554acf0-8374-48ee-893e-425523a8f958 /home xfs defaults 0 0
UUID=5c5e4732-4d0d-4040-a26a-5b86ad2293b7 swap swap defaults 0 0
UUID=e481f266-5f69-4db5-9b5c-0f731da57d21 /disk1 ext4 defaults 0 0
/dev/sdb5 /disk5 ext4 defaults 0 0
/dev/sdb6 swap swap defaults 0 0
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/fstab" 15L, 767C 已写入
[root@localhost ~]# mount -a  注释:这步很重要,可在系统重启前检测/etc/fstab文件是否写错,避免导致系统崩溃。
[root@localhost ~]#