Using a sparse file for portage tree

As I’m running gentoo on my notebook I have to do a sync of the portage tree from time to time. The sync process uses rsync and touches a lot of small files, which leads to some performance impacts as I’m using btrfs on this system. A way to deal with this is using a sparse file containing a filesystem which can handle small files better than btrfs.

At first, the sparse file must be created:

truncate -s 10G /usr/portage.img

This file can now be formated using ext2 with a reduced block size:

mke2fs  -b 1024 -i 2048 -m 0 -O "dir_index" -F /usr/portage.img

To disable file system check complains during boot, the following command can be used:

tune2fs -c 0 -i 0 /usr/portage.img

btrfs would do all its COW magic on the sparse file, too. As this would also impact the performance, the behavior could be disable for the sparse file:

chattr +C /usr/portage.img

Now, it’s time to mount the sparse file and move the data from the existing portage tree to the sparse file (this can be skipped the sparse file is created and mounted before the first sync during installation):

# Move away the existing portage
mv /usr/portage{,.old}
 
# And recreate it
mkdir /usr/portage
 
# Mount the sparse file
mount -o loop,noatime,nodev /usr/portage.img /usr/portage
 
# Copy the data
cp -vR /usr/portage.old/* /usr/portage
 
# Remove the old portage tree
rm -fR /usr/portage.old

To make the portage tree available after boot, the following line must be added to /etc/fstab:

/usr/portage.img    /usr/portage    ext2    noatime,nodev,loop    0 0

That’s it.

↑Back to top