Skip to content

Instantly share code, notes, and snippets.

@DennisLfromGA
Created October 31, 2017 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennisLfromGA/44f641bfb99d293c6dd3c5494c2b4121 to your computer and use it in GitHub Desktop.
Save DennisLfromGA/44f641bfb99d293c6dd3c5494c2b4121 to your computer and use it in GitHub Desktop.
chrx-setup-storage [chrx]
#!/bin/bash
#
# setup_storage
#
# This script is _sourced_ from install-chrx (not exec'ed),
# so all functions and variables from there are available.
#
# It cannot be run directly from the commandline.
#
eval_try umount -R ${CHRX_INSTALL_ROOT} 2>/dev/null ## be safe.
if [ "${CHRX_TARGET_DISK}" ]; then
echo_info "Installing to target disk ${ANSI_WHT}${CHRX_TARGET_DISK}${ANSI_RST}"
cat <<- EOF
${ANSI_YEL}
WARNING: All data on ${CHRX_TARGET_DISK} will be erased!
Continue at your own risk!
${ANSI_RST}
EOF
echo_warn "Press [enter] to continue or CTRL-C to quit: \c"
prompt_if_interactive
echo_info "Setting up target disk ${CHRX_TARGET_DISK}..."
CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}1"
if [[ "${CHRX_TARGET_DISK}" =~ "mmcblk" ]]; then
CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}p1"
fi
#dd if=/dev/zero of="${CHRX_ROOT_PARTITION}" bs=512 count=1
#printf ",,L,*\n" | sfdisk "${CHRX_TARGET_DISK}"
printf "g\nn\n\n\n\nw\n" | fdisk "${CHRX_TARGET_DISK}" > /dev/null
fdisk -l "${CHRX_TARGET_DISK}"
#echo debug ; read line
#blockdev --rereadpt "${CHRX_TARGET_DISK}"
echo_info "OK"
else
CHRX_TARGET_DISK="`rootdev -d -s`"
echo_info "Setting up target disk ${CHRX_TARGET_DISK}..."
# Do partitioning (if we haven't already)
ckern_size="`cgpt show -i 6 -n -s -q ${CHRX_TARGET_DISK}`"
croot_size="`cgpt show -i 7 -n -s -q ${CHRX_TARGET_DISK}`"
state_size="`cgpt show -i 1 -n -s -q ${CHRX_TARGET_DISK}`"
max_os_size=$((${state_size}/1024/1024/2))
rec_os_size=$((${max_os_size} - 1))
# 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
cat <<- EOF
How much disk space should we reserve for Linux?
Choose between 5 and ${max_os_size} GB (recommended: ${rec_os_size}).
EOF
echo_info "Enter size in GB [${rec_os_size}]: \c"
prompt_if_interactive
os_size=${PROMPT_RESPONSE}
[ -z "${os_size}" ] && os_size=${rec_os_size}
os_size_test=`echo ${os_size} | grep "^[0-9][0-9]*$"`
if [ -z "${os_size_test}" ]; then
echo_fail "\nNumbers only please!"
continue
fi
if [ ${os_size} -lt 5 -o ${os_size} -gt ${max_os_size} ]; then
echo_fail "\nPlease enter a number between 5 and ${max_os_size}!"
continue
fi
break
done
# We've got our size in GB for ROOT-C so do the math...
rootc_size=$((${os_size}*1024*1024*2)) # sector size for rootc
kernc_size=32768 # kernc is always 16MB
#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 ${CHRX_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}))
echo " stateful_start ${stateful_start}"
echo " stateful_size ${stateful_size}"
echo " kernc_start ${kernc_start}"
echo " kernc_size ${kernc_size}"
echo " rootc_start ${rootc_start}"
echo " rootc_size ${rootc_size}"
## Do the real work
echo_info "\n\nModifying partition table to make room for Linux."
umount -f /mnt/stateful_partition > /dev/null 2>&1
cgpt add -i 1 -b ${stateful_start} -s ${stateful_size} -l STATE ${CHRX_TARGET_DISK}
cgpt add -i 6 -b ${kernc_start} -s ${kernc_size} -l KERN-C -t "kernel" ${CHRX_TARGET_DISK}
cgpt add -i 7 -b ${rootc_start} -s ${rootc_size} -l ROOT-C ${CHRX_TARGET_DISK}
cat <<- EOF
${ANSI_GRN}
Your Chromebook will reboot and clear your data.
Run chrx again to install Linux.
${ANSI_RST}
EOF
echo_warn "Press [enter] to continue: \c"
prompt_if_interactive
## reboot without dropping user to shell
reboot
sleep 10
exit
fi
fi
## set targets
if [ -z "$CHRX_ROOT_PARTITION" ]; then
CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}7"
if [[ "${CHRX_TARGET_DISK}" =~ "mmcblk" ]]; then
CHRX_ROOT_PARTITION="${CHRX_TARGET_DISK}p7"
fi
fi
echo_info "\nInstalling to ${CHRX_ROOT_PARTITION}..."
eval_try umount -R ${CHRX_ROOT_PARTITION} 2>/dev/null
eval_try umount -R ${CHRX_INSTALL_ROOT} 2>/dev/null
if [ "`mount | grep ${CHRX_ROOT_PARTITION}`" ]; then
echo_fail "Install target ${CHRX_ROOT_PARTITION} is already mounted!\n"
echo_warn "Unmount manually (sudo umount -R ${CHRX_INSTALL_ROOT}) or reboot"
echo_warn "to try again."
exit 1
fi
eval_crit mkfs.ext4 ${VERBOSITY_MKFS} ${CHRX_ROOT_PARTITION}
echo_info "Preparing installation root directory ${CHRX_INSTALL_ROOT}"
rm -rf ${CHRX_INSTALL_ROOT}
mkdir -p ${CHRX_INSTALL_ROOT}
if [ ! -d "${CHRX_INSTALL_ROOT}" ]; then
echo_fail "fatal: Unable to create installation root directory!"
exit 1
fi
eval_crit mount -t ext4 ${CHRX_ROOT_PARTITION} ${CHRX_INSTALL_ROOT}
## clear error variable, which is checked in the calling script
SETUP_STORAGE_ERROR=""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment