終於將UbuntuKylin 13.04安裝到了SSD上,過程很艱苦啊!
建議通過USB Live的方式進行安裝,速度很快,實測128G SSD + 8G 內存,在聯網下載的情況下,大約8分鐘安裝完畢。
第一步:關閉ext4的日誌功能
注意:系統掛載時無法停用日誌功能,所以需要使用安裝用的U盤系統,利用root權限執行:
- # sudo tune2fs -O ^has_journal /dev/sda1
執行操作檢測:
- # sudo e2fsck -f /dev/sda1
特別提示:如果SSD的硬盤被分了不止一個區,需要將每一個區的日誌功能都設置好。
重啓,進入SSD中的系統,檢查是否設置成功:
- # dmesg | grep EXT4
如果出現類似如下的結果表明設置成功:
- [ 7.003337] EXT4-fs (sda3): mounted filesystem without journal. Opts: (null)
- [ 7.983753] EXT4-fs (sda3): re-mounted. Opts: discard,errors=remount-ro
- [ 8.025093] EXT4-fs (sda1): mounted filesystem without journal. Opts: discard
- [ 8.033006] EXT4-fs (sda4): mounted filesystem without journal. Opts: discard
- [ 8.199882] EXT4-fs (sdb1): mounted filesystem with ordered data mode. Opts: (null)
第二步:啓用TRIM,並設置讀寫選項
TRIM是一種操作系統調度SSD塊寫入的方式。主要是因爲同一個SSD的閃存單元頻繁操作會磨損,影響使用壽命,區別於傳統的機械硬盤處理刪除數據。Linux內核自2.6.33開始支持TRIM。
首先,檢查內核版本及SSD硬盤是否支持TRIM:
- # sudo uname -a
- # sudo hdparm -I /dev/sda | grep TRIM
如果硬件支持,會提示類似如下的信息:
- * Data Set Management TRIM supported (limit 8 blocks)
- * Deterministic read ZEROs after TRIM
啓用TRIM功能,需要修改/etc/fstab文件,在選項中加入discard。
也可以通過noatime,nodiratime選項,強制Linux不對訪問文件的時間做記錄,以減少對SSD的寫操作。
文件修改例:
- UUID=9064cc67-5f20-4cf6-b71c-f18d78bfb60b / ext4 noatime,nodiratime,discard,errors=remount-ro 0 1
- UUID=869f54b7-b9f2-414f-9436-565412827c39 /boot ext4 noatime,nodiratime,discard,defaults 0 2
- UUID=cec0f78a-581f-4748-a686-acbbdea23270 /home ext4 noatime,nodiratime,discard,defaults 0 2
重新啓動後,用mount命令查看是否成功。
第三步:交換空間
對於大內存來說swap基本上都是空閒的,除非電腦進入休眠狀態,系統會將內存內容轉到swap中。有了SSD後swap基本沒用,可以不分配。(但安裝不通過,強制要有swap分區)。
但在安裝時,如果沒有swap分不通過,我的作法是儘量分配得小(1G),並且通過設置來減少swap換出量。
可先查看當前的/proc/sys/vm/swappiness值(我的默認爲60)。
設置/etc/sysctl.conf,如下:
- vm.swappiness=1
- vm.vfs_cache_pressure=50
第四步:運行時的I/O調度
Linux有4種I/O調度策略,其中有些適合於普通機械硬盤,有些適合虛擬機或者數據庫,具體可參考Linux IO Scheduler。
對於SSD來說noop和deadline調度器都是不錯的選擇。前者優勢是簡單,後者主要用於數據庫等有高性能磁盤的場景,對於讀更有優化。
設置的方法是建立規則文件:
- # sudo gedit /etc/udev/rules.d/60-schedulers.rules
內容如下:
- # Handle the scheduler choice according to the type of disk detected
- # system default : set cfq scheduler for rotating disks
- ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"
- # SSD specific : set deadline scheduler for non-rotating disks
- ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline"
通過如下命令性能實測:
- # sudo hdparm -Tt /dev/sda
noop模式:
- /dev/sda:
- Timing cached reads: 12434 MB in 2.00 seconds = 6220.51 MB/sec
- Timing buffered disk reads: 1472 MB in 3.00 seconds = 490.11 MB/sec
deadline模式:
- /dev/sda:
- Timing cached reads: 11510 MB in 2.00 seconds = 5757.64 MB/sec
- Timing buffered disk reads: 1544 MB in 3.00 seconds = 514.00 MB/sec
第五步:內存分區加速
如果內存夠大,可以用ramdisk的方式,將一些經常變化的位置如/tmp放入內存,加快速度,減少對SSD的訪問。
在/etc/fstab中加入:
- # SSD tweak : temporary directories as tmpfs
- tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
- tmpfs /var/spool tmpfs defaults,noatime,mode=1777 0 0
- # SSD tweak : log directory as tmpfs
- tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
如需要控制tmp目錄大小,在mod選項前插入“size=nn[G | M]”的選項。
特別提示:會有很多應用程序將日誌保存在/var/log路徑下並造成SSD的大量訪問,如果不在意這些日誌丟失,可以將其放在內存中。
限制:
- 需要至少4G的內存空間。
- 需要監控應用程序對/tmp空間的使用(例如:burners等)。
- 如果是多用戶系統,其它用戶會看到/tmp路徑中的內容。
第六步:設置瀏覽器的緩存
可通過graysky程序來設置所有瀏覽器的緩存。
- # sudo add-apt-repository ppa:graysky/utils
- # sudo apt-get update
- # sudo apt-get install profile-sync-daemon
- ...
- --------------------------------------------------------------------------
- Define which users will make use of the sync in /etc/psd.conf
- Read the manpage before use or see the wiki page
- https://wiki.archlinux.org/index.php/Profile-sync-daemon
- ALWAYS backup your profiles data before using utils like psd!
- --------------------------------------------------------------------------
- Adding system startup for /etc/init.d/psd ...
- /etc/rc0.d/K20psd -> ../init.d/psd
- /etc/rc1.d/K20psd -> ../init.d/psd
- /etc/rc6.d/K20psd -> ../init.d/psd
- /etc/rc2.d/S20psd -> ../init.d/psd
- /etc/rc3.d/S20psd -> ../init.d/psd
- /etc/rc4.d/S20psd -> ../init.d/psd
- /etc/rc5.d/S20psd -> ../init.d/psd
設置/etc/psd.conf
- ......
- USERS="yourlogin"
- ......
檢測各瀏覽器的設置:
- # sudo profile-sync-daemon parse
- Profile-sync-daemon v5.32 on Ubuntu 13.04.
- Daemon file /var/run/psd is not present.
- Psd will manage the following per /etc/psd.conf settings:
- browser/psname: firefox/firefox
- owner/group: nicholas/nicholas
- sync target: /home/nicholas/.mozilla/firefox/gu0ne6mm.default
- tmpfs dir: /run/shm/nicholas-firefox-gu0ne6mm.default
- profile size: 25M
- browser/psname: google-chrome/chrome
- owner/group: nicholas/nicholas
- sync target: /home/nicholas/.config/google-chrome
- tmpfs dir: /run/shm/nicholas-google-chrome
- profile size: 44M
啓動psd服務:
- # sudo service psd start
參考文獻:
[1] 調度算法與性能:http://dbanotes.net/database/rhel_io_scheduler_database.html
[2] UBUNTU - TWEAKS FOR SSD DRIVE: http://bernaerts.dyndns.org/linux/250-ubuntu-tweaks-ssd