From Gentoo Wiki
Jump to: navigation, search


Helpful commands

Force the download of distfiles after installation

root #emerge --ask --fetchonly --emptytree @world

Quick serve files with Python


Quickly share files (be them binary packages or just simple text files). Navigate to the directory containing the files and run:

user $python3 -m http.server

The other party who wants to download only needs the IP address (files are actually hosted on, which accounts for all current IP addresses on the interfaces).

List GPG subkeys

user $gpg --with-fingerprint --with-fingerprint --list-keys <username>

Dynamic link finder

Just dynamic links:

user $ldd /bin/bash | awk -F '=>' '{print $2}' | awk -F ' ' '{print $1}' | sed '/^ *$/d'

Linked packages (needs qfile from app-portage/portage-utils and xargs from sys-apps/findutils:

user $ldd /bin/bash | awk -F '=>' '{print $2}' | awk -F ' ' '{print $1}' | sed '/^ *$/d' | xargs qfile | uniq

netifrc ethtool

CODE netifrc ethtool example
ethtool_change_eth0="speed 100 duplex full autoneg off"

Global search/replace

Overlay wide simple find and replace:

user $find ~/path/to/overlay -type f -exec sed -i 's/original/replacement/g' {} \;

For example, to replace a URL, be sure to escape all the forward slashes:

user $find ./ -type f -exec sed -i 's/https:\/\/\/dtd\/metadata.dtd/http:\/\/\/dtd\/metadata.dtd/g' {} \;

systemd boot time chart:

root #systemd-analyze plot > boot.svg


root #inotifywait --monitor --format '%T: %e %f' --timefmt '%Y %B %d %H:%M:%S' -r /media/


Use dev-python/pychroot or follow the instructions below to manually prepare the chroot environment.

Create a symlink from /mnt/custom to the extracted stage3 or stage4 location:

root #ln -sf /path/to/chroot /mnt/custom

Make sure the network available in the chroot:

root #cp -f /etc/resolv.conf /mnt/custom/etc/resolv.conf

Standard chroot mounts:

root #mount --rbind /dev /mnt/custom/dev && mount --make-rslave /mnt/custom/dev && mount -t proc /proc /mnt/custom/proc && mount --rbind /sys /mnt/custom/sys && mount --make-rslave /mnt/custom/sys && mount --rbind /tmp /mnt/custom/tmp


Separate tree

Make sure /etc/portage/repos.conf/ is created and gentoo.conf is copied:

root #mkdir /mnt/custom/etc/portage/repos.conf && cp /mnt/custom/usr/share/portage/config/repos.conf /mnt/custom/etc/portage/repos.conf/gentoo.conf

Copy the local Portage tree into the chroot:

root #rsync --verbose --recursive --links --safe-links --perms --times --omit-dir-times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages --exclude=/.git /usr/portage/* /mnt/custom/usr/portage
Mounted tree and overlay

Instead of maintaining a separate Portage tree in the chroot(s), it is most reasonable to simply mount the main Gentoo repository in the chroot using the mount command:

root #mount --rbind /usr/portage /mnt/custom/usr/portage

If the overlay is local copy <overlay_name>.conf file to the /etc/portage/repos.conf/ directory in the chroot. If it is remote, the file can be downloaded with a tool such as wget.

root #cp /etc/portage/repos.conf/<overlay_name>.conf /mnt/custom/etc/portage/repos.conf/<overlay_name>.conf

This same approach can be performed with the overlay (be sure to replace <overlay_dir> in the command below with the actual overlay name):

root #mount --rbind /usr/local/overlay/<overlay_name> /mnt/custom//usr/local/overlay/<overlay_name>
Any changes made to the main Gentoo repository or the overlay will result in changes being made to the host system, since the directories are recursively bound to the chroot. Make sure work is done in the host system so that git is setup properly.

Development tools (optional)

root #emerge --ask app-portage/portage-utils app-portage/eix

Environment setup

root #chroot /mnt/custom /bin/bash
root #source /etc/profile && env-update && export PS1="(chroot) ${PS1}"

Tarball compressor

Keep Portage, but not distfiles, packages, or virtual filesystems

When creating a stage 4 tarball for release, be sure to create the tarball from within the chroot! This will guarantee accuracy of the owner and group attributes in the file permissions. If the tarball is compressed from the host system, user and group IDs will be pulled from the host's /etc/passwd and /etc/group and files. This will probably break permissions on various files, especially none-default system daemons, because the host's user and group IDs numbers will probably not line up with the ones in the chroot.

The following commands are designed to be ran from the base of the rootfs (/).

Long options

.gz long:

root #tar --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --create --preserve-permissions --gzip --xattrs --acls --verbose --file tarball.tar.gz --directory / .

.bz2 long:

root #tar --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --create --preserve-permissions --bzip2 --xattrs --acls --verbose --file tarball.tar.bz2 --directory / .

.xz long:

root #tar --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --exclude='./boot/efi/*' --create --preserve-permissions --xz --xattrs --acls --verbose --file tarball.tar.xz --directory / .
Short options

.gz short:

root #tar czvf tarball.tar.gz --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --preserve-permissions --xattrs --acls --directory / .

.bz2 short:

root #tar cjvf tarball.tar.bz2 --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --preserve-permissions --xattrs --acls --directory / .

.xz short:

root #tar cJvf tarball.tar.xz --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --exclude='./boot/efi/*' --preserve-permissions --xattrs --acls --directory / .

Do not keep Portage, or virtual filesystems

The following commands are designed to be ran from the base of the rootfs (/).

Long options

.gz long:

root #tar --exclude='./*tarball*' --exclude='./usr/portage/distfiles/*' --exclude='./usr/portage/packages/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --create --preserve-permissions --gzip --xattrs --acls --verbose --file tarball.tar.gz --directory / .

.bz2 long:

root #tar --exclude='./*tarball*' --exclude='./usr/portage/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --create --preserve-permissions --bzip2 --xattrs --acls --verbose --file tarball.tar.bz2 --directory / .

.xz long:

root #tar --exclude='./*tarball*' --exclude='./usr/portage/*' --exclude='./proc/*' --exclude='./sys/*' --exclude='./dev/*' --exclude='./run/*' --exclude='./media/*' --exclude='./tmp/*' --exclude='./boot/efi/*' --create --preserve-permissions --xz --xattrs --acls --verbose --file tarball.tar.xz --directory / .

Tarball decompressor

The following commands are designed to be ran from any location, provided the /output/location section is modified to the location the tarball is to extract.

Short options

.gz short:

root #tar xvzpf stage3-*.tar.bz2 --xattrs --acls -C /output/location

.bz2 short:

root #tar xvjpf stage3-*.tar.bz2 --xattrs --acls -C /output/location

.xz short:

root #tar xvJpf stage3-*.tar.xz --xattrs --acls -C /output/location

Long options

.gz long:

root #tar --extract --gzip --preserve-permissions --xattrs --acls --verbose --file stage3-*.tar.gz --directory /output/location

.bz2 long:

root #tar --extract --bzip2 --preserve-permissions --xattrs --acls --verbose --file stage3-*.tar.bz2 --directory /output/location

.xz long:

root #tar --extract --xz --preserve-permissions --xattrs --acls --verbose --file stage3-*.tar.xz --directory /output/location

Tarball file count

root #tar --list --file tarball.tar.* | wc --lines

Squashfs compressor

root #mksquashfs /input/location /output/location/archive.squashfs -b 1M -comp xz -Xdict-size 100%

Squashfs decompressor

root #unsquashfs -x -p 9 -d /output/location archive.squashfs

List installed package licenses

eix must be installed:

root #NAMEVERSION="<category>/<name>-<version>" eix -I --format 'Package: <installedversions:NAMEVERSION>,License: <licenses>,Homepage: <homepage>\n' >> /tmp/tarball_packages_`date +\%Y.\%m.\%d`.csv

Mounting Active Directory network shares

user $mount.cifs //SERVER/SHARE /tmp/SHARE -o user=$(whoami),dom=DOMAIN

Chroot build failures

FILE /build/failure
Traceback (most recent call last):
  File "/usr/lib/portage/python3.4/", line 282, in <module>
  File "/usr/lib/portage/python3.4/", line 279, in ebuild_ipc_main
    return ebuild_ipc.communicate(args)
  File "/usr/lib/portage/python3.4/", line 139, in communicate
    return self._communicate(args)
  File "/usr/lib/portage/python3.4/", line 245, in _communicate
    if not self._daemon_is_alive():
  File "/usr/lib/portage/python3.4/", line 124, in _daemon_is_alive
    wantnewlockfile=True, flags=os.O_NONBLOCK)
  File "/usr/lib64/python3.4/site-packages/portage/", line 113, in lockfile
    raise PermissionDenied(func_call)
portage.exception.PermissionDenied: open('/var/tmp/portage/app-shells/.bash-4.3_p42-r1.portage_lockfile')

In order to correct the error above, disable userpriv and usersandbox from the FEATURES variable:

FILE /etc/portage/make.conf
FEATURES="-userpriv -usersandbox"