HOWTO: Using a GNU screen inside another GNU screen

I found this feature this morning, and the irc folks being enthusiastic, I decided to blag about it.

I use GNU screen on a daily basis for almost three years. Not only able to provide an almost complete terminal emulation, with detach and reattach abilities, extensive keyboard bindings, tabs (a.k.a windows) management, Gnu screen is a versatile tool, although surprisingly more than often ignored by a lot of people.

There is a little reminder of what GNU screen consists in.

Requirements, or at least highly recommended setup

It is highly recommended to have a screenrc that provides a hardstatus line, the only practical way to distinguish one screen from another.

Here is mine:

shell /bin/zsh
attrcolor b ".I"
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm' 
defbce "on"
term screen-256color
maptimeout 5
termcapinfo xterm* ti@:te@
hardstatus alwayslastline
hardstatus string '%{= kG}[%{G}%H%{g} %l][%=%{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u
)%?%{r})%{w}%?%+Lw%?%?%=%{g}][%{B}%d/%m %{W}%c%{g}]'
defnonblock 5

Of course, you should adapt this to fit your needs, only the lines beginning with hardstatus are relevant there.

Now that your GNU screen is properly configured, I can highlight the bullet point of this howto :)

Pictures are said to help comprehension. I say LIES!

My X terminal emulator is Terminator. Combined with GNU screen, you can obtain … that…thing:

'what. the. heck?'

(click on the thumbnail to maximize)

Please bear with me, even if this caused some eye-melting :P

Some explanations: each zone delimited by light grey borders are in fact splits in Terminator. Gnu screen sessions are contained in the numbered splits.

1: err… not really a screen, I guess I’m tired and too lazy to correct the picture.

2: lwatch on dresda

3: lwatch on pandora

4: htop on dresda

5: htop on pandora

6: irssi on dresda

The only thing I had to do is to execute screen -e ^ee on pandora, therefore rebinding all default ^a (Ctrl+a) bindings to ^e (Ctrl+e).

Then you control the main screen with the default binding, while controlling the inner screen with these new binds (i.e ^ac to create a new window in the main screen, ^ec to do the same in the inner screen :)

