Created
December 21, 2015 06:04
-
-
Save DennisLfromGA/42aa4cde1b401801ce1f to your computer and use it in GitHub Desktop.
Chrome OS script to add or delete ChrUbuntu partitions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
BNAME=$(basename $0) | |
USAGE=" | |
Usage: sudo bash $0 [ChrUbuntu size in GB] | |
$BNAME will resize the Chromebook to add or delete ChrUbuntu partitons. | |
$BNAME should be run from VT2 when NOT logged in. | |
" | |
# fw_type will always be developer for Mario. | |
# Alex and ZGB need the developer BIOS installed though. | |
fw_type="`crossystem mainfw_type`" | |
if [ ! "$fw_type" = "developer" ] | |
then | |
echo -e "\nYou're Chromebook is not running a developer BIOS!" | |
echo -e "You need to run:" | |
echo -e "" | |
echo -e "sudo chromeos-firmwareupdate --mode=todev" | |
echo -e "" | |
echo -e "and then re-run this script." | |
exit | |
fi | |
if [ $(id -u) -ne 0 ]; | |
then | |
env echo -e "\nNeed to be root to make changes! Try 'sudo bash ${BNAME}'"; exit 1 | |
fi | |
## powerd_status="`initctl status powerd 2>/dev/null`" | |
## if [ ! "$powerd_status" = "powerd stop/waiting" ] | |
## then | |
## echo -e "\nStopping powerd to keep display from timing out..." | |
## initctl stop powerd 2>/dev/null | |
## fi | |
## setterm -blank 0 2>/dev/null | |
if [ "$1" != "" ]; then | |
chrubuntu_size=$1 | |
echo -e "\nGot Ubuntu size as: ${chrubuntu_size}GB" | |
if [ ! $chrubuntu_size -ne 0 2>/dev/null ] | |
then | |
echo -e "\nNumbers only please..." | |
exit 1 | |
fi | |
else | |
chrubuntu_size="" | |
echo "$USAGE" | |
fi | |
ubuntu_size=$chrubuntu_size | |
target_disk="`rootdev -d -s`" | |
echo -e "Got target drive as: ${target_disk}" | |
disk_size="`blockdev --getsz ${target_disk}`" | |
disk_gb=$(($disk_size/1024/1024/2)) | |
echo "Got target drive size as: ${disk_gb}GB" | |
set -u | |
state_size="`cgpt show -i 1 -n -s -q ${target_disk}`" | |
ckern_size="`cgpt show -i 6 -n -s -q ${target_disk}`" | |
croot_size="`cgpt show -i 7 -n -s -q ${target_disk}`" | |
state_gb=$(($state_size/1024/1024/2)) | |
if [ $ckern_size = 1 ] | |
then ckern_mb=0 | |
else ckern_mb=$(($ckern_size/1024/2)) | |
fi | |
croot_gb=$(($croot_size/1024/1024/2)) | |
if [ "$ckern_size" -gt "1" -o "$croot_size" -gt "1" ] | |
then | |
echo -e "\n** Found existing ChrUbuntu partitons -" | |
echo " KERN-C partition: $ckern_size (${ckern_mb}MB)" | |
echo " ROOT-C partition: $croot_size (${croot_gb}GB)" | |
echo " Stateful partition: $state_size (${state_gb}GB)" | |
fi | |
max_ubuntu_size=$(($state_size/1024/1024/2)) | |
rec_ubuntu_size=$(($max_ubuntu_size - 1)) | |
#echo -e "\nWARNING! All data on this device will be wiped out! Continue at your own risk!" | |
#read -p "Press [Enter] to install or resize ChrUbuntu on ${target_disk} or CTRL+C to quit " | |
# Do partitioning (if we haven't already) | |
# 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" -o "$ubuntu_size" = "" ] | |
then | |
while : | |
do | |
if [ "$chrubuntu_size" -gt 5 2>/dev/null ]; then | |
ubuntu_size=$chrubuntu_size | |
break | |
fi | |
echo -e "\nEnter the size in gigabytes you want to reserve for Ubuntu." | |
echo "Acceptable range is 5 to $max_ubuntu_size but $rec_ubuntu_size is the recommended maximum." | |
read -p "Or you can enter '1' to remove an existing ChrUbuntu partition: " ubuntu_size | |
if [ -z "$ubuntu_size" ] | |
then | |
echo -e "\nYou must enter something..." | |
continue | |
elif [ "$ubuntu_size" = "q" ] | |
then | |
echo -e "\nQuitting..." | |
exit 0 | |
fi | |
if [ ! $ubuntu_size -ne 0 2>/dev/null ] | |
then | |
echo -e "\nNumbers only please..." | |
continue | |
fi | |
if [ $ubuntu_size = 1 -o "$chrubuntu_size" = 1 ] | |
then | |
break | |
fi | |
if [ $ubuntu_size -lt 5 -o $ubuntu_size -gt $max_ubuntu_size ] | |
then | |
echo -e "\nThat number is out of range. Enter a number 5 through $max_ubuntu_size" | |
continue | |
fi | |
break | |
done | |
fi | |
if [ "$ubuntu_size" = 1 ] | |
then | |
echo -e "\n** Restoring Chromebook to pre-ChrUbuntu state...." | |
elif [ "$ubuntu_size" -lt 5 ] | |
then | |
echo -e "\nThe number $ubuntu_size is too small (minimum 5) - it won't work." | |
exit 1 | |
elif [ "$ubuntu_size" -gt $max_ubuntu_size ] | |
then | |
echo -e "\nThe number $ubuntu_size is too big (maximum ${max_ubuntu_size}) - it won't work." | |
exit 1 | |
fi | |
# We've got our size in GB for ROOT-C so do the math... | |
if [ "$ubuntu_size" = 1 -o "$chrubuntu_size" = 1 ] | |
then | |
echo -e "\n** Removing all ChrUbuntu partitons and restoring the stateful_partiton." | |
rootc_size=1 | |
kernc_size=1 | |
#new stateful size with rootc and kernc added to current | |
stateful_size=$(($state_size + $croot_size + $ckern_size)) | |
else | |
#calculate sector size for rootc | |
rootc_size=$(($ubuntu_size*1024*1024*2)) | |
#kernc is always 16mb | |
kernc_size=32768 | |
if [ "$ckern_size" -gt "1" -o "$croot_size" -gt "1" ] | |
then | |
echo -e "\n** Recomputing for existing ChrUbuntu partitons..." | |
#new stateful size with croot and ckern added to current | |
stateful_temp=$(($state_size + $croot_size + $ckern_size)) | |
#new stateful size with rootc and kernc subtracted from computed | |
stateful_size=$(($stateful_temp - $rootc_size - $kernc_size)) | |
else | |
#new stateful size with rootc and kernc subtracted from original | |
stateful_size=$(($state_size - $rootc_size - $kernc_size)) | |
fi | |
fi | |
#start stateful at the same spot it currently starts at | |
stateful_start="`cgpt show -i 1 -n -b -q ${target_disk}`" | |
#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)) | |
stateful_gb=$(($stateful_size/1024/1024/2)) | |
if [ $kernc_size = 1 ] | |
then kernc_mb=0 | |
else kernc_mb=$(($kernc_size/1024/2)) | |
fi | |
rootc_gb=$(($rootc_size/1024/1024/2)) | |
echo -e "\nWARNING! All data on this device will be wiped out! Continue at your own risk!" | |
#Report current and proposed sizes | |
echo "" | |
echo "Current sizes are:" | |
echo " KERN-C partition: $ckern_size (${ckern_mb}MB)" | |
echo " ROOT-C partition: $croot_size (${croot_gb}GB)" | |
echo " Stateful partition: $state_size (${state_gb}GB)" | |
echo "" | |
echo "Proposed sizes are:" | |
echo " KERN-C partition: $kernc_size (${kernc_mb}MB)" | |
echo " ROOT-C partition: $rootc_size (${rootc_gb}GB)" | |
echo " Stateful partition: $stateful_size (${stateful_gb}GB)" | |
echo -e "\n\nIf you're satisfied with the above proposed sizes and are ready to go," | |
read -p "then to install or resize ChrUbuntu on ${target_disk} type: Do It Now " doit | |
if [ ! "$doit" = "Do It Now" ] | |
then | |
echo -e "\nSorry, wrong response (got '${doit}') - quitting." | |
exit 1 | |
fi | |
## echo -e "\nDONE FOR NOW\n" | |
## exit 0 | |
#Do the real work | |
echo -e "\nModifying partition table to accommodate the ChrUbuntu changes.\n" | |
if ! umount -f /mnt/stateful_partition | |
then | |
echo -e "\n** Could not unmount the stateful_partition!" | |
echo "Reboot and run this script again..." | |
exit | |
fi | |
# stateful first | |
cgpt add -i 1 -b $stateful_start -s $stateful_size -l STATE ${target_disk} | |
# now kernc | |
cgpt add -i 6 -b $kernc_start -s $kernc_size -l KERN-C ${target_disk} | |
# finally rootc | |
cgpt add -i 7 -b $rootc_start -s $rootc_size -l ROOT-C ${target_disk} | |
sync | |
blockdev --rereadpt ${target_disk} | |
hdparm -z ${target_disk} | |
#partprobe ${target_disk} | |
partx -u ${target_disk} | |
crossystem dev_boot_usb=1 dev_boot_legacy=1 dev_boot_signed_only=0 | |
echo -e "\n\nThe partitoning on $target_disk has been modified." | |
echo "Your Chromebook will reboot and wipe your data, then your good to go." | |
read -p "Press [Enter] to REBOOT now or CTRL+C to quit and reboot later." | |
reboot | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment