Skip to content

Instantly share code, notes, and snippets.

@infowolfe
Created March 4, 2015 12:38
Show Gist options
  • Save infowolfe/d72697117c82124cc30b to your computer and use it in GitHub Desktop.
Save infowolfe/d72697117c82124cc30b to your computer and use it in GitHub Desktop.
#!/bin/bash
export DEVICE="${DEVICE:-/dev/sdb1}"
export GENTOO="${GENTOO:-/mnt/gentoo}"
export MKFS="mkfs.xfs"
export URI="http://gentoo.osuosl.org/releases/amd64/autobuilds/current-install-amd64-minimal/stage3-amd64-20141204.tar.bz2"
export portage=1
HELP() {
cat << EOF
Usage: $0 [options]
Will setup/destroy ${GENTOO} for you automatically
Options:
-i init: ${MKFS} ${DEVICE}
-f fresh: unpack tarball from preconfigured URI (implies --init)
-m mount: auto-called from -i, -f
-n noinit: disable --init for --fresh
-U URI: sets the URI to use for curl
-u unmount: umount ${GENTOO} children and ${GENTOO}
EOF
}
[[ $# -eq 0 ]] && HELP
while getopts fhimnuU: opt; do
case "${opt}" in
i) export init=1;;
f) export fresh=1;;
m) export mount=1;;
n) export noinit=1;;
U) echo "note: you must provide other options for -U to take effect"
export URI=${OPTARG};;
u) export umount=1;;
\?|h)
HELP
exit 2;;
esac
done
_fancy_mount() {
if mount | grep ${GENTOO} > /dev/null 2>&1 ; then
mount | grep ${GENTOO} | awk {'printf "%-10s %-20s type: %s\n", $1,$3,$5'}
else
return 1
fi
}
_do_unmount() {
_fancy_mount && (
echo "Unmounting ${DEVICE} from ${GENTOO}"
mount | grep ${GENTOO}/ | awk {'print $3'} | sort -R | while read a; do umount $a || umount -lf $a;done
mount | grep ${GENTOO}/ | awk {'print $3'} | sort -R | while read a; do umount $a || umount -lf $a;done
umount ${GENTOO})
}
_pre_mount() {
mount | grep ${GENTOO} > /dev/null 2>&1 && _do_unmount > /dev/null 2>&1
mount ${DEVICE} ${GENTOO}
}
if [[ "${noinit}" -gt 0 ]]; then
echo "Disabling -i (init)"
export init=0
fi
if [[ "${init}" -gt 0 ]]; then
export mount=1 fresh=1
echo "Running ${MKFS}..."
_do_unmount > /dev/null 2>&1
${MKFS} -f ${DEVICE}
fi
if [[ "${mount}" -gt 0 ]]; then
echo "Mounting ${DEVICE} to ${GENTOO}"
_pre_mount
fi
if [[ "${fresh}" -gt 0 ]]; then
echo "Unpacking ${URI} to ${GENTOO}"
pushd ${GENTOO} > /dev/null
curl ${URI} | pbzip2 -dc | tar x
popd
fi
if [[ "${mount}" -gt 0 ]]; then
echo "Final mounting of ${DEVICE} to ${GENTOO}"
_pre_mount &&
mount -t proc proc ${GENTOO}/proc &&
for i in sys dev; do mount --rbind /${i} ${GENTOO}/${i} ; mount --make-rslave ${GENTOO}/${i};done &&
[[ -h ${GENTOO}/dev/shm ]] && rm ${GENTOO}/dev/shm ; export shm=1
[[ -d ${GENTOO}/dev/shm ]] && mount | grep ${GENTOO}/dev/shm > /dev/null 2>&1 || export shm=1
[[ -e ${GENTOO}/dev/shm ]] || mkdir -p ${GENTOO}/dev/shm && export shm=1
if [[ "${shm}" -gt 0 ]]; then
mount -t tmpfs none ${GENTOO}/dev/shm
fi
if [[ "${portage}" -gt 0 ]]; then
mount -t tmpfs none ${GENTOO}/usr/portage
fi
echo "Mounts:"
_fancy_mount
fi
if [[ "${umount}" -gt 0 ]];then
_do_unmount
fi
unset DEVICE GENTOO URI fresh init mount noinit portage shm umount
# vim: set ft=sh sw=4 ts=4 sts=4 :#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment