Monday, August 31, 2009

Ubuntu Netbook Remix in VMware

I needed to install Ubuntu Netbook Remix (UNR) in a VMware Virtual Machine so that I could try to reproduce a bug. It was kind of an asspain. Problem is, UNR is made to be installed from a USB flash drive, and VMware can't boot from USB devices. There isn't a DVD or CD ISO image for UNR 9.04 available. It turns out that you can make a DVD image from the UNR .img.

I found the following guide on the Ubuntu forums (see below for a link). It should work from most linux distros:

Make a directory to hold the contents of the UNR DVD image:

$ mkdir unr

Make a mount point to mount the UNR .img file:

$ mkdir realunr

Mount the UNR .img as a loop device:

$ sudo mount -o loop -t vfat ubuntu-9.04-netbook-remix-i386.img realunr/

Copy all the contents from the UNR image to your unr/ directory. Note that realunr/.diskimage is critical:
$ rsync -a realunr/ unr/

Rename syslinux to isolinux:
unr$ mv syslinux isolinux
unr$ mv isolinux/syslinux.cfg isolinux/isolinux.cfg

Generate a DVD image from the contens of unr with the following incantation:
mkisofs -o ubuntu-9.04-netbook-remix-i386.iso -r -J -l \
-V "Ubuntu-Netbook-Remix 9.04 i386" -b isolinux/isolinux.bin \
-c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
-boot-info-table unr/

mkisofs will generate a DVD image that you can either boot a VM from or burn to a DVD.

Guide on ubuntuforums.org

Friday, August 07, 2009

Jailbroken iPhone, Cydia Apps, Upgrading iPhone to 3.0.1

I've been running my iPhone jailbroken for a couple of weeks. At first I felt dirty about having callously cast aside many of the legitimate security mechanisms in the iPhone, such as sandboxing of 3rd party apps. Utlimately, I have given into my baser instincts and am sticking with it.

Today I upgraded my iPhone to 3.0.1. There isn't an official jailbreak for 3.0.1 yet, but the 3.0 jailbreak works. The problem in general with upgrading a jailbroken phone is that there's no real way to back up all of your unauthorized customizations. You've basically had your way with the underlying Unix OS. Not everything you install from Cydia is a self-contained app. There are system modifications like installing OpenSSH and system utilities. Anyway, what it comes down to is that you need to reinstall all of your Cydia packages after upgrading and re-jailbreaking.

Here's how the process went for me:
In iTunes:
-do a backup
-choose to restore, which will also update. Don't choose upgrade.

After this is done, leave itunes, and follow the normal jailbreak process
-Run redsn0w
-Point redsn0w at your 3.0 iPhone OS ipsw file.

Once redsn0w is done, you're jailbroken again and running (mostly) 3.0.1. You still need to reinstall your Cydia packages. I found some of my customizations returned once I had reinstalled the Cydia packages, but not all.

On the topic of Cydia packages, here's a list of packages that I installed and really like:
  • BigBoss Recommended; This gets you a bunch of command line utilities that you'd expect to be on most unix systems. It actually is a metapackage that brings a ton of other packages with it.
  • MobileTerminal; A terminal application (not an SSH client)
  • MusicControls; A paid app that lets you background and control many music applications like Slacker and Pandora
  • OpenSSH; client and server. Be sure to reset passwords for root and mobile. Also disable root logins. Disable sshd from running automatically.
  • SBSettings; let's you tweak many hidden settings, replacing BossPrefs. Also required for disabling autostarting of the sshd service
  • Safari Download Manager; A paid app. Lets you download and save files, even ones that Safari doesn't know how to handle. Great for downloading files and getting them off you phone.
  • iFile; A paid app that lets you browse the entire filesystem and open files. Also has a built-in web server that will serve up files to your laptop. Useful when you have no Internet, but need to download a file.
  • SysInfoPlus; Shows you lots of technical information about your iPhone. Mostly just neat.
  • Status Notifier; shows various status indicators like new mail icon in the iPhones status bar at the top of the screen
  • BlackDarkness theme; This is a complete system theme that replaces lots of things, including many icons. I think it's kind of neat. Brings winterboard with it.
That's about it. The BigBoss blog is recommending that you stand pat until there's a redsn0w update. Supposedly it's going to make things a bit easier. It's not clear in what way. At the very least, they point out that for now, you'll still end up with a 3.0 kernel. Hmmm. :-/

http://thebigboss.org/2009/07/31/iphone-v301-released/

Thursday, June 11, 2009

GEICO Music

I hate to admit it, but GEICO commercials have a pretty good track record for featuring good pop music. Whether it's 3 Doors Down, Royksopp, or The Sounds, the songs in GEICO's ads are usually pretty good.

The latest that I like is "Somebody's Watching Me" by Mysto & Pizzi. This song is featured in the ridiculous "Kash" commercials. You can download it for free. While you're there, check out the "making of" video. It's surprisingly entertaining.

