Linux: mirroring my usb installation for more space

No Comments

I recently installed Ubuntu 9.10 Beta on a 4GB USB drive and was running my laptop from it. Although the Ubuntu default installation from the Live CD took less than the same installation from Fedora 12 I quickly found that I needed more space for the system (/home included). So I needed to move the system to a larger USB drive. This article explains how I moved my system from a 4GB USB drive to a 8GB USB drive including partition resizing.

My laptop has 2 limitations. One, it doesn’t have a CD-ROM drive. Second, it only has 3 USB ports. I got a Pioneer DVR-XD09 external hard drive. This drive takes 2 USB ports because of power requirements and that means that if I boot from the live CD I only have 2 remaining port for other external drive.

So I did the following:

Step 1:

Boot from the 4GB USB installation. Install gparted using Ubuntu Software Center from Applications menu.

Delete any partition  existing on the 8GB USB drive using gparted. Make sure you select the correct drive.

Step 2:

Boot Ubuntu Live CD 9.10.

Step 3:

Mount the internal hard drive. It has enough space for an image of the USB installation.

Step 4:

Plug in the USB where the system is installed (4GB) . Unmount it.

Step 5:

Make an image to the internal harddrive with the dd command:

dd if=/dev/sdc/ of=/media/internal_drive/ubuntu-image

Step 6:

Unplug the 4GB USB drive. Plug in the 8GB USB drive. Unmount it.

Step 7:

Copy the Ubuntu image to the 8GB USB drive using dd:

dd if=/media/internal_drive/ubuntu-image of=/dev/sdc

Step 8:

Boot the system from the 4GB USB drive.

Plug the 8GB drive. Using gpartd remove the swap drive on the 8GB drive.

Use uuidgen and tune2fs to change the uuid of the root partition on the 8GB drive:

$ sudo -i

# uuidgen
0b2eaab8-fddb-41fc-85f9-12ff421ce984

# tune2fs -U 0b2eaab8-fddb-41fc-85f9-12ff421ce984 /dev/sdc1

Step 9:

Open gparted (or click Refresh Devices from GParted menu if you didn’t close GParted on last step).

Select the 8GB device. Select partition 1 (/dev/sdc1 in my ca.se). Press the mouse right button and select resize/move.

Drag the right edge of the partition until your happy with the free space for the swap partition. I left 1.20 GiB for the swap partition.

Right-click the unallocated space and select New.

On File System select linux-swap and press Add.

Back to GParted main window Apply the changes.

Close GParted.

Step 10:

Mount the new root partition using the Ubuntu Places menu.

On a terminal window change to the etc folder on that partition:

$ sudo -i

# cd /media/0b2eaab8-fddb-41fc-85f9-12ff421ce984/etc/

Check the previous UUID values on fstab:

# cat fstab | grep UUID=
# for a device; this may be used with UUID= as a more robust way to name
UUID=4c557514-3b1e-47b2-ad53-475e26f0e520 / ext4 errors=remount-ro 0 1
UUID=1042ef8e-bcf0-4890-a5b0-fe24a1aec625 none swap sw 0 0

Backup your fstab:

# cp fstab fstab.old

Get the new values:

# blkid /dev/sdc1
/dev/sdc1: UUID=”0b2eaab8-fddb-41fc-85f9-12ff421ce984″ TYPE=”ext4″
# blkid /dev/sdc2
/dev/sdc2: UUID=”d8989008-f39c-42f4-81ad-5c1a70c8b3c9″ TYPE=”swap”

Replace the old values for the new ones:

# sed ‘s/4c557514-3b1e-47b2-ad53-475e26f0e520/0b2eaab8-fddb-41fc-85f9-12ff421ce984/’ fstab.old > fstab

# sed ‘s/1042ef8e-bcf0-4890-a5b0-fe24a1aec625/d8989008-f39c-42f4-81ad-5c1a70c8b3c9/’ fstab > fstab.new

Now your fstab should have the new values:

# cat fstab | grep UUID=
# for a device; this may be used with UUID= as a more robust way to name
UUID=0b2eaab8-fddb-41fc-85f9-12ff421ce984 / ext4 errors=remount-ro 0 1
UUID=d8989008-f39c-42f4-81ad-5c1a70c8b3c9 none swap sw 0 0

Step 11:

Now you need to install GRUB on the 8GB USB drive.

First, the UUID of the root partition needs to be corrected also:

# cd ../boot/grub

# cp grub.cfg grub.cfg.old

# sed ‘s/4c557514-3b1e-47b2-ad53-475e26f0e520/0b2eaab8-fddb-41fc-85f9-12ff421ce984/’ grub.cfg.old > grub.cfg

Install grub on 8GB USB device:

First check if sdc is on device.map:

# cat device.map
(hd0) /dev/sda
(hd1) /dev/sdb

Add the missing device:

# echo “(hd2) /dev/sdc” >> device.map

# grub-setup -d ./ -r ‘(hd1,1)’ -m ./device.map ‘(hd2)’

# grub-install –root-directory=/media/0b2eaab8-fddb-41fc-85f9-12ff421ce984/ /dev/sdc

Step 12:

Shutdown the system. Remove the 4GB drive and plugin the 8GB drive.

Turn on the system.

Now I have 50% of free space instead of the 5% I had before migrating my system from an 4GB USB drive to an 8GB  USB drive.

# tune2fs -U 0b2eaab8-fddb-41fc-85f9-12ff421ce984 /dev/sdc1