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
##做完了要刪除哦