Lazy Hacker Babble

Just some random babblings from a lazy hacker…

Archive for the 'Linux' Category

2009 OS of the Year… LINUX!

Posted by hsin on 1st January 2010

Looking back on my computer usage this past year, I realized that I’ve stopped using Windows. For the first time, I don’t even have a Windows computer at work. Having mostly used DOS/Windows (usually along side a linux server) for so long I didn’t expect that my usage to stop so quickly, but before I knew it I was doing everything I needed to do on another OS. One of the key changes that I made that contributed to being able to stop using Windows is that I stopped PC gaming. However, I would have expected that OSX would have been the primary OS following Windows (especially since I replaced my main Windows machines with a MBP), but the work horse OS that I used the most turned out to be Linux. Where consumer apps on Linux used to lag behind Windows and OSX, developers are now building them for Linux including media players (VLC) and programs like Picasa and Handbrake with the arcane interfaces that are often associated with the Unix world. I believe having these types of user accessible applications will drive Linux more then just having windows/osx-like window managers.

Posted in Linux, OSX, Software, Windows | No Comments »

Restoring RPM and YUM on Fedora after an accidental yum remove rpm

Posted by hsin on 28th December 2009

By default there is nothing that prevents an user from accidentally removing both yum and rpm from a Fedora system and once removed it is very painful to try to put them back. If only yum was removed it would be a little easier to restore. Yum can be reinstalled through rpm using the yum-*.rpm package downloaded from Fedora or the install disc. Without RPM, it’s like a chicken-and-the-egg problem. The packages from Fedora and install discs (including the rpm package itself) are .rpm files and needs rpm to install.

Today I found myself in the situation where both YUM and RPM got removed by accident. I was following Fedora’s instructions on finding and removing orphaned packages after upgrading and had yum remove some Fedora 11 packages that was left over. Being distracted by other things at the time, I noticed too late that removing those F11 orphaned packages also led yum to remove a bunch of F12 packages including rpm and yum.

So, what does one do in this situation besides “backup and reinstall? which I wanted to avoid if at all possible. It took me awhile to figure out but I was get rpm back on my system and restore it to its earlier state. Here are the steps I took.

My first inclination was to try to compile RPM. However, this was a FAIL since it required additional packages that I didn’t have installed and I didn’t have a package management system… Instead, I downloaded the rpm-*.rpm package from Fedora and was hoping to extract the files so I can use them to reinstall rpm and yum. To extract files out of an RPM package requires… rpm! Oh, the irony. There is a rpm2cpio program that will do that but that is available in . . . the rpm-build-*.rpm package! Fortunately, there are a lot of scripts available that will do it including some shell scripts. The Fedora RPM docs included some scripts and I downloaded the bash script version but it didn’t work. After a little more digging, I found out that Fedora changed the compression scheme it uses in F12 from gzip to xz, so I modified the shell script as follows:

#!/bin/sh

pkg=$1
if [ "$pkg" = "" -o ! -e "$pkg" ]; then
   echo "no package supplied" 1>&2
   exit 1
fi
leadsize=96
o=`expr $leadsize + 8`
set `od -j $o -N 8 -t u1 $pkg`
il=`expr 256 \* \( 256 \* \( 256 \* $2 + $3 \) + $4 \) + $5`
dl=`expr 256 \* \( 256 \* \( 256 \* $6 + $7 \) + $8 \) + $9`
# echo "sig il: $il dl: $dl"
sigsize=`expr 8 + 16 \* $il + $dl`
o=`expr $o + $sigsize + \( 8 – \( $sigsize \% 8 \) \) \% 8 + 8`
set `od -j $o -N 8 -t u1 $pkg`
il=`expr 256 \* \( 256 \* \( 256 \* $2 + $3 \) + $4 \) + $5`
dl=`expr 256 \* \( 256 \* \( 256 \* $6 + $7 \) + $8 \) + $9`
# echo "hdr il: $il dl: $dl"
hdrsize=`expr 8 + 16 \* $il + $dl`
o=`expr $o + $hdrsize`
dd if=$pkg ibs=$o skip=1 2>/dev/null | xz -d
 

With this script, I extracted the rpm (rpm, rpm-build, rpm-libs, rpm-devel, rpm-python) files to a temp directory and install the files manually:

rpm2cpio.sh rpm-*.rpm | cpio -d -i
tar cf – ./usr ./etc ./bin ./var | (cd /; tar xvf -)
 

This installs the files onto the system but doesn’t update the rpm databases, so I did a ‘rpm -ivh rpm*.rpm’ in addition to the dependent packages they needed.

With RPM installed, I was able to install YUM . I had kept a list of all the files that got removed earlier so I had yum reinstall all those packages and my system is now restored.

