All pastes #2057785 Raw Edit

Something

public text v1 · immutable
#2057785 ·published 2011-05-13 07:00 UTC
rendered paste body
elpowerd_status="`initctl status powerd`"
if [ ! "$powerd_status" = "powerd stop/waiting" ]
then
  echo "Stopping powerd to keep display from timing out..."
  initctl stop powerd
fi

# Do partitioning (if we haven't already)
ckern_size="`cgpt show -i 6 -n -s -q /dev/sda`"
croot_size="`cgpt show -i 7 -n -s -q /dev/sda`"
state_size="`cgpt show -i 1 -n -s -q /dev/sda`"

# If KERN-C and ROOT-C are one, we partition, otherwise assume they're what they need to be...
if [ "$ckern_size" =  "1" -o "$croot_size" = "1" ]
then
  while :
  do
    read -p "Enter the size in gigabytes you want to reserve for Ubuntu. Acceptable range is 5 to 10 but 9 is the recommended maximum: " ubuntu_size
    if [ ! $ubuntu_size -ne 0 2>/dev/null ]
    then
      echo "\n\nNumbers only please...\n\n"
      continue
    fi
    if [ $ubuntu_size -lt 5 -o $ubuntu_size -gt 10 ]
    then
      echo "\n\nThat number is out of range. Enter a number 5 through 10\n\n"
      continue
    fi
    break
  done
  # We've got our size in GB for ROOT-C so do the math...

  #calculate sector size for rootc
  rootc_size=$(($ubuntu_size*1024*1024*2))

  #kernc is always 16mb
  kernc_size=32768

  #new stateful size with rootc and kernc subtracted from original
  stateful_size=$(($state_size - $rootc_size - $kernc_size))

  #start stateful at the same spot it currently starts at
  stateful_start="`cgpt show -i 1 -n -b -q /dev/sda`"

  #start kernc at stateful start plus stateful size
  kernc_start=$(($stateful_start + $stateful_size))

  #start rootc at kernc start plus kernc size
  rootc_start=$(($kernc_start + $kernc_size))

  #Do the real work
    
  echo "Modifying partition table to make room for Ubuntu. Computer will reboot, wipe your data and then this script will need to be re-run..."
  umount /mnt/stateful_partition
    
  # stateful first
  cgpt add -i 1 -b $stateful_start -s $stateful_size -l STATE /dev/sda

  # now kernc
  cgpt add -i 6 -b $kernc_start -s $kernc_size -l KERN-C /dev/sda

  # finally rootc
  cgpt add -i 7 -b $rootc_start -s $rootc_size -l ROOT-C /dev/sda

  # wipe the resized stateful then reboot
  dd if=/dev/zero of=/dev/sda bs=131072 seek=1040 count=47280
  reboot
fi

cd /mnt/stateful_partition/

# try mounting a USB / SD Card if it's there...
if [ ! -d /tmp/usb_files ]
  then
    mkdir /tmp/usb_files
  fi
mount /dev/sdb /tmp/usb_files > /dev/null 2>&1
mount /dev/sdb1 /tmp/usb_files > /dev/null 2>&1

# Download and copy ubuntu root filesystem, keep track of successful parts so we can resume
SEEK=0
FILESIZE=102400
for one in a b
do
  for two in a b c d e f g h i j k l m n o p q r s t u v w x y z
  do
    FILENAME="ubuntu-1104.bin$one$two.bz2"
    if [ ! -f /mnt/stateful_partition/ubuntu-$FILENAME-finished.txt ]
    then
      if [ -f /tmp/usb_files/$FILENAME ]
      then
        echo "Found $FILENAME on flash drive. Using it..."
        get_cmd="cat /tmp/usb_files/$FILENAME"
      else
        echo "\n\nDownloading $FILENAME\n\n"
        get_cmd="wget -O - http://cr-48-ubuntu.googlecode.com/files/$FILENAME"
      fi
      $get_cmd | bunzip2 -c | dd bs=1024 seek=$SEEK of=/dev/sda7
      touch /mnt/stateful_partition/ubuntu-$FILENAME-finished.txt
    fi
    SEEK=$(( $SEEK + $FILESIZE ))
  done
done

#Mount Ubuntu rootfs and copy cgpt + modules over
if [ ! -d /tmp/urfs ]
then
  mkdir /tmp/urfs
fi
mount -t ext4 /dev/sda7 /tmp/urfs
cp /usr/bin/cgpt /tmp/urfs/usr/bin/
chmod a+rx /tmp/urfs/usr/bin/cgpt
cp -ar /lib/modules/* /tmp/urfs/lib/modules/
umount /tmp/urfs

#Copy running Chrome OS kernel to KERN-C partition for Ubuntu's usage
current_rootfs="`rootdev -s`"
current_kernfs="/dev/sda2"
if [ "$current_rootfs" = "/dev/sda5" ]
then
  current_kernfs=/dev/sda4
fi
dd if=$current_kernfs of=/dev/sda6

echo "Setting Ubuntu kernel arguments and resigning kernel partition..."
#echo "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" > /mnt/stateful_partition/foo.6
if [ ! -f /mnt/stateful_partition/foo.6 ]
then
  wget http://cr-48-ubuntu.googlecode.com/files/foo.6
fi

/usr/share/vboot/bin/make_dev_ssd.sh --partitions '6' --set_config /mnt/stateful_partition/foo

#Set Ubuntu partition as top priority for next 2 boots
cgpt add -i 6 -P 5 -T 2 -S 0 /dev/sda
reboot