1.换空间或交换区是磁盘驱动器上的时间,用作当前未使用部分内存的溢出。这样 , 系统就能在主内存中留出空间用于储存当前正在处理的数据 , 并在系统面临主内存空间不足的风险时提供应急溢出。
2.管理交换分区
•使用 fdisk 创建新分区。此外 , 在用 fdisk 保存更改之前 , 将分区类型更改为 “ 0x82 Linux Swap”
• mkswap /dev/vdaN 会准备好将分区用作交换区
• blkid /dev/vdaN 将确定 UUID
• 将新交换空间添加到 /etc/fstab :
UUID=uuid swap swap defaults 0 0
• swapon -a 将激活新交换区
• swapon -s 将显示当前交换区的状态
• swapoff /dev/vdaN 将停用该特定交换区
11 fdisk -l
12 fdisk /dev/vdb ##划分swap分区 注意:要改类型 t 82
13 partprobe
14 cat /proc/partitions
15 blkid ##查看可用的分区,不显示的系统不能用
16 mkswap /dev/vdb1 ##对这块分区进行格式化,让这块分区可用
17 blkid ##查看可用分区
18 swapon -a /dev/vdb1 ##将激活新交换区
19 swapon -s ##显示当前交换区的状态
20 fdisk /dev/vdb ##再划分一块,比较
21 partprobe
22 mkswap /dev/vdb2
23 swapon -a /dev/vdb2
24 swapon -s
25 man swapon
swapoff /dev/vdb2
swapon -a /dev/vdb2 -p 1 ##更改第二块分区的优先级
26 swapoff /dev/vdb2 ##停用该特定交换区
27 swapoff /dev/vdb1 ##停用该特定交换区
28 swapon -s ##显示当前交换区的状态
29 vim /etc/fstab ##你编写的那个文件
30 swapon -s
31 swapon -a ##激活新交换区
32 swapon -s ##显示交换区状态
33 swapoff /dev/vdb{1,2} ##停用这两个交换区
34 swapon -s
35 history
文件编写:
vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# 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=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/vdb1 swap swap defaults 0 0
/dev/vdb2 swap swap defaults,pri=1 0 0
具体步骤:
---------------------------------------------------------------------
[root@localhost ~]# swapon -s
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x23f9e63f
Device Boot Start End Blocks Id System
[root@localhost ~]# fdisk /dev/vdb
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
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +4G
Partition 1 of type Linux and of size 4 GiB is set
Command (m for help): m
Command action
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)
Command (m for help): t
Selected partition 1
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): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
253 17 4194304 vdb1
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
[root@localhost ~]# mkswap /dev/vdb1
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=2d60d9e0-532e-41c4-ac65-64c130613e29
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="2d60d9e0-532e-41c4-ac65-64c130613e29" TYPE="swap"
[root@localhost ~]# swapon -a /dev/vdb1
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
[root@localhost ~]# fdisk /dev/vdb
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 (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (8390656-20971519, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set
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): wq
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@localhost ~]# partprobe
[root@localhost ~]# mkswap /dev/vdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=8806b0dc-5669-4aa4-b8e7-c0f2b8b5ba9d
[root@localhost ~]# swapon -a /dev/vdb2
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
/dev/vdb2 partition 1048572 0 -2
[root@localhost ~]# man swapon
[root@localhost ~]# swapoff /dev/vdb2
[root@localhost ~]# swapoff /dev/vdb1
[root@localhost ~]# swapon -s
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# swapon -s
[root@localhost ~]# swapon -a
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb1 partition 4194300 0 -1
/dev/vdb2 partition 1048572 0 1
[root@localhost ~]# swapoff /dev/vdb{1,2}
[root@localhost ~]# swapon -s
[root@localhost ~]#
-----------------------------------------------------------------------------
2.设置还原
1 swapon -s
2 swapoff /dev/vdb{1,2}
3 vim /etc/fstab
4 fdisk /dev/vdb
5 partprobe
6 cat /proc/partitions
五.对指定的用户限制一块设备的空间使用大小
注意:不能在挂载之前设置/pub的权限,否则挂载后又会被还原
步骤:
22 mkdir /pub
23 fdisk -l
24 df
25 mount -o usrquota,grpquota /dev/vdb1 /pub ##显示错误
26 mkfs.xfs /dev/vdb1 -f ##格式化文件系统,使其可用(blkid)。强制更改文件类型 =--force
27 mount -o usrquota,grpquota /dev/vdb1 /pub ##指定挂载文件系统时的选项,有些也可写在/etc/fstab中
28 ls -ld /pub
29 quotaon -ugv /dev/vdb1 ##激活并显示过程
32 useradd westos
33 edquota -u westos ##编写文件,限制可用大小(hard选项)
35 chmod 777 /pub ##在挂载后进行对/pub权限的管理,否则切换用户后使用dd对/dev/vdb1进行操作时会出现权限的问题
34 su - westos ##切换用户,尝试占用文件系统大小
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00585176 s, 179 MB/s
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=250
dd: error writing ‘/pub/file’: Disk quota exceeded
201+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.938883 s, 223 MB/s
36 ls -l /pub ##查看被划分出来的区域
37 ll -l /pub
文件:(hard即为分配给用户的可使用空间)
Disk quotas for user westos (uid 1001):
Filesystem blocks soft hard inodes soft hard
/dev/vdb1 0 0 204800 0 0 0
~
~
[root@localhost ~]# mkdir /pub
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x23f9e63f
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 83 Linux
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3809224 6664676 37% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 140 942520 1% /dev/shm
tmpfs 942660 17032 925628 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
[root@localhost ~]# mount -o usrquota,grpquota /dev/vdb1 /pub
mount: unknown filesystem type 'swap'
[root@localhost ~]# mkfs.xfs /dev/vdb1 -f
meta-data=/dev/vdb1 isize=256 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount -o usrquota,grpquota /dev/vdb1 /pub
[root@localhost ~]# ls -ld /pub
drwxr-xr-x. 2 root root 6 Apr 22 01:49 /pub
[root@localhost ~]# quotaon -ugv /dev/vdb1
quotaon: Enforcing group quota already on /dev/vdb1
quotaon: Enforcing user quota already on /dev/vdb1
[root@localhost ~]# edquoya -u westos
bash: edquoya: command not found...
[root@localhost ~]# edquota -u westos
edquota: user westos does not exist.
[root@localhost ~]# useradd westos
[root@localhost ~]# edquota -u westos
[root@localhost ~]# su - westos
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M
dd: failed to open ‘/pub/file’: Permission denied
[westos@localhost ~]$ exit
logout
[root@localhost ~]# chmod 777 /pub
[root@localhost ~]# su - westos
Last login: Sat Apr 22 01:55:58 EDT 2017 on pts/1
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00585176 s, 179 MB/s
[westos@localhost ~]$ dd if=/dev/zero of=/pub/file bs=1M count=250
dd: error writing ‘/pub/file’: Disk quota exceeded
201+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.938883 s, 223 MB/s
[westos@localhost ~]$ exit
logout
[root@station ~]# ll -ld /pub
drwxrwxrwx. 2 root root 17 Apr 25 10:14 /pub
[root@station ~]# ls -l /pub
total 204800
-rw-rw-r--. 1 westos westos 209715200 Apr 25 10:15 file
##做完了要删除哦