Creative Commons

2007-01-13 21:23:06

Update linux on a thumb drive with `yupdate`

I just got a new 4GB USB thumb drive. How should I use it? You guessed it, Linux. Just to be safe, I took the hard drive out of my work laptop, plugged in the USB drive, popped in the Fedora Cora 6 DVD, and away I went. I actually installed a pretty complete system. I wanted to be able to use it like a real system if I had the need. So after the install, I only had around 600MB available. I tried to do a `yum update`, but that failed. I didn't have enough disk space to download and install all 250+ updates. What do I do?
Lucky for me Frank sends me Ruby code every now and then. A while back he sent me one that was supposed to do the equivalent of `yum -y update` one package at a time. He wrote it for a system that only had 8.5MB of space left *yikes*. Anyway, I took that and massaged it a little bit into a wonderful little utility to update my thumb drive installation.
#!/usr/bin/env ruby `yum clean all` (`yum check-update | awk -F" " '{print $1}' | sed 's/\.i386//' | sed 's/\.noarch//'`).to_a[4..-1].each do |p| command = "yum -y update " + p.chomp system command `yum clean packages` end `yum clean all`

So lets run it through line by line...
#!/usr/bin/env ruby
This script was intended to be run as if it were a binary, that is, invoking it like `./yupdate`, or throwing it in /bin and just running `yupdate`. This line tells your system that this is a Ruby script. (Alternatively, we could run the script as such, `ruby yupdate`, and this line wouldn't be necessary).
`yum clean all`
This makes sure your disk is clean of any yum temporary files. Since we are limited on disk space, this is a good thing to start with.
(`yum check-update | awk -F" " '{print $1}' | sed 's/\.i386//' | sed 's/\.noarch//'`).to_a[4..-1].each do |p|
Ok, this is a biggie. This is the start of Ruby's equivalent of a foreach statement. First, let's look inside of the ()'s. We are running system commands to produce a result set. First, we run `yum check-update` to see what updates we need. Then we pipe that result set through awk, which will separate each line into space-delimited columns, and give us the first column in each line, which happens to be the name of the package needing updated. Then we run that result set through sed twice. This will strip off the arch type. For instance, if gaim needed updating, it would appear as "gaim.i386". We don't care what arch type it is, we just need to know the package name.
That takes care of what's inside of the ()'s. The results are then converted to an array and the first 4 rows of that array are stripped. Why? If you notice when you run yum, there's always loading and setting up and reading lines saying what the program is doing at the beginning. We don't need those, we just need packages.
In English, the rest of that line says, for each result in the array, store that result in the variable "p" and do the following code.
command = "yum -y update " + p.chomp
I created a variable named "command", which will be assigned the command we want to run. In this case, if we're updating gaim, we would want to run `yum -y update gaim`. Chomp is a method that will strip any trailing junk off of our string, such as hidden characters. I don't think there are any, but you can never be too sure.
system command
This line executes our command, which we cleverly stored in a variable named "command". You'll notice this is different that the other way we ran system commands (placing them in `'s). The first reason for this is that the "system" command will wait until the supplied command has completed before moving on. The purpose of this script is to do one package at a time, so we wouldn't want this thing flying by and trying to do 200 at once. The second reason is that any commands executed by `'s will not have their output displayed on the stdout (the screen). The system command will show the executed command's output on the screen.
`yum clean packages`
After installing each package, we want to make sure that yum tidy's up. We need our disk space!
This ends the code that will be executed every iteration of our foreach loop.
`yum clean all`
And again, better to be safe than sorry. Make sure yum deletes any temp files it doesn't need.



2007-02-02 14:49:55

anonymous says...
try slax

Post a comment!