Posted in Linux | No Comments »

Upgrading to Fedora 12 (part 2)

Posted by hsin on 27th December 2009

It took awhile but Fedora upgraded itself and booted in to the GUI without a hitch. Everything seems to be running even with both the LCD TV and monitor hooked up (past upgrades would forget which was the primary display). Then I noticed that there was no audio. My first thought was that somehow Fedora 12 didn’t configure the audio correctly (uh oh) which wouldn’t be the first time that a piece of hardware worked in a previous version but not the upgraded version. Fortunately, I thought to just double check the volume setting and noticed that the upgrade set the audio-out to MUTE for some reason. Unchecking that re-enabled the sound.

Posted in Linux | No Comments »

Upgrading to Fedora 12 (part 1)

Posted by hsin on 27th December 2009

I decided to take the time off from the holidays to upgrade from Fedora 11 to Fedora 12. My past few upgrades of Fedora have all hit some sort of snag some of which I felt were real obstacles that the typical computer user would find it very difficult to overcome.

For this upgrade, I was ambitious and decided to try the live upgrade feature that was introduced in Fedora 10. By running “preupgrade”, Fedora will download all the packages needed to upgrade to the new version in the background so the system can still be used. When everything is downloaded, the system will reboot, install the packages and then the system will be updated. That’s the theory at least. This method of upgrading should be faster since it only downloads the packages needed instead of every package. Given my complaint from the last post where I downloaded the entire DVD iso then having to download nearly another DVD worth of packages on the first ‘yum update’, this method would address that since it will bypass the first part.

The instructions for using using Preupgrade is on the Fedora wiki. Essentially, it is suppose to just be running the command ‘preupgrade’ and everything else is taken care of automatically. That was kind of accurate. I ran the command and immediately ran into the most common bug with F12 upgrade. I used method 1 to work around it, but I didn’t have any extra kernels so it was the tunefs that got me the extra space I needed. Seriously does the Fedora team really think that consumers would be comfortable having to do this in order to upgrade an OS? What Fedora did very well is that it warned me about the problem and none of it effected the current OS and as it promised, I was able to continue to use the system.

Okay, with that out of the way, it completed the download and it was now time to reboot. This is the stage where the system is not able to be used as it install the downloaded packages. However, at reboot, the system just hanged with the monitor showing “GRUB” and a blinking cursor… The upgrade corrupted GRUB and in order to fix it would require re-installing GRUB. The existing linux system is still there unharmed, the boot process just never made it that far. The irony of this bug is that “preupgrade” was meant to avoid having to create a bootable Fedora disc, but a bootable disc is needed to reinstall GRUB. I dug out my Fedora 11 install disc and booted into rescue mode. The following will install GRUB back to the system:

chroot /mnt/sysimage
grub-install /dev/sda  # change /dev/sda to the drive you have your /boot
 

Rebooting after this, the system booted back into Fedora 11…?!? I guess I was able to confirm that so far my F11 instance is still completed unharmed, but this is suppose to be an automated upgrade to F12. I looked at the grub.conf file and it was suppose to give me the option to do a F12 Upgrade. So I rebooted again and made sure that I selected that option in GRUB before it booted back to F11.

Now the system is installing the package so let’s see how it goes. I took a bigger chance this time by keeping both my LCD TV and USB drive connected. In the past, I already had to disconnection in order to upgrade successfully, so let’s see what happen this time.

Posted in Linux | No Comments »

One script with different script names.

Posted by hsin on 29th November 2009

Sometimes I want to have my bash script behave differently based on how I call it without having to set up different parameter inputs. For example, I might have a script that can sync a file from a local server to one of two remote servers. I can make my script to take in parameters so I can call it using ’syncfiles -h serverA; syncfiles -h serverB’, but sometimes I forget what my params are and it’s easier to remember ’syncToA’ or ’syncToB’.

To do this is pretty simple, yet I always have to look it up each time so I’m writing this as note to myself.

#!/bin/sh

echo $0

if echo $0 | grep "syncToA" > /dev/null
then
   # do stuff
elif echo$0 | grep "syncToB" > /dev/null
then
   # do stuff
fi
 

Next, create symlinks to the original source files with the names you chose (i.e. ’symlink syncToA syncfiles’).

Posted in Linux, OSX, Programming | No Comments »

GNU Screen and Bash

Posted by hsin on 29th November 2009

As pretty as GUIs are, sometimes it is most efficient to work in a text console which is why I find myself using GNU Screen. However, Screen’s command starts with CTRL-A which is also Bash’s move to start of line command. To have it work, use ‘ctrl-a a’.

Also, screen sets the value of TERM to be screen so if you have any settings based on the TERM value, update it appropriately.