I don't like the commercials, and I'm not about to switch insurance providers, but, in general, if you hear a song in a GEICO spot, you can probably add it to your library with no regrets.

Thursday, May 14, 2009

Spotlight does Math

I just discovered that Leopard's Spotlight does math. Try it. Bring up spotlight with ⌘+space. Then start typing something like
7pi*8.3 or
sqrt(2)
and the answer is the first search result.

This isn't quite a powerful as the Google calculator. It doesn't appear to do base conversion, and it doesn't let you copy the result for pasting into an application, but it seems handy nonetheless.

Wednesday, May 06, 2009

Mac OS X handy bash aliases

Here are a few handy aliases that I keep in my .bashrc in OS X:
alias cppwd='eval "echo `pwd` | tr -d \\\\n | pbcopy"'
This copies my current working directory to the OS X pasteboard so I can Cmd+V it into another terminal. Often I want several terminals open in the same directory at once.
alias burn='drutil burn -noverify'
This one lets me type
burn mydisc.iso
to burn an ISO disc image to CD or DVD without having to open Disk Utility.
And a variation:
alias vburn='drutil burn'
This does the same as the other but also does the disc verification step.

Wednesday, April 15, 2009

A Gun for the Time Hole

An ordinary AK-47 Kalashnikov automatic rifle would not be allowed through the time hole. One made of bacon, however, is an ideal candidate for the time-traveling freedom fighter.

Sunday, March 15, 2009

Handling HTTP Redirection in Ruby

I have a Ruby project where I'm dumping a bunch of bookmarks from delicious.com, then fetching each bookmarked page for analysis.

One of the problems I encountered early on is that the some of the web pages bookmarked would redirect to some other location. Simply checking for HTTP response code 200 was insufficient. I needed to check for redirection as well.

A quick Google search for "ruby follow http redirect" yields lots of results. Unfortunately, they're all very similar, and not quite right. In general, the examples you come across (even the one in the official Ruby documentation) don't handle the case when the redirected location is path relative to the original location. So you end up doing a get on a URL that looks like "../../redirected/location/index.html," which clearly won't work.

It turns out that detecting relative redirection is fairly straightforward:

until( found || attempts>=@@MAX_ATTEMPTS)
attempts+=1
http=Net::HTTP.new(url.host,url.port)
http.open_timeout = 10
http.read_timeout = 10
path=url.path
path="/" if path==""

req=Net::HTTP::Get.new(path,{'User-Agent'=>@@AGENT})
if url.instance_of? URI::HTTPS
http.use_ssl=true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
resp=http.request(req)
if resp.code=="200"
break
end
if (resp.header['location']!=nil)
newurl=URI.parse(resp.header['location'])
if(newurl.relative?)
puts "url was relative"
newurl=url+resp.header['location']
end
url=newurl

else
found=true #resp was 404, etc
end #end if location
end #until


The trick here is to ask the redirected url object if it is relative. If it is, then add the redirected path onto the old url object. the URI class overrides the '+' operator (what is this, C++?) so that you can concatenate the new path onto the old URL, by doing:
newurl=url+resp.header['location']

Thursday, March 12, 2009

Mounting LVM Disks in Ubuntu

