swap分割槽簡介:
Linux下的swap分割槽類似於Windows下的虛擬記憶體,都是當實體記憶體不夠用時臨時充當“記憶體”角色的一塊磁碟空間。
Linux下增加及新建swap分割槽有兩種方法,一種是基於硬碟分割槽的方式,另一種是基於檔案的方式。
1、基於磁碟分割槽的swap
要增加基於分割槽的swap分割槽,首先得保證你有一塊空餘的磁碟空間可供分割槽,這裡因為我是在虛擬機器環境下實驗,所以直接新增一塊8G的硬碟用於分割槽。
下面是操作過程,每一步都有註釋。
[root@localhost ~]# fdisk /dev/sdb //使用fdisk分割槽工具為新增加的磁碟分割槽 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n //新建一個分割槽 Command action e extended p primary partition (1-4) p //新建為主分割槽 Partition number (1-4): 1 //分割槽編號為1 First cylinder (1-1044, default 1): 1 /分割槽起始扇區為1 Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): //這裡我直接按回車了,使用全部可用磁碟空間 Using default value 1044 Command (m for help): t //更改分割槽型別,預設型別為“Linux”,要將其更改為“Linux swap” Selected partition 1 //如果存在多個分割槽,則這裡會要求選擇要操作的分割槽的編號,因為本實驗中只有一個分割槽,所有直接預設是1 Hex code (type L to list codes): 82 //82就是“Linux swap”分割槽型別的編號,這裡可以輸入“list”檢視所有分割槽型別對應的編號 Changed system type of partition 1 to 82 (Linux swap / Solaris) Command (m for help): p //檢視剛剛劃分的swap型別的分割槽 Disk /dev/sdb: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa8cc39af Device Boot Start End Blocks Id System /dev/sdb1 1 1044 8385898+ 82 Linux swap / Solaris Command (m for help): w //儲存對磁碟分割槽做的更改 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
將剛剛建立的分割槽格式化為swap檔案系統。
[root@localhost ~]# mkswap /dev/sdb1 Setting up swapspace version 1, size = 8385892 KiB no label, UUID=4b5f9371-3df6-47d5-bc40-15e6f7bb3db8
檢視當前系統中啟用的swap分割槽的大小。
[root@localhost ~]# free total used free shared buffers cached Mem: 502216 84088 418128 0 6892 31096 -/+ buffers/cache: 46100 456116 Swap: 835576 0 835576
啟用新增加的swap分割槽之後再次檢視系統中已生效的swap分割槽大小,可以發現,其完美增加了8個G~。
[root@localhost ~]# swapon /dev/sdb1 [root@localhost ~]# free total used free shared buffers cached Mem: 502216 90048 412168 0 6944 31088 -/+ buffers/cache: 52016 450200 Swap: 9221464 0 9221464
2、基於檔案的swap
先使用dd命令生成一個2GB大小的檔案。
[root@localhost ~]# dd if=/dev/zero of=/var/swap bs=1M count=2048 記錄了2048+0 的讀入 記錄了2048+0 的寫出 2147483648位元組(2.1 GB)已複製,6.25394 秒,343 MB/秒</pre> 在這個檔案上新建swap檔案系統。 <pre>[root@localhost ~]# mkswap /var/swap mkswap: /var/swap: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 2097148 KiB no label, UUID=344232eb-9654-4167-873b-bdda9d2b01e6
檢視當前系統中生效的swap分割槽的大小。
[root@localhost ~]# free total used free shared buffers cached Mem: 502216 495380 6836 0 1056 438852 -/+ buffers/cache: 55472 446744 Swap: 9221464 8532 9212932
啟用新建的swap分割槽,並檢視系統中生效的swap分割槽的大小,可以發現,增加了2G。
[root@localhost ~]# swapon /var/swap [root@localhost ~]# free total used free shared buffers cached Mem: 502216 496256 5960 0 1152 437816 -/+ buffers/cache: 57288 444928 Swap: 11318608 8440 11310168
開機自動掛載swap分割槽:
將要開機掛載的swap分割槽加入到/etc/fstab
檔案中。
[root@localhost ~]# vim /etc/fstab
以下是修改後的檔案內容,最後兩行就是剛剛新建的swap分割槽,第三行是系統原本的swap分割槽。
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=143f17a3-2d07-44b9-a577-f1609d3fb0c0 /boot ext4 defaults 1 2 /dev/mapper/VolGroup-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/sdb1 swap swap defaults 0 0 /var/swap swap swap defaults 0 0
停用swap分割槽:
先檢視系統中已生效的swap分割槽的大小。
[root@localhost ~]# free total used free shared buffers cached Mem: 502216 494008 8208 0 3700 431888 -/+ buffers/cache: 58420 443796 Swap: 11318608 7472 11311136
停用掉系統中原本的swap分割槽,並檢視系統中生效的swap分割槽的大小,可以發現少了800MB左右的空間。
[root@localhost ~]# swapoff /dev/VolGroup/lv_swap [root@localhost ~]# free total used free shared buffers cached Mem: 502216 495748 6468 0 3872 428504 -/+ buffers/cache: 63372 438844 Swap: 10483032 88 10482944
54
what?
大佬
將RAID10的/dev/md10分割槽,分出一個大小為1G的空間,格式化為swap分割槽,設為開機生效
的意思是開啟自動掛載嘛
是的