Ubuntu_User

Restore a drive/partition from backup partition table entries

In Recovery on July 29, 2009 at 11:51 am

Having detailed knowledge of your partition table is worth it’s weight in gold if you’re ever faced with a disk that just lost its partition table entries.

I was recently faced with such a tragedy, and I was only saved by virtue of the fact that I had the partition table entries to hand. I had just deleted an ext3 partition on my external drive using the Gnome Partition Editor, and then queued up two operations:

  1. Create a fat32 file system in its place
  2. Assign it a label

When I applied these operations – BOOM! – the whole partition table on the external drive just disappeared. This was the drive that held my personal data, my photos and my entire media collection – basically any data of value to me. Thankfully, I had a backup of my partition table entries and instructions on how to recreate the partition table using these entries.

So, remember to back up your partition table entries to a file and store it offsite or at multiple locations on your home network. These days there are plenty of free offsite backup options like Dropbox and Ubuntu One, so there is never an excuse not to.

Advertisements

Improve responsiveness of Linux desktop: decrease “swappiness”

In Performance on July 25, 2009 at 4:54 pm

The swappiness parameter controls how aggressively the kernel swaps processes out of physical memory and onto the swap disk.

When swappiness=0, the server kernel avoids swapping programs out of physical memory for as long as possible. When swappiness=100, the server aggressively swaps processes out of physical memory.

The Ubuntu default is 60 at time of writing. You can check this through the following command:

cat /proc/sys/vm/swappiness

We want to change this value, and lower it to (typically) 20, as swap space is much slower than physical memory and involves expensive I/O writes. Also, the constant swapping heats up the hard drive and lowers its life expectancy.

You can change this value temporarily ( till the next reboot ) through the following command:

sudo sysctl vm.swappiness=20

To make this change permanent, we’ll need to edit /etc/sysctl.conf

gksudo  gedit /etc/sysctl.conf

and add or modify this parameter.

vm.swappiness=20

If this parameter does not exist in the file, add it at the end. You’ll need to reboot to have this change take effect. Authoritative source for this tip is the Ubuntu Community Documentation Swap FAQ.

Open a terminal, and run the free command to check how much swap space is being used after implementing this tweak.

Sample output on my system after the tweak:


$free             total       used       free     shared    buffers     cached
Mem:       1017416    1001440      15976          0      14304     233252
-/+ buffers/cache:     753884     263532
Swap:      1004020     133460     870560

Purge packages that are deinstalled

In cmd-fu, System Maintenance on July 25, 2009 at 12:13 pm

Before starting on the command, an explanation of why this is generally useful. When a package is deinstalled (because it’s not required or because a newer version of the package is installed), the package is removed from the system, but the configuration files remain. To remove the config files too, you need to purge the package.

If you have manually configured a package earlier, and would like to retain the configuration files for when you’ll install the package in future, this is not a command you should run.

In all other cases,  you should purge the packages in the interests of good housekeeping. If that isn’t enough motivation, it also means that future installs of the package get a blank state, and there are no potentially messy interactions of earlier version config files with newer packages.

With that out of the way, lets get down to bass tacks:

dpkg --get-selections | grep "[[:space:]]deinstall$" | awk '{print $1}' | sudo xargs dpkg -P

The first command in the pipeline returns the list of packages in the system, the second filters out the deinstalled packages, the third filters out the package names from the previous output, and the fourth purges each package. Consequently, only the fourth command requires sudo permissions.

There’s a downside to running this command – you cannot stop the command by hitting <Ctrl-C> on the terminal as the xargs command spawns an individual process for each dpkg -P

On linux systems, there are typically many ways to achieve the same result, so here’s another way to purge deinstalled packages:

sudo aptitude purge $(aptitude search ~c -F "%p")

So you know what’s happening, here’s a brief explanation of what the command does. aptitude search -c returns the list of deinstalled packages. The -F “%p” argument ensures only the package names are printed. The outer aptitude purge command operates on the returned list of packages from the inner command.

An example of why you’d run this is to purge old linux kernels.

Before running such commands, it is imperative you know what you are running, and what the results are, so I’d suggest you run it in smaller chunks and examine the results before running the actual command.