Gentoo 历程(7):压缩portage树,并放到/var

    当初装gentoo的时候以为gentoo和arch一样,把软件包的各种信息和缓存都放到/var,就给/分了10g,/var分了6g,后来用着才发现,gentoo的portage在/usr/portage下,下载的包也在/usr/portage下,结果用了不长时间/就满了,各种悲催……上linuxsir问了问老鸟,有人提议清理缓存,有人提议用squashfs把portage压缩,那时正好比较忙,没时间折腾,就采用了清理缓存这个办法,直到前几天,/又满了,看来portage这个东西必须得处理下了,linuxsir上那位前辈给提供了几个链接,从这样做的原理到做法都有,帖子地址在这里,不过不知道论坛升级后这地址还能不能正常看。

    具体原因我这里就不说了,那帖子里说的很明白,只说说我折腾的具体过程。

    官方wiki在这里

    首先,当然是看官方wiki,不过由于我升级到一半/满了,导致无法进系统,懒得先修它,就用arch chroot了。

    第一步,按照wiki里的说法,先安装squashfs-tools和aufs3(2.x的内核装aufs2),其中aufs3要修改use,执行:

echo sys-fs/squashfs-tools kernel-patch >> /etc/portage/package.use

然后再emerge安装aufs3,装完aufs3按提示重新编译内核,编译内核的时候要保证下面的这些被选中:

Device Drivers --->
  Block Devices --->
    <M> Loopback device support
File systems --->
  Miscellaneous Filesystems --->
    <M> SquashFS

上面是wiki推荐的,我不喜欢把这种每次都要用的东西编译成模块,所以直接编译进了内核。

    第二步,修改几个配置文件,挪几个文件夹,具体如下:

1.创建/var/portage文件夹

2.将/usr/portage/distfiles文件夹挪到/var/portage/

3.将/var/lib/layman文件夹挪到/var/portage/(这个我觉得我用不太着,不过官方wiki这么说我就这么做吧,以前有过一次乱搞portage出错的经历,不这么做说不准会出啥问题)

4./etc/make.conf文件:

DISTDIR="/var/portage/distfiles"

source /var/portage/layman/make.conf

5./var/portage/layman/make.conf文件:

将所有/var/lib/xxx改成/var/portage/xxx


    完成上述步骤后,准备工作就算完成了,下面是生成sqfs,并将这个压缩的portage tree 用起来


    第一步,添加/etc/init.d/squash_portage ,这个文件的内容如下:

#!/sbin/runscript
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
#
# /etc/init.d/squash_portage allows efficient compression of
# Gentoo portage arborescence
#
# It requires support for the loop device and squashfs enabled in the kernel,
# module autoloading is also *highly* recommended.
# sys-fs/squashfs and sys-fs/aufs are necessary for read-write support.
#
# Author: Mathias Laurin <mathias_laurin@users.sourceforge.net>
# 2006-11-28, v.0.1.5(4)
# 2009-02-24, v.0.1.6(1) Weedy <weedy2887@gmail.com>
# 2009-03-20, v.0.1.7(1) j0inty <j0inty@stollfuss.net>
# 2009-07-10, v.0.1.8(1) j0inty
# 2009-09-01. v.0.1.9(1) nall <soir@fuzzysock.net>

extra_started_commands="sync"

source /etc/make.globals
source /etc/make.conf
SQFS_CUR="$SQFS_DIRNAME/portage.sqfs"
SQFS_NEW="$SQFS_DIRNAME/portage-current.sqfs"
SQFS_OLD="$SQFS_DIRNAME/portage-old.sqfs"
DEF_RW="/dev/shm/.portage-rw"
SQFS_OPTS="-force-uid portage -force-gid portage -no-duplicates"

depend() {
	need localmount modules
}


check_support() {
	if ! [ -x /usr/bin/mksquashfs ] ; then
		eerror "ERROR: sys-fs/squashfs-tools is not installed."
		return 1
	fi
	if ! [ -w /dev/loop0 ] ; then
		eerror "ERROR: loopback support is not available."
		return 1
	fi
	if ! [[ $(grep -s $'\taufs$' /proc/filesystems) ]] ; then
		eerror "ERROR: aufs filesystem support is not available."
		return 1
	fi
	if ! [[ $(grep -s $'\tsquashfs$' /proc/filesystems) ]] ; then
		eerror "ERROR: squashfs filesystem support is not available."
		return 1
	fi
	return 0
}

