Created
January 4, 2016 19:38
-
-
Save DennisLfromGA/853c6c4b64bea82dd307 to your computer and use it in GitHub Desktop.
Functions to find and mount Crouton & 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
# Exits the script with return code $1, spitting out message $@ to stderr | |
error() { | |
local ecode="$1" | |
shift | |
echo "$*" 1>&2 | |
return "$ecode" | |
} | |
# Find the root drive | |
# Sets: | |
# - $ROOTDEVICE as the root device (e.g. /dev/sda or /dev/mmcblk0) | |
# - $ROOTDEVICEPREFIX as a prefix for partitions (/dev/sda, /dev/mmcblk0p) | |
findrootdevice() { | |
if hash rootdev 2>/dev/null; then | |
# ROOTDEVICE="`rootdev -s`" | |
ROOTDEVICE="`rootdev -d -s`" | |
elif hash rdev 2>/dev/null; then | |
# grab the root partition and extract the drive id | |
ROOTDEVICE="`rdev|cut -d' ' -f1|sed 's/.$//'`" | |
# elif hash blkid 2>/dev/null; then | |
# # This can be DANGEROUS! | |
# # Just grab the FIRST partition and extract the drive id | |
# ROOTDEVICE=`blkid -o device|head -n1|sed 's/.$//'` | |
else | |
RDEV=$(mountpoint -d /) | |
for file in $(find /dev); do | |
if [ $(stat --printf="%t:%T" "$file") = $RDEV ]; then | |
ROOTPART="$file"; break | |
fi | |
done | |
ROOTDEVICE="`echo "$ROOTPART"|sed 's/.$//'`" | |
fi | |
if [ -z "$ROOTDEVICE" ]; then | |
error 1 "Cannot find root device." | |
fi | |
if [ ! -b "$ROOTDEVICE" ]; then | |
error 1 "$ROOTDEVICE is not a block device." | |
fi | |
# If $ROOTDEVICE ends with a number (e.g. mmcblk0), partitions are named | |
# ${ROOTDEVICE}pX (e.g. mmcblk0p1). If not (e.g. sda), they are named | |
# ${ROOTDEVICE}X (e.g. sda1). | |
ROOTDEVICEPREFIX="$ROOTDEVICE" | |
if [ "${ROOTDEVICE%[0-9]}" != "$ROOTDEVICE" ]; then | |
ROOTDEVICEPREFIX="${ROOTDEVICE}p" | |
fi | |
} | |
# Try to mount the CROUTON partition, if it exists, on $MOUNTCROUTON. | |
mountcrouton() { | |
# Define CROUTON mountpoint | |
# $1 Sets: | |
# - $MOUNTCROUTON as mountpoint for CROUTON partition | |
if [ -n "$1" ]; then | |
MOUNTCROUTON="$1" | |
else | |
MOUNTCROUTON='/var/crouton' | |
fi | |
if [ -z "$ROOTDEVICE" -o -z "$ROOTDEVICEPREFIX" ]; then | |
findrootdevice | |
fi | |
PRIOR=' not' | |
local croutonpart="`sudo cgpt find -n -l CROUTON "$ROOTDEVICE"`" | |
if [ -z "$croutonpart" ]; then | |
error 1 "Warning: CROUTON partition was not found on $ROOTDEVICE." | |
return 1 | |
fi | |
# Check to make sure $croutonpart is greater than 1 | |
if [ ! `sudo cgpt show -i $croutonpart -s $ROOTDEVICE` -gt 1 ]; then | |
error 1 "!!!CROUTON partition $croutonpart is not formatted." | |
return 1 | |
fi | |
# Check if crouton is mounted already | |
if grep -q "^${ROOTDEVICEPREFIX}$croutonpart " /proc/mounts; then | |
# If mounted, it must be mounted to $mountpoint | |
if ! grep -q "^${ROOTDEVICEPREFIX}$croutonpart $MOUNTCROUTON " /proc/mounts; then | |
error 1 "Error: CROUTON partition is not mounted on $MOUNTCROUTON." | |
else | |
PRIOR=' already' | |
fi | |
else | |
sudo mkdir -p "$MOUNTCROUTON" || error 1 "Cannot create $MOUNTCROUTON." | |
sudo mount "${ROOTDEVICEPREFIX}$croutonpart" "$MOUNTCROUTON" || \ | |
error 1 "Cannot mount $MOUNTCROUTON" | |
PRIOR=' now' | |
fi | |
return 0 | |
} | |
# Try to mount the ChrUbuntu partition, if it exists, on $MOUNTCHRUBUNTU. | |
mountchrubuntu() { | |
# Define CHRUBUNTU mountpoint | |
# $1 Sets: | |
# - $MOUNTCHRUBUNTU as mountpoint for CHRUBUNTU partition | |
if [ -n "$1" ]; then | |
MOUNTCHRUBUNTU="$1" | |
else | |
MOUNTCHRUBUNTU='/var/chrubuntu' | |
fi | |
if [ -z "$ROOTDEVICE" -o -z "$ROOTDEVICEPREFIX" ]; then | |
findrootdevice | |
fi | |
PRIOR=' not' | |
local chrubuntupart="`sudo cgpt find -n -l ROOT-C "$ROOTDEVICE"`" | |
if [ -z "$chrubuntupart" ]; then | |
chrubuntupart="`sudo cgpt find -n -l CHRUBUNTU "$ROOTDEVICE"`" | |
fi | |
if [ -z "$chrubuntupart" ]; then | |
error 1 "Warning: CHRUBUNTU partition was not found on $ROOTDEVICE." | |
return 1 | |
fi | |
# Check to make sure $chrubuntupart is greater than 1 | |
if [ ! `sudo cgpt show -i $chrubuntupart -s $ROOTDEVICE` -gt 1 ]; then | |
error 1 "!!!CHRUBUNTU partition $chrubuntupart is not formatted." | |
return 1 | |
fi | |
# Check if chrubuntu is mounted already | |
if grep -q "^${ROOTDEVICEPREFIX}$chrubuntupart " /proc/mounts; then | |
# If mounted, it must be mounted to $mountpoint | |
if ! grep -q "^${ROOTDEVICEPREFIX}$chrubuntupart $MOUNTCHRUBUNTU " /proc/mounts; then | |
error 1 "Error: CHRUBUNTU partition is not mounted on $MOUNTCHRUBUNTU." | |
else | |
PRIOR=' already' | |
fi | |
else | |
sudo mkdir -p "$MOUNTCHRUBUNTU" || error 1 "Cannot create $MOUNTCHRUBUNTU." | |
sudo mount "${ROOTDEVICEPREFIX}$chrubuntupart" "$MOUNTCHRUBUNTU" || \ | |
error 1 "Cannot mount $MOUNTCHRUBUNTU" | |
PRIOR=' now' | |
fi | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment