Skip to content

Commit 5e90bef

Browse files
committed
split of a functions script to make the main script more streamlined, might need to switch to ubuntu base
1 parent 5c89da4 commit 5e90bef

File tree

3 files changed

+155
-54
lines changed

3 files changed

+155
-54
lines changed

builder.sh

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,42 @@
1919
# this is the main script for controlling the build environment and such
2020

2121
source configuration
22+
source functions
2223

23-
if [ 'clean' == "$1" ]; then
24-
echo "Removing the debian chroot"
25-
rm -rf $ROOT_TARGET
26-
rm -rf $DEBOOT_TMP
27-
exit 0;
24+
# figure out what architecture we want for our chroot
25+
# but only if it wasn't manually set in the configuration file
26+
if [ -z "$ROOT_ARCH" ]; then
27+
get_architecture;
28+
echo "Selecting $ROOT_ARCH arcitecture for you";
29+
else
30+
echo "Using $ROOT_ARCH architecture from configuration file";
2831
fi
2932

30-
MACHINE_TYPE=`uname -m`
31-
case "$MACHINE_TYPE" in
32-
i?86)
33-
echo "Selecting i386 architecture"
34-
ARCH=i386
33+
# decide what to do depending on the first command line parameter
34+
case "$1" in
35+
clean)
36+
echo "Cleaning out the deboot, chroot, and kernel directories"
37+
clean_directories
38+
exit 0
3539
;;
36-
x86_64)
37-
echo "Selecting amd64 architecture"
38-
ARCH=amd64
39-
;;
40-
*)
41-
echo "Your machine is not supported by this script"
42-
exit 1
40+
setup)
41+
echo "Creating the deboot, chroot, and kernel directories"
42+
create_directories
43+
echo "Setting up deboot"
44+
get_deboot
45+
echo "Patching deboot"
46+
patch_deboot
47+
echo "Creating the chroot build environment"
48+
deboot_chroot
4349
;;
4450
esac
45-
46-
mkdir -p $DEBOOT_TMP
47-
cd $DEBOOT_TMP
48-
wget -c $DEBOOT_SRC/$DEBOOT_DEB
49-
ar -x $DEBOOT_DEB
50-
tar -xf data.tar.xz
51-
printf '%s' "$MOUNT_PATCH"|patch --ignore-whitespace --verbose usr/share/debootstrap/functions
51+
exit 0
5252

5353
cd $BASE_DIR
5454
mkdir -p $ROOT_TARGET
55-
DEBOOTSTRAP_DIR=$BASE_DIR/$DEBOOT_TMP/usr/share/debootstrap
55+
DEBOOTSTRAP_DIR=$BASE_DIR/$DEBOOT_DIR/usr/share/debootstrap
5656
export DEBOOTSTRAP_DIR
57-
$BASE_DIR/$DEBOOT_TMP/usr/sbin/debootstrap --arch=$ARCH --include=$REQUIRED_PACKAGES --no-check-gpg $ROOT_SUITE $BASE_DIR/$ROOT_TARGET $ROOT_SOURCE
57+
$BASE_DIR/$DEBOOT_DIR/usr/sbin/debootstrap --arch=$ROOT_ARCH --include=$REQUIRED_PACKAGES --no-check-gpg $ROOT_SUITE $BASE_DIR/$ROOT_TARGET $ROOT_SOURCE
5858

5959
echo Unmounting proc from chroot
6060
umount $BASE_DIR/$ROOT_TARGET/proc

configuration

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# this file contains the configuration variables for the main script
2-
# vim: noai:ts=4:sw=4
2+
# vim: noai:ts=4:sw=4:syn=sh
33

44
# Copyright 2013 Jon Allen ([email protected])
55
#
@@ -19,40 +19,27 @@
1919
# this is a shell script meant to be sourced by builder.sh to provide
2020
# configuration options for the package
2121

