Saturday, February 25, 2012

Making git commands clear screen when they exit

One thing I don’t like about git is that commands like git diff don’t clear screen when they exit.  If I view a file using less, e.g. by running less ~/.zshrc, less would clear the file content from the screen when it exits.  But git diff, git log, etc. don’t.  With some Googling around, I found that:
  • git uses the default pager the user has configured.  On Ubuntu machines it’s less when $PAGER is not defined.
  • You can configure less by defining $LESS variable with the flags you want to pass by default.
  • Nigel McNie has posted a set of flags that are a good default flags for less.
  • -X option to less tells it to not clear the screen when it exits.  From a related question on Super User I found that passing the flag -+X would force less to always clear the screen when it exits.
Combining all these together, I have added this to my ~/.zshrc, and everything is just the way I want.
export LESS="-+X -FR"
While adding this line made git work fine, it resulted in less behaving erratically.  Now I have the following config in my ~/.gitconfig, and things seem to be good.
[core]
  pager = less -+X -+F

Wednesday, January 18, 2012

Inserting single quote character inside a single-quoted string

“Strings” (not the same as strings in languages like C or Python) in shells (bash and zsh especially) can be enclosed within either single quotes (') or double quotes (").  Double quotes escape (i.e. remove special meaning from) characters like space and single quotes, but single quotes remove meaning from almost every special character including spaces, $, !, new lines, etc.  (Learn more)

You can use \ escape to insert a double quote character (") inside a double-quoted string, but you cannot do the same for inserting a single quote character (') inside a single-quoted string.  Because \ has no special meaning inside a single-quoted string.
% echo "abc \" def"
abc " def
% echo 'abc \' def'                   
quote> blah'
abc \ def
blah
%
But I often find myself needing to insert apostrophes inside single-quoted strings.  I use the following syntax then:
% echo 'There'\''s always a way out'
There's always a way out
Shells usually let you concatenate strings by just writing them together without any space between them: "abc""def" is the same as "abcdef" which is the same as "abc"'def' which is the same as abcdef (without quotes).  We use the same technique here: we have two different single-quoted strings: 'There' and 's always a way out'.  In between them we have a single-quote character escaped with \ to mean that we want a literal apostrophe character inserted.

Saturday, January 14, 2012

Running Ubuntu on a Toshiba Z830

I bought a Toshiba Z830 recently.  This post is to document how I am running Kubuntu on it.  First, let me show you how fast it boots :)

  1. The machine has only 128GB storage, which cannot be upgraded yet, which means having both Windows and Linux on the machine may not be realistic.  But there’s a way to back up Windows!  Toshiba bundles a program with the computer that creates recovery USB drives for the Z830.  I bought a new 16GB USB drive and made it a recovery drive.  The plan is to use this for restoring Windows when I am giving this laptop away to somebody in future.
  2. I downloaded 64-bit Kubuntu 11.10 installer from kubuntu.org.  Since I have a backup copy of Windows, I removed all NTFS and recovery partitions from the disk and created new partitions for installing Kubuntu.
  3. After the installation, almost everything worked out of the box.  I had no issues with the laptop sleeping or waking up; 3D graphics worked fine; audio, bluetooth, webcam... everything worked.  Except the following one issue.
  4. Screen brightness controls sometimes worked, but sometimes they didn’t.  The technique that helped in 2008 helped me this time too, except I needed to make a minor modification to the program: the file that required changing was /sys/class/backlight/intel_backlight/brightness (i.e. intel_backlight instead of acpi_video0).  And the brightness value seem to be in the range of 100 through 4400.
  5. Battery life was bad.  More worrying than battery life was the heat generated by the laptop.  I found some tips to reduce power usage on Putokaz blog, and tried it (thank you, voloder!).  While it did reduce power consumption, it also resulted in some screen update issues.  After some trial and error, I found that frame buffer compression was causing issues, so I left that one out.
    Update: Kubuntu 12.04 provides a very good battery life out of the box.
  6. ZDNet recommends disabling “elevator” I/O scheduler by passing elevator=noop to kernel.  Based on tips from both sources, I have updated my /etc/default/grub to have this line:
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash pcie_aspm=force i915.i915_enable_rc6=1 i915.lvds_downclock=1 elevator=noop"
    (Power management flags may work on other computers with the same Core i5-2467M Sandy Bridge processor as well.)
  7. Based on the same ZDNet story mentioned before, I updated my /etc/fstab as well.  I added noatime flag to my ext4 partitions and mounted /tmp on RAM using tmpfs.  (I didn’t change the caching parameters as that sounds a bit scary.)
    UUID=dbccce1d-8e31-428e-a25a-227afc54651f /      ext4  noatime  0  1
    UUID=720c576c-2c1a-435f-ae90-9151acda2005 /home  ext4  noatime  0  2
    tmpfs /tmp tmpfs nodev,nosuid,noexec,size=1G 0 0
    
Update: Someone was asking about battery life, so here's a screenshot of expected battery life.  With Ubuntu 12.04, you'd get more than 5 hours of battery life on a full charge.

Saturday, January 7, 2012

Partitioning disk for installing Linux

Are you installing Ubuntu on a new machine and you’re not sure how much storage space the installation would need or how to partition your disk?  I have a few suggestions:
  • Create separate partitions for Linux installation (mounted at /) and data (mounted at /home).  This way, you can completely wipe and reinstall your OS or switch to a different distro without worrying about data in your home directory.
  • A typical Linux installation doesn’t need a lot of space.  I allocated 20GB for my root (/) partition purely based on some guess work, but I’m using only about 7.2GB in that partition.  If I’m starting over again, I’d go with a 12 or even a 10GB partition.  (You’re a lot more likely to eventually run out of space on your home partition than on the root partition.)
    % df -hl
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda1              19G  7.2G   11G  40% /
    udev                  2.9G  4.0K  2.9G   1% /dev
    tmpfs                 1.2G  996K  1.2G   1% /run
    none                  5.0M     0  5.0M   0% /run/lock
    none                  2.9G  2.8M  2.9G   1% /run/shm
    /dev/sda6              93G   78G   11G  89% /home

Wednesday, November 16, 2011

Migrating to a new SSD without data loss

I have a Samsung N150 netbook that came with Windows 7 preinstalled on a 250GB hard disk.  I created a bunch of new ext4 partitions on this disk and was running Kubuntu on it.  I almost never used Windows, but I had kept it on the disk anyway.  Last week I thought of upgrading to an SSD, and bought a 240GB SSD.  This post is to document how I copied over the Windows installation and recovery partition to the SSD before swapping the disks.

I connected the SSD to my computer using an USB interface and ran sudo fdisk -l to see the partition tables of both disks.
% sudo fdisk -l

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd937cf59

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    31459327    15728640   27  Hidden NTFS WinRE
/dev/sda2   *    31459328    31664127      102400    7  HPFS/NTFS/exFAT
/dev/sda3        31664128   232622079   100478976    7  HPFS/NTFS/exFAT
/dev/sda4       232624126   488394751   127885313    5  Extended
/dev/sda5       480397312   488394751     3998720   82  Linux swap / Solaris
/dev/sda6       232624128   291215359    29295616   83  Linux
/dev/sda7       291217408   480391167    94586880   83  Linux

Partition table entries are not in disk order

Disk /dev/sdb: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders, total 468862128 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00045231

   Device Boot      Start         End      Blocks   Id  System
/dev/sda is the original hard disk that came with the laptop, and /dev/sdb, which is empty currently, is the new SSD.  I have to clone the first 3 partitions (/dev/sda1, /dev/sda2, and /dev/sda3) bit-by-bit to retain all the preinstalled stuff — this includes Windows 7 installation and the recovery partition.  Replicating the Windows partitions is the tricky part, so this post will describe that in detail.  Copying data from Linux partitions can be done with a simple rsync.

The first step is to create partitions on the new disk that resemble the old disk.  I followed the fdisk guide of TLDP and created the first 3 partitions.  Now fdisk -l shows this configuration:
% sudo fdisk -l      

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd937cf59

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    31459327    15728640   27  Hidden NTFS WinRE
/dev/sda2   *    31459328    31664127      102400    7  HPFS/NTFS/exFAT
/dev/sda3        31664128   232622079   100478976    7  HPFS/NTFS/exFAT
/dev/sda4       232624126   488394751   127885313    5  Extended
/dev/sda5       480397312   488394751     3998720   82  Linux swap / Solaris
/dev/sda6       232624128   291215359    29295616   83  Linux
/dev/sda7       291217408   480391167    94586880   83  Linux

Partition table entries are not in disk order

Disk /dev/sdb: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders, total 468862128 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00045231

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    31459327    15728640   27  Hidden NTFS WinRE
/dev/sdb2   *    31459328    31664127      102400    7  HPFS/NTFS/exFAT
/dev/sdb3        31664128   232622079   100478976    7  HPFS/NTFS/exFAT
The partitions in the new disk are of the same size and same type as in the old one.  /dev/sdb2 is bootable as is /dev/sda2.  (It won’t boot yet though, since the disk has no OS yet.)  Now to copy the data bits over.  I first unmounted all three partitions.  This is critical because changing data underneath when it’s being copied over is a darn good recipe for data corruption.

dd is the low-level data copying utility I used to clone the partitions.  Copying the data over was as simple as running these commands one by one.  (Swapping if and of can result in wiping out all data from the old partition.  dd cannot even know if you’re passing wrong arguments to it.)
% sudo dd if=/dev/sda1 of=/dev/sdb1 conv=notrunc
% sudo dd if=/dev/sda2 of=/dev/sdb2 conv=notrunc
% sudo dd if=/dev/sda3 of=/dev/sdb3 conv=notrunc

Copying can be painfully slow since we are moving hundreds of GBs around.  Blog O’ Matty has a post that shows how to find status of a running dd command.  Essentially you’d send SIGUSR1 signal to the dd process and it’d print the current status of the transfer.  One of the commenters suggests running sudo pkill -SIGUSR1 dd so that you don’t have to think about process IDs.

Once this was done, I installed Kubuntu on the SSD using the standard installation process, and everything went just fine.  Windows doesn’t boot probably because it thinks mine is a pirated copy.  (Shows an error saying some ‘important’ hardware has gone missing.)  But I can boot into the recovery partition, so I can restore factory settings to get Windows running again when I want it.

I restored all my installed software from the package selection list I had already generated.  That’s it... the computer is exactly like it was before with all my programs and configuration.