Skip to content

Instantly share code, notes, and snippets.

@DennisLfromGA
Last active August 29, 2015 14:04
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/f653eed9ce1f67cf3816 to your computer and use it in GitHub Desktop.
Save DennisLfromGA/f653eed9ce1f67cf3816 to your computer and use it in GitHub Desktop.
A shell script to list the chroot location and chroot name(s) and optionally show the chroot's 'architecture' or complete 'croutonversion' info.
#!/bin/sh -e
# Copyright (c) 2014 The crouton Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
APPLICATION="${0##*/}"
CHROOT_LOC='/usr/local/chroots'
[ -d /var/crouton/chroots ] && CHROOT_LOC='/var/crouton/chroots'
[ ! -d "$CHROOT_LOC" ] && CHROOT_LOC=''
CHROOT_NAME=''
DISPLAY_ALL=''
EACH=''
LIST_ONLY='y'
# VERSION format: date '+%Y%m%d%H%M%S'
VERSION='20140728190944'
VERHIST="\
$APPLICATION-20140728190944 : Tweaked option to list one chroot on the command line
$APPLICATION-20140728140134 : Added Copyright, tweaked option description
$APPLICATION-20140707150953 : Initial version"
###
### Define USAGE
USAGE="USAGE: $ APPLICATION [options] [chroot_name]
A shell script to list the chroot location and chroot name(s)
and optionally show the chroot's 'croutonversion' info.
Options:
Lists the chroot and it's targets given on the command line & exit
-l Lists the chroot(s) in $CHROOT_LOC [default] or given chroot & exit
-L Lists the chroot(s) in $CHROOT_LOC [default] or given chroot along with the 'croutonversion' info. & exit
-s Lists the 'croutonversion' & 'architecture' of all [default] or given chroot
-p CHROOTS Changes the location of your chroot(s) folder - Default: '$CHROOT_LOC'
-h Displays this usage message and exits
-v Version - print version number & exit
-V Version & History - print version number plus version history & exit
"
###
### Exits the script with exit code $1, spitting out message $@ to stderr
error() {
local ecode="$1"
shift
echo "$*" 1>&2
exit "$ecode"
}
###
### Display a help message if requested
if [ "$1" = "--help" ]
then error 0 "$USAGE"; fi
###
### Get command line parameters
while getopts slLp:hvV OPT; do
case $OPT in
s) DISPLAY_ALL='n'; LIST_ONLY='';;
l) LIST_ONLY='y';;
L) DISPLAY_ALL='y'; LIST_ONLY='';;
p) CHROOT_LOC="`readlink -f "$OPTARG"`";;
h) error 0 "$USAGE";;
v) error 0 "${APPLICATION}-${VERSION}" ;;
V) error 0 "${VERHIST}" ;;
\?) error 2 "$USAGE";;
esac
done
shift "$((OPTIND-1))"
###
### Get a chroot_name from the command line
if [ "$#" -gt 0 ]; then CHROOT_NAME="$1"; shift; fi
###
### List chroot's targets if requested
if [ -n "$LIST_ONLY" -a -n "$CHROOT_NAME" ]
then
echo "chroot is in '$CHROOT_LOC': " 1>&2
targetfile="$CHROOT_LOC/$CHROOT_NAME/etc/crouton/targets"
if [ -r "$targetfile" ]; then
echo -n "DEs/targets in '$CHROOT_NAME': " 1>&2
error 0 "`sed 's/^,//' "$targetfile" 2>/dev/null`"
else
error 1 "Sorry, '$targetfile' not found."
fi
fi
###
### List chroots if requested
if [ -n "$LIST_ONLY" ]
then
echo -n "chroot(s) in '$CHROOT_LOC': " 1>&2
error 0 "`ls -Cm "$CHROOT_LOC" 2>/dev/null`"
fi
###
### Get a chroot_name or names
if [ -z "$CHROOT_NAME" ]
then
CHROOT_NAME="`ls "$CHROOT_LOC" 2>/dev/null`"
fi
###
### Display 'croutonversion', 'architecture' and optionally other info.
echo "chroot(s) in '$CHROOT_LOC': " 1>&2
if [ "$DISPLAY_ALL" = 'y' ]
then
for EACH in $CHROOT_NAME
do
if [ ! -d "$CHROOT_LOC/$EACH" ]
then
error 1 "Sorry, no chroot named: $EACH"
continue
fi
(
echo '----------------------------------------------------------------------' 1>&2
echo "chroot name: $EACH" 1>&2
sudo enter-chroot -c $CHROOT_LOC -n $EACH -x /usr/local/bin/croutonversion 2>&1 | grep -v -e ^Unmount -e ^Entering
)
done
elif [ "$DISPLAY_ALL" = 'n' ]
then
for EACH in $CHROOT_NAME
do
if [ ! -d "$CHROOT_LOC/$EACH" ]
then
error 1 "Sorry, no chroot named: $EACH"
continue
fi
echo '----------------------------------------------------------------------' 1>&2
vers="`grep ^VERSION $CHROOT_LOC/$EACH/usr/local/bin/croutonversion|cut -d= -f2 2>&1`"
arch="`grep ^ARCH $CHROOT_LOC/$EACH/usr/local/bin/croutonversion|cut -d= -f2 2>&1`"
if [ -z "$arch" ]; then echo "chroot name: $EACH - version: $vers" 1>&2; fi
if [ -n "$arch" ]; then echo "chroot name: $EACH - version: $vers - architecture: $arch" 1>&2; fi
done
fi
echo '----------------------------------------------------------------------' 1>&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment