| 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_CONF=${DEBIRF_CONF:-"/etc/debirf/debirf.conf"} |
|---|
| 19 | [ -f "$DEBIRF_CONF" ] && source "$DEBIRF_CONF" |
|---|
| 20 | |
|---|
| 21 | ### defaults |
|---|
| 22 | # debirf label |
|---|
| 23 | DEBIRF_LABEL=${DEBIRF_LABEL:-"debirf"} |
|---|
| 24 | # where to build the debirf |
|---|
| 25 | DEBIRF_PATH=${DEBIRF_PATH:-"/var/lib/debirf"} |
|---|
| 26 | # the debirf root, used by plugins |
|---|
| 27 | DEBIRF_ROOT=${DEBIRF_ROOT:-"$DEBIRF_PATH/root"} |
|---|
| 28 | # the plugins directory |
|---|
| 29 | DEBIRF_PLUGINS=${DEBIRF_PLUGINS:-"/usr/share/debirf/plugins"} |
|---|
| 30 | # debirf boot method |
|---|
| 31 | DEBIRF_METHOD=${DEBIRF_METHOD:-"stupid_simple"} |
|---|
| 32 | # Debian mirror |
|---|
| 33 | DEBIRF_MIRROR=${DEBIRF_MIRROR:-"http://mirrors.kernel.org/debian"} |
|---|
| 34 | # what distribution should debirf be built from? |
|---|
| 35 | DEBIRF_DISTRO=${DEBIRF_DISTRO:-"lenny"} |
|---|
| 36 | |
|---|
| 37 | DEBIRF_COMMON="/usr/share/debirf/common" |
|---|
| 38 | |
|---|
| 39 | DEBIRF_INFO_TARGET="${DEBIRF_ROOT}/etc/debirf/debirf.info" |
|---|
| 40 | |
|---|
| 41 | # set locale default |
|---|
| 42 | export LC_CTYPE="C" |
|---|
| 43 | export LANGUAGE="C" |
|---|
| 44 | export LANG="C" |
|---|
| 45 | |
|---|
| 46 | ############################################################### |
|---|
| 47 | ### FUNCTIONS |
|---|
| 48 | |
|---|
| 49 | usage() { |
|---|
| 50 | cat <<EOF |
|---|
| 51 | Usage: $CMD kernel-image.deb |
|---|
| 52 | |
|---|
| 53 | Create a debirf initramfs. |
|---|
| 54 | |
|---|
| 55 | 'man make-debirf' for more information. |
|---|
| 56 | EOF |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | create_debootstrap() { |
|---|
| 60 | msg "creating debirf root..." |
|---|
| 61 | # include initramfs-tools because they'll be handy |
|---|
| 62 | # exclude aptitude |
|---|
| 63 | mkdir -p "$DEBIRF_ROOT" |
|---|
| 64 | /usr/sbin/debootstrap --exclude=aptitude --include=initramfs-tools "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR" |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | run_plugins() { |
|---|
| 68 | # export all the DEBIRF_* environment variables: |
|---|
| 69 | for var in ${!DEBIRF_*}; do |
|---|
| 70 | export $var |
|---|
| 71 | done |
|---|
| 72 | run-parts --verbose "$DEBIRF_PLUGINS" |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | ## create_initrd functions take the name of the targeted initrd as |
|---|
| 76 | ## their first argument. |
|---|
| 77 | |
|---|
| 78 | # stupid simple method |
|---|
| 79 | create_initrd_stupid_simple() { |
|---|
| 80 | #ln sbin/init to /init |
|---|
| 81 | ln -sf sbin/init "$DEBIRF_ROOT/init" |
|---|
| 82 | |
|---|
| 83 | # create root image |
|---|
| 84 | msg "creating debirf initrd..." |
|---|
| 85 | ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) > "$1" |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | # tweak initrd method: untested, probably doesn't work right yet. soon! |
|---|
| 89 | create_initrd_tweak_initrd() { |
|---|
| 90 | cat "$DEBIRF_ROOT"/scripts/debirf <<EOF |
|---|
| 91 | mountroot() { |
|---|
| 92 | rootmnt="/" |
|---|
| 93 | } |
|---|
| 94 | EOF |
|---|
| 95 | cat > "$DEBIRF_ROOT"/bin/run-init <<EOF |
|---|
| 96 | #!/bin/sh |
|---|
| 97 | if [ "$1" != "/" ] ; then |
|---|
| 98 | exec /usr/lib/klibc/bin/run-init "$@" |
|---|
| 99 | else |
|---|
| 100 | MOUNT_POINT="$1" |
|---|
| 101 | INIT="$2" |
|---|
| 102 | shift 2 |
|---|
| 103 | exec "$INIT" "$@" |
|---|
| 104 | fi |
|---|
| 105 | EOF |
|---|
| 106 | # create root image |
|---|
| 107 | msg "creating debirf initrd..." |
|---|
| 108 | cp initrd.img* "$1" |
|---|
| 109 | (cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) >> "$1" |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | # mount sys, proc |
|---|
| 113 | mount_proc-sys() { |
|---|
| 114 | msg "mounting proc..." |
|---|
| 115 | mount -t proc proc "$DEBIRF_ROOT/proc" |
|---|
| 116 | msg "mounting sys..." |
|---|
| 117 | mount -t sysfs sys "$DEBIRF_ROOT/sys" |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | # umount sys, proc |
|---|
| 121 | umount_proc-sys() { |
|---|
| 122 | if mount | grep -q "$DEBIRF_ROOT/proc" ; then |
|---|
| 123 | msg "umounting proc..." |
|---|
| 124 | umount "$DEBIRF_ROOT/proc" |
|---|
| 125 | fi |
|---|
| 126 | if mount | grep -q "$DEBIRF_ROOT/sys" ; then |
|---|
| 127 | msg "umounting sys..." |
|---|
| 128 | umount "$DEBIRF_ROOT/sys" |
|---|
| 129 | fi |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | ############################################################### |
|---|
| 133 | ### MAIN |
|---|
| 134 | |
|---|
| 135 | trap umount_proc-sys EXIT |
|---|
| 136 | |
|---|
| 137 | # check specified kernel package exists, and determine kernel name |
|---|
| 138 | DEBIRF_KERNEL_PACKAGE="$1" |
|---|
| 139 | if [ ! -f "$DEBIRF_KERNEL_PACKAGE" ] ; then |
|---|
| 140 | echo "Specified kernel package $DEBIRF_KERNEL_PACKAGE not found." |
|---|
| 141 | echo |
|---|
| 142 | usage |
|---|
| 143 | exit 1 |
|---|
| 144 | fi |
|---|
| 145 | |
|---|
| 146 | # check for common functions |
|---|
| 147 | [ -f "$DEBIRF_COMMON" ] || failure "debirf function file $DEBIRF_COMMON not found." |
|---|
| 148 | source "$DEBIRF_COMMON" |
|---|
| 149 | |
|---|
| 150 | # check plugin directory exists |
|---|
| 151 | if [ ! -d "$DEBIRF_PLUGINS" ] ; then |
|---|
| 152 | read -p "Plugin directory $DEBIRF_PLUGINS not found. Continue? [y|N]: " -n 1 OK ; [ -z $OK ] || echo |
|---|
| 153 | if [ "${OK/y/Y}" != 'Y' ] ; then |
|---|
| 154 | failure "aborting." |
|---|
| 155 | fi |
|---|
| 156 | fi |
|---|
| 157 | |
|---|
| 158 | # create debootstrap root |
|---|
| 159 | if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 160 | echo "Directory $DEBIRF_ROOT already exists. Select one of the following:" |
|---|
| 161 | select foo in 'rewrite' 'overwrite' 'skip' 'exit' ; do |
|---|
| 162 | case "$foo" in |
|---|
| 163 | 'rewrite') |
|---|
| 164 | msg "clearing old debirf root..." |
|---|
| 165 | rm -rf "$DEBIRF_ROOT" |
|---|
| 166 | create_debootstrap |
|---|
| 167 | ;; |
|---|
| 168 | 'overwrite') |
|---|
| 169 | create_debootstrap |
|---|
| 170 | ;; |
|---|
| 171 | 'skip') |
|---|
| 172 | ;; |
|---|
| 173 | *) |
|---|
| 174 | failure "aborting." |
|---|
| 175 | ;; |
|---|
| 176 | esac |
|---|
| 177 | break |
|---|
| 178 | done |
|---|
| 179 | fi |
|---|
| 180 | |
|---|
| 181 | # if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 182 | # read -p "Directory $DEBIRF_ROOT already exists. write over? [y|N]: " -n 1 OK ; [ -z $OK ] || echo |
|---|
| 183 | # if [ "${OK/y/Y}" = 'Y' ] ; then |
|---|
| 184 | # msg "installing new root..." |
|---|
| 185 | # create_debootstrap |
|---|
| 186 | # fi |
|---|
| 187 | # else |
|---|
| 188 | # msg "installing root..." |
|---|
| 189 | # create_debootstrap |
|---|
| 190 | # fi |
|---|
| 191 | |
|---|
| 192 | setup_debirf_info |
|---|
| 193 | |
|---|
| 194 | mount_proc-sys |
|---|
| 195 | |
|---|
| 196 | # update apt |
|---|
| 197 | debirf_exec apt-get update |
|---|
| 198 | |
|---|
| 199 | # install plugins |
|---|
| 200 | msg "installing plugins..." |
|---|
| 201 | run_plugins |
|---|
| 202 | |
|---|
| 203 | umount_proc-sys |
|---|
| 204 | |
|---|
| 205 | # clear mtab |
|---|
| 206 | > "$DEBIRF_ROOT/etc/mtab" |
|---|
| 207 | # add proc to fstab |
|---|
| 208 | if ! awk '{ print $2 }' < "$DEBIRF_ROOT/etc/fstab" | grep -q '^/proc' ; then |
|---|
| 209 | echo proc /proc proc defaults 0 0 >> "$DEBIRF_ROOT/etc/fstab" |
|---|
| 210 | fi |
|---|
| 211 | |
|---|
| 212 | KERNAVAIL=$(ls -1 "$DEBIRF_ROOT/lib/modules" | head -n1) |
|---|
| 213 | DEBIRF_INITRD="${DEBIRF_LABEL}_${DEBIRF_DISTRO}_${KERNAVAIL}.cgz" |
|---|
| 214 | create_initrd_${DEBIRF_METHOD} "$DEBIRF_PATH/$DEBIRF_INITRD" |
|---|
| 215 | |
|---|
| 216 | # final output |
|---|
| 217 | DEBIRF_KERNEL=$(ls "$DEBIRF_PATH" | grep "vmlinu" | grep "$KERNAVAIL") |
|---|
| 218 | msg "debirf initrd created." |
|---|
| 219 | if [ "$DEBIRF_PATH/$DEBIRF_KERNEL" ] ; then |
|---|
| 220 | msg "kernel: $DEBIRF_PATH/$DEBIRF_KERNEL" |
|---|
| 221 | fi |
|---|
| 222 | msg "initrd: $DEBIRF_PATH/$DEBIRF_INITRD" |
|---|