在 AWS 上的 Ubuntu安裝zram-config出錯

放在 Amazon Web Services (AWS) 上的機器因為用途跟價格的關係, ram 的規格開很小,靠 swap 來緩衝的話吃 IO 吃比較兇會被多收錢,想要用 zram 來壓記憶體

zram 的說明可以參考wikipedia
http://zh.wikipedia.org/wiki/Zram

zram是Linux核心的一個模組,之前被稱為「compcache」。zram透過在RAM內的壓縮塊裝置上分頁,直到必須使用硬碟上的交換空間,以避免在磁碟上進行分頁,從而提高效能。由於zram可以用記憶體替代硬碟為系統提供交換空間的功能,zram可以讓Linux在需要大量RAM的情況下在記憶體上進行交換/分頁,而提高記憶體的使用率,顯著得減少系統啟動時(此時Linux還不能使用外部儲存)對記憶體大小的要求。在實體記憶體較少的舊電腦上,尤其如此。

Lubuntu(13.10開始) 以及 Chrome OS 預設都有啟用這項功能
所以就來安裝 zram-config,沒想到竟然失敗 …
(不過用這方法來解這問題被學長唸就是了XD)

錯誤訊息大概是這樣:

testuser@ubuntu:~$ sudo apt-get install zram-config
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
zram-config
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/3,078 B of archives.
After this operation, 42.0 kB of additional disk space will be used.
Selecting previously unselected package zram-config.
(Reading database … 107939 files and directories currently installed.)
Unpacking zram-config (from …/zram-config_0.1_all.deb) …
Processing triggers for ureadahead …
Setting up zram-config (0.1) …
start: Job failed to start
invoke-rc.d: initscript zram-config, action “start” failed.
dpkg: error processing zram-config (–configure):
subprocess installed post-installation script returned error exit status 1

Errors were encountered while processing:
zram-config
E: Sub-process /usr/bin/dpkg returned an error code (1)

從這邊看只知道 post-installation script returned error exit status 1
可能資質比較鈍 … 把 script 挖出來看也沒看出所以然 …

後來看到另一個 script:
https://github.com/gionn/etc/blob/edac27cf705d8ca12d39effa0f80ba62e3907288/init.d/zram

把這拿來一用… 馬上就看到是缺 zram module 了Orz.. (可參考上方script第50行前後的code)
* Loading zRAM kernel module
FATAL: Module zram not found.

原來是 AWS 提供的 Ubuntu image 預設沒有 extra modules,按照 Linux kernel 版本把對應的 extra modules 裝上就行了:

得到結果:

testuser@ubuntu:~$ sudo apt-get install zram-config
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following package was automatically installed and is no longer required:
linux-image-3.8.0-34-generic
Use ‘apt-get autoremove’ to remove it.
The following NEW packages will be installed:
zram-config
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/3,078 B of archives.
After this operation, 42.0 kB of additional disk space will be used.
Selecting previously unselected package zram-config.
(Reading database … 97293 files and directories currently installed.)
Unpacking zram-config (from …/zram-config_0.1_all.deb) …
Processing triggers for ureadahead …
Setting up zram-config (0.1) …
zram-config start/running

來確認一下:

testuser@ubuntu:~$ free -h
total used free shared buffers cached
Mem: 588M 536M 52M 0B 9.3M 338M
-/+ buffers/cache: 188M 400M
Swap: 294M 88K 294M

testuser@ubuntu:~$ cat /proc/swaps
Filename Type Size Used Priority
/dev/zram0 partition 301552 124 5

搞定收工啦!

害我抓了兩台 Ubuntu 13.04 來測試結果是好的, 還把它們升級 13.10 … 一不小心蓋掉了一份資料 …

發表迴響