User:Bastien/Some-custom-gentoo-scripts

This page simply presents some simple scripts I use to manage my gentoo machines, that might be interesting for other users. It is recommended to update a gentoo system between daily or weekly, which, on the long run, implies writing the same commands regularly.

syncing the gentoo repo automatically
It is simple to create a cronjob for the user root like this @weekly emaint sync --allrepos to "automate the boring stuff"(ref.).

For machines that are not up all the time, chances are that the weekly update will fall at a time the machine is down. Anacron could be useful to keep track of the real interval between each comman.

Yet, when a machine is not up all the time and doesn't have access to internet all the time (like a laptop), this setup can be quite unreliable, as cron or anacron normally don't care for the output of the command. If a command fails because precisely at this moment the machine is offline, cron won't retry until the next week.

Then again, it is a common gentoo netiquette not to synchronize too often "just in case". Here is a simple workaround with a script that can run much more often (daily, hourly), but only synchronizes the repository if it is older than X seconds:

This script reads the timestamp of local repo metadata folder: when using git instead of the default rsync, replace this value with. It compares this reference with the actual time, and only synchronizes the repository with upstreams source if the difference exceeds 259200 seconds (72h - do the math and adapt as needed). If the command fails, the reference file won't change, so the script will try to  again every time after this point. If the command succeeds, the timestamp of the reference file will change, and for the next 72 hours, the script will simply do nothing and exit.

The output of the command is redirected to a log file (a synchronization might fail for other reasons).

To print the variable diff in a human friendly format (in hours), adding this short snippet of code to the script should work:. A second option for the same script could be:

When run as root, and only if the repo wasn't updated in a while, the scripts synchronizes the repo. When used as a normal user, the output would be something like:  It could be useful to check if everything works as expected.

notification when the repository has been updated
Some package managers in linux offer a system tray or a notification utility when updates are available. When managing several machines and with automatic synchronisations happening in the background, some kind of notification could be useful.

Cron doesn't know anything about the display and it cannot easily be tricked to send desktop notifications. Instead, a small utility script can be started in a (a desktop environment or a display manager). The principle behind the following scripts is also to use unix timestamps. The easier and more useful way is to compare the timestamp of the gentoo repo with the date of the last emerge command. A simple way to know when the last emerge command was run is to use portage built-in an make it simply touch a file after each install:

The file in question is obviously empty, but it is enough for this purpose. To really have a date written to this file, an option could be:.

Then, a simple script could be:

This script should be started within a graphical environment: it reads and compares the timestamps of the two reference files and sends a notification if the gentoo repo is more recent than the last emerge command, then sleeps for two hours; and starts again as long as it is running.

Simplifying the gentoo update routine
Portage is a very powerful package-managers with a lot of commands and options. For routinal updates, it implies to enter the same complex commands all the time. Why not wrap all those commands in a script ? The following examples uses to create interactive menus.

This is an example use-case where:
 * is not mounted by default: dist-kernels,, , and others require it to be mounted.
 * Portage's build directory is mounted on (see: Portage_TMPDIR_on_tmpfs): some big packages like Mozilla Firefox require it to be resized.
 * There is a utility to make root snapshots (with BRTFS, rsync, timeshifter, etc.)
 * Sometimes, the user wants to start an update at the end of a session and let the computer automatically shutdown when it's complete.

The first part of the update script shows which packages will be updated:

At this point, the script pauses: it is possible to exit with n or continue with y. It is possible to edit some use flags or keywords in another terminal. Any other input on the current terminal restarts the loop, which is useful to check if we have the expected result after a change in the config.

Then, a menu offers a comfortable way to select the right options depending on the packages that will be updated:

The option "gentoo_postupdate" is on by default, and exec into a simple script after the main update:

Now, the command for routinal updates is as simple as:

Bonus: syncing gentoo repo between local machines
Since I have several gentoo machines, it would be a waste of bandwidth for everyone to let them sync the repo against the same gentoo mirror all in turn. Instead, one of the machine can be defined as a server -- the server would sync the repo against upstream mirrors -- then let the other machines on the same network sync against the local server. When using the default rsync method, a rsync daemon has to run on the server. The default config on gentoo is all prepared for this usecase:

When using git, it is even easier. For example, for the repo guru, simply change the address "sync-uri" on the config for git to pull updates from a local machine over ssh:

The change can be reverted at any time.