Avoiding invalid commands in Bash history

To avoid having stuff like “ls” in my bash history, I added “ls” to the “HISTIGNORE” environment variable.
However, on late nights, the result of this, and my left and right hand becoming out of sync with each other when I’m tired, is that the entry “sl”, which really doesn’t do anything, started showing up in my history instead.

Now, invalid commands in bash result in two things. A message saying “command not found” and exit status 127.
The latter is interesting. We can use that to avoid having the invalid entries showing up in the history.

If you don’t already have a bash PROMPT_COMMAND set, add this to .bashrc:

PROMPT_COMMAND="mypromptcommand"
function mypromptcommand {
}

That function will now be run every time your prompt is about to appear. Within it, we put this:

local exit_status=$?
# If the exit status was 127, the command was not found. Let's remove it from history

local number=$(history 1)
number=${number%% *}
if [ -n "$number" ]; then
    if [ $exit_status -eq 127 ] && ([ -z $HISTLASTENTRY ] || [ $HISTLASTENTRY -lt $number ]); then
        history -d $number
    else
        HISTLASTENTRY=$number
    fi
fi

What this does:

  • Gets the exit status of the last command
  • Gets the number for the latest entry in your history, if there is one
  • Checks if that entry was set to anything
  • If it was, we check if the exit status and see if it’s 127. We also require the HISTLASTENTRY variable to be either unset or less than the number of the latest history entry. This means the command in question was either added to the history, increasing the number of entries already there, or it was the first command ever (empty history)
  • If the above is true, we delete the last history entry
  • Otherwise, we set the last history entry to the amount of entries we found, so we can check if it is increased next time.

Windows 7 autologin

Short and simple: to allow automatically logging in on Windows 7, you need to press Win+R to open the run menu, then type “control userpasswords2” without the quotes and run it.

XFCE: Unable to perform shutdown

To fix this issue, add the following lines between <config …..> and </config> in /etc/PolicyKit/PolicyKit.conf

<match action="org.freedesktop.hal.storage.mount-removable">
<return result="yes"/>
</match>
<match action="org.freedesktop.hal.power-management.shutdown">
<return result="yes"/>
</match>
<match action="org.freedesktop.hal.power-management.reboot">
<return result="yes"/>
</match>

Resizing an ext3 filesystem on an lvm

Just a short reference:

Keep in mind: There is no longer a need to convert the filesystem to ext2 to resize it. resize2fs can handle ext3 just fine.

First, unmount the filesystem in question, unless you have and trust online resizing (I can’t subscribe to either). Then check it for errors like this:

# e2fsck -f /dev/VolGroup00/LogVol00

(replace the path with that of the device you just unmounted)

Get an overview of what you currently have allocated

# lvs

Save said overview

# lvs > maybe_useful.txt

Resize a given filesystem to 50 gigs (read man lvreduce/lvextend to learn how to resize using relative sizes, like reducing the filesystem by 5 gigs)

# resize2fs -p /dev/VolGroup00/LogVol00 50G

Run a new check on the filesystem, just to be sure it worked well

# e2fsck -f /dev/VolGroup00/LogVol00

Resize the lvm partition to fit the filesystem (I usually go a bit higher than the filesystem, just to make sure there’s room for it, then resize the filesystem again to fill the partition afterwards)

# lvreduce -L55G /dev/VolGroup00/LogVol00

Expand the other partition to use the space you just freed up (this will use all available space. again, read manual for how to use only part of the space if you want to extend more lvm’s to fit the space you just made available)

# lvextend /dev/VolGroup00/LogVol01 /dev/md1

(md1 is the physical device I wish to fill)

Extend both filesystems to fit their partitions and check them both afterwards

# e2fsck -f /dev/VolGroup00/LogVol01
# resize2fs -p /dev/VolGroup00/LogVol00
# resize2fs -p /dev/VolGroup00/LogVol01
# e2fsck -f /dev/VolGroup00/LogVol00
# e2fsck -f /dev/VolGroup00/LogVol01

Re-mount your filesystems, and hopefully everything went well 🙂

Moving or removing ~/.xsession-errors

The location of ~/.xsession-errors is, at the time of writing, specified in /etc/X11/Xsession in Debian Squeeze (testing).

ERRFILE=$HOME/.xsession-errors

To disable logging, change this to

ERRFILE=/dev/null

I use several computers with the same, networked, home directory, and having all of them output to the same file was a hopeless mess. However, this solved the problem for me:

ERRFILE=$HOME/.xsession-errors-$(hostname -s)
rm -f "$ERRFILE"

