{"id":77924,"date":"2023-09-10T21:58:04","date_gmt":"2023-09-10T13:58:04","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=77924"},"modified":"2023-09-10T21:58:04","modified_gmt":"2023-09-10T13:58:04","slug":"linux-swap-partition","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-swap-partition.html","title":{"rendered":"Linux \u4e0a\u521b\u5efa\u6216\u6269\u5c55\u4ea4\u6362\u5206\u533a\u7684\u65b9\u6cd5!"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u7528\u6237\u53ef\u4ee5\u5728\u4efb\u4f55 Linux \u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u6216\u8005\u662f\u5176\u5b83\u5fc5\u8981\u7684\u65f6\u5019\u521b\u5efa\u4ea4\u6362\u7a7a\u95f4\u3002\u5982\u679c\u4f60\u5728\u5b89\u88c5 Linux \u7684\u65f6\u5019\u5fd8\u8bb0\u4e86\u521b\u5efa\u6216\u662f\u4f60\u60f3\u8981\u518d\u589e\u52a0\u4ea4\u6362\u5206\u533a\u7684\u7a7a\u95f4\uff0c\u4f60\u968f\u65f6\u90fd\u53ef\u4ee5\u518d\u521b\u5efa\u6216\u589e\u52a0\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6709\u65f6\u5019\u5728\u4f60\u5b89\u88c5\u540e\u6447\u5347\u7ea7 RAM \u7684\u65f6\u5019\u9700\u8981\u589e\u52a0\u4e00\u70b9\u4ea4\u6362\u5206\u533a\u7684\u7a7a\u95f4\uff0c\u6bd4\u5982\u4f60\u8981\u5c06\u4f60\u7684\u7cfb\u7edf\u7684 RAM \u4ece 1GB \u5347\u7ea7\u5230 2GB \u4f60\uff0c\u90a3\u4e48\u4f60\u5c31\u4e0d\u5f97\u4e0d\u5c06\u4f60\u7684\u4ea4\u6362\u5206\u533a\u7a7a\u95f4\u4e5f\u5347\u7ea7\u4e00\u4e0b\uff08\u4ece 2GB \u5230 4GB\uff09\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u4f7f\u7528\u7684\u5bb9\u91cf\u662f\u7269\u7406 RAM \u7684\u53cc\u500d\u5bb9\u91cf\u3002\uff08LCTT \u8bd1\u6ce8\uff1a\u5176\u5b9e\u8fd9\u91cc\u662f\u4e2a\u8bef\u533a\uff0c\u4ea4\u6362\u5206\u533a\u4e0d\u4e00\u5b9a\u975e\u5f97\u662f\u53cc\u500d\u7684\u7269\u7406\u5185\u5b58\u5bb9\u91cf\uff0c\u53ea\u662f\u60ef\u4f8b\u5982\u6b64\u3002\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u4f60\u7684\u7269\u7406\u5185\u5b58\u8db3\u591f\u7684\u8bdd\uff0c\u4f60\u5b8c\u5168\u53ef\u4ee5\u4e0d\u7528\u4ea4\u6362\u5206\u533a\u2014\u2014\u5728\u8fd9\u91cc\u7684\u60c5\u5f62\u4e0b\uff0c\u6216\u8bb8\u4f60\u589e\u52a0\u4e86\u7269\u7406\u5185\u5b58\uff0c\u5c31\u6ca1\u5fc5\u8981\u589e\u52a0\u4ea4\u6362\u5206\u533a\u5927\u5c0f\u4e86\u3002\uff09<\/p>\n

\u4ea4\u6362\u7a7a\u95f4\u662f\u5f53\u7269\u7406\u5185\u5b58\uff08RAM \u968f\u673a\u5b58\u53d6\u5b58\u50a8\u5668\uff09\u7684\u7528\u91cf\u5df2\u6ee1\u65f6\uff0c\u88ab\u4fdd\u7559\u7528\u4f5c\u865a\u62df\u5185\u5b58\u7684\u78c1\u76d8\u4e0a\u7684\u7a7a\u95f4\u3002 \u5982\u679c\u7cfb\u7edf\u5728 RAM \u6ee1\u8f7d\u65f6\u9700\u8981\u66f4\u591a\u7684\u5185\u5b58\u8d44\u6e90\uff0c\u5185\u5b58\u4e2d\u7684\u975e\u6d3b\u52a8\u9875\u9762\u5c06\u88ab\u79fb\u52a8\u5230\u4ea4\u6362\u7a7a\u95f4\uff0c\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u7cfb\u7edf\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u66f4\u591a\u7684\u65f6\u95f4\uff0c\u4f46\u4e0d\u5e94\u8be5\u628a\u5b83\u5f53\u505a RAM \u7684\u6269\u5c55\u3002<\/p>\n

\u5efa\u8bae\u4f60\u521b\u5efa\u4e00\u4e2a\u4e13\u7528\u7684\u4ea4\u6362\u5206\u533a\uff0c\u4f46\u662f\u5982\u679c\u4f60\u6ca1\u6709\u53ef\u7528\u7684\u5206\u533a\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4ea4\u6362\u6587\u4ef6\uff0c\u6216\u4ea4\u6362\u5206\u533a\u548c\u4ea4\u6362\u6587\u4ef6\u7684\u7ec4\u5408\u3002 \u4ea4\u6362\u7a7a\u95f4\u901a\u5e38\u5efa\u8bae\u7528\u6237\u81f3\u5c11 4 GB\uff0c\u7528\u6237\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u8981\u6c42\u548c\u73af\u5883\u521b\u5efa\u4ea4\u6362\u7a7a\u95f4\u3002<\/p>\n

\u6211\u53d1\u73b0\u5927\u90e8\u5206 VM \u548c \u4e91\u670d\u52a1\u5668\u90fd\u6ca1\u6709\u4ea4\u6362\u5206\u533a\uff0c\u6240\u4ee5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e09\u79cd\u65b9\u6cd5\u521b\u5efa\uff0c\u6269\u5c55\u6216\u589e\u52a0\u4ea4\u6362\u7a7a\u95f4\u3002<\/p>\n

\u5982\u4f55\u68c0\u6d4b\u5f53\u524d\u4ea4\u6362\u5206\u533a\u5927\u5c0f<\/strong><\/div>\n

\u901a\u8fc7 free & swapon \u547d\u4ee4\u6765\u68c0\u6d4b\u5f53\u524d\u7684\u4ea4\u6362\u5206\u533a\u7a7a\u95f4\u7684\u5927\u5c0f\u3002<\/p>\n

$ free -h\r\ntotal used free shared buff\/cache available\r\nMem: 2.0G 1.3G 139M 45M 483M 426M\r\nSwap: 2.0G 655M 1.4G\r\n$ swapon --show\r\nNAME TYPE SIZE USED PRIO\r\n\/dev\/sda5 partition 2G 655.2M -1<\/pre>\n

\u4e0a\u9762\u7684\u8f93\u51fa\u663e\u793a\u4e86\u5f53\u524d\u7684\u4ea4\u6362\u5206\u533a\u7a7a\u95f4\u662f 2GB \u3002<\/p>\n

\u65b9\u6cd5 1 : \u901a\u8fc7 fallocate \u547d\u4ee4\u521b\u5efa\u4ea4\u6362\u6587\u4ef6<\/strong><\/div>\n

fallocate \u7a0b\u5e8f\u662f\u7acb\u5373\u521b\u5efa\u9884\u5206\u914d\u5927\u5c0f\u7684\u6587\u4ef6\u7684\u6700\u4f73\u65b9\u6cd5\u3002<\/p>\n

\u4e0b\u9762\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u521b\u5efa\u4e00\u4e2a 1GB \u5927\u5c0f \u7684 \/swapfile\u3002<\/p>\n

$ sudo fallocate -l 1G \/swapfile<\/pre>\n

\u68c0\u67e5\u4e00\u4e0b\u521b\u5efa\u7684\u6587\u4ef6\u7684\u5927\u5c0f\u662f\u5426\u6b63\u786e\u3002<\/p>\n

$ ls -lh \/swapfile\r\n-rw-r--r-- 1 root root 1.0G Jun 7 09:49 \/swapfile<\/pre>\n

\u5c06\u8be5\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e\u4e3a 600 \u8fd9\u6837\u53ea\u6709 root \u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e2a\u6587\u4ef6\u3002<\/p>\n

$ sudo chmod 600 \/swapfile<\/pre>\n

\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u7684\u547d\u4ee4\u6765\u5c06\u6b64\u6587\u4ef6\u8f6c\u6362\u4e3a\u4ea4\u6362\u6587\u4ef6\u3002<\/p>\n

$ sudo mkswap \/swapfile\r\nSetting up swapspace version 1, size = 1024 MiB (1073737728 bytes)\r\nno label, UUID=cda50e0e-41f3-49c7-af61-b8cb4a33a464<\/pre>\n

\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u7684\u547d\u4ee4\u6765\u4f7f\u4ea4\u6362\u6587\u4ef6\u751f\u6548\u3002<\/p>\n

$ sudo swapon \/swapfile<\/pre>\n

\u5c06\u65b0\u521b\u5efa\u7684\u4ea4\u6362\u6587\u4ef6\u6dfb\u52a0\u5230 fstab \u6587\u4ef6\u4e2d\uff0c\u8fd9\u6837\u4ea4\u6362\u5206\u533a\u7a7a\u95f4\u7684\u4fee\u6539\u5373\u4f7f\u5728\u91cd\u542f\u540e\u4e5f\u53ef\u4ee5\u751f\u6548\u3002<\/p>\n

$ vi \/etc\/fstab\r\n\/swapfile swap swap defaults 0 0<\/pre>\n

\u68c0\u67e5\u4e00\u4e0b\u65b0\u521b\u5efa\u7684\u4ea4\u6362\u6587\u4ef6\u3002<\/p>\n

$ swapon --show\r\nNAME TYPE SIZE USED PRIO\r\n\/dev\/sda5 partition 2G 657.8M -1\r\n\/swapfile file 1024M 0B -2<\/pre>\n

\u73b0\u5728\u6211\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u65b0\u7684 1GB \u7684 \/swapfile1 \u6587\u4ef6\u4e86\u3002\u91cd\u542f\u7cfb\u7edf\u4ee5\u4f7f\u65b0\u7684\u4ea4\u6362\u6587\u4ef6\u751f\u6548\u3002<\/p>\n

\u65b9\u6cd5 2 : \u901a\u8fc7 dd \u547d\u4ee4\u6765\u521b\u5efa\u4ea4\u6362\u6587\u4ef6<\/strong><\/div>\n

dd \u547d\u4ee4\u662f\u53e6\u4e00\u4e2a\u5b9e\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7acb\u5373\u521b\u5efa\u9884\u5206\u914d\u5927\u5c0f\u7684\u6587\u4ef6\u3002<\/p>\n

\u4ee5\u4e0b dd \u547d\u4ee4\u5c06\u521b\u5efa 1GB \u7684 \/swapfile1\u3002<\/p>\n

$ sudo dd if=\/dev\/zero of=\/swapfile1 bs=1G count=1\r\n1+0 records in\r\n1+0 records out\r\n1073741824 bytes (1.1 GB, 1.0 GiB) copied, 16.6154 s, 64.6 MB\/s<\/pre>\n

\u8be6\u89e3\uff1a<\/p>\n