2012-09-25

Ubuntu preseed.cfg with encrypted lvm

I wanted to automate my laptop setup with preseeding and ran into a wall when trying to automate an encrypted setup (which is the only sane option for a laptop).
I took a look at the Ubuntu guide, the preseed example file and the partman auto recipe document - all of them mentioned the possibility to create an encrypted setup but none said how. So I google'd and I google'd but couldn't find anybody who had actually done it and shared their results.
I did however find some failed attempts and many dead forum posts where the question had been raised but not answered. One person said that the setup should be really close to an LVM setup but hadn't posted a working example with the encryption part. (Sorry, I don't have a link to this post) So I took most of the example and modified it to fit my needs and started experimenting. 16 hours and about a hundred tries on my test VM later, I seem to have figured out how to do it. Here is the relevant part of the preseed.cfg file:
#Partitioning
d-i partman-auto/disk string /dev/sda
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/method string crypto
d-i partman-auto/choose_recipe select boot-crypto
d-i partman-auto-lvm/new_vg_name string crypt
d-i partman-auto/expert_recipe string boot-crypto :: \
        250 35 250 ext4 $primary{ } $bootable{ } \
        method{ format } format{ } \
        use_filesystem{ } filesystem{ ext4 } \
        mountpoint{ /boot } \
        .\
        3072 75 3072 ext4 $lvmok{ } lv_name{ root } \
        in_vg { crypt } method{ format } format{ } \
        use_filesystem{ } filesystem{ ext4 } mountpoint{ / } \
        .\
        100% 75 100% linux-swap $lvmok{ } lv_name{ swap } \
        in_vg { crypt } method{ swap } format{ } \
        .\
        2048 50 3072 ext4 $lvmok{ } lv_name{ usr } \
        in_vg { crypt } method{ format } format{ } \
        use_filesystem{ } filesystem{ ext4 } mountpoint{ /usr } \
        .\
        512 50 1024 ext4 $lvmok{ } lv_name{ tmp } \
        in_vg { crypt } method{ format } format{ } \
        use_filesystem{ } filesystem{ ext4 } mountpoint{ /tmp } \
        .\
        256 25 1000000 ext4 $lvmok{ } lv_name{ home } \
        in_vg { crypt } method{ format } format{ } \
        use_filesystem{ } filesystem{ ext4 } mountpoint{ /home } \
        .\
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
This gets the encryption working and is a decent bootable setup. I did run into some trouble when trying to use btrfs and ext2 filesystems for some partitions, but it didn't bother me so much as to try to fix it.
I hope this will help somebody in the future or myself, should my PXE machine die.

2012-05-10

Windows 7 login keyboard layout

I have to use Windows for some work related stuff (no worries, just a VM) and I got really annoyed with having to type in my password using the US keyboard.
So I did a Google search and found-out how to change it.
Just in case I forget - or somebody else finds this on their search:
Edit the RegKey HKEY_USERS\.DEFAULT\Keyboard Layout\Preload and change it to the appropriate value.
The original post that gave me this info didn't contain all the keycodes.
So the keycode for Estonian layout is 00000438, which I found here.

2012-04-29

Display modes for xrandr

I got a new laptop and decided to use ScrotWM as my desktop. The details of the setup will be covered in a later post. For now I would like to share with the world a quick and dirty script I made to cycle trough the different display modes one would expect to achieve when pressing the XF86Display button.
I had some trouble getting ScrotWM to recognise some other XF86* key-bindings (mainly the volume buttons), so I installed xbindkeys and configured the shortcuts with that.
So I set-up a script in /usr/local/bin/ and called it "xrandr-cycle".
There are four display modes I could imagine would be of use to me:
  1. Show all external displays as being right-of the laptop's own display.
  2. Show the same picture on all displays.
  3. Show output on the external displays only (useful for docking).
  4. Turn all external displays off and only use the internal display.
Havig considered these modes, here is what I came up with:
#!/bin/bash
#--help
if [ "$1" == "--help" ];then
        echo "`basename $0` - this script is intended to cycle trough all the different display modes"
        exit 0
fi
#basenline variables
_internaldisplay="LVDS1"
_internaloffset="`xrandr -q | grep -A 1 $_internaldisplay | tail -1 | tr -s ' ' | cut -d' ' -f2 | cut -d'x' -f1`"
#working part
xrandr -q | cut -d' ' -f1-3 | grep -v "Screen" | while read _monitor _status _mode;do
        if [ -z "$_monitor" ] || [ "$_status" == "disconnected" ];then
                continue
        fi
        if [ "$_monitor" == "$_internaldisplay" ] && [ "$_mode" == "(normal" ]; then
                _internal=false
        elif [ "$_monitor" == "$_internaldisplay" ] && [ "$_mode" != "(normal" ]; then
                _internal=true
        else
                if $_internal && [ "$_mode" == "(normal" ];then
                        echo "Internal online and $_monitor offline - showing right-of"
                        xrandr --output $_monitor --right-of $_internaldisplay --auto
                        nitrogen --restore
                elif $_internal && [ "`echo $_mode | cut -d'+' -f2`" == "$_internaloffset" ];then
                        echo "Internal online and $_monitor right-of - showing same"
                        xrandr --output $_internaldisplay --off
                        xrandr --auto
                        nitrogen --restore
                elif $_internal && [ "`echo $_mode | cut -d'+' -f2`" == "0" ];then
                        echo "Internal online and $_monitor online - turning internal off"
                        xrandr --output $_internaldisplay --off
                        nitrogen --restore
                elif ! $_internal;then
                        echo "Internal offline - turning internal on and evrything else off"
                        xrandr --output $_internaldisplay --auto
                        xrandr --output $_monitor --off
                        nitrogen --restore
                fi

        fi
done
If you would like to use it for your own systems, be sure to change the _internaldisplay variable to the correct value.

P.S. Yes I know one could get the _internaldisplayoffset using some sed or awk magic, but I have yet to master those and this also works.
P.P.S. After having seen a video about licensing I should add one to this script as well. Since this is stupid/simple Bash, BSD should be more than enough.