2013年7月20日 星期六

How to create a chroot environment for your team



ETA: 20 mins

On-line Version: (view)


Chroot redefines the "ROOT" directory or "/" for a program or login session. Basically, everything outside of the directory you use chroot on doesn't exist as far a program or shell is concerned.


Quick Guide
Step 1: Host Utility Installation


# java (for Jenkins slave connection)
$ sudo vi /etc/apt/sources.list
/etc/apt/sources.list
deb http://archive.canonical.com/ lucid partner
deb http://ftp.debian.org/debian squeeze main contrib non-free


$ sudo apt-get update
$ sudo apt-get install sun-java6-jdk


# repo (for pull the code)
1. download repo
https://code.google.com/p/git-repo/
2. copy repo to /usr/local/bin/repo


# git (for pull the code)
$ sudo apt-get install git-core


$ git config --global user.email "autobuild@local.com"
$ git config --global user.name "autobuild"

Step 2: schroot/chroot configuration
# proxy
$ sudo vi /etc/profile
/etc/profile
export http_proxy=http://1.2.3.4:8080
export https_proxy=https://1.2.3.4:8080

# install schroot & debootstrap
sudo apt-get install schroot debootstrap


# install the ubuntu 10.04
# create a folder for chroot env.
mkdir chroot
sudo http_proxy=http://1.2.3.4:8080 debootstrap --variant=buildd --arch amd64 lucid /home/autobuild/chroot/chroot-amd64-ubuntu10.04/ http://free.nchc.org.tw/ubuntu/


# setup the chroot configuration
/etc/schroot/schroot.conf
[android-lucid]
description=Ubuntu 10.04 64 bit
directory=
/home/autobuild/chroot/chroot-amd64-ubuntu10.04
users=jing
groups=
jing
root-users=jing
root-groups=
root
type=directory


# check the permission in the env
Note: the system files permission should be root.


# test the env
$ schroot -l
android-lucid



# [optional] setup the pseudo-terminal slave/master, related system information for chroot env.
Optional
# Prevent Warning Message: Can not write log, openpty() failed (/dev/pts not mounted?)
# when you apt-get install [package]
sudo mount --bind /dev ~/chroot/chroot-amd64-ubuntu10.04/dev
sudo mount --bind /dev/pts ~/chroot/chroot-amd64-ubuntu10.04/dev/pts
sudo mount --bind /proc ~/chroot/chroot-amd64-ubuntu10.04/proc/
sudo mount --bind /sys ~/chroot/chroot-amd64-ubuntu10.04/sys


Step 3: Guest (chroot env) Utility Installation


# Update time-zone (You are in the host env)
sudo cp /usr/share/zoneinfo/Asia/Taipei
/home/jing/chroot/chroot-amd64-ubuntu10.04/etc/localtime


# install repo (for build script that would like to repo code)
sudo cp /usr/local/bin/repo
/home/jing/chroot/chroot-amd64-ubuntu10.04/usr/local/bin/repo


# Enter chroot environment (for install the utility in chroot env)
$ sudo chroot /home/jing/chroot/chroot-amd64-ubuntu10.04(Please note: sudo chroot. NOT schroot -c xxx)


# [optional] setup locale
Optional
# Prevent Warning Message:
# W: perl: warning: Setting locale failed


# Step 1: Create locale files for en_US, en_US.UTF-8, zh_TW and zh_TW.UTF-8
locale-gen en_US en_US.UTF-8 zh_TW zh_TW.UTF-8


# Step 2: Reconfigures packages after they have already been installed
dpkg-reconfigure locales



# set up proxy for apt-get
export http_proxy=http://1.2.3.4:8080


# add apt source (You are in chroot env)
# Note: If you doesnot add the following source, you will get ia32-lib missing
# error message
/etc/apt/sources.list
deb http://free.nchc.org.tw/ubuntu lucid main
deb http://archive.canonical.com/ lucid partner
deb http://ftp.debian.org/debian squeeze main contrib non-free
or
echo "deb http://free.nchc.org.tw/ubuntu lucid main" >> /etc/apt/sources.list
echo "deb http://archive.canonical.com/ lucid partner" >> /etc/apt/sources.list
echo "deb http://ftp.debian.org/debian squeeze main contrib non-free" >> /etc/apt/sources.list


apt-get update


# java for Android build (you are in chroot env)
# apt-get install sun-java6-jdk


# basic tool-chain (you are in chroot env)
apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev libc6-dev lib32ncurses5-dev ia32-libs x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc


# Now, common platform ready




You are ready to go!
Test
schroot -c sqm-android-lucid -- [your build command]



Optional Utility
  • apt-get install apt-file: check which package provide the software (detail)
  • apt-get install apt-add-repository: use ppa to add the package source list




Test Build on Jenkins
  • build shell:
$ export http_proxy=http://1.2.3.4:8080
$ export https_proxy=https://1.2.3.4:8080
build.sh



How to pack thebuild environment for your team?
# pack the environment:
sudo tar -pczf env.tgz ./chroot/chroot-amd64-ubuntu10.04/


# unpack the environment: using root
sudo tar -xzf env.tgz


Note: Use root privilege to do the pack/unpack procedure otherwise you will get error message:  tar: Exiting with failure status due to previous errors.


Note:
  1. Close all chroot sessions before you pack the environment