一、交换分区创建与挂载
交换分区是系统内存的补充,为什么出来交换分区这一概念了,那就还得先从计算机的发展说起,毕竟对于早期的计算机设备来说,内存是一个比较昂贵的耗材,所以在当时内存资源比较紧张,而应用程序对内存的需求又在不断膨胀的情况下,专家们提出了一种新的思路,那就是把高速的磁盘设备划分一部分出来当作内存来使用,当然对于计算机设备工作效率有一定了解的同学们都知道,硬盘的读写速度和内存根本不在一个层面上,所以说即使把高速的硬盘分区拿来当内存用,那也只不过是一个模拟操作,计算机只会把一些不是当前活动最为频繁的进程的数据,暂时存放到交换分区中,当该进程转换为当前进程时,计算机会及时的把交换分区上的数据及时调入内存继续工作。在高速磁盘上划分的这一区域我们就把它称之为交换分区。
当然我们要想提升这个交换分区的性能,最好是在安装操作系统的过程中就直接划分好,为什么这么说了,因为磁盘最外圈的磁道的读写速度是最快的,而磁盘分区工具默认划分分区时,都是从最外圈磁道开始的,所以说,如果你是在操作系统已经安装完之后再去创建交换分区,很显然你是无法再次划分到最快的那一部分区域来做交换分区了。
(一)使用硬盘分区做交换分区
在这里由于我的机器内存是比较大的,完全没有必须在拿硬盘来模拟内存了,所以我在安装操作系统的过程中就直接没有划分交换分区。所以为了给大家演示这一操作过程,只能用剩余硬盘空间划分一个分区来做交换分区了。
第一步:划分分区,调整分区类型为SWAP(82)
示例代码:
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 299M 1.0G 9.0M 455M 1.3G
Swap: 0B 0B 0B
[root@centos7 test]# fdisk /dev/sda
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/sda: 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: 0x000e74ad
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 10897407 5242880 83 Linux
/dev/sda3 10897408 21383167 5242880 83 Linux
/dev/sda4 21383168 41943039 10279936 5 Extended
/dev/sda5 21385216 31870975 5242880 83 Linux
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (31873024-41943039, default 31873024):
Using default value 31873024
Last sector, +sectors or +size{K,M,G} (31873024-41943039, default 41943039): +2G
Partition 6 of type Linux and of size 2 GiB is set
Command (m for help): t
Partition number (1-6, default 6):
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.
[root@centos7 test]#
第二步:通知内核重读硬盘分区表(否则系统无法识别划分的分区)
示例代码:
[root@centos7 test]# partprobe /dev/sda
[root@centos7 test]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
├─sda3 8:3 0 5G 0 part /test
├─sda4 8:4 0 512B 0 part
├─sda5 8:5 0 5G 0 part /home
└─sda6 8:6 0 2G 0 part
[root@centos7 test]#
第三步:使用mkswap格式化交换分区(方便区分,最好添加卷标)
示例代码:
[root@centos7 test]# mkswap -L swappart /dev/sda6
Setting up swapspace version 1, size = 2097148 KiB
LABEL=swappart, UUID=082ebfdf-e8ad-4874-af8e-6a5f2a01c7ac(当前交换分区的UUID)
[root@centos7 test]#
第四步:使用swapon [DEVICE]挂载交换分区,并用free -h检验结果
示例代码:
[root@centos7 test]# swapon /dev/sda6
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 302M 1.0G 9.0M 459M 1.3G
Swap: 2.0G 0B 2.0G
[root@centos7 test]#
第五步:在/etc/fstab文件中添加自动挂载条目(如想期使用,就必须写入磁盘挂载配置文件)
UUID="当前交换分区的UUID" swap swap defaults 0 0
1、挂载交换分区
swapon [OPTION]... [DEVICE]
-a:激活所有的交换分区;
如果是临时使用,至此就可以正常使用刚才划分出来的2G的交换分区了。如果想长
-p PRIORITY:指定优先级
当使用多个交换分时,可以为其指定优先级
长期生效写入fstab文件中的defaults字段:pri=value(值越大,优先级越高)
-s:查看交换分区使用情况(包括优先级)
示例代码:
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 302M 1.0G 9.0M 459M 1.3G
Swap: 2.0G 0B 2.0G
[root@centos7 test]# swapon -a
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 303M 1.0G 9.0M 464M 1.3G
Swap: 3.0G 0B 3.0G
[root@centos7 test]# swapon -s
Filename Type Size Used Priority
/dev/sda6 partition 2097148 0 -1
/dev/sda7 partition 1048572 0 -2
[root@centos7 test]#
2、卸载交换分区
swapoff [OPTION]... [DEVICE]
/dev/sda* 卸载某个交换分区
-a 卸载所有交换分区
示例代码:
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 304M 1.0G 9.0M 463M 1.3G
Swap: 3.0G 0B 3.0G
[root@centos7 test]# swapoff /dev/sda7
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 304M 1.0G 9.0M 463M 1.3G
Swap: 2.0G 0B 2.0G
[root@centos7 test]# swapoff -a
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 302M 1.0G 9.0M 463M 1.3G
Swap: 0B 0B 0B
[root@centos7 test]#
3、SWAP的优先级
用户可以给某个swap指定一个0到32767的优先级
如果用户没有指定,那么核心会自动给swap指定一个优先级,这个优先级从-1开始,每加入一个新的没有用户指定优先级的swap,会给这个优先级减一
先添加的swap的缺省优先级比较高,除非用户自己指定一个优先级,而用户指定的优先级(是正数)永远高于核心缺省指定的优先级(是负数)
(二)特殊情况,如遇没有单独的分区用作交换分区时,可用文件制作交换分区
第一步:使用dd命令创建一个交换文件
注意:文件不可使用跳过方式创建,否则格式化时会报错:分区有漏洞
示例代码:
[root@centos7 test]# dd if=/dev/zero of=/test/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 34.1887 s, 31.4 MB/s
[root@centos7 test]#
第二步:使用mkswap命令格式化交换文件
示例代码:
[root@centos7 test]# mkswap -L swapfile /test/swapfile
Setting up swapspace version 1, size = 1048572 KiB
LABEL=swapfile, UUID=4618be9e-f44d-4962-b1ce-7e04317e3e5e
[root@centos7 test]#
第三步:使用swapon [DEVICE]挂载交换分区,并用free -h检验结果
示例代码:
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 303M 80M 9.0M 1.4G 1.3G
Swap: 0B 0B 0B
[root@centos7 test]# swapon /test/swapfile
swapon: /test/swapfile: insecure permissions 0644, 0600 suggested.
[root@centos7 test]# free -h
total used free shared buff/cache available
Mem: 1.8G 303M 79M 9.0M 1.4G 1.3G
Swap: 1.0G 0B 1.0G
[root@centos7 test]#
第四步:在/etc/fstab文件中添加自动挂载条目
/test/swapfile swap swap defaults 0 0(注意这里和普通分区的挂载有所不同)
二、移动介质的挂载与卸载
挂载意味着使外来的文件系统看起来如同是主目录树的一部分
访问前、介质必须被挂载
摘除时,介质必须被卸载
按照默认设置,非root用户只能挂载某些设备(光盘、DVD、软盘、USB等等)
挂载点通常在/media 或/mnt下
(一)光盘的挂载与卸载:
光驱安装后,会被系统自动加载,并以/dev/cdrom命名设备文件
手工挂载:mount /dev/cdrom /media/cdrom
1、自动挂载:
前提是系统安装有autofs服务,验证方法,rpm -q autofs,并且服务是自启动状态
CentOS6默认安装的有,CentOS7需要手动安装
触发自动挂载的操作:cd /misc/cd
示例代码:
[root@centos7 ~]# rpm -q autofs
package autofs is not installed
[root@centos7 ~]# mkdir /media/cdrom
[root@centos7 ~]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@centos7 ~]# rpm -ivh /media/cdrom/Packages/autofs-5.0.7-54.el7.x86_64.rpm
warning: /media/cdrom/Packages/autofs-5.0.7-54.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:autofs-1:5.0.7-54.el7 ################################# [100%]
[root@centos7 ~]# umount /dev/cdrom
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 5.0G 4.2G 874M 83% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 913M 84K 913M 1% /dev/shm
tmpfs 913M 8.9M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda5 4.8G 21M 4.6G 1% /home
/dev/sda3 4.8G 1.1G 3.6G 23% /test
/dev/md0 40G 49M 38G 1% /md0
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
[root@centos7 ~]# automount
[root@centos7 ~]# cd /misc/cd
[root@centos7 cd]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 5.0G 4.2G 873M 83% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 913M 84K 913M 1% /dev/shm
tmpfs 913M 8.9M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda5 4.8G 21M 4.6G 1% /home
/dev/sda3 4.8G 1.1G 3.6G 23% /test
/dev/md0 40G 49M 38G 1% /md0
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 183M 12K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/sr0 7.3G 7.3G 0 100% /misc/cd
[root@centos7 cd]# ls
CentOS_BuildTag LiveOS
EFI Packages
EULA repodata
GPL RPM-GPG-KEY-CentOS-7
p_w_picpaths RPM-GPG-KEY-CentOS-Testing-7
isolinux TRANS.TBL
[root@centos7 cd]# pwd
/misc/cd
2、想长期使用automount,到/etc/profil.d目录下创建一个系统自启动脚本文件即可
示例代码:
[root@centos7 cd]# echo "automount" > /etc/profile.d/automount.sh
[root@centos7 cd]#
3、eject命令卸载或弹出磁盘
示例代码:
[root@centos7 test]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 5.0G 4.2G 875M 83% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 913M 84K 913M 1% /dev/shm
tmpfs 913M 9.0M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda5 4.8G 21M 4.6G 1% /home
/dev/sda3 4.8G 1.1G 3.6G 23% /test
/dev/md0 40G 49M 38G 1% /md0
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/sr0 7.3G 7.3G 0 100% /misc/cd
[root@centos7 test]# eject
[root@centos7 test]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 5.0G 4.2G 875M 83% /
devtmpfs 899M 0 899M 0% /dev
tmpfs 913M 84K 913M 1% /dev/shm
tmpfs 913M 9.0M 904M 1% /run
tmpfs 913M 0 913M 0% /sys/fs/cgroup
/dev/sda5 4.8G 21M 4.6G 1% /home
/dev/sda3 4.8G 1.1G 3.6G 23% /test
/dev/md0 40G 49M 38G 1% /md0
/dev/sda1 197M 139M 59M 71% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
[root@centos7 test]#
4、创建ISO镜像:cp /dev/cdrom /root/centos7.iso
mkisofs -r -o /root/boot.iso /boot
5、刻录光盘:wodim –v –eject centos.iso
(二)U盘的挂载与卸载
U盘设备插入后会被内核探测为SCSI设备,会以/dev/sd*命名设备文件
手动挂载:mount /dev/sdb1 /media/usb
1、插入U盘之前:
示例代码:
[root@centos7 test]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
├─sda3 8:3 0 5G 0 part /test
├─sda4 8:4 0 512B 0 part
├─sda5 8:5 0 5G 0 part /home
├─sda6 8:6 0 2G 0 part
└─sda7 8:7 0 1G 0 part
sr0 11:0 1 7.2G 0 rom
插入U盘之后:
示例代码:
[root@centos7 test]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 5G 0 part /
├─sda3 8:3 0 5G 0 part /test
├─sda4 8:4 0 512B 0 part
├─sda5 8:5 0 5G 0 part /home
├─sda6 8:6 0 2G 0 part
└─sda7 8:7 0 1G 0 part
sdb 8:80 1 7.6G 0 disk
└─sdb1 8:81 1 7.2G 0 part
sr0 11:0 1 7.2G 0 rom
2、挂载U盘:
示例代码:
[root@centos7 test]# mkdir /media/usb
[root@centos7 test]# mount /dev/sdb1 /media/usb
[root@centos7 test]# ll /media/usb
total 369100
drwxr-xr-x. 2 root root 12288 Jun 10 2013 DJ
-rwxr-xr-x. 1 root root 377925386 Aug 18 2008 GHOSTXP.GHO
drwxr-xr-x. 2 root root 4096 Jul 11 2012 Music
drwxr-xr-x. 3 root root 4096 Mar 5 2012 Software
drwxr-xr-x. 2 root root 4096 Jul 11 2012 Viedo
[root@centos7 test]#
3、卸载U盘
示例代码:
[root@centos7 test]# umount /dev/sdb1
[root@centos7 test]# ls /media/usb
[root@centos7 test]#
做为一名需要做维护的运维人员来讲,这是必须掌握的,交换分区和一些常用外设的挂载与卸载,日常生产环境中是运用是多的。所以我把详细的使用过程全部贴出来了,只要大家认真去看,再对着博文练习几遍相信基本掌握是没问题的。
转载于:https://blog.51cto.com/183530300/1843130