Skip to main content

Installing a Ubuntu Hardy Heron Java Development Environment on a USB flash drive

Posted by inder on July 6, 2008 at 12:16 PM PDT

USB flash drives are really cheap these days, so I decided to create a portable environment that contains all my favorite applications, as well as Java IDEs and utilities. This is attractive since I can boot up any computer with this thumb drive, and have the same environment to work with. Besides, it is kind of cool.

You would think that this would be as easy as booting from the install disk of Ubuntu, and choosing USB disk as the target drive. Alas, it is not so. Ubuntu will try to install Grub which will be unable to boot from your USB disk. Moreover, I started running into problems with using the same USB disk at different computers. So, I searched around and found some excellent articles at debuntu.org and by Ryan Cloke. Both were written for the prior version 8.04 of Hardy Heron but are still largely appropriate. There were a few typos in the instructions, so I decided to reproduce instructions here with my edits, as well as add instructions related to setting up the Java development environment.

Prepare the USB Disk

Get a USB disk, at least 1 GB in size. I got an 8GB one since then I dont have to worry about conserving disk space for a while.

These instructions assume that you already have a Linux installation to use to prepare the USB linux installation. If you know how to do this with Windows, please share instructions as comments to this blog.

The first step is to partition the USB disk using fdisk. But to use fdisk, you will need to identify the device associated with the USB disk. You can do so by using fdisk itself:

sudo fdisk -l

Scan output for entries similar to this.

Disk /dev/sdc: 8017 MB, 8017412096 bytes
255 heads, 63 sectors/track, 974 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xcb527ec4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1   *           1          95      763056   83  Linux
/dev/sdc2              96         974     7060567+  83  Linux

As you can notice, the disk /dev/sdc is 8GB that matches with my USB disk size. So, /dev/sdc is the correct device for my USB disk. Note that it is very important to identify the correct device. If you choose /dev/sda you will probably overwrite your existing hard disk losing all data and the OS on it!).

In my case above, I already have the USB disk properly partitioned. But you will need to do the same if you haven't already done so (Note that this will erase ALL the data on the disk, so back up anything important before executing these instructions):

  1. sudo fdisk /dev/sdc
  2. Delete any existing partitions by typing "d". Type "p" to check that the disk is cleaned out.
  3. Type "n" to create a new partition. Choose primary, with id 1. For size, choose the default starting track, but for end use +770M instead of a track number. This will create a disk of size 770MB, just enough to hold the live CD image needed for booting. You can choose to increase this a little bit, but making it larger has no benefit since the contents of this partition will never change. The second partition is what needs to hold all of your data and future updates, so make that occupy the rest of the space.
  4. Type "n" to create the second partition. Again, choose primary, with id 2. For size, choose default settings.
  5. Type "a", and select 1 to mark the first partition as the boot partition.
  6. Type "w" to write these changes to the USB disk and exit fdisk.

Now we need to format these partitions:

sudo mkfs.vfat -F 16 -n UbuntuLive /dev/sdc1
sudo mkfs.ext2 -b 4096 -L casper-rw /dev/sdc2

Prepare the Ubuntu Installation

Download the Hardy Heron 8.04.1 ISO image from Ubuntu website. I chose the 32-bit i386 image (ubuntu-8.04.1-desktop-i386.iso) because it is going to run on almost all CPUs. The first step is to mount the downloaded iso image.

sudo mkdir -p /media/iso
sudo mount -o loop ubuntu-8.04.1-desktop-i386.iso /media/iso

Copy the contents of this image to a temporary directory (/home/inder/hardy-usb in my case):

