Tag Archive: Bash

Bash Script to Install Ubuntu to CR-48 through USB easier (Updated)

Update (01/18/2011): Thanks to flyboy415′s comment below, it inspires me to add an optional parameter to the script that allows you to customize the path of your USB drive. You probably read my another post shows you how to mount the USB drive manually in Chrome OS.  Now you can run this script with that directory that you created:

bash ubuntu.sh /tmp/usb

I spent last several days writing and testing the script which could help some of you installing Ubuntu onto CR-48 using USB drive a little easier.

This script is inspired by Jay’s script over at chromeos-cr48.blogspot.com. It’s also based on the steps over at the Chromium projects website. However, it uses your custom generated rootfs.bin in the USB drive instead of downloading the pre-made image.

Below is the entire script. You can either copy it directly to gEdit and save as .sh file or download the same script at github.

The Code

#!/bin/bash
#
# This script is used to install Ubuntu to CR-48 (or any other Chrome OS devices) from USB drive
#

# Display the purpose of this script
echo -e "n==============================================================="
echo -e "This script helps you to install Ubuntu on CR-48 (or any other nChrome OS devices) from USB drive a little easier.n"
echo -e "NOTE: You can pass the location of your USB drive as the only nparameter of this script"
echo -e "For example, bash chrome-os-ubuntu.sh /tmp/usb"
echo -e "================================================================="

# Disabled powerd service
echo -e "nDisabling power management service..."
sudoV="`initctl start powerd`"
sudoV2="`initctl stop powerd`"
if [ "$sudoV" = "" -a "$sudoV2" = "" ]
then
 echo "Make sure you run this script in the root account. Enter the following to enter root:"
 echo "sudo su"
 echo "After you are in the root account, run this script again."
 exit
fi
initctl stop powerd
echo -e "Power management disabled."

echo -e "nChecking the partitions size..."

# Do the following tasks if they were not already done
resizeV="`sudo cgpt show /dev/sda | grep 12103680`"
if [ "$resizeV" = "" ]; then

 # Resize the partitions
 echo -e "Resizing the partitions..."
 sudo umount /mnt/stateful_partition
 sudo cgpt add -i 1 -b 266240    -s 12103680 -l STATE   /dev/sda
 sudo cgpt add -i 6 -b 12369920  -s 32768    -l KERN-C  /dev/sda
 sudo cgpt add -i 7 -b 12402688  -s 10485760 -l ROOT-C  /dev/sda

 # Destory the stateful_partition
 echo -e "Clearing the stateful_partition, it will take some time..."
 sudo dd if=/dev/zero of=/dev/sda bs=131072 seek=1040 count=47280

 # Restart the notebook
 echo -e "nYou need to reboot your notebook in order to continue.nMake sure to press CTRL + ALT + => (Left arrow), login as chronos, and run this script one more time after Chrome OS was rebooted.nPress ENTER to continue or wait 1 minute to reboot automatically."
 read -t 60 iputs
 sudo reboot

fi

echo -e "The partitions are resized."

# USB drive verification
usbV="`mount | grep sd | grep -v sda`"
listing="`ls /media | tail -n 1`"
if [ "$1" == "" ]; then
 usbDr="`ls /media/$listing`"
else
 usbDr="$1"
fi
while [ "$usbV" = "" -o "$usbDr" = "" ]; do
 echo -e "nPlease insert the USB drive with rootfs.bin, make_dev_ssd.sh, and common.sh in it.nPress ENTER when the drive is inserted.nIf you are not signed on to Chrome OS. Please press CTRL (left) + ALT (left) + <= (left arrow) to return to the graphical interface and sign on in order to detect yout USB drive by Chrome OS. Press CTRL + ALT + => (right arrow) to return to this script and press ENTER to continue.n";
 echo -e "Note that if you already mounted your USB driver manually, you can kill this script by pressing CTRL + Z and rerun this script with a parameter that points to the path of your USB drive.nFor example, bash chrome-os-ubuntu.sh /tmp/usb"
 read ready
 echo -e "Detecting USB device..."
 sleep 10
 usbV="`mount | grep sd | grep -v sda`"
 listing="`ls /media | tail -n 1`"
 usbDr="`ls /media/$listing`"
done

# Mount the USB drive
echo -e "nMounting USB drive..."
if [ "$1" == "" ]; then
 usbDir="/media/$listing"