Finally, the relevant part in man screen:

       -e xy
            specifies the command character to be x and the character generat‐
            ing a literal command character to y (when typed after the command
            character).  The default is "C-a" and `a', which can be  specified
            as  "-e^Aa".  When creating a screen session, this option sets the
            default command character. In a multiuser session all users  added
            will  start off with this command character. But when attaching to
            an already running session, this option changes only  the  command
            character  of  the  attaching  user.  This option is equivalent to
            either the commands "defescape" or "escape" respectively.

I really enjoy it, it’s like a Christmas present for all BOFHs, blinking like a Christmas Tree when some jerk is trying to mess with your servers/networks :D

Lenovo Thinkpad X300 , battery management and tp_smapi

This article briefly presents a « solution » to the battery switching issue mentioned at the bottom of this blog entry.

Indeed, while having two batteries ensures a (very) long battery life, you have to switch manually from the secondary battery to the primary, in order to prevent deep discharges that harms lithium batteries.

Until now, I used to manage it by typing some aliases in my shell, but it is clearly not convenient nor reliable (Sometimes, I simply forget it).

But now, one little bash script and one line in your root’s crontable, and the job’s done :)

There is battery_switch.sh (deprecated, see baswim.sh below). It basically allows automatic switching to main battery when a given percentage level is reached by the secondary battery. Some parameters are available by editing variables values directly in the script, but only LOW_LEVEL is really interesting.

The cron line I use:

# sudo crontab -e

# m h  dom mon dow   command
*/5 *   *   *   *   /opt/bin/battery_switch.sh

Of course it is aware of your laptop’s status. In fact, if it switched to the main battery (by forcing the main battery to discharge), and then you re-plug your laptop, it will cease to force discharge and will return to normal state.

Since I may not be clear, do a

tail -F /var/log/battery_switch.log

to get relevant information.

I hope that will help :)

EDIT: please find there baswim (battery switching improved) a new script that provides a *really* interesting feature: It permits to switch from one battery to another according to both remaining percentage and cycles count.

It follows this simple scheme:

1) Get batteries cycle status 
2) Decide which battery should discharge first based on cycles and remaining percentage
(above the low_level) 
3) Get AC status
3) If AC is unplugged, begin discharge with chosen battery
4) If needed by long unplugged operation, switch to the other battery

It basically a hack from the first script, there are some oddities (one line functions…) but hey it works (surprisingly)!

EDIT: A little but likely important update. This time, both scripts can handle two cases: one battery plus dvd-drive or both batteries.

EDIT:

If you want baswim to be a little more aware of acpi events,

you can make some symbolic links in /etc/acpi/battery.d and /etc/acpi/ac.d.

ln -s /opt/bin/baswim.sh /etc/acpi/battery.d/
ln -s /opt/bin/baswim.sh /etc/acpi/ac.d/

Then, baswim will be executed every time you plug or unplug your AC.

Lenovo Thinkpad X300 and Linux – Configuration and power management

I recently replaced my Macbook with a Lenovo Thinkpad X300 notebook. Here are some notes and tips concerning GNU/Linux installation and configuration for an optimal day to day use.

UPDATE (2011/07)

Here are my current settings for power management :

/etc/rc.local: Simplified, no more vm.laptop_mode (not useful with a ssd).

# Audio powersave
echo 1 > /sys/module/snd_hda_intel/parameters/power_save

# For thinkfan
echo watchdog 0 >| /proc/acpi/ibm/fan
chmod 666 /proc/acpi/ibm/fan

# For PHC Voltage regulation kernel patch http://www.linux-phc.org
echo 15 15 15 >| /sys/devices/system/cpu/cpu0/cpufreq/phc_vids
echo 15 15 15 >| /sys/devices/system/cpu/cpu1/cpufreq/phc_vids

/etc/thinkfan.conf: Simplified, less fan, more silence :)

(0,     0,      40)
(1,     35,     58)
(2,     56,     64)
(5,     58,     128)

Do not forget to follow powertop indications.

First, the specs of this new toy:

* Intel® Core 2 Duo SL7100

* 2x1Go PC2-5300 DDR

* 64GB SSD

* 1.61 Kg with 6 cells main battery and Ultrabay 3 cells secondary battery

Hardware

Then, lspci output:

00:00.0 Host bridge: Intel Corporation Mobile PM965/GM965/GL960 Memory Controller Hub (rev 0c)
00:02.0 VGA compatible controller: Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)
00:02.1 Display controller: Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)
00:19.0 Ethernet controller: Intel Corporation 82566MM Gigabit Network Connection (rev 03)
00:1a.0 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #4 (rev 03)
00:1a.1 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #5 (rev 03)
00:1a.7 USB Controller: Intel Corporation 82801H (ICH8 Family) USB2 EHCI Controller #2 (rev 03)
00:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03)
00:1c.0 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 1 (rev 03)
00:1c.1 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 2 (rev 03)
00:1c.2 PCI bridge: Intel Corporation 82801H (ICH8 Family) PCI Express Port 3 (rev 03)
00:1d.0 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #1 (rev 03)
00:1d.1 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #2 (rev 03)
00:1d.2 USB Controller: Intel Corporation 82801H (ICH8 Family) USB UHCI Controller #3 (rev 03)
00:1d.7 USB Controller: Intel Corporation 82801H (ICH8 Family) USB2 EHCI Controller #1 (rev 03)
00:1e.0 PCI bridge: Intel Corporation 82801 Mobile PCI Bridge (rev f3)
00:1f.0 ISA bridge: Intel Corporation 82801HBM (ICH8M-E) LPC Interface Controller (rev 03)
00:1f.1 IDE interface: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) IDE Controller (rev 03)
00:1f.2 SATA controller: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) SATA AHCI Controller (rev 03)
00:1f.3 SMBus: Intel Corporation 82801H (ICH8 Family) SMBus Controller (rev 03)
03:00.0 Network controller: Intel Corporation PRO/Wireless 4965 AG or AGN [Kedron] Network Connection (rev 61)

About lsusb, just one useful line:

Bus 003 Device 002: ID 0483:2016 SGS Thomson Microelectronics Fingerprint Reader

Important note: since I disabled Bluetooth, WiMAX and the touchpad in the BIOS,

they are not present in lspci and lsusb outputs, but both Bluetooth and touchpad are functional with Ubuntu.

Quite a modern hardware, and thus some issues concerning Linux support. Indeed, only the 2.6.28 offers a good support, previous versions are either bugged or hackish.

Operating System

I chose Ubuntu 9.04 beta for an easy yet fine setup, with LVM+Crypto for the root partition. However a bug in xserver-xorg-evdev in 64 bits edition breaks the must-have trackpoint scrolling feature. Thus, I decided to run 32-bit version, despite a 64 bits CPU.

Most of the hardware components work out the box, without particular setup, including the webcam. The only regression compared to previous kernel versions is the integrated microphone, which remains disabled. I hope to fix it out soon. However, the jack input line is fully functional. Update: just run alsamixer, type tab to enter in the Capture section and enable the internal microphone. Speakers offer a good (and loud) sound experience, quite surprising from a Thinkpad notebook.

No particular thoughts on Ubuntu itself, except that Jaunty Jackalope will certainly bring a better user experience than ever (new themes, new notification system, general speed improvements, boot time dramatically reduced…).

Power management

Now the real deal: how to maximize battery operation and lifetime with the Lenovo Thinkpad X300?

The average base power consumption is 14 W, WiFi on, 75% backlight, some terminals and Firefox.

First, disable everything you will never use in the BIOS. Currently, I disabled Bluetooth, WiMAX, Wireless WAN (3G), »USB Wifi » (wtf?) and touchpad. Saving: 2 W

Then disable Wake On Lan, unless you need it. It keeps the Gigabit Ethernet Controller to remain in an active state that actually use energy. Saving: 1W

Moreover, I blacklisted the e1000e module, since I mainly use WiFi. When I need wired connection, I simply modprobe it.

Then concerning the wlan0 WiFi interface, just run:

iwconfig wlan0 power on

in order to active Power Management. Saving: 0.5W

Some sysctl tweaking in /etc/sysctl.conf:

vm.laptop_mode = 5  # Activating laptop mode, power friendly I/Os
vm.dirty_writeback_centisecs = 2000
kernel.nmi_watchdog = 0
vm.swappiness = 5

And in /etc/rc.local:

echo 10 > /sys/module/snd_hda_intel/parameters/power_save
echo min_power > /sys/class/scsi_host/host0/link_power_management_policy
echo min_power > /sys/class/scsi_host/host1/link_power_management_policy

Saving: 2W

Quite simple, heh? :) Now, my average consumption is 8.8W, allowing a real use of the X300 for more than 6 hours, instead of 4h30.

Last thoughts

The internal fan management is kinda broken in Linux. The workaround is to append thinkpad_acpi to /etc/modules and options thinkpad_acpi fan_control=1 to /etc/modprobe.d/fan for instance.

Then you could use thinkfan and this sample thinkfan.conf:

# thinkfan example config file
# ============================
#
# Syntax:
# (LEVEL, LOW, HIGH)
#
# LEVEL is the fan level that's written to /proc/acpi/ibm/fan
# LOW is the temperature at which to step down to the previous level
# HIGH is the temperature at which to step up to the next level
# All numbers are integers.
#
# You can put spaces anywhere you want. Anything that doesn't match this
# syntax is ignored.
#
# The number of tuples is limited by the size of your system memory.
#
# WARNING: Sanity checks are only made for correct ordering of the fan levels
# and for (LOW < HIGH) within one tuple. You can still use any kind of insane
# temperature limits.

(0,     0,      50)
(1,     48,     58)
(2,     54,     61)
(3,     56,     63)
(4,     58,     65)
(5,     60,     66)
(7,     63,     128)

Concerning lithium batteries, you should keep them charged between 40% and 80%. It is quite easy to do with Thinkpads: just append tp_smapi to /etc/modules, and the upper charge limit will be set to 86%, which is a good compromise. Moreover, instead of completely drain the Ultrabay battery before switching to the main battery, you can manually force discharge; as root type:

echo 1 > /sys/devices/platform/smapi/BAT0/force_discharge

But do not forget that even if you replug your laptop, discharge will resume anyway!

Thus, you have to bypass forced discharge; as root type:

echo 0 > /sys/devices/platform/smapi/BAT0/force_discharge

Annoying workaround, but I will try to find or code a proper solution.

UPDATE: See my Baswim script here.

That’s all folks!

XMMS2 – Garbage Collector

Dave dans ses bonnes œuvres a développé un petit script bien sympa qui vient combler une lacune actuelle de xmms2, à savoir le retrait de la Media List (mlib) des pistes supprimées ou modifiées dans le système de fichier.

En plus simple, après un rehash de la media lib, avec un simple xmms2 mlib rehash, un fichier renommé ou supprimé ne sera pas mis à jour ou retiré dans la mlib.

En attendant l’implémentation propre de la fonctionnalité, mlib-gc.py permet de corriger le tir. C’est un simple script python, à installer dans le chemin de votre choix (pour ma part /opt/bin/xmms2-mlib-gc). Seule dépendance à installer avec le gestionnaire de paquets de votre distribution GNU/Linux : python-xmmsclient.

Le fonctionnement est simple : après modification de la bibliothèque musicale, exécuter mlib-gc.py (chez moi xmms2-mlib-gc.py), exécuter xmms2 mlib addpath /chemin/mediathèque/.

J’espère que cette astuce se montrera utile pour certains, je continuerai régulièrement la rédaction de billets concernant XMMS2.

Mac OS X and LaTeX

Since I use Apple Mac OS X on my Macbook laptop, I often have to deal with its weaknesses. Someone may rather say ‘differences’, but since today I lost almost one hour for eventually finding a simple workaround, I would rather stick with « weaknesses »…

I used to believe that OS X fully supports UTF-8. In fact, it’s a slightly different version from Apple, and almost invisible for an everyday use. Even for using LaTeX with french accentuated characters, since pdflatex can handle them. But if you usually declare \usepackage[utf8]{inputenc}, which is the classic charset declaration in a LaTeX document, this will success if working with a single file, but will fail if using file inclusion! Thus, it is not the classic error claiming a missing latex-ucs package.

Indeed, even if I had already written some documents with LaTeX on OS X, I never used the \input command until today. This allows to fragment your LaTeX document in several files for a more convenient writing.

Except that pdflatex will fail with this error message :

! Package inputenc Error: Unicode char \u8: not set up for use with LaTeX

And the faulty line was always the first line of the .tex included, despite any correction attempt.

After a long search, I have found that you have to explicitly use Apple’s UTF-8 with \usepackage[utf8x]{inputenc}.

And voilà, pdflatex does not complain anymore…