mkdir -p /home/inder/hardy-usb
sudo cp -rf casper dists install pics pool preseed .disk isolinux/* md5sum.txt README.diskdefines casper/vmlinuz casper/initrd.gz install/mt86plus /home/inder/hardy-usb/

Next, we will modify this installation to fix some bugs specific to USB disk installation.

 

cd /home/inder/hardy-usb/
mv isolinux.cfg syslinux.cfg
sudo vi syslinux.cfg

You should replace the contents of this file with the following:

DEFAULT usblive
GFXBOOT bootlogo
APPEND  file=/preseed/ubuntu.seed boot=casper initrd=initrd.gz quiet splash --
LABEL usblive
  menu label ^Start Ubuntu and save changes to USB
  kernel vmlinuz
  append  file=/preseed/ubuntu.seed boot=casper persistent initrd=initrd.gz quiet splash --
LABEL live
  menu label ^Try Ubuntu without any change to your computer
  kernel vmlinuz
  append  file=/preseed/ubuntu.seed boot=casper initrd=initrd.gz quiet splash --
LABEL live-install
  menu label ^Install Ubuntu
  kernel vmlinuz
  append  file=/preseed/ubuntu.seed boot=casper only-ubiquity initrd=initrd.gz quiet splash --
LABEL memtest
  menu label Test ^memory
  kernel mt86plus
  append -
LABEL hd
  menu label ^Boot from first hard disk
  localboot 0x80
  append -
DISPLAY isolinux.txt
TIMEOUT 300
PROMPT 1
F1 f1.txt
F2 f2.txt
F3 f3.txt
F4 f4.txt
F5 f5.txt
F6 f6.txt
F7 f7.txt
F8 f8.txt
F9 f9.txt
F0 f10.txt

Save the file, and move on to the next step of fixing another bug in initrd.gz:

cd /home/inder/hardy-usb/
mkdir initrd_old
cd initrd_old
gunzip < ../initrd.gz | cpio -i --make-directories
cd scripts
sudo vi casper

Now, search for "mount ${cowdevice} -t ${cow_fstype} -o rw,noatime,mode=755 /cow". Remove "mode=755". So the line should look like this:

mount ${cowdevice} -t ${cow_fstype} -o rw,noatime /cow || panic "Can not mount $cowdevice on /cow"

After your have modified and saved the casper script, we need to repack the initrd file system.

cd ..
find ./ | cpio -H newc -o > ../initrdnew
cd ..
sudo rm initrd.gz
sudo mv initrdnew initrd
sudo gzip initrd
sudo rm -rf initrd_old

Copying Ubuntu to the USB disk

sudo mount /dev/sdc1 /media/usb
cd /home/inder/hardy-usb/
sudo cp -rf .disk * /media/usb
sudo umount /media/usb

Make the disk bootable

sudo syslinux -f /dev/sdc1

If you had made the mistake of trying to install Ubuntu directly from the CDROM, you will need to get rid of Grub. You can do so by installing Lilo to overwrite MBR:

sudo lilo -M /dev/sdc

Yay, you have the USB disk with Ubuntu. Now reboot the PC with the USB disk plugged in. You may have to select the bios setting to boot from the USB. On my laptop, I have to press F12, and on my home desktop, I have to press F11. You can also make the USB disk as a higher priority boot device.

Configuring the Ubuntu Installation

Boot up your PC from the USB disk. The system will boot into the default Ubuntu account. Open a terminal and run sudo passwd to change the root password.

Create a user account for yourself by using the System|Administration|Users and Groups menu. Ensure that in the create account screen, you have given this user the permissions to administer the system.

Disable the automatic login to the ubuntu user. Select System|Administration|Login Window menu. Select the Security tab, and unselect "Enable Automatic Login" and "Enable Timed Login". Note that you can still go to the ubuntu user by typing Ctrl-Alt-F1. I could not figure out a simple way to prevent the system from automatically logging in ubuntu user. If you know how to do this, please share that as comments to this blog.

Prevent Ubunutu from hibernating. Launch sudo gconf-editor. Go to apps|gnome-power-manager and uncheck the can_hibernate option. This setting didn't really work on my system though.

Configure Ubuntu to allow software from multiple repositories. Go to System|Administration|Software Sources Menu. Under the Ubuntu tab, choose "Community-maintained Open Source Software (universe)". Also, choose "Software restricted by copyright and legal issues (multiverse)". I also unselected the "Source code" option hoping that it may save me precious space on my USB disk by avoiding source-code for programs.

At this time, you may want to update your system with the latest versions of the various packages from the System | Administration | Update Manager menu. However, be selective in updating especially if you have a USB disk less than 2GB in size. The reason being that all the new packages that you install will be installed on your second partition, while the original packages will remain on the first partition. You may still want to upgrade commonly-used packages such as firefox. Also, dont bother trying to remove any of the unused packages since, again, they are on the first partition, and will not really free up any space on the second partition.

Install flash player for firefox. This is useful if you want to play YouTube videos.

sudo apt-get install flashplugin-nonfree

You may also want Firefox configured such that it does not use the USB disk for storing caches. For that, start firefox, and type about:config in the URL bar. search for cache, and set browser.cache.disk.enable, and browser.cache.offline.enable properties to false.

Installing Java

Ubuntu does not come pre-installed with Java. Unfortunately, there are some bugs in Java packaqes, so just using apt-get will give you some weird errors regarding libjli.so. Here are steps to fix this problem (instructions adapated from swik.net):

sudo su   // switch to administrator role
apt-get install sun-java6-jdk 
ln -s / /cow
export LD_LIBRARY_PATH=/usr/lib/jvm/java-6-sun/jre/lib/i386/jli
dpkg --configure -a

Run javac --version to ensure that the JDK installed correctly.

You can also install openjdk-6-jdk package instead. In that case, make sure that you adjust LD_LIBRARY_PATH accordingly before running dpkg.

Install other useful tools such as subversion and maven:

sudo apt-get install maven2
sudo apt-get install subversion

Installing Eclipse Ganymede

Download Eclipse from eclipse.org or issue the following command from command-line:

wget http://ganymede-mirror1.eclipse.org/technology/epp/downloads/release/gan... xvfz eclipse-jee-ganymede-linux-gtk.tar.gz
sudo mv eclipse /opt/

You can now run eclipse from /opt/eclipse/eclipse. I also installed a short-cut for Eclipse on my menu-bar. You can choose the icon images for Eclipse from /opt/eclipse/plugins/org.eclipse.platform_3.3.100.v200806172000/. You may also want to update or add Eclipse plugins at this time.

Once you have a configuration you are pleased with, you may want to create a backup so that you can restore it in case anything goes wrong. I did this by plugging-in the USB disk in my other linux desktop, and then using the dd command:

sudo dd if=/dev/sdc1 of=/home/inder/hardy-working-disk1.iso
sudo dd if=/dev/sdc2 of=/home/inder/hardy-working-disk2.iso

You can restore this by issuing the dd commands in reverse:

sudo dd if=/home/inder/hardy-working-disk1.iso of=/dev/sdc1
sudo dd if=/home/inder/hardy-working-disk2.iso of=/dev/sdc2

That concludes this rather long blog. It took me several installs and reinstalls to get it right, but the results are worth it. I am enjoying my USB linux installation and using it to write this blog.

Have you installed linux on USB disks as well? If so, share your experiences as comments to this blog. Thanks for reading.

Related Topics >>

Comments

OK, now I see it. sdc2 is used for Live CD Image. Is it not possible to add the iso file to sdc1 and then mount it? Or should it be a writable filesystem.

Yes, it should be sdc (I have updated the instructions). sdc2 is needed because that is where the OS is booted into. sdc1 just keeps the Live CD image, which is read-only. All of your OS updates, and user home directories, etc are kept on sdc2.

Good story! Coincidentally last weekend I gave it a try (to install ubuntu on an epia) and it almost worked. A few questions: 1. After the step 'Make disk bootable' you switch from sdc to sdb. I assume this is a mistake? 2. Why do you need sdc2? It doesn't look like you use it and I would like to keep my flashdrive vfat.

I am glad to see this type of article on Java forum. I have been doing this for some time now and found the following site indispensable! http://www.pendrivelinux.com/ The whole purpose of this site is 'Easily run Linux from a portable USB device'! Do check it out.

nice one.