makeImage() {
	mksquashfs $PORTDIR $SQFS_NEW $SQFS_OPTS # 2>/dev/null
	retval=$?
	ln -sf $SQFS_NEW $SQFS_CUR
 	eend $retval
}

sync() {
	ebegin "Syncing portage tree"
	eval $SYNC_CMDS
 	#svc_stop; svc_start
	stop
	start
	eend 0
}

start() {
	check_support || return 1
	if [ -f "$SQFS_CUR" ]; then
		ebegin "SQFS-PORTAGE: Mounting read-only squashfs image"
		mount -rt squashfs -o loop,nodev,noexec $SQFS_CUR $PORTDIR
		retval=$?
		[ $retval -ne 0 ] && return $retval
	else
		if [ ! -f "/usr/portage/metadata/timestamp.chk" ]; then
			ebegin "SQFS-PORTAGE: $PORTDIR looks empty or corrupted, syncing"
			eval $SYNC_CMDS
		fi
		einfo "  $SQFS_CUR does not exist, creating"
		mkdir -p $SQFS_DIRNAME
		makeImage
		[ $? -ne 0 ] && eerror "ERROR: failed to create initial tree image"
		einfo "Clearing ${PORTDIR}"
		rm -r ${PORTDIR}
		mkdir ${PORTDIR}
		start
		eend 0
	fi

	ebegin "Mounting read-write with aufs"
	if [ ! $PORTAGE_RW ] ; then
		einfo "  mounted in tmpfs (RAM)"
		PORTAGE_RW="${DEF_RW}"
	fi
	[ -d $PORTAGE_RW ] || mkdir -p $PORTAGE_RW
	chmod 0750 $PORTAGE_RW
	chown portage:portage $PORTAGE_RW
	mount -t aufs -o nodev,noexec,br=$PORTAGE_RW=rw:$PORTDIR=ro aufs $PORTDIR
	eend $?

	if [ "$DISTDIR" == "/usr/portage/distfiles" ]; then
		mkdir -p /usr/local/distfiles 
		mount -o bind /usr/local/distfiles /usr/portage/distfiles
		ewarn "DISTDIR is currently inside the portage tree. It has been bind 
		mounted to keep the SquashFS image small."
	fi
}

stop() {
	ebegin "SQFS-PORTAGE: Stopping and unmounting"
	[ ! $PORTAGE_RW ] && PORTAGE_RW="${DEF_RW}"
	if [ $(du -s --exclude=.w* $PORTAGE_RW | cut -f 1) -gt 4 ]; then
		einfo "  Changes detected, updating image."
		mv -f $SQFS_NEW $SQFS_OLD
		makeImage
		rm -f $SQFS_OLD
	else
		einfo "  No changes detected, skipping update."
		eend 0
	fi

	if [ "$DISTDIR" == "/usr/portage/distfiles" ]; then
		einfo "  Unmounting distfiles"
		umount /usr/local/distfiles
	fi;

	einfo "  Unmounting the tree"
	umount -t aufs  $PORTDIR
	umount -t squashfs $PORTDIR
	rm -rf $PORTAGE_RW
	eend 0
}
使之可执行:

#chmod 755 /etc/init.d/squashfs_portage

将其添加到default runlevel:

#rc-update add squash_portage default

    第二步,添加文件/etc/conf.d/squash_portage:

# /etc/conf.d/squash_portage

# SQFS_DIRNAME points to the directory that will contain the sqfs
# images, recommended value is /var/portage
SQFS_DIRNAME="/var/portage"

# Leave PORTAGE_RW empty for use with tmpfs, a ram-based filesystem,
# This is recommended unless you are short of RAM
PORTAGE_RW=""

# If you need more then just emerge --sync, or are using another
# package manager add them here. Example SYNC_CMDS="/usr/bin/layman -S; /usr/bin/eix-sync"
SYNC_CMDS="emerge --sync"

    第三步(不是每人都用):我用的是openrc,所以还要修改一下/etc/config.d/modules:在modules="nvidia vboxdrv ..."里加上aufs。

   

    这样重新启动应该就好了(为啥说应该呢?因为我第一次启动前忘了第三步,所以我的squash_portage启动失败了,sqfs文件是启动后自己手动生成的),第一次启动需要生成一个portage.sqfs文件,我的这个文件有2.2G,这文件大小是因人而异的,所以生成速度也不一样,据说有人的只有几十M……

sync的时候执行/etc/init.d/squash_portage sync,其它的按原来的习惯正常使用就成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值