| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | # DEBIRF creation script. You must pass a kernel .deb as the first argument. |
|---|
| 4 | |
|---|
| 5 | CMD=$(basename $0) |
|---|
| 6 | CMD_PATH=$(dirname $0) |
|---|
| 7 | |
|---|
| 8 | # source debirf.conf |
|---|
| 9 | source "$CMD_PATH/debirf.conf" |
|---|
| 10 | |
|---|
| 11 | if [ "$1" ]; then |
|---|
| 12 | DEBIRF_KERNEL_PACKAGE="$1" |
|---|
| 13 | fi |
|---|
| 14 | |
|---|
| 15 | cd "$DEBIRF_PATH" |
|---|
| 16 | |
|---|
| 17 | msg() { |
|---|
| 18 | echo " debirf: $@" |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | setup_debirf_info() { |
|---|
| 22 | export DEBIRF_INFO_TARGET="${DEBIRF_ROOT}/etc/debirf/debirf.info" |
|---|
| 23 | mkdir -p $(dirname "$DEBIRF_INFO_TARGET") |
|---|
| 24 | |
|---|
| 25 | cat > "$DEBIRF_INFO_TARGET" <<EOF |
|---|
| 26 | #!/bin/sh |
|---|
| 27 | # this debirf initramfs was generated on $(hostname) |
|---|
| 28 | # at $(date -R) |
|---|
| 29 | |
|---|
| 30 | EOF |
|---|
| 31 | debirf_info_comment() { |
|---|
| 32 | echo "$@" | sed 's|^\(.\)|\# \1|' >> "$DEBIRF_INFO_TARGET" |
|---|
| 33 | } |
|---|
| 34 | debirf_info_sh() { |
|---|
| 35 | echo "$@" >> "$DEBIRF_INFO_TARGET" |
|---|
| 36 | } |
|---|
| 37 | export -f debirf_info_comment |
|---|
| 38 | export -f debirf_info_sh |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | create_debootstrap() { |
|---|
| 42 | # include initramfs-tools because they'll be handy |
|---|
| 43 | # exclude aptitude |
|---|
| 44 | mkdir -p "$DEBIRF_ROOT" |
|---|
| 45 | /usr/sbin/debootstrap --exclude=aptitude --include=initramfs-tools "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR" |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | install_plugins() { |
|---|
| 49 | # export all the DEBIRF_* environment variables: |
|---|
| 50 | for var in ${!DEBIRF_*}; do |
|---|
| 51 | export $var |
|---|
| 52 | done |
|---|
| 53 | run-parts --verbose "$DEBIRF_PLUGINS" |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | ### stupid simple method |
|---|
| 57 | create_initrd_stupid_simple() { |
|---|
| 58 | #ln sbin/init to /init |
|---|
| 59 | ln -sf sbin/init "$DEBIRF_ROOT/init" |
|---|
| 60 | # create root image |
|---|
| 61 | msg "creating debirf initrd..." |
|---|
| 62 | ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) > "${DEBIRF_LABEL}_${DEBIRF_DISTRO}_${DEBIRF_".cgz |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | ### tweak initrd method |
|---|
| 66 | create_initrd_tweak_initrd() { |
|---|
| 67 | cat scripts/debirf <<EOF |
|---|
| 68 | mountroot() { |
|---|
| 69 | rootmnt="/" |
|---|
| 70 | } |
|---|
| 71 | EOF |
|---|
| 72 | cat > 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 extra/boot/"$INITRD" debirf.cgz |
|---|
| 86 | (cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) >> debirf.cgz |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | ##### MAIN ##### |
|---|
| 91 | |
|---|
| 92 | ##### create debootstrap root |
|---|
| 93 | if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 94 | echo "directory $DEBIRF_ROOT already exists." |
|---|
| 95 | read -p "write over? [y|N]: " -n 1 OK ; [ -z $OK ] || echo |
|---|
| 96 | if [ "${OK/y/Y}" = 'Y' ] ; then |
|---|
| 97 | msg "installing new root..." |
|---|
| 98 | create_debootstrap |
|---|
| 99 | fi |
|---|
| 100 | else |
|---|
| 101 | msg "installing root..." |
|---|
| 102 | create_debootstrap |
|---|
| 103 | fi |
|---|
| 104 | |
|---|
| 105 | setup_debirf_info |
|---|
| 106 | |
|---|
| 107 | # mount sys, proc |
|---|
| 108 | mount -t proc proc "$DEBIRF_ROOT/proc" |
|---|
| 109 | mount -t sysfs sys "$DEBIRF_ROOT/sys" |
|---|
| 110 | |
|---|
| 111 | # update apt |
|---|
| 112 | chroot "$DEBIRF_ROOT" apt-get update |
|---|
| 113 | |
|---|
| 114 | ##### install plugins |
|---|
| 115 | msg "installing plugins..." |
|---|
| 116 | install_plugins |
|---|
| 117 | |
|---|
| 118 | ##### prune |
|---|
| 119 | msg "pruning..." |
|---|
| 120 | |
|---|
| 121 | # clean apt |
|---|
| 122 | chroot "$DEBIRF_ROOT" apt-get clean |
|---|
| 123 | rm "$DEBIRF_ROOT/var/cache/apt/"*.bin |
|---|
| 124 | rm -rf "$DEBIRF_ROOT/var/lib/apt/lists/"* |
|---|
| 125 | mkdir "$DEBIRF_ROOT/var/lib/apt/lists/partial" |
|---|
| 126 | |
|---|
| 127 | # remove/backup locales: |
|---|
| 128 | mkdir -p extras/usr/share/locale |
|---|
| 129 | # FIXME: this won't work if "extras" isn't a peer of "$DEBIRF_ROOT" |
|---|
| 130 | (cd "$DEBIRF_ROOT" && find usr/share/locale -maxdepth 1 -mindepth 1 -type d ! -iname 'en*' -exec mv '{}' '../extras/{}' \; ) |
|---|
| 131 | |
|---|
| 132 | # umount sys,proc |
|---|
| 133 | umount "$DEBIRF_ROOT/proc" |
|---|
| 134 | umount "$DEBIRF_ROOT/sys" |
|---|
| 135 | |
|---|
| 136 | # clear mtab |
|---|
| 137 | > "$DEBIRF_ROOT/etc/mtab" |
|---|
| 138 | # add proc to fstab |
|---|
| 139 | if ! grep '/proc' root/etc/fstab | grep -q -v "^[:space:]*#" ; then |
|---|
| 140 | echo proc /proc proc defaults 0 0 >> "$DEBIRF_ROOT/etc/fstab" |
|---|
| 141 | fi |
|---|
| 142 | |
|---|
| 143 | create_initrd_${DEBIRF_METHOD} |
|---|
| 144 | |
|---|
| 145 | # final output |
|---|
| 146 | if [ "$KERNEL" ] ; then |
|---|
| 147 | ln -sf "extras/boot/$KERNEL" vmlinuz |
|---|
| 148 | msg "kernel: $DEBIRF_PATH/vmlinuz" |
|---|
| 149 | fi |
|---|
| 150 | msg "initrd: $DEBIRF_PATH/debirf.cgz" |
|---|