Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / setup-chroot.sh
1 #!/bin/bash
2
3 die() {
4         echo ${@}
5         exit 1
6 }
7
8 usage()
9 {
10         cat << EOF
11 $0: sets up a chroot environment for building the ceph server
12 usage:
13 -h                      Show this message
14
15 -r [install_dir]        location of the root filesystem to install to
16                         example: -r /images/sepia/
17
18 -s [src_dir]            location of the directory with the source code
19                         example: -s ./src/ceph
20 EOF
21 }
22
23 cleanup() {
24         umount -l "${INSTALL_DIR}/mnt/tmp"
25         umount -l "${INSTALL_DIR}/proc"
26         umount -l "${INSTALL_DIR}/sys"
27 }
28
29 INSTALL_DIR=
30 SRC_DIR=
31 while getopts “hr:s:” OPTION; do
32         case $OPTION in
33         h) usage; exit 1 ;;
34         r) INSTALL_DIR=$OPTARG ;;
35         s) SRC_DIR=$OPTARG ;;
36         ?) usage; exit
37         ;;
38         esac
39 done
40
41 [ $EUID -eq 0 ] || die "This script uses chroot, which requires root permissions."
42
43 [ -d "${INSTALL_DIR}" ] || die "No such directory as '${INSTALL_DIR}'. \
44 You must specify an install directory with -r"
45
46 [ -d "${SRC_DIR}" ] || die "no such directory as '${SRC_DIR}'. \
47 You must specify a source directory with -s"
48
49 readlink -f ${SRC_DIR} || die "readlink failed on ${SRC_DIR}"
50 ABS_SRC_DIR=`readlink -f ${SRC_DIR}`
51
52 trap cleanup INT TERM EXIT
53
54 mount --bind "${ABS_SRC_DIR}" "${INSTALL_DIR}/mnt/tmp" || die "bind mount failed"
55 mount -t proc none "${INSTALL_DIR}/proc" || die "mounting proc failed"
56 mount -t sysfs none "${INSTALL_DIR}/sys" || die "mounting sys failed"
57
58 echo "$0: starting chroot."
59 echo "cd /mnt/tmp before building"
60 echo
61 chroot ${INSTALL_DIR} env HOME=/mnt/tmp /bin/bash
62
63 echo "$0: exiting chroot."
64
65 exit 0