This makes a separate .xsession-errors for each of my machines and also removes the file each time X is started so it doesn’t grow too big. If X itself crashes, the errors to to /var/log/messages anyway, so there’s no need to save the .xsession-errors for this.

Wake on Lan on an Intel Mac Mini with Linux

Both Wake on Lan and turning on after a power loss can be enabled on the Intel Mac Mini with the use of the setpci command. I stuffed this into my /etc/rc.local (to run them at boot time. yes, they need to be run on each boot)

# reboot on power loss
setpci -s 0:1f.0 0xa4.b=0

# wake on lan
setpci -d 8086:27b9 0xa4.b=0
ethtool -s eth1 wol g

Don’t ask me what they mean. I have no idea.

Note: this requires ethtool to be installed

EDIT: For some newer mac mini’s, here are two other setpci commands for the WOL part, if the one above doesn’t work:

setpci -s 00:03.0 0xa4.b=0
setpci -s 00:03.0 0x7b.b=19

Try one at a time, see which works.

Improve the mouse response and acceleration in Debian

Note: This information is now slightly outdated, as the patch is already in Debian Squeeze.

One of my biggest issues with moving from Windows to Linux as my desktop is how the mouse is handled in xorg. In xorg, the mouse had (and still has, in Debian Lenny) two settings for mouse speed: acceleration and threshold. Simply put, what these do is that whenever the mouse is moving faster than the threshold, it gets accelerated by the acceleration value, and otherwise it’s unaccelerated.

While this may work for some people, I found it horribly annoying. Before anyone jumps up and bites me, I do know that I can set the threshold to 0 and get some sort of normal acceleration, but I hate that one too. Luckily, Simon Thum has come up with a solution.

His solution has been patched into the newer versions of xorg. The bug is available at freedesktop.org, and the latest patch is here. However, while this patch goes into version 1.4 or xorg without a hitch, the Debian (and probably Ubuntu) build system requires an additional change in the Makefile.in file.

In short, here’s a howto for Debian Lenny on getting the new and much improved mouse handling in:

Become root

su (type root password)

First, make sure you’ve installed everything required to build stuff in the first place:

apt-get update && apt-get install build-essential fakeroot

Become a normal user again

exit

Make a temporary directory, cd to it, then get the sources for xserver-xorg-core

mkdir mousepatch
cd mousepatch
apt-get source xserver-xorg-core

apt-get will download the sources, extract them and apply the Debian-specific patches.
Next, we need all the build-dependencies. These are the libraries used in xserver-xorg-core, which we’ll need to compile the source code.

apt-get build-dep xserver-xorg-core

Now make a backup of the source code (This is not strictly necessary, but I like to do so anyway before messing around with it. Saves doing another apt-get download.)

cp -r xorg-server-1.4.2 xorg-server-1.4.2-orig

Now we want to apply the mouse patch. You can either use Simon’s original patch from the link above, or you can download a Debian-specific version of it here. This is the exact same patch as the one above, except it only fits into this specific source code, and it contains the changes to the Makefile.in file, which allows dpkg-buildpackage to successfully compile it.

LINK TO DEBIAN-SPECIFIC PATCH

cd xorg-server-1.4.2
patch -p1 <path to mouse patch>

Now build the package.

dpkg-buildpackage -rfakeroot

This will generate quite a few .deb files in the parent directory, and will probably also exit with a return value of 1, complaining it is unable to find Julien Christau’s private key. You are most likely not Julien Christau, so you will not be able to sign the package as him. This is irrelevant, as we’re not going to distribute this package anyway.

The only .deb you have actually changed is the one named xserver-xorg-core_1.4.2-10_i386.deb, so this is the one we want to install:

cd ..
su   (root password again)
dpkg -i xserver-xorg-core_1.4.2-10_i386.deb

Now, assuming all went well, you may restart X or reboot, whatever your preference, and enjoy your newfound mouse bliss. I actually prefer my new Linux mouse-setup to my dualbooted WinXP IntelliMouse drivers now.

To learn how to set up your new mouse acceleration, please refer to the developer documentation. For the record, these are the relevant lines from xorg.conf for my current setup:

Section "InputDevice"
    Identifier	"Configured Mouse"
    Driver		"mouse"
    Option		"Device"	"/dev/input/mice"
    Option		"Protocol"	"auto"
    Option		"Buttons"	"7"
    Option		"ButtonMapping"	"1 2 3 6 7"
    Option		"ZAxisMapping"	"4 5"
    Option		"AccelerationProfile" "1"
    Option		"VelocityScale" "10"
EndSection

Only the AccelerationProfile and VelocityScale is specific to this patch. Also, I use “xset m 18/10 0” to set my mouse speed.