Skip to content
Snippets Groups Projects
Commit da2af1bb authored by Eduardo L. Buratti's avatar Eduardo L. Buratti
Browse files

build: Add build environment main script

parent 8e5f628a
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
set -o errexit
if [ $EUID -ne 0 ]; then
echo "This script must be run as root!" 1>&2
exit 1
fi
###########
## UTILS ##
###########
program=`basename $0`
function confirm() {
while true; do
read -p "$1" yn
# if user didnt input anything, overwrite with default ($2)
case ${yn:-"$2"} in
[Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
esac
done
}
function usage() {
echo "Usage of ${program}"
exit 1
}
function fatal() {
echo "${program}: ${1:-"unkown fatal error"}" >&2
exit 1
}
function terminate() {
echo
echo "Signal caught, terminating..."
bootstrap_umount
exit 1
}
function warn_mounted() {
echo
echo "The image is mounted. Remember to run '${program} umount' when you finished your work."
}
function is_mounted() {
grep -qs `readlink -f "${img_mount}"` /proc/mounts && return 0
return 1
}
function mount_image() {
if ! is_mounted; then
mkdir -p "${img_mount}"
mount -o loop "${img_file}" "${img_mount}"
if [ ! -d "${build_home}" ]; then
mkdir -p "${build_home}"
chown "${orig_uid}:${orig_gid}" "${build_home}"
fi
[ -d "${img_mount}/home/user/build" ] && mount -o bind "${build_home}" \
"${img_mount}/home/user/build"
mkdir -p "${img_mount}/dev" && mount -o bind /dev "${img_mount}/dev"
mkdir -p "${img_mount}/proc" && mount -t proc -o nosuid,nodev,noexec proc "${img_mount}/proc"
mkdir -p "${img_mount}/dev/pts" && mount -t devpts -o nosuid,noexec,gid=5,mode=620 devpts "${img_mount}/dev/pts"
mkdir -p "${img_mount}/sys" && mount -t sysfs -o nosuid,nodev,noexec sysfs "${img_mount}/sys"
mkdir -p "${img_mount}/run" && mount -t tmpfs -o nosuid,nodev,mode=755 tmpfs "${img_mount}/run"
mkdir -p "${img_mount}/tmp" && mount -t tmpfs -o nodev,nosuid tmpfs "${img_mount}/tmp"
mkdir -p "${img_mount}/etc"
cp "${img_mount}/proc/mounts" "${img_mount}/etc/mtab"
cp /etc/hosts "${img_mount}/etc/hosts"
cp /etc/resolv.conf "${img_mount}/etc/resolv.conf"
fi
}
function umount_image() {
if is_mounted; then
set +e
[ -d "${img_mount}/home/user/build" ] && umount "${img_mount}/home/user/build"
[ -d "${img_mount}/tmp" ] && umount "${img_mount}/tmp"
[ -d "${img_mount}/run" ] && umount "${img_mount}/run"
[ -d "${img_mount}/sys" ] && umount "${img_mount}/sys"
[ -d "${img_mount}/dev/pts" ] && umount "${img_mount}/dev/pts"
[ -d "${img_mount}/proc" ] && umount "${img_mount}/proc"
[ -d "${img_mount}/dev" ] && umount "${img_mount}/dev"
set -e
umount ${img_mount}
fi
}
########################
## BOOTSTRAP COMMANDS ##
########################
function bootstrap_help() {
echo "HELP"
}
function bootstrap_mount() {
mount_image
warn_mounted
}
function bootstrap_umount() {
umount_image
}
function bootstrap_create() {
local fromscratch=1
if [ -f "${img_file}" ]; then
confirm "Image file ${img_file} already exists, overwrite? [y/N] " "n" || return
if [ "$(stat -c%s ${img_file})" -eq "$((img_size*1048576))" ]; then
fromscratch=0
fi
fi
if is_mounted; then
confirm "The image is mounted, unmount? [Y/n] " "y" || return
umount_image
fi
if [ $fromscratch -eq 1 ]; then
echo "Creating empty image file..."
dd if=/dev/zero of="${img_file}" bs=1M count="${img_size}" 2>/dev/null
fi
echo "Create filesystem (ext2)..."
mke2fs -Fq "${img_file}"
echo "Mounting..."
mkdir -p "${img_mount}"
mount -o loop "${img_file}" "${img_mount}"
echo "Installing base Debian packages..."
debootstrap --variant=buildd --arch=i386 squeeze "${img_mount}" "${mirror_url}"
echo "Remounting..."
umount "${img_mount}"
mount_image
echo "Creating user..."
if [ -z "${orig_uid}" ]; then
orig_uid=1000
read -p "Not a suitable user, please enter your user ID [1000]: " orig_uid
fi
if confirm "Create user with UID=${orig_uid}? [Y/n]" "y"; then
chroot "${img_mount}" /usr/sbin/groupadd --force usergroup
chroot "${img_mount}" /usr/sbin/useradd --home-dir /home/user --no-create-home \
--gid usergroup --uid "${orig_uid}" --shell /bin/bash user
mkdir -p "${img_mount}/home/user/build"
chroot "${img_mount}" /bin/chown --recursive "${orig_uid}:usergroup" /home/user
fi
echo "Unmounting..."
umount_image
echo
echo "Image created. Mount it with '${program} mount'."
}
function bootstrap_update() {
echo "NOTE: You should NOT update any package in order to build the binaries."
echo " Updating packages defeat the purpouse of the bootstrap-build system."
confirm "Do you really want to update? [y/N] " "n" || return
automount=false
if ! is_mounted; then
cofirm "The image is not mounted. Mount it? [Y/n] " "y" || return
automount=true
mount_image
fi
chroot "${img_mount}" /usr/bin/apt-get update
chroot "${img_mount}" /usr/bin/apt-get upgrade
$automount && warn_mounted
}
function bootstrap_shell() {
automount=false
if ! is_mounted; then
confirm "The image is not mounted. Mount it? [Y/n] " "y" || return
automount=true
mount_image
fi
chroot "${img_mount}" /bin/bash -l
$automount && warn_mounted
}
##########
## MAIN ##
##########
# default values fot parameters
build_home=build
img_size=1024
mirror_url="http://debian.c3sl.ufpr.br/debian"
no_confirm=0
passwd=$(getent passwd `logname`)
orig_uid=$(echo $passwd | awk -F':' '{print $3}')
orig_gid=$(echo $passwd | awk -F':' '{print $4}')
trap "terminate" SIGHUP SIGINT SIGTERM
long_opts="mirror:,img-size:,build-path:,noconfirm,help"
short_opts="m:h"
set -- `getopt --name $0 --unquoted --longoptions="${long_opts}" "${short_opts}" "$@"` || usage
[ $# -eq 0 ] && usage
while [ $# -gt 0 ]
do
case "$1" in
--mirror|-m) mirror_url="$2"; shift ;;
--img-size) img_size="$2"; shift ;;
--build-home) build_home="$2"; shift ;;
--noconfirm) no_confirm=1; shift ;;
--help|-h) bootstrap_help; exit 0; ;;
--) shift; break ;;
-*) usage ;;
*) break ;;
esac
shift
done
img_file="proinfodata-bootstrap.img"
img_mount=".root"
if [ $# -lt 1 ]; then
echo "No command specified." >&2
bootstrap_help
exit 1
fi
cmd=$1
shift
case $cmd in
help) bootstrap_help $* ;;
create) bootstrap_create $* ;;
mount) bootstrap_mount $* ;;
umount) bootstrap_umount $* ;;
update) bootstrap_update $* ;;
shell) bootstrap_shell $* ;;
*)
echo "Invalid command specified: '$cmd'" >&2
exit 1
;;
esac
exit 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment