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.

Mointing partitions from disk images

disk_image For analysis and repairing it is sometimes required to mount disk images from visualization systems like KVM on a host system.
These disk images contain multiple partitions and there own partition table like a real disk – therefore the can not be mounted directly.

fdisk can be used to list the partitions in the disk image and shows the beginning of each partition:

fdisk -l /path/to/disk.img

giving the following output:

Disk disk.img: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00024628

   Device Boot      Start         End      Blocks   Id  System
disk.img1   *          63    16048934     8024436   83  Linux
disk.img2        16048935    16771859      361462+   5  Extended
disk.img5        16048998    16771859      361431   82  Linux swap / Solaris

The partition start sector, multiplied by the sector size in bytes (512 in the example), can be used as the value for the offset parameter for mount to specify which partition to mount.

The following command can be used to mount the first partition, starting at sector 63:

mount -o loop,offset=$((63 * 512)) disk.img /mnt/disk