|
| 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