22-
# basically our working directory
22+
# basically our working directory you can change this
23+
# if you want the script to dump everything in a different
24+
# location from where the script is run
2325
BASE_DIR=`pwd`
2426

25-
# target and sources for our chroor
27+
# target and sources for our chroot
2628
ROOT_TARGET=build_environment
2729
ROOT_SUITE=wheezy
2830
ROOT_SOURCE=http://http.debian.net/debian
29-
REQUIRED_PACKAGES=gcc-arm-linux-gnueabihf,ccache,git,kernel-wedge,libncursesw5-dev
31+
# these packages will be installed into the chroot build environment
32+
# and are needed to build the kernel
33+
REQUIRED_PACKAGES=gcc-arm-linux-gnueabihf,ccache,kernel-wedge,libncursesw5-dev
34+
35+
# the script should be able to figure this out, but you can override
36+
# or set it manually here, leaving it blank is fine
37+
ROOT_ARCH=
3038

3139
# debootstrap sources and working directory
3240
DEBOOT_DEB=debootstrap_1.0.55_all.deb
3341
DEBOOT_SRC=http://ftp.debian.org/debian/pool/main/d/debootstrap
34-
DEBOOT_TMP=deboot_working
35-
36-
# this patch from http://blog.tsunanet.net/2013/01/using-debootstrap-with-grsec.html?m=1
37-
# it fixed deboot throwing an error when it tries to mount the proc system
38-
MOUNT_PATCH="--- usr/share/debootstrap/functions.orig 2013-11-26 07:15:53.909242727 -0600
39-
+++ usr/share/debootstrap/functions 2013-11-26 07:17:39.464665969 -0600
40-
@@ -998,12 +998,14 @@
41-
umount_on_exit /proc/bus/usb
42-
umount_on_exit /proc
43-
umount "'"'"\$TARGET/proc"'"'" 2>/dev/null || true
44-
- in_target mount -t proc proc /proc
45-
+# in_target mount -t proc proc /proc
46-
+ sudo mount -o bind /proc "'"'"\$TARGET/proc"'"'"
47-
if [ -d "'"'"\$TARGET/sys"'"'" ] && \\
48-
grep -q '[[:space:]]sysfs' /proc/filesystems 2>/dev/null; then
49-
umount_on_exit /sys
50-
umount "'"'"\$TARGET/sys"'"'" 2>/dev/null || true
51-
- in_target mount -t sysfs sysfs /sys
52-
+# in_target mount -t sysfs sysfs /sys
53-
+ sudo mount -o bin /sys "'"'"\$TARGET/sys"'"'"
54-
fi
55-
on_exit clear_mtab
56-
;;
57-
"
42+
DEBOOT_DIR=deboot_working
5843

44+
# directory where the kernel source tree is kept
45+
KERNEL_DIR=kernel_source

