| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # DEBIRF creation script. You must pass a kernel .deb as the first argument. |
|---|
| 4 | |
|---|
| 5 | ############################################################### |
|---|
| 6 | ### VARIABLES |
|---|
| 7 | |
|---|
| 8 | CMD=$(basename $0) |
|---|
| 9 | |
|---|
| 10 | DEBIRF_CONF=${DEBIRF_CONF:-"/etc/debirf/debirf.conf"} |
|---|
| 11 | [ -f "$DEBIRF_CONF" ] && source "$DEBIRF_CONF" |
|---|
| 12 | |
|---|
| 13 | ### defaults |
|---|
| 14 | # debirf label |
|---|
| 15 | DEBIRF_LABEL=${DEBIRF_LABEL:-"debirf"} |
|---|
| 16 | # where to build the debirf |
|---|
| 17 | DEBIRF_PATH=${DEBIRF_PATH:-"/var/lib/debirf"} |
|---|
| 18 | # the debirf root, used by plugins |
|---|
| 19 | DEBIRF_ROOT=${DEBIRF_ROOT:-"$DEBIRF_PATH/root"} |
|---|
| 20 | # the plugins directory |
|---|
| 21 | DEBIRF_PLUGINS=${DEBIRF_PLUGINS:-"/usr/share/debirf/plugins"} |
|---|
| 22 | # debirf boot method |
|---|
| 23 | DEBIRF_METHOD=${DEBIRF_METHOD:-"stupid_simple"} |
|---|
| 24 | # Debian mirror |
|---|
| 25 | DEBIRF_MIRROR=${DEBIRF_MIRROR:-"http://mirrors.kernel.org/debian"} |
|---|
| 26 | # what distribution should debirf be built from? |
|---|
| 27 | DEBIRF_DISTRO=${DEBIRF_DISTRO:-"etch"} |
|---|
| 28 | |
|---|
| 29 | DEBIRF_COMMON="/usr/share/debirf/common" |
|---|
| 30 | |
|---|
| 31 | # set locale default |
|---|
| 32 | export LC_CTYPE="C" |
|---|
| 33 | export LANGUAGE="C" |
|---|
| 34 | export LANG="C" |
|---|
| 35 | |
|---|
| 36 | ############################################################### |
|---|
| 37 | ### FUNCTIONS |
|---|
| 38 | |
|---|
| 39 | usage() { |
|---|
| 40 | cat <<EOF |
|---|
| 41 | Usage: $CMD kernel-image |
|---|
| 42 | |
|---|
| 43 | Creat a debirf initramfs. |
|---|
| 44 | |
|---|
| 45 | Default config file: /etc/debirf/debirf.conf |
|---|
| 46 | All config variables are bash shell variables. |
|---|
| 47 | Specify alternate config file with "DEBIRF_CONF=". |
|---|
| 48 | EOF |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | create_debootstrap() { |
|---|
| 52 | # include initramfs-tools because they'll be handy |
|---|
| 53 | # exclude aptitude |
|---|
| 54 | mkdir -p "$DEBIRF_ROOT" |
|---|
| 55 | /usr/sbin/debootstrap --exclude=aptitude --include=initramfs-tools "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR" |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | run_plugins() { |
|---|
| 59 | # export all the DEBIRF_* environment variables: |
|---|
| 60 | for var in ${!DEBIRF_*}; do |
|---|
| 61 | export $var |
|---|
| 62 | done |
|---|
| 63 | run-parts --verbose "$DEBIRF_PLUGINS" |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | ## create_initrd functions take the name of the targeted initrd as |
|---|
| 67 | ## their first argument. |
|---|
| 68 | |
|---|
| 69 | ### stupid simple method |
|---|
| 70 | create_initrd_stupid_simple() { |
|---|
| 71 | #ln sbin/init to /init |
|---|
| 72 | ln -sf sbin/init "$DEBIRF_ROOT/init" |
|---|
| 73 | |
|---|
| 74 | # create root image |
|---|
| 75 | msg "creating debirf initrd..." |
|---|
| 76 | ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) > "$1" |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | ### tweak initrd method: untested, probably doesn't work right yet. soon! |
|---|
| 80 | create_initrd_tweak_initrd() { |
|---|
| 81 | cat "$DEBIRF_ROOT"/scripts/debirf <<EOF |
|---|
| 82 | mountroot() { |
|---|
| 83 | rootmnt="/" |
|---|
| 84 | } |
|---|
| 85 | EOF |
|---|
| 86 | cat > "$DEBIRF_ROOT"/bin/run-init <<EOF |
|---|
| 87 | #!/bin/sh |
|---|
| 88 | if [ "$1" != "/" ] ; then |
|---|
| 89 | exec /usr/lib/klibc/bin/run-init "$@" |
|---|
| 90 | else |
|---|
| 91 | MOUNT_POINT="$1" |
|---|
| 92 | INIT="$2" |
|---|
| 93 | shift 2 |
|---|
| 94 | exec "$INIT" "$@" |
|---|
| 95 | fi |
|---|
| 96 | EOF |
|---|
| 97 | # create root image |
|---|
| 98 | msg "creating debirf initrd..." |
|---|
| 99 | cp initrd.img* "$1" |
|---|
| 100 | (cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) >> "$1" |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | ############################################################### |
|---|
| 104 | ### MAIN |
|---|
| 105 | |
|---|
| 106 | # check specified kernel package exists, and determine kernel name |
|---|
| 107 | DEBIRF_KERNEL_PACKAGE="$1" |
|---|
| 108 | if [ ! -f "$DEBIRF_KERNEL_PACKAGE" ] ; then |
|---|
| 109 | echo "Specified kernel package $DEBIRF_KERNEL_PACKAGE not found." |
|---|
| 110 | echo |
|---|
| 111 | usage |
|---|
| 112 | exit 1 |
|---|
| 113 | fi |
|---|
| 114 | |
|---|
| 115 | # check for common functions |
|---|
| 116 | [ -f "$DEBIRF_COMMON" ] || debirf_fail "debirf function file $DEBIRF_COMMON not found." |
|---|
| 117 | source "$DEBIRF_COMMON" |
|---|
| 118 | |
|---|
| 119 | # check plugin directory exists |
|---|
| 120 | if [ ! -d "$DEBIRF_PLUGINS" ] ; then |
|---|
| 121 | read -p "Plugin directory $DEBIRF_PLUGINS not found. Continue? [y|N]: " -n 1 OK ; [ -z $OK ] || echo |
|---|
| 122 | if [ "${OK/y/Y}" != 'Y' ] ; then |
|---|
| 123 | debirf_fail "aborting." |
|---|
| 124 | fi |
|---|
| 125 | fi |
|---|
| 126 | |
|---|
| 127 | cd "$DEBIRF_PATH" |
|---|
| 128 | |
|---|
| 129 | # create debootstrap root |
|---|
| 130 | if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 131 | read -p "Directory $DEBIRF_ROOT already exists. write over? [y|N]: " -n 1 OK ; [ -z $OK ] || echo |
|---|
| 132 | if [ "${OK/y/Y}" = 'Y' ] ; then |
|---|
| 133 | msg "installing new root..." |
|---|
| 134 | create_debootstrap |
|---|
| 135 | fi |
|---|
| 136 | else |
|---|
| 137 | msg "installing root..." |
|---|
| 138 | create_debootstrap |
|---|
| 139 | fi |
|---|
| 140 | |
|---|
| 141 | setup_debirf_info |
|---|
| 142 | |
|---|
| 143 | # mount sys, proc |
|---|
| 144 | mount -t proc proc "$DEBIRF_ROOT/proc" |
|---|
| 145 | mount -t sysfs sys "$DEBIRF_ROOT/sys" |
|---|
| 146 | |
|---|
| 147 | # update apt |
|---|
| 148 | debirf_exec apt-get update |
|---|
| 149 | |
|---|
| 150 | # install plugins |
|---|
| 151 | msg "installing plugins..." |
|---|
| 152 | run_plugins |
|---|
| 153 | |
|---|
| 154 | # umount sys,proc |
|---|
| 155 | umount "$DEBIRF_ROOT/proc" |
|---|
| 156 | umount "$DEBIRF_ROOT/sys" |
|---|
| 157 | |
|---|
| 158 | # clear mtab |
|---|
| 159 | > "$DEBIRF_ROOT/etc/mtab" |
|---|
| 160 | # add proc to fstab |
|---|
| 161 | if ! awk '{ print $2 }' < "$DEBIRF_ROOT/etc/fstab" | grep -q '^/proc' ; then |
|---|
| 162 | echo proc /proc proc defaults 0 0 >> "$DEBIRF_ROOT/etc/fstab" |
|---|
| 163 | fi |
|---|
| 164 | |
|---|
| 165 | KERNAVAIL=$(ls -1 "$DEBIRF_ROOT/lib/modules" | head -n1) |
|---|
| 166 | DEBIRF_INITRD="${DEBIRF_LABEL}_${DEBIRF_DISTRO}_${KERNAVAIL}.cgz" |
|---|
| 167 | create_initrd_${DEBIRF_METHOD} "$DEBIRF_INITRD" |
|---|
| 168 | |
|---|
| 169 | # final output |
|---|
| 170 | DEBIRF_KERNEL="vmlinu"*"$KERNAVAIL" |
|---|
| 171 | msg "debirf initrd created." |
|---|
| 172 | if [ "$DEBIRF_INITRD/$DEBIRF_KERNEL" ] ; then |
|---|
| 173 | msg "kernel: $DEBIRF_PATH/"$DEBIRF_KERNEL |
|---|
| 174 | fi |
|---|
| 175 | msg "initrd: $DEBIRF_PATH/$DEBIRF_INITRD" |
|---|