else
 usbDir="$1"
fi
echo -e "USB drive is mounted."

# Determine the existence of three required files
rootfs="`test -e $usbDir/rootfs.bin;echo -e $?`"
makeDev="`test -e $usbDir/make_dev_ssd.sh;echo -e $?`"
commons="`test -e $usbDir/common.sh;echo -e $?`"
if [ "$rootfs" = 1 -o "$makeDev" = 1 -o "$commons" = 1 ]; then
 echo -e "nSome of the required files cannot be found on the drive.nMake sure rootfs.bin, make_dev_ssd.sh, and common.sh are copied to the drive and reinsert it to the notebook.nRestart this script when you are ready."
 sudo umount $usbDir
 exit
fi

# Copy rootfs.bin in USB drive to /dev/sda7
echo -e "nCopying rootfs.bin to /dev/sda7, this will take some time..."
sudo dd if=$usbDir/rootfs.bin of=/dev/sda7
echo -e "rootfs.bin successfully copied."

# Mount /dev/sda7
echo -e "nMounting Ubuntu partition..."
sudo mkdir /tmp/urfs
sudo mount /dev/sda7 /tmp/urfs
echo -e "Ubuntu partition is mounted."

# Copy cgpt and /lib/modules/ to Ubuntu partition
echo -e "nCopying necessary files to Ubuntu..."
sudo cp /usr/bin/cgpt /tmp/urfs/usr/bin/
sudo chmod a+rx /tmp/urfs/usr/bin/cgpt
sudo cp -ar /lib/modules/* /tmp/urfs/lib/modules/
echo -e "The files are copied successfully."

# Unmount /dev/sda7
echo -e "nUnmounting Ubuntu partition..."
sudo umount /tmp/urfs
sudo rmdir /tmp/urfs
echo -e "Ubuntu partition successfully unmounted."

# Decide the rootdev
echo -e "nDetermining the Chrome OS kernel partition..."
rootfs="`rootdev -s`"
if [ "$rootfs" = "/dev/sda3" ]; then
 ker="/dev/sda2"
else
 ker="/dev/sda4"
fi
echo -e "Your kernel partition is in $ker."

# Copy the kernel to /dev/sda6
echo -e "nCopying $ker to /dev/sda6..."
sudo dd if=$ker of=/dev/sda6
echo -e "Copied successfully."
echo -e "nNow is the critical time to check the above output for any errors. If there are some errors, press Ctrl+z to stop this script and correct them. By not correcting them, you might need recover image from Google to restore Chrome OS. Otherwise, press ENTER to continue."
read checkEr

# Change kernel command line
echo -e "nChanging the kernel command line..."
cd $usbDir
sudo sh ./make_dev_ssd.sh --partitions '6' --save_config foo
echo -e "console=tty1 init=/sbin/init add_efi_memmap boot=local rootwait ro noresume noswap i915.modeset=1 loglevel=7 kern_guid=%U tpm_tis.force=1 tpm_tis.interrupts=0 root=/dev/sda7 noinitrd" > foo.6
sudo sh ./make_dev_ssd.sh --partitions '6' --set_config foo
echo -e "Changed successfully."

# Generate ubuntu alias in .profile
echo -e "nGenerating ubuntu alias..."
echo -e "alias ubuntu="sudo cgpt add -i 6 -P 5 -S 1 /dev/sda;sudo cgpt add -i 2 -P 0 -S 0 /dev/sda;echo 'Swiched to Ubuntu, restart to take effect'"n" >> /home/chronos/.profile
echo -e "ubuntu alias generated."
echo -e "nYou can type 'ubuntu' (without quotes) in Chrome OS command line to switch to Ubuntu from now on.nIn Ubuntu, add the following line to .bashrc to use 'chromeos' (without quotes) to switch back to Chrome OS:"
echo -e "alias chromeos="sudo cgpt add -i 2 -P 5 -S 1 /dev/sda;sudo cgpt add -i 6 -P 0 -S 0 /dev/sda;echo 'Switched to Chrome OS, restart to take effect'""
echo -e "nPress ENTER after you copied the above alias down."
read chromeos

# Complete Ubuntu installation
sudo cgpt add -i 6 -P 5 -S 1 /dev/sda
sudo cgpt add -i 2 -P 0 -S 0 /dev/sda
echo -e "nUbuntu installation is complete.nPress ENTER or wait 30 seconds to enter the newly installed Ubuntu."
read -t 30 ubuntu
sudo reboot

Few warnings before running this script

You need to running the script under bash instead of sh, for example

bash ubuntu.sh

where ubuntu is the name of your script.

If you use sh instead, you won’t see the error messages produced on the screen and might need the recovery image to restore Chrome OS as stated in the script above.

The make_dev_ssd.sh and common.sh files can be downloaded here which are provided by chromeos-cr48.blogspot.com. You might not have these two files during the chroot installation if you chose to get minilayout instead of the full layout of Chromium OS source code.

Other requirements, such as login as root, will be provided as you execute through the script.

Issues while running the script

If you have any issues while running this script, feel free to leave a comment or create an issue over at github.

Remove Part of File Names using PHP

Since David’s Batch Processor was pre-installed in GIMP on Ubuntu Studio, I used it extensively in the ICAF Gallery project.

My goal for these images is to resize their thumbnail version to 10KB or smaller and retain the original size for the enlarged version.

  1. Because most of these images are in different resolution, I randomly selected the scale value in the Batch Processor to resize the images.
  2. Then I had to manually view the file size of each image to see whether or not an image has to be resized again.
  3. After the differentiation, I moved the images that need to be resized once more to a new sub-directory and started the above process one more time.
  4. I would repeat these steps again and again until all the thumbnail version of the images are under or equal to 10KB.

During the early use of the Batch Processor, I missed a very important feature – that is to save the newly processed images to a different folder. This resulted my decision to write this Batch File Name Remover script.

The source code for this script is very easy to understand. It’s just some PHP functions plus two Linux commands: ls and mv.

I always place a postfix after the file name of processed thumbnails: “-thumb”. Because of that, I need to remove this postfix whenever I repeated the above resize steps.

The ls command is used to list the file name that contains the specified characters into an array. The mv command is used to remove the specified characters from the file name using PHP explode function. I used PHP instead of native Linux Bash script because PHP is easier than Bash for me :).

The source code for this script is available here.

Below is its source code:

<!doctype html>
<html>
    <head>
        <title>Batch File Name Remover</title>
        <style>
            table {
                width: 80%;
                margin: 0 auto;
            }
            table tr td {
                width: 50%;
                text-align: left;
            }
            table tr td[colspan="2"] {
                text-align: center;
            }
            .original, .changed {
                font-style: italic;
            }
            .original {
                color:red;
            }
            .changed {
                color:green;
            }
            #error {
                color:red;
                font-weight: bold;
            }
        </style>
    </head>
    <body>
        <h1>Batch File Name Remover</h1>
        <?php
            if (!isset($_POST['submitted'])) {
        ?>
        <table border="0" cellpadding="0" cellspacing="0">
        <form action="" method="post">
            <input type="hidden" name="submitted" />
            <tr>
                <td><label for="location">Folder Location: </label></td>
                <td><input type="text" id="location" name="location" size="100" value="/home/" /></td>
            </tr>
            <tr>
                <td><label for="remove">Characters to remove: </label></td>
                <td><input type="text" name="remove" id="remove" size="100" value="-" /></td>
            </tr>
            <tr><td>&emsp;</td></tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Remove" />
                    <input type="reset" value="Restore default value" />
                </td>
            </tr>
        </form>
        </table>
        <?php
            }
            else {
                $location = $_POST['location'];
                $remove = $_POST['remove'];
                $fileList = exec("ls '{$location}'", $files);
                $notContain = 0;    // Initialize the number of file names that doesn't contain the specified characters

                for ($i = 0; $i < sizeof($files); $i++):
                    if (strstr($files[$i], $remove)):
                        $charsRemain = explode($remove, $files[$i]);
                        exec("mv '{$location}/{$files[$i]}' '{$location}/{$charsRemain[0]}{$charsRemain[1]}'");
                        echo "<span class='original'>{$files[$i]}</span> renamed to <span class='changed'>{$charsRemain[0]}{$charsRemain[1]}</span><br /><br />";
                    else:
                        $notContain++;
                    endif;
                endfor;
                echo "<br />{$notContain} file name(s) do(es)n't contain the character(s) \"{$remove}\".";
            }
        ?>
    </body>
</html>

And here is the code in action:

Video coming soon …