I always thought LVM (Linux's Logical Volume Manager) was kind of neat for the flexibility it gives you in adding and removing disks and resizing volumes such. However, in practice, I find it's usually more trouble than it's worth. It adds a layer of complexity between me and my data.

Often I need to mount a disk configured with LVM on another Linux machine or in an Ubuntu live CD environment. Out of the box the logical volumes aren't recognized, so I can't mount them.

It's fairly easy to add LVM support and mount the volumes though.

You install the lvm2 package, load the device mapper kernel module, and then activate any lvm volume groups on your disk.

$ sudo apt-get install lvm2
$ sudo modprobe dm-mod
$ sudo vgchange -a y

(assuming your disk with logical volumes is already connected)
$ sudo mount /dev/mapper/<logical volume name> /mnt

And that's all there is to it.

If you want to deactivate the volume groups (recommended before unplugging a USB disk with logical volumes):
$ sudo vgchange -a n
Warning: the above command will deactivate all volume groups, so check the vgchange manpage first, if that's not what you want.

Monday, March 09, 2009

The Rules of the Time Hole

There was a great video on College Humor addressing why terminators travel back in time naked.



That said, I think it's worth discussing the rules of time travel or "the time hole" as my colleague Ross and I put it. So if you've followed the Terminator movies, and, more recently, Terminator: The Sarah Connor Chronicles, the rules of the time hole are fairly easy to infer.

Things can go through the time hole if:
  1. They are made of meat
  2. They are wrappped in meat
  3. They look very much like they are made of meat
  4. If several things meeting the above rules go through all at once, something else may sneak through behind them.
In the original Terminator movie, we saw Kyle Reese, John Connor's soon-to-be father, come through the time hole. As he is 100% human, Kyle clearly qualifies for time travel under rule #1--he is made of meat. Also in the same film, the Terminator, a Cyberdyne Systems Model 101, comes through the time hole, qualifying under rule #2. The 101 consists of a metal skeleton that is wrapped in meat.

In Terminator 2, we see the T-1000, which is constructed of a "mimetic poly-alloy," i.e., liquid metal, travel through time. Clearly, the T-1000 qualifies under rule #3 as it looks, very convincingly, like it is made of meat. Similarly, in Terminator 3, we see the T-X come back in time. The T-X is much like the T-1000, except hot.

Later, in the The Sarah Connor Chronicles, we see Cameron (a terminator wrapped in meat), John, and Sarah (both made of meat) travel back in time. However, just before the time hole closes, Cromartie's severed head (devoid of its meat wrapping) flies through after them.

I checked all terminator-related facts presented above on wikipedia.org.

Saturday, January 10, 2009

OS X: How to detect whether you're on battery from the shell

Sometimes you need to determine programatically whether a computer is on battery or on AC. For example, I have a backup script that takes hourly snapshots of my home directory on my MacBook Pro (which I hopefully will post about at some point). However, I don't want the script to run if I'm on battery. Ideally, launchd, which kicks off the script, could give me this option, but I don't think it can. I'll have to let the script make the decision when it gets executed.

If I were writing the script for Linux, I'd have it look through /proc/acpi/ to check the state of the battery. There's no /proc in OS X. There is, however, a power management command, pmset.

If I run pmset -g while on AC, I get:

Currently drawing from 'AC Power'
-InternalBattery-0 100%; AC attached; not charging

If I run it while on battery, I get:

Currently drawing from 'Battery Power'
-InternalBattery-0 100%; discharging; (no estimate)

I simply have my script grep the output from pmset for "discharging":

checkBattery()
{
local ret=0;
if $PMSET -g batt | $GREP -q discharging;
then
ret=1;
fi
return $ret;
}

See the pmset(1) manpage for more info.

Friday, January 09, 2009

Converting a .dmg to a .iso

I have an operating system install image that I need to install in VMware Fusion. The problem is the install image is a MacOS disk image file, or .dmg. I need a DVD image, or a .iso, in order to boot the VM and do the install.

It turns out that the Mac OS X command hdiutil to convert the .dmg to a .iso:

hdiutil convert .dmg -format UDTO -o .iso

See the hdiutil(1) manpage for more info.

Thursday, January 08, 2009

How to sudoedit non-interactively

Okay, this one's a bit esoteric, but I think it's pretty cool. How do you use 'sudoedit' non-interactively such as from a script? Just a brief background about sudo: sudo is an authentication mechanism in Unix & Linux that allows unprivileged users to run specific commands (as defined by the system administrator) with root privileges without having the root password. This has several advantages over logging in as root:
  • Users can have specific, limited set of root privileges without having the entire set of root privileges.
  • Users use their own password, so the root password doesn't have to be shared. If a user's sudo privileges are revoked, the root password doesn't have to be reset.
  • Each use of sudo is audited per user, so that each time sudo privileges are invoked, there is an event in the system logs that identifies the specific user and the command they ran.
Sudoedit is a command that is related to sudo. It lets users edit files that normally only root can edit, such as system configuration files. However instead of using "sudo" followed by the editing command, the user runs "sudoedit filename" and sudo invokes the user's default editor, letting them edit the file.

So, what if you want to make changes to a system file via a script, and the only access you have to the file is via sudoedit? It isn't useful to have the script call sudoedit and then bring up vi for you to manually make the changes.

Well, the way sudoedit works, is that sudo makes a temporary copy of the file you want to edit, then calls your default editor, giving it the name of the temp file as the first argument. So, say your default editor is "fooedit". You run "sudoedit /etc/systemfile" and sudo makes a copy of /etc/systemfile to /tmp then runs "fooedit /tmp/tempfile". Your changes are saved to the temp file. When your editor exits, sudo copies the temp file over the original, and then removes the temp file.

From your main script, stage the pre-edited version of the system file, then set your default editor to a custom script that will copy that staged file over the temp file. When it exits, sudo will copy the temp file into place as normal.

Your script would go like this:


#!/bin/sh

export EDITOR="/bin/sh ./editor.sh";
PRE_STAGE=/tmp/stage.tmp;

echo "my new config"> $PRE_STAGE;

sudoedit /etc/config;

/bin/rm $PRE_STAGE;

exit;

Your custom script would look like:


#!/bin/sh
CAT=/bin/cat
PRE_STAGE="/tmp/stage.tmp"

$CAT $PRE_STAGE > "$1";
exit;


This script just cats your pre-staged file over whatever sudo passed in as the first argument ($1).

The only interaction you'll have to do is type your password for sudo, if you haven't already done so in the last few minutes (sudo temporarily remembers your authentication).