星期三, 12月 27, 2006

擠壓你的檔案系統

/***********************************
大肥警告!
Big Fat Warning!

這篇文章裡提到的技術可能讓您的資料爆炸,請斟酌使用!
The techniques introduced in this article may blow your data, use them at your own risk!
***********************************/

最近總覺得硬碟很慢... 應該說,作一些需要磁碟 I/O 的動作的時候,感覺硬碟一直嘎啦嘎啦的上氣不接下氣。所以有了這樣的想法:

  1. 現在 CPU 速度呈指數成長
  2. 磁碟傳輸速度呈線性成長(大部分的瓶頸在磁頭讀寫動作)

既然如此,何不適當的要求 CPU 作多一點計算,減少磁碟 I/O 呢?於是就想到要壓縮檔案系統。

可是... 要怎麼壓縮呢?可以從兩個地方著手:

  1. Loop Device Level:使用如 cloopcompFUSEd 等辦法,在 regular filesystem 的 regular file 上再多加一層間接層來壓縮。
  2. Block Device Level:在 Device Driver 加上一層間接層來壓縮,例如使用 dm-cryptcryptsetup-luks
  3. Filesystem Level:讓 Filesystem Driver 來負責處理壓縮,例如使用 jffs2、ext2compr 或 Reiser4experimental cryptcompress plugin

我的想法是,我是要來增進磁碟效能的,所以不應該搞一大堆間接層來拖慢系統速度,所以 cloop 那種東西就根本不考慮了。後來又想從 Block Device Level 來做的,可是 dm-crypt 與 cryptsetup-luks 非常悲慘,還真的只能作 CRYPTographic,沒辦法做 COMPRESSion。我嘗試用 cryptsetup -c lzf luksFormat 來建立 lzf compressed block device,可是殘念的失敗了 XD~

於是,只好讓 Filesystem 來壓啦!可是 jffs2 的間接更多,必須先有個 loopfile map 到 mtd(Memory Technology Device)上,再 mount mtdblock 來用。處理起來速度比沒壓縮的 ext3 還慢,所以... 還是算了吧!而 ext2compr 這個東西實在太老了,只有在 google 搜尋的時候隱約發現他的蹤跡,可是真的想要找 howto 的時候卻什麼也找不到。所以... 這個也算了吧 = =

最後的救星是 Reiser4 的 Cryptcompress plugin,也是要 patch kernel source,也要升級 reiser4progs。

安裝的方法比想像中簡單的多。首先,你需要一個支援 Reiser4 與 cryptcomress 的 kernel,再來就是支援 cryptcompress 的 {mkfs,fsck,debugfs,measurefs}.reiser4。讓我們從 kernel 開始:

// 如果您已經有裝 app-portage/layman,可以省略這個步驟:
# emerge app-portage/layman
// 如果您已經有加入該 custom-kernels overlay,可以省略這個步驟:
# layman -a custom-kernels
// 最後,這是支援 reiser4 與 cryptcompress 的 kernel:
# emerge sys-kernel/beyond-sources

然後,在 make *config 的時候,把 reiser4 support 打開(廢話!),編譯您的新 kernel,然後用它開機。

再來是 userspace tools - 新版的 reiser4progs。這個我沒有找到任何 overlay 裡面有(其實它也還沒有正式 release),所以先貼在這吧!

# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-fs/reiser4progs/reiser4progs-1.0.5.ebuild,v 1.5 2006/08/20 21:23:43 vapier Exp $

inherit toolchain-funcs

MY_P=${PN}-${PV/_p/-}
DESCRIPTION="reiser4progs: mkfs, fsck, etc..."
HOMEPAGE="http://www.namesys.com/v4/v4.html"
#SRC_URI="ftp://ftp.namesys.com/pub/reiser4progs/${MY_P}.tar.gz"
SRC_URI="ftp://ftp.namesys.com/pub/tmp/cryptcompress/2.6.19-rc4-mm1/${MY_P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
#KEYWORDS="amd64 ppc ppc64 -sparc x86"
KEYWORDS="~amd64"
IUSE="static debug readline"

DEPEND=">=sys-libs/libaal-1.0.5
       readline? ( sys-libs/readline )"

S=${WORKDIR}/${MY_P}

src_unpack() {
    unpack ${A}
    cd "${S}"
    # bundled libtool sucks, so rebuild autotools #74817
    aclocal && libtoolize -c -f && autoconf && automake || die "autotools failed"
    cat <<-EOF > run-ldconfig
        #!/bin/sh
        true
    EOF
}

src_compile() {
    econf \
        $(use_enable static full-static) \
        $(use_enable static mkfs-static) \
        $(use_enable static fsck-static) \
        $(use_enable static debugfs-static) \
        $(use_enable static measurefs-static) \
        $(use_enable static cpfs-static) \
        $(use_enable static resizefs-static) \
        $(use_enable debug) \
        $(use_with readline) \
        --enable-libminimal \
        --sbindir=/sbin \
        || die "configure failed"
    emake || die "make failed"
}

src_install() {
    make DESTDIR="${D}" install || die
    dodoc AUTHORS BUGS CREDITS ChangeLog NEWS README THANKS TODO
    #resizefs binary doesnt exist in this release
    rm -f "${D}"/usr/share/man/man8/resizefs*

    # move shared libs to /
    dodir /$(get_libdir)
    mv "${D}"/usr/$(get_libdir)/lib*.so* "${D}"/$(get_libdir)/ || die
    gen_usr_ldscript libreiser4-minimal.so libreiser4.so librepair.so
}

把這個檔案放到 /usr/local/portage/sys-fs/reiser4progs/reiser4progs-1.0.6.ebuild,然後...

# cd /usr/local/portage/sys-fs/reiser4progs
# ebuild reiser4progs-1.0.6.ebuild digest
# emerge reiser4progs -va

編完以後,就可以來作新的分割區了!別忘了將各參數改成您的設定。

// 先將檔案備份起來
# cd /
# tar cp /path/to/mounted_dir > /path/to/backup_file.tar
// 卸載檔案系統
# umount /dev/hdXY
// 建立檔案系統(使用 lzo 壓縮演算法)
# mkfs.reiser4 -o create=ccreg40 /dev/hdXY
// 建立檔案系統(使用 gzip 壓縮演算法,較耗費 CPU)
# mkfs.reiser4 -o create=ccreg40,compress=gzip1 /dev/hdXY
// 掛載檔案系統
# mount /dev/hdXY
// 還原備份檔
# cd /
# tar xf /path/to/backup_file.tar
# sync

在 umount 之前可以先 df 紀錄用掉的空間,跟改用 cryptcompress 以後比較看看。我的狀況是 855M 變成 524M,省了 331M 的空間。