One thing about using screen is that you lose your terminal’s scroll buffer so as line scroll past your view you can’t just use the scrollbar to move up to see what you missed. Instead, screen has it’s own scroll buffer that you can use. You can set the buffer size using ‘defscrollback #’ (where # is the number of lines) in your .screenrc or in the screen command line (ctrl-a). You can switch to the buffer with ‘ctrl-a [‘ (and exit buffer mode with ESC). Navigation follows normal VI keys.

Finally, I noticed that hitting the delete key didn’t send backspace as expected with using the OSX terminal. To correct that, add the following to your .screenrc:

termcapinfo xterm-color kD=\E[3~
 

This tip came from here.

Posted in Linux, OSX, Programming, Software | No Comments »

Trying to switch to VIM.

Posted by hsin on 26th November 2009

I’ve always been an EMACS user on UNIX and it sometimes seems like it can do EVERYTHING. The one problem with EMACS is that a lot of production systems (and even many non-prod) won’t install it which means that when I’d have to use vi/vim and I’m just not as fluent with it. Especially since when I’m on those systems means that I’m trying to do fast debugging and navigation, being slowed down by my lack of experience with the editor is really frustrating.

I’ve decided to try to switch to VI/VIM to be the primary editor for awhile to force me to learn. There is no need to tell me that EMACS kicks butt (I agree), but unless you can get emacs installed everywhere, this is really more a decision based on my current situation.

Posted in Linux, OSX, Programming, Software | No Comments »

Nice font for terminal and code editing

Posted by hsin on 25th November 2009

Terminus is a very nice looking font that is well suited for programming editors and terminals.

terminus font

It is an open source font and many linux distributions have it available. I haven’t tried any for OSX, but you can compile for xterm on OSX/X11 from macports.

Posted in Linux, OSX, Programming | No Comments »

Pretty printing source code

Posted by hsin on 25th November 2009

Sometimes it is just more comfortable reading source code on paper, but printing directly from a text editor often gives an output that is ugly and wasteful. Just try opening up your source code in notepad, textmate, etc. and print. It might look very nice on your screen but on paper the fonts are big with lots of white space.

Printing source code is a different beast then printing narrative text and has some special requirements. The first is to include line numbers for easier reference. Different languages have different formatting and sometimes you want to print with color syntax to help find what you’re looking for. It can also be helpful to print two pages on one sheet so you can see more of the code and reduce paper waste.

On Windows/DOS, programmer editors usually have features for handling these types of pretty source code printing. Strangely, none of the editors I’ve seen on OSX have it (TextMate, Eclipse, Smultron, etc.) and since I’ve been dealing with code more I wanted a solution.

Fortunately, OSX’s UNIX underpinning allows me to use the classic enscript tool to handle this.

 enscript -2 -q -C -Ec –color -f Courier8 -r -p OUTPUTFILE SOURCEFILE
 

This generates a Postscript file (default), with 2 columns (2), quiet mode (-q), line numbers (-C) , using C syntax (-Ec), in color with Courier 8 pt font (-f), in landscape mode (-r) to a file called OUTPUTFILE (-p) from SOURCEFILE.

You can find different language syntax available for the -E option with “enscript –help-highlight” and you can set different output formats besides PostScript with the -W option (i.e. html).

“-p -” will tell enscript to print to stdout so you can pipe it to lpr.

Posted in Linux, OSX, Programming, Windows | No Comments »

Fedora install needs to be quicker.

Posted by hsin on 6th November 2009

Upgrading my system from Fedora 8 to something more recent has been on my todo list for awhile and last night I finally took the plunge. I had already downloaded iso images for Fedora 10 and 11 so I was ready to do the install from the DVDs. I couldn’t go straight from 8 to 11 because there has been some significant changes in the distribution so I had to first upgrade to 10 first.

The upgrade to 10 was pretty smooth but as with my previous experience, I had to manually clean up some dependency issues before I can do the first ‘yum update’. I expected the upgrade from 10 to 11 to be smooth but I actually ran into more issues. Firstly, there was some problems on the first reboot where Fedora couldn’t start X. It didn’t tell me there was a problem and instead just hung on a blinking cursor. Booting into level 3 (‘init 3′) allowed me to get a command prompt where I could fix the various dependency issues again and do a ‘yum update’.

While the hung screen is pretty serious (an average user might not be able to figure out the work-around), what annoyed me the most is how long it takes to do the first ‘yum update’. That’s because it updates EVERYTHING. First there was the 2 GB of download then the time to calculate dependency issues before it actually starts the update. What’s the point of doing a DVD install if it is going to download everything again?

It would be nicer if there was an option of first download the system updates and then do application level updates in the background once the system is already up.

Posted in Linux | 1 Comment »