| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # make-debirf: script to build debirf system (must be root). |
|---|
| 4 | # |
|---|
| 5 | # The debirf scripts were written by |
|---|
| 6 | # Jameson Rollins <jrollins@fifthhorseman.net> |
|---|
| 7 | # and |
|---|
| 8 | # Daniel Kahn Gillmor <dkg-debian.org@fifthhorseman.net>. |
|---|
| 9 | # |
|---|
| 10 | # They are Copyright 2007, and are all released under the GPL, version 2 |
|---|
| 11 | # or later. |
|---|
| 12 | |
|---|
| 13 | ############################################################### |
|---|
| 14 | ### VARIABLES |
|---|
| 15 | |
|---|
| 16 | CMD=$(basename $0) |
|---|
| 17 | |
|---|
| 18 | DEBIRF_COMMON=${DEBIRF_COMMON:-"/usr/share/debirf/common"} |
|---|
| 19 | source "$DEBIRF_COMMON" |
|---|
| 20 | |
|---|
| 21 | ############################################################### |
|---|
| 22 | ### FUNCTIONS |
|---|
| 23 | |
|---|
| 24 | usage() { |
|---|
| 25 | cat <<EOF |
|---|
| 26 | Usage: $CMD profile kernel-image.deb |
|---|
| 27 | $CMD kernel-image.deb |
|---|
| 28 | EOF |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | failure() { |
|---|
| 32 | echo "$1" >&2 |
|---|
| 33 | exit ${2:-'1'} |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | create_debootstrap() { |
|---|
| 37 | msg "creating debirf root..." |
|---|
| 38 | # include initramfs-tools because they'll be handy |
|---|
| 39 | # exclude aptitude |
|---|
| 40 | mkdir -p "$DEBIRF_ROOT" |
|---|
| 41 | /usr/sbin/debootstrap --exclude=aptitude --include=initramfs-tools "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR" |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | run_plugins() { |
|---|
| 45 | # export all the DEBIRF_* environment variables: |
|---|
| 46 | for var in ${!DEBIRF_*}; do |
|---|
| 47 | export $var |
|---|
| 48 | done |
|---|
| 49 | run-parts --verbose "$DEBIRF_PLUGINS" |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | ## create_initrd functions take the name of the targeted initrd as |
|---|
| 53 | ## their first argument. |
|---|
| 54 | |
|---|
| 55 | # stupid simple method |
|---|
| 56 | create_initrd_stupid_simple() { |
|---|
| 57 | #ln sbin/init to /init |
|---|
| 58 | ln -sf sbin/init "$DEBIRF_ROOT/init" |
|---|
| 59 | |
|---|
| 60 | # create root image |
|---|
| 61 | msg "creating debirf initrd..." |
|---|
| 62 | ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) > "$1" |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | # tweak initrd method: untested, probably doesn't work right yet. soon! |
|---|
| 66 | create_initrd_tweak_initrd() { |
|---|
| 67 | cat "$DEBIRF_ROOT"/scripts/debirf <<EOF |
|---|
| 68 | mountroot() { |
|---|
| 69 | rootmnt="/" |
|---|
| 70 | } |
|---|
| 71 | EOF |
|---|
| 72 | cat > "$DEBIRF_ROOT"/bin/run-init <<EOF |
|---|
| 73 | #!/bin/sh |
|---|
| 74 | if [ "$1" != "/" ] ; then |
|---|
| 75 | exec /usr/lib/klibc/bin/run-init "$@" |
|---|
| 76 | else |
|---|
| 77 | MOUNT_POINT="$1" |
|---|
| 78 | INIT="$2" |
|---|
| 79 | shift 2 |
|---|
| 80 | exec "$INIT" "$@" |
|---|
| 81 | fi |
|---|
| 82 | EOF |
|---|
| 83 | # create root image |
|---|
| 84 | msg "creating debirf initrd..." |
|---|
| 85 | cp initrd.img* "$1" |
|---|
| 86 | (cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) >> "$1" |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | # mount sys, proc |
|---|
| 90 | mount_proc-sys() { |
|---|
| 91 | msg "mounting proc..." |
|---|
| 92 | mount -t proc proc "$DEBIRF_ROOT/proc" |
|---|
| 93 | msg "mounting sys..." |
|---|
| 94 | mount -t sysfs sys "$DEBIRF_ROOT/sys" |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | # umount sys, proc |
|---|
| 98 | umount_proc-sys() { |
|---|
| 99 | [ "$DEBIRF_ROOT" ] || exit |
|---|
| 100 | if mount | grep -q "$DEBIRF_ROOT/proc" ; then |
|---|
| 101 | msg "umounting proc..." |
|---|
| 102 | umount "$DEBIRF_ROOT/proc" |
|---|
| 103 | fi |
|---|
| 104 | if mount | grep -q "$DEBIRF_ROOT/sys" ; then |
|---|
| 105 | msg "umounting sys..." |
|---|
| 106 | umount "$DEBIRF_ROOT/sys" |
|---|
| 107 | fi |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | ############################################################### |
|---|
| 111 | ### MAIN |
|---|
| 112 | |
|---|
| 113 | trap umount_proc-sys EXIT |
|---|
| 114 | |
|---|
| 115 | if [ "$1" = '-h' -o "$1" = '--help' ] ; then |
|---|
| 116 | usage |
|---|
| 117 | exit 0 |
|---|
| 118 | fi |
|---|
| 119 | |
|---|
| 120 | case $# in |
|---|
| 121 | 1) |
|---|
| 122 | DEBIRF_KERNEL_PACKAGE="$1" |
|---|
| 123 | ;; |
|---|
| 124 | 2) |
|---|
| 125 | DEBIRF_PROFILE="$1" |
|---|
| 126 | DEBIRF_KERNEL_PACKAGE="$2" |
|---|
| 127 | ;; |
|---|
| 128 | *) |
|---|
| 129 | echo "Improper number of input arguments." |
|---|
| 130 | usage |
|---|
| 131 | exit 1 |
|---|
| 132 | ;; |
|---|
| 133 | esac |
|---|
| 134 | |
|---|
| 135 | # check specified kernel package exists |
|---|
| 136 | if [ ! -f "$DEBIRF_KERNEL_PACKAGE" ] ; then |
|---|
| 137 | failure "Specified kernel package '$DEBIRF_KERNEL_PACKAGE' not found." |
|---|
| 138 | fi |
|---|
| 139 | |
|---|
| 140 | # check for debirf profile |
|---|
| 141 | if [ -z "$DEBIRF_PROFILE" ] ; then |
|---|
| 142 | echo "Profile not specified." |
|---|
| 143 | read -p "Use default profile ('$DEBIRF_PROFILE_DEFAULT')? [Y|n]: " OK |
|---|
| 144 | if [ -z "$OK" -o "${OK/y/Y}" = 'Y' ] ; then |
|---|
| 145 | DEBIRF_PROFILE="$DEBIRF_PROFILE_DEFAULT" |
|---|
| 146 | fi |
|---|
| 147 | fi |
|---|
| 148 | if [ "$DEBIRF_PROFILE" ] ; then |
|---|
| 149 | if [ ! -d "$DEBIRF_PROFILE" ] ; then |
|---|
| 150 | if [ ! -d "$DEBIRF_PROFILED/$DEBIRF_PROFILE" ] ; then |
|---|
| 151 | failure "Profile '$DEBIRF_PROFILE' not found." |
|---|
| 152 | else |
|---|
| 153 | DEBIRF_PROFILE="$DEBIRF_PROFILED/$DEBIRF_PROFILE" |
|---|
| 154 | fi |
|---|
| 155 | fi |
|---|
| 156 | echo "Using profile '$DEBIRF_PROFILE'." |
|---|
| 157 | DEBIRF_CONF="$DEBIRF_PROFILE/debirf.conf" |
|---|
| 158 | DEBIRF_PLUGINS="$DEBIRF_PROFILE/plugins.d" |
|---|
| 159 | fi |
|---|
| 160 | |
|---|
| 161 | # find debirf.conf |
|---|
| 162 | if [ "$DEBIRF_CONF" ] ; then |
|---|
| 163 | if [ -f "$DEBIRF_CONF" ] ; then |
|---|
| 164 | source "$DEBIRF_CONF" |
|---|
| 165 | else |
|---|
| 166 | failure "Config file '$DEBIRF_CONF' not found." |
|---|
| 167 | fi |
|---|
| 168 | fi |
|---|
| 169 | |
|---|
| 170 | # find plugin directory |
|---|
| 171 | if [ "$DEBIRF_PLUGINS" ] ; then |
|---|
| 172 | if [ ! -d "$DEBIRF_PLUGINS" ] ; then |
|---|
| 173 | failure "Plugin directory '$DEBIRF_PLUGINS' not found." |
|---|
| 174 | fi |
|---|
| 175 | else |
|---|
| 176 | echo "Plugin directory file not specified." |
|---|
| 177 | read -p "Continue without plugins? [y|N]: " OK |
|---|
| 178 | if [ "${OK/y/Y}" != 'Y' ] ; then |
|---|
| 179 | failure "aborting." |
|---|
| 180 | fi |
|---|
| 181 | fi |
|---|
| 182 | |
|---|
| 183 | # echo |
|---|
| 184 | # for var in ${!DEBIRF_*}; do |
|---|
| 185 | # export $var |
|---|
| 186 | # done |
|---|
| 187 | # env | /bin/grep "^DEBIRF_" |
|---|
| 188 | # exit |
|---|
| 189 | |
|---|
| 190 | # create debootstrap root |
|---|
| 191 | if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 192 | echo "Directory $DEBIRF_ROOT already exists." |
|---|
| 193 | echo "Select one of the following:" |
|---|
| 194 | select foo in 'rewrite' 'overwrite' 'skip' 'exit' ; do |
|---|
| 195 | case "$foo" in |
|---|
| 196 | 'rewrite') |
|---|
| 197 | msg "clearing old debirf root..." |
|---|
| 198 | rm -rf "$DEBIRF_ROOT" |
|---|
| 199 | create_debootstrap |
|---|
| 200 | ;; |
|---|
| 201 | 'overwrite') |
|---|
| 202 | create_debootstrap |
|---|
| 203 | ;; |
|---|
| 204 | 'skip') |
|---|
| 205 | ;; |
|---|
| 206 | *) |
|---|
| 207 | failure "aborting." |
|---|
| 208 | ;; |
|---|
| 209 | esac |
|---|
| 210 | break |
|---|
| 211 | done |
|---|
| 212 | fi |
|---|
| 213 | |
|---|
| 214 | setup_debirf_info |
|---|
| 215 | |
|---|
| 216 | if [ "$DEBIRF_PLUGINS" ] ; then |
|---|
| 217 | mount_proc-sys |
|---|
| 218 | |
|---|
| 219 | # update apt |
|---|
| 220 | debirf_exec apt-get update |
|---|
| 221 | |
|---|
| 222 | # install plugins |
|---|
| 223 | msg "installing plugins..." |
|---|
| 224 | run_plugins |
|---|
| 225 | |
|---|
| 226 | umount_proc-sys |
|---|
| 227 | fi |
|---|
| 228 | |
|---|
| 229 | # clear mtab |
|---|
| 230 | > "$DEBIRF_ROOT/etc/mtab" |
|---|
| 231 | # add proc to fstab |
|---|
| 232 | if ! awk '{ print $2 }' < "$DEBIRF_ROOT/etc/fstab" | grep -q '^/proc' ; then |
|---|
| 233 | echo proc /proc proc defaults 0 0 >> "$DEBIRF_ROOT/etc/fstab" |
|---|
| 234 | fi |
|---|
| 235 | |
|---|
| 236 | KERNAVAIL=$(ls -1 "$DEBIRF_ROOT/lib/modules" | head -n1) |
|---|
| 237 | DEBIRF_INITRD="${DEBIRF_LABEL}_${DEBIRF_DISTRO}_${KERNAVAIL}.cgz" |
|---|
| 238 | create_initrd_${DEBIRF_METHOD} "$DEBIRF_BUILDD/$DEBIRF_INITRD" |
|---|
| 239 | |
|---|
| 240 | # final output |
|---|
| 241 | DEBIRF_KERNEL=$(ls "$DEBIRF_BUILDD" | grep "vmlinu" | grep "$KERNAVAIL") |
|---|
| 242 | msg "debirf initrd created." |
|---|
| 243 | if [ "$DEBIRF_BUILDD/$DEBIRF_KERNEL" ] ; then |
|---|
| 244 | msg "kernel: $DEBIRF_BUILDD/$DEBIRF_KERNEL" |
|---|
| 245 | fi |
|---|
| 246 | msg "initrd: $DEBIRF_BUILDD/$DEBIRF_INITRD" |
|---|