functions

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
# vim: noai:ts=4:sw=4:syn=sh
3+
4+
# Copyright 2013 Jon Allen ([email protected])
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# functions
19+
# this is the script that builder.sh sources to do all the dirty work
20+
21+
# this function simply deletes everything
22+
clean_directories ()
23+
{
24+
rm -rf $BASE_DIR/$ROOT_TARGET;
25+
rm -rf $BASE_DIR/$DEBOOT_DIR;
26+
rm -rf $BASE_DIR/$KERNEL_DIR;
27+
}
28+
# just make the directories we'll need
29+
create_directories ()
30+
{
31+
mkdir -p $BASE_DIR/$DEBOOT_DIR
32+
mkdir -p $BASE_DIR/$ROOT_TARGET
33+
mkdir -p $BASE_DIR/$KERNEL_DIR
34+
}
35+
36+
# this function gets the debootstrap package and unpacks it into the appropriate directory, all ready to be used
37+
get_deboot ()
38+
{
39+
cd $BASE_DIR/$DEBOOT_DIR;
40+
wget -c $DEBOOT_SRC/$DEBOOT_DEB;
41+
ar -x $DEBOOT_DEB;
42+
tar -xf data.tar.xz;
43+
cd $BASE_DIR;
44+
}
45+
46+
# this function just creates the chroot environment
47+
deboot_chroot ()
48+
{
49+
# do the bootstrap
50+
DEBOOTSTRAP_DIR=$BASE_DIR/$DEBOOT_DIR/usr/share/debootstrap $BASE_DIR/$DEBOOT_DIR/usr/sbin/debootstrap --arch=$ROOT_ARCH --include=$REQUIRED_PACKAGES --no-check-gpg $ROOT_SUITE $BASE_DIR/$ROOT_TARGET $ROOT_SOURCE;
51+
52+
#deboot feels the need to mount proc, we don't need it here
53+
umount $BASE_DIR/$ROOT_TARGET/proc;
54+
55+
#stop dpkg from running daemons
56+
cat > $BASE_DIR/$ROOT_TARGET/usr/sbin/policy-rc.d <<EOF
57+
#!/bin/sh
58+
exit 101
59+
EOF
60+
chmod a+x $BASE_DIR/$ROOT_TARGET/usr/sbin/policy-rc.d;
61+
62+
#divert ischroot
63+
#note that this throws error, my need to be fixed, not sure
64+
chroot $BASE_DIR/$ROOT_TARGET dpkg-divert --divert /usr/bin/ischroot.debianutils --rename /usr/bin/ischroot;
65+
chroot $BASE_DIR/$ROOT_TARGET /bin/ln -s /bin/true /usr/bin/ischroot;
66+
}
67+
68+
# this function gets the architecture for our chroot
69+
# currently only ix86 32 and 64 bit targets are supported
70+
# but I'll eventually get around to adding more
71+
get_architecture ()
72+
{
73+
case `uname -m` in
74+
i?86)
75+
ROOT_ARCH=i386
76+
;;
77+
x86_64)
78+
ROOT_ARCH=amd64
79+
;;
80+
*)
81+
echo "I don't know what kind of architecture the chroot needs to be"
82+
exit 1
83+
;;
84+
esac;
85+
}
86+
87+
# deboot comes out of the box with an offensive mount command.
88+
# this patches it to fix the error when mounting the proc file system
89+
# this patch from http://blog.tsunanet.net/2013/01/using-debootstrap-with-grsec.html?m=1
90+
patch_deboot ()
91+
{
92+
local MOUNT_PATCH="--- usr/share/debootstrap/functions.orig 2013-11-26 07:15:53.909242727 -0600
93+
+++ usr/share/debootstrap/functions 2013-11-26 07:17:39.464665969 -0600
94+
@@ -998,12 +998,14 @@
95+
umount_on_exit /proc/bus/usb
96+
umount_on_exit /proc
97+
umount "'"'"\$TARGET/proc"'"'" 2>/dev/null || true
98+
- in_target mount -t proc proc /proc
99+
+# in_target mount -t proc proc /proc
100+
+ sudo mount -o bind /proc "'"'"\$TARGET/proc"'"'"
101+
if [ -d "'"'"\$TARGET/sys"'"'" ] && \\
102+
grep -q '[[:space:]]sysfs' /proc/filesystems 2>/dev/null; then
103+
umount_on_exit /sys
104+
umount "'"'"\$TARGET/sys"'"'" 2>/dev/null || true
105+
- in_target mount -t sysfs sysfs /sys
106+
+# in_target mount -t sysfs sysfs /sys
107+
+ sudo mount -o bin /sys "'"'"\$TARGET/sys"'"'"
108+
fi
109+
on_exit clear_mtab
110+
;;
111+
";
112+
113+
printf '%s' "$MOUNT_PATCH"|patch --ignore-whitespace --verbose $BASE_DIR/$DEBOOT_DIR/usr/share/debootstrap/functions;
114+
}

0 commit comments

Comments
 (0)