| 1 | #!/bin/bash -e |
|---|
| 2 | |
|---|
| 3 | # debirf: script to build debirf system. |
|---|
| 4 | # |
|---|
| 5 | # The debirf scripts were written by |
|---|
| 6 | # Jameson Graef Rollins <jrollins@finestructure.net> |
|---|
| 7 | # and |
|---|
| 8 | # Daniel Kahn Gillmor <dkg@fifthhorseman.net>. |
|---|
| 9 | # |
|---|
| 10 | # They are Copyright 2007-2009, and are all released under the GPL, |
|---|
| 11 | # version 3 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 | # default label |
|---|
| 22 | DEBIRF_LABEL=${DEBIRF_LABEL:-debirf} |
|---|
| 23 | |
|---|
| 24 | # default debirf boot method |
|---|
| 25 | DEBIRF_METHOD=${DEBIRF_METHOD:-nested} |
|---|
| 26 | |
|---|
| 27 | # default build type |
|---|
| 28 | export ROOT_BUILD=false |
|---|
| 29 | |
|---|
| 30 | # stages to run by default |
|---|
| 31 | STAGE_ROOT=${STAGE_ROOT:-true} |
|---|
| 32 | STAGE_MODULES=${STAGE_MODULES:-true} |
|---|
| 33 | STAGE_INITRD=${STAGE_INITRD:-true} |
|---|
| 34 | |
|---|
| 35 | # warn if running as root |
|---|
| 36 | ROOT_WARNING=true |
|---|
| 37 | |
|---|
| 38 | # location of devices.tar.gz file |
|---|
| 39 | export DEVICE_ARCHIVE=${DEVICE_ARCHIVE:-/usr/share/debirf/devices.tar.gz} |
|---|
| 40 | |
|---|
| 41 | # list of packages to include/exclude from debootstrap |
|---|
| 42 | export INCLUDE=cpio,gpgv,less |
|---|
| 43 | export EXCLUDE=apt-utils,bsdmainutils,cron,ed,info,logrotate,man-db,manpages,tasksel,tasksel-data,tcpd,traceroute |
|---|
| 44 | |
|---|
| 45 | ############################################################### |
|---|
| 46 | ### FUNCTIONS |
|---|
| 47 | |
|---|
| 48 | usage() { |
|---|
| 49 | cat <<EOF |
|---|
| 50 | Usage: $CMD <subcommand> [options] [args] |
|---|
| 51 | Debirf system tool. |
|---|
| 52 | |
|---|
| 53 | subcommands: |
|---|
| 54 | make [options] PROFILE build debirf kernel and initramfs |
|---|
| 55 | -c|--check-vars check variables before make |
|---|
| 56 | -n|--new create new root, even if old one exists |
|---|
| 57 | -o|--overwrite debootstrap on top of old root if it exists |
|---|
| 58 | -s|--skip skip debootstrap step if old root exists |
|---|
| 59 | -r|--root-build use real chroot to build instead of fakechroot |
|---|
| 60 | (requires superuser privileges or CAP_SYS_CHROOT) |
|---|
| 61 | -w|--no-warning skip superuser warning |
|---|
| 62 | -d|--no-initrd do not make initramfs |
|---|
| 63 | -i|--initrd-only just remake initramfs from existing root |
|---|
| 64 | -k|--kernel=KERNEL install KERNEL .deb, instead of default kernel |
|---|
| 65 | enter PROFILE [CMDS] enter shell in debirf root (or execute CMDS) |
|---|
| 66 | makeiso PROFILE create a bootable ISO using the given profile |
|---|
| 67 | help this help |
|---|
| 68 | |
|---|
| 69 | EOF |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | usage_profile() { |
|---|
| 73 | cat <<EOF |
|---|
| 74 | It looks like your profile is not correctly formed. Please refer to the |
|---|
| 75 | README file included with this package on how to setup a profile. |
|---|
| 76 | EOF |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | create_debootstrap() { |
|---|
| 80 | msg "distro/suite: ${DEBIRF_DISTRO}/${DEBIRF_SUITE}" |
|---|
| 81 | |
|---|
| 82 | local OPTS="--include=$INCLUDE --exclude=$EXCLUDE $DEBIRF_SUITE $DEBIRF_ROOT $DEBIRF_MIRROR" |
|---|
| 83 | |
|---|
| 84 | if [ "$DEBIRF_KEYRING" ] ; then |
|---|
| 85 | OPTS="--keyring='$DEBIRF_KEYRING' $OPTS" |
|---|
| 86 | fi |
|---|
| 87 | |
|---|
| 88 | if [ "$DEBIRF_ARCH" ] ; then |
|---|
| 89 | OPTS="--arch='$DEBIRF_ARCH' $OPTS" |
|---|
| 90 | fi |
|---|
| 91 | |
|---|
| 92 | mkdir -p "$DEBIRF_ROOT" |
|---|
| 93 | |
|---|
| 94 | if [ "$ROOT_BUILD" = 'true' ] ; then |
|---|
| 95 | eval "/usr/sbin/debootstrap $OPTS" |
|---|
| 96 | else |
|---|
| 97 | eval "fakeroot_if_needed fakechroot /usr/sbin/debootstrap --variant=fakechroot $OPTS" |
|---|
| 98 | fi |
|---|
| 99 | |
|---|
| 100 | # FIXME: should this be done in a trap so that the log |
|---|
| 101 | # debootstrap.log is retrieved from the root in case of failure? |
|---|
| 102 | fakeroot_if_needed mv "$DEBIRF_ROOT"/var/log/bootstrap.log "$DEBIRF_BUILDD"/.bootstrap.log |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | # fix the device tree in the debirf root if fakechroot variant was |
|---|
| 106 | # used with debootstrap (default non-privileged behavior) |
|---|
| 107 | fix_dev() { |
|---|
| 108 | if [ -L "$DEBIRF_ROOT"/dev -o ! -d "$DEBIRF_ROOT"/dev ] ; then |
|---|
| 109 | msg "fixing debirf root dev tree..." |
|---|
| 110 | |
|---|
| 111 | # remove old dev |
|---|
| 112 | fakeroot_if_needed rm -f "$DEBIRF_ROOT"/dev |
|---|
| 113 | |
|---|
| 114 | # create new dev from devices archive |
|---|
| 115 | fakeroot_if_needed sh -c "cd $DEBIRF_ROOT; tar -xzf $DEVICE_ARCHIVE" |
|---|
| 116 | |
|---|
| 117 | # create /dev/console |
|---|
| 118 | fakeroot_if_needed sh -c "mknod $DEBIRF_ROOT/dev/console c 5 1; chmod 0600 $DEBIRF_ROOT/dev/console" |
|---|
| 119 | fi |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | # run modules in modules directory |
|---|
| 123 | run_modules() { |
|---|
| 124 | fakeroot_if_needed run-parts --verbose --exit-on-error "$DEBIRF_MODULES" |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | # pack the rootfs archive |
|---|
| 128 | # takes one input argument as name of output archive file |
|---|
| 129 | pack_rootfs() { |
|---|
| 130 | # need to pack archive in fakechroot/chroot so that symlinks correctly |
|---|
| 131 | # point within debirf root, instead of to host path |
|---|
| 132 | fakeroot_if_needed fakechroot chroot "$DEBIRF_ROOT" sh -c "find * | cpio --create -H newc" | gzip > "$1" |
|---|
| 133 | } |
|---|
| 134 | export -f pack_rootfs |
|---|
| 135 | |
|---|
| 136 | # initrd method: stupid simple |
|---|
| 137 | # takes path to initrd as first input |
|---|
| 138 | create_initrd_stupid_simple() { |
|---|
| 139 | fakeroot_if_needed ln -sf /sbin/init "$DEBIRF_ROOT/init" |
|---|
| 140 | pack_rootfs "$1" |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | # initrd method: nested cpio archives |
|---|
| 144 | # takes path to initrd as first input |
|---|
| 145 | create_initrd_nested() { |
|---|
| 146 | local util lib |
|---|
| 147 | local INITRD="$1" |
|---|
| 148 | local NEST_ROOT="$DEBIRF_BUILDD"/nest |
|---|
| 149 | |
|---|
| 150 | # make the nested root |
|---|
| 151 | rm -rf "$NEST_ROOT" |
|---|
| 152 | mkdir -p "$NEST_ROOT"/{bin,lib} |
|---|
| 153 | |
|---|
| 154 | # copy needed executables into nest |
|---|
| 155 | cp -f /bin/busybox "$NEST_ROOT"/bin/ |
|---|
| 156 | for util in awk cpio free grep gunzip ls mkdir mount rm sh umount ; do |
|---|
| 157 | ln "$NEST_ROOT"/bin/busybox "$NEST_ROOT"/bin/"$util" |
|---|
| 158 | done |
|---|
| 159 | cp -f /usr/lib/klibc/bin/run-init "$NEST_ROOT"/bin/ |
|---|
| 160 | |
|---|
| 161 | # copy in needed libraries |
|---|
| 162 | for lib in $(ldd "$NEST_ROOT"/bin/* | \ |
|---|
| 163 | egrep '*.so.[[:digit:]]+ \(0x[[:xdigit:]]{8,16}\)$' | \ |
|---|
| 164 | sed -r 's|.*[[:space:]](/[^[:space:]]*)[[:space:]]\(0x[[:xdigit:]]{8,16}\)$|\1|' | \ |
|---|
| 165 | sort -u) ; do |
|---|
| 166 | # pull libraries from most basic place libraries can live |
|---|
| 167 | # (avoid arch change between build env and debirf) |
|---|
| 168 | lib=/lib/$(basename "$lib") |
|---|
| 169 | echo -e "$lib\n$(readlink -f $lib)" | cpio --pass-through --make-directories "$NEST_ROOT"/ |
|---|
| 170 | done |
|---|
| 171 | cp -f /lib/klibc-* "$NEST_ROOT"/lib/ |
|---|
| 172 | if [ -L /lib64 ] && [ $(readlink /lib64) = /lib ] ; then |
|---|
| 173 | ln -s /lib "$NEST_ROOT"/lib64 |
|---|
| 174 | fi |
|---|
| 175 | |
|---|
| 176 | # create nest init |
|---|
| 177 | cat > "$NEST_ROOT"/init <<EOF |
|---|
| 178 | #!/bin/sh |
|---|
| 179 | mkdir /proc |
|---|
| 180 | mount -t proc proc /proc |
|---|
| 181 | if (grep -q break=top /proc/cmdline); then |
|---|
| 182 | echo "honoring break=top kernel arg" |
|---|
| 183 | /bin/sh |
|---|
| 184 | fi |
|---|
| 185 | mkdir /newroot |
|---|
| 186 | MEMSIZE=\$(free | grep 'Mem:' | awk '{ print \$2 }') |
|---|
| 187 | mount -t tmpfs -o size=\${MEMSIZE}k tmpfs /newroot |
|---|
| 188 | if (grep -q break=preunpack /proc/cmdline); then |
|---|
| 189 | echo "honoring break=preunpack kernel arg" |
|---|
| 190 | /bin/sh |
|---|
| 191 | fi |
|---|
| 192 | cd /newroot |
|---|
| 193 | echo unpacking rootfs... |
|---|
| 194 | gunzip - < /rootfs.cgz | cpio -i |
|---|
| 195 | if (grep -q break=bottom /proc/cmdline); then |
|---|
| 196 | echo "honoring break=bottom kernel arg" |
|---|
| 197 | /bin/sh |
|---|
| 198 | fi |
|---|
| 199 | umount /proc |
|---|
| 200 | echo running /sbin/init... |
|---|
| 201 | exec /bin/run-init . /sbin/init < ./dev/console > ./dev/console |
|---|
| 202 | EOF |
|---|
| 203 | chmod a+x "$NEST_ROOT"/init |
|---|
| 204 | |
|---|
| 205 | msg "creating rootfs.cgz..." |
|---|
| 206 | fakeroot_if_needed ln -sf /sbin/init "$DEBIRF_ROOT/init" |
|---|
| 207 | pack_rootfs "$NEST_ROOT"/rootfs.cgz |
|---|
| 208 | |
|---|
| 209 | msg "creating wrapper cgz..." |
|---|
| 210 | fakeroot_if_needed sh -c "cd $NEST_ROOT && find * | cpio --create -H newc" | gzip > "$INITRD" |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | # determine what the host system distro is, and set debirf build defaults |
|---|
| 214 | # accordingly |
|---|
| 215 | set_distro() { |
|---|
| 216 | RUN_DISTRO=$(head -1 /etc/issue | awk '{ print $1 }' | tr "[:upper:]" "[:lower:]") |
|---|
| 217 | case "$RUN_DISTRO" in |
|---|
| 218 | ubuntu) |
|---|
| 219 | DEBIRF_SUITE=${DEBIRF_SUITE:-"karmic"} |
|---|
| 220 | ;; |
|---|
| 221 | *) |
|---|
| 222 | DEBIRF_SUITE=${DEBIRF_SUITE:-"squeeze"} |
|---|
| 223 | ;; |
|---|
| 224 | esac |
|---|
| 225 | |
|---|
| 226 | case "$DEBIRF_SUITE" in |
|---|
| 227 | hoary|breezy|dapper|edgy|feisty|gutsy|hardy|intrepid|jaunty|karmic|lucid) |
|---|
| 228 | DEBIRF_DISTRO=${DEBIRF_DISTRO:-"ubuntu"} |
|---|
| 229 | ;; |
|---|
| 230 | *) |
|---|
| 231 | DEBIRF_DISTRO=${DEBIRF_DISTRO:-"debian"} |
|---|
| 232 | ;; |
|---|
| 233 | esac |
|---|
| 234 | |
|---|
| 235 | DEBIRF_MIRROR=${DEBIRF_MIRROR:-"http://mirrors.kernel.org/${DEBIRF_DISTRO}"} |
|---|
| 236 | DEBIRF_KEYRING=${DEBIRF_KEYRING:-"/usr/share/keyrings/${DEBIRF_DISTRO}-archive-keyring.gpg"} |
|---|
| 237 | |
|---|
| 238 | if ! [ -f "$DEBIRF_KEYRING" -a -r "$DEBIRF_KEYRING" ] ; then |
|---|
| 239 | failure "Cannot read keyring '$DEBIRF_KEYRING' for debootstrap verification." |
|---|
| 240 | fi |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | # setup profile environment |
|---|
| 244 | setup_environment() { |
|---|
| 245 | # get the debirf profile path |
|---|
| 246 | # FIXME: should we canonicalize DEBIRF_PROFILE to an absolute |
|---|
| 247 | # path? |
|---|
| 248 | DEBIRF_PROFILE=${1%%/} |
|---|
| 249 | |
|---|
| 250 | DEBIRF_PROFILE_NAME=$(cd "$DEBIRF_PROFILE" && basename $(pwd)) |
|---|
| 251 | |
|---|
| 252 | # check profile |
|---|
| 253 | if [ -d "$DEBIRF_PROFILE" ] ; then |
|---|
| 254 | msg "loading profile '$DEBIRF_PROFILE_NAME'..." |
|---|
| 255 | DEBIRF_CONF="$DEBIRF_PROFILE/debirf.conf" |
|---|
| 256 | DEBIRF_MODULES="$DEBIRF_PROFILE/modules" |
|---|
| 257 | else |
|---|
| 258 | failure "Profile '$DEBIRF_PROFILE' not found." |
|---|
| 259 | fi |
|---|
| 260 | |
|---|
| 261 | # source profile debirf.conf |
|---|
| 262 | if [ -f "$DEBIRF_CONF" ] ; then |
|---|
| 263 | source "$DEBIRF_CONF" |
|---|
| 264 | else |
|---|
| 265 | echo "Configuration file '$DEBIRF_CONF' not found." |
|---|
| 266 | usage_profile |
|---|
| 267 | exit 1 |
|---|
| 268 | fi |
|---|
| 269 | |
|---|
| 270 | # check modules directory |
|---|
| 271 | if [ ! -d "$DEBIRF_MODULES" ] || [ -z "$(ls "$DEBIRF_MODULES")" ] ; then |
|---|
| 272 | echo "Modules directory '$DEBIRF_MODULES' does not exist or is empty." |
|---|
| 273 | usage_profile |
|---|
| 274 | exit 1 |
|---|
| 275 | fi |
|---|
| 276 | for MODULE in $(find "$DEBIRF_MODULES") ; do |
|---|
| 277 | if [ ! -s "$MODULE" ] ; then |
|---|
| 278 | failure "Module '$MODULE' is a broken link or empty file." |
|---|
| 279 | fi |
|---|
| 280 | done |
|---|
| 281 | |
|---|
| 282 | # set/check buildd |
|---|
| 283 | DEBIRF_BUILDD=${DEBIRF_BUILDD:-"$DEBIRF_PROFILE"} |
|---|
| 284 | if [ ! -d "$DEBIRF_BUILDD" ] ; then |
|---|
| 285 | failure "Could not find build directory '$DEBIRF_BUILDD'." |
|---|
| 286 | fi |
|---|
| 287 | |
|---|
| 288 | # "absolutize" the buildd path |
|---|
| 289 | # This is needed because of trouble with fakechroot (see |
|---|
| 290 | # http://bugs.debian.org/548691) |
|---|
| 291 | DEBIRF_BUILDD=$(cd "$DEBIRF_BUILDD" && pwd) |
|---|
| 292 | |
|---|
| 293 | # set root directory |
|---|
| 294 | DEBIRF_ROOT="$DEBIRF_BUILDD/root" |
|---|
| 295 | |
|---|
| 296 | # set fakechroot save file |
|---|
| 297 | DEBIRF_FAKEROOT_STATE="$DEBIRF_BUILDD/.fakeroot-state.${DEBIRF_LABEL}" |
|---|
| 298 | |
|---|
| 299 | # set the debirf distro variables based on host distro |
|---|
| 300 | set_distro |
|---|
| 301 | |
|---|
| 302 | # export all the DEBIRF_* environment variables: |
|---|
| 303 | for var in ${!DEBIRF_*}; do |
|---|
| 304 | if [ $var ] ; then |
|---|
| 305 | export $var |
|---|
| 306 | else |
|---|
| 307 | failure "Variable '$var' not properly set." |
|---|
| 308 | fi |
|---|
| 309 | done |
|---|
| 310 | |
|---|
| 311 | # check variables |
|---|
| 312 | if [ "$CHECK_VARS" ] ; then |
|---|
| 313 | msg "debirf variables:" |
|---|
| 314 | env | /bin/grep "^DEBIRF_" |
|---|
| 315 | read -p "enter to continue: " OK |
|---|
| 316 | fi |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | # make profile |
|---|
| 320 | make() { |
|---|
| 321 | # option parsing |
|---|
| 322 | TEMP=$(getopt --options -hcnosrwdik: --longoptions help,check-vars,new,overwrite,skip,root-build,no-warning,no-initrd,initrd-only,kernel: -n "$CMD" -- "$@") |
|---|
| 323 | |
|---|
| 324 | if [ $? != 0 ] ; then |
|---|
| 325 | echo "Invalid options." >&2 |
|---|
| 326 | usage |
|---|
| 327 | exit 1 |
|---|
| 328 | fi |
|---|
| 329 | |
|---|
| 330 | # Note the quotes around `$TEMP': they are essential! |
|---|
| 331 | eval set -- "$TEMP" |
|---|
| 332 | |
|---|
| 333 | while true ; do |
|---|
| 334 | case "$1" in |
|---|
| 335 | -c|--check-vars) |
|---|
| 336 | CHECK_VARS=true |
|---|
| 337 | shift 1 |
|---|
| 338 | ;; |
|---|
| 339 | -n|--new) |
|---|
| 340 | WRITE_MODE=rewrite |
|---|
| 341 | shift 1 |
|---|
| 342 | ;; |
|---|
| 343 | -o|--overwrite) |
|---|
| 344 | WRITE_MODE=overwrite |
|---|
| 345 | shift 1 |
|---|
| 346 | ;; |
|---|
| 347 | -s|--skip) |
|---|
| 348 | WRITE_MODE=skip |
|---|
| 349 | shift 1 |
|---|
| 350 | ;; |
|---|
| 351 | -r|--root-build) |
|---|
| 352 | ROOT_BUILD=true |
|---|
| 353 | shift 1 |
|---|
| 354 | ;; |
|---|
| 355 | -w|--no-warning) |
|---|
| 356 | ROOT_WARNING=false |
|---|
| 357 | shift 1 |
|---|
| 358 | ;; |
|---|
| 359 | -d|--no-initrd) |
|---|
| 360 | STAGE_INITRD=false |
|---|
| 361 | shift 1 |
|---|
| 362 | ;; |
|---|
| 363 | -i|--initrd-only) |
|---|
| 364 | STAGE_ROOT=false |
|---|
| 365 | STAGE_MODULES=false |
|---|
| 366 | shift 1 |
|---|
| 367 | ;; |
|---|
| 368 | -k|--kernel) |
|---|
| 369 | DEBIRF_KERNEL_PACKAGE="$2" |
|---|
| 370 | shift 2 |
|---|
| 371 | ;; |
|---|
| 372 | --) |
|---|
| 373 | shift |
|---|
| 374 | ;; |
|---|
| 375 | *) |
|---|
| 376 | if (( $# < 1 )) ; then |
|---|
| 377 | echo "Improper number of input arguments." |
|---|
| 378 | usage |
|---|
| 379 | exit 1 |
|---|
| 380 | fi |
|---|
| 381 | break |
|---|
| 382 | ;; |
|---|
| 383 | esac |
|---|
| 384 | done |
|---|
| 385 | |
|---|
| 386 | if [ $(id -u) = '0' ] ; then |
|---|
| 387 | cat <<EOF |
|---|
| 388 | Warning: You are running debirf as root. There is a potential |
|---|
| 389 | for improperly written modules to damage your system. |
|---|
| 390 | EOF |
|---|
| 391 | if [ "$ROOT_WARNING" = 'true' ] ; then |
|---|
| 392 | read -p "Are you sure you wish to continue? [y|N]: " OK; OK=${OK:=N} |
|---|
| 393 | if [ "${OK/y/Y}" != 'Y' ] ; then |
|---|
| 394 | failure "aborting." |
|---|
| 395 | fi |
|---|
| 396 | fi |
|---|
| 397 | fi |
|---|
| 398 | |
|---|
| 399 | setup_environment "$1" |
|---|
| 400 | shift |
|---|
| 401 | |
|---|
| 402 | if [ "$DEBIRF_KERNEL_PACKAGE" ] ; then |
|---|
| 403 | if [ -f "$DEBIRF_KERNEL_PACKAGE" ] ; then |
|---|
| 404 | msg "using kernel package '$DEBIRF_KERNEL_PACKAGE'" |
|---|
| 405 | else |
|---|
| 406 | failure "Kernel package '$DEBIRF_KERNEL_PACKAGE' not found." |
|---|
| 407 | fi |
|---|
| 408 | fi |
|---|
| 409 | |
|---|
| 410 | ### BUILD ROOT |
|---|
| 411 | if [ "$STAGE_ROOT" = 'true' ] ; then |
|---|
| 412 | # determine write mode |
|---|
| 413 | if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 414 | if [ -z "$WRITE_MODE" ] ; then |
|---|
| 415 | msg "debirf root already exists. select one of the following:" |
|---|
| 416 | CASE1='new: delete the old root and create a new one' |
|---|
| 417 | CASE2='overwrite: leave the old root and debootstrap on top of it' |
|---|
| 418 | CASE3='skip: skip building the root and go right to installing modules' |
|---|
| 419 | CASE4='exit' |
|---|
| 420 | select CASE in "$CASE1" "$CASE2" "$CASE3" "$CASE4" ; do |
|---|
| 421 | case "$REPLY" in |
|---|
| 422 | 1) |
|---|
| 423 | WRITE_MODE=rewrite |
|---|
| 424 | ;; |
|---|
| 425 | 2) |
|---|
| 426 | WRITE_MODE=overwrite |
|---|
| 427 | ;; |
|---|
| 428 | 3) |
|---|
| 429 | WRITE_MODE=skip |
|---|
| 430 | ;; |
|---|
| 431 | *) |
|---|
| 432 | failure "aborting." |
|---|
| 433 | ;; |
|---|
| 434 | esac |
|---|
| 435 | break |
|---|
| 436 | done |
|---|
| 437 | fi |
|---|
| 438 | else |
|---|
| 439 | WRITE_MODE=new |
|---|
| 440 | fi |
|---|
| 441 | case "$WRITE_MODE" in |
|---|
| 442 | 'new') |
|---|
| 443 | msg "creating debirf root..." |
|---|
| 444 | > "$DEBIRF_FAKEROOT_STATE" |
|---|
| 445 | create_debootstrap |
|---|
| 446 | ;; |
|---|
| 447 | 'rewrite') |
|---|
| 448 | msg "clearing old debirf root..." |
|---|
| 449 | rm -rf "$DEBIRF_ROOT" |
|---|
| 450 | msg "creating debirf root..." |
|---|
| 451 | > "$DEBIRF_FAKEROOT_STATE" |
|---|
| 452 | create_debootstrap |
|---|
| 453 | ;; |
|---|
| 454 | 'overwrite') |
|---|
| 455 | msg "overwriting old debirf root..." |
|---|
| 456 | create_debootstrap |
|---|
| 457 | ;; |
|---|
| 458 | 'skip') |
|---|
| 459 | msg "skipping debootstrap..." |
|---|
| 460 | ;; |
|---|
| 461 | *) |
|---|
| 462 | failure "aborting." |
|---|
| 463 | ;; |
|---|
| 464 | esac |
|---|
| 465 | |
|---|
| 466 | # fix the dev tree if running as non-priv user (fakechroot debootstrap) |
|---|
| 467 | fix_dev |
|---|
| 468 | |
|---|
| 469 | else |
|---|
| 470 | msg "not building root" |
|---|
| 471 | fi |
|---|
| 472 | ### END BUILD ROOT |
|---|
| 473 | |
|---|
| 474 | ### RUN MODULES |
|---|
| 475 | if [ "$STAGE_MODULES" = 'true' ] ; then |
|---|
| 476 | msg "executing modules..." |
|---|
| 477 | run_modules |
|---|
| 478 | msg "modules complete" |
|---|
| 479 | else |
|---|
| 480 | msg "not running modules" |
|---|
| 481 | fi |
|---|
| 482 | ### END RUN MODULES |
|---|
| 483 | |
|---|
| 484 | ### BUILD INITRD |
|---|
| 485 | if [ "$STAGE_INITRD" = 'true' ] ; then |
|---|
| 486 | if [ ! -d "$DEBIRF_ROOT" ] ; then |
|---|
| 487 | failure "Debirf root '$DEBIRF_ROOT' not found." |
|---|
| 488 | fi |
|---|
| 489 | # determine initramfs name |
|---|
| 490 | local KERNEL_VERS=$(ls -1 "${DEBIRF_ROOT}/lib/modules" | head -n1) |
|---|
| 491 | local INITRD="${DEBIRF_LABEL}_${DEBIRF_SUITE}_${KERNEL_VERS}.cgz" |
|---|
| 492 | |
|---|
| 493 | msg "creating debirf initrd ('$DEBIRF_METHOD')..." |
|---|
| 494 | create_initrd_${DEBIRF_METHOD} "${DEBIRF_BUILDD}/${INITRD}" |
|---|
| 495 | |
|---|
| 496 | # final output |
|---|
| 497 | local KERNEL=$(ls "$DEBIRF_BUILDD" | grep "vmlinu" | grep "$KERNEL_VERS$") |
|---|
| 498 | msg "debirf initrd created." |
|---|
| 499 | if [ "${DEBIRF_BUILDD}/${KERNEL}" ] ; then |
|---|
| 500 | msg "kernel: ${DEBIRF_BUILDD}/${KERNEL}" |
|---|
| 501 | fi |
|---|
| 502 | msg "initrd: ${DEBIRF_BUILDD}/${INITRD}" |
|---|
| 503 | else |
|---|
| 504 | msg "not creating initramfs." |
|---|
| 505 | fi |
|---|
| 506 | ### END BUILD INITRD |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | # enter profile root |
|---|
| 510 | enter() { |
|---|
| 511 | setup_environment "$1" |
|---|
| 512 | shift |
|---|
| 513 | |
|---|
| 514 | if [ "$1" ] ; then |
|---|
| 515 | fakeroot_if_needed bash -c ". $DEBIRF_COMMON && debirf_exec $@" |
|---|
| 516 | else |
|---|
| 517 | fakeroot_if_needed bash -c ". $DEBIRF_COMMON && debirf_exec bash -i" |
|---|
| 518 | fi |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | # create an ISO from the given kernel and initramfs (requires GRUB, |
|---|
| 522 | # see: |
|---|
| 523 | # http://www.gnu.org/software/grub/manual/html_node/Making-a-GRUB-bootable-CD-ROM.html) |
|---|
| 524 | makeiso() { |
|---|
| 525 | setup_environment "$1" |
|---|
| 526 | shift |
|---|
| 527 | |
|---|
| 528 | (which genisoimage > /dev/null) || failure "genisoimage is not in your path. Maybe you need to install it?" |
|---|
| 529 | |
|---|
| 530 | [ -d "$DEBIRF_PROFILE" ] || failure "'$DEBIRF_PROFILE' does not seem to be a directory" |
|---|
| 531 | |
|---|
| 532 | # find kernel |
|---|
| 533 | case $(cd "${DEBIRF_BUILDD}" && ls -1 | grep -c 'vmlinu*') in |
|---|
| 534 | 0) |
|---|
| 535 | failure "Failed to find a kernel. Maybe you need to run 'debirf make $DEBIRF_PROFILE' first?" |
|---|
| 536 | ;; |
|---|
| 537 | 1) |
|---|
| 538 | msg "kernel found." |
|---|
| 539 | local KERNEL=${KERNEL:-$(cd "${DEBIRF_BUILDD}" && ls -1 vmlinu*)} |
|---|
| 540 | ;; |
|---|
| 541 | *) |
|---|
| 542 | failure "Multiple kernels found. Please clear out all but the desired kernel." |
|---|
| 543 | ;; |
|---|
| 544 | esac |
|---|
| 545 | |
|---|
| 546 | # find initramfs |
|---|
| 547 | case $(cd "${DEBIRF_BUILDD}" && ls -1 | grep -c "${DEBIRF_LABEL}_*.cgz") in |
|---|
| 548 | 0) |
|---|
| 549 | failure "Failed to find a single initramfs. Maybe you need to run 'debirf make $DEBIRF_PROFILE' first?" |
|---|
| 550 | ;; |
|---|
| 551 | 1) |
|---|
| 552 | msg "initramfs found." |
|---|
| 553 | local INITRAMFS=${INITRAMFS:-$(cd "${DEBIRF_BUILDD}" && ls -1 "${DEBIRF_LABEL}"_*.cgz)} |
|---|
| 554 | ;; |
|---|
| 555 | *) |
|---|
| 556 | failure "Multiple initramfs found. Please clear out all but the desired initramfs." |
|---|
| 557 | ;; |
|---|
| 558 | esac |
|---|
| 559 | |
|---|
| 560 | # determine which eltorito boot loader we're using |
|---|
| 561 | if [ -z "$DEBIRF_ELTORITO" ] ; then |
|---|
| 562 | if which grub-mkrescue >/dev/null ; then |
|---|
| 563 | local DEBIRF_ELTORITO=grub |
|---|
| 564 | elif [ -f /usr/lib/syslinux/isolinux.bin ] ; then |
|---|
| 565 | local DEBIRF_ELTORITO=isolinux |
|---|
| 566 | else |
|---|
| 567 | failure "Suitable El Torito boot loader not found. Please install syslinux-common or grub-pc." |
|---|
| 568 | fi |
|---|
| 569 | fi |
|---|
| 570 | |
|---|
| 571 | msg "creating debirf iso..." |
|---|
| 572 | |
|---|
| 573 | # determine the iso name from the initramfs |
|---|
| 574 | local ISO=$(basename "$INITRAMFS" .cgz).iso |
|---|
| 575 | |
|---|
| 576 | # create clean iso directory |
|---|
| 577 | local ISODIR="${DEBIRF_BUILDD}/iso" |
|---|
| 578 | rm -rf "$ISODIR" |
|---|
| 579 | mkdir -p "$ISODIR" |
|---|
| 580 | |
|---|
| 581 | case "$DEBIRF_ELTORITO" in |
|---|
| 582 | grub) |
|---|
| 583 | msg "using $DEBIRF_ELTORITO as bootloader..." |
|---|
| 584 | |
|---|
| 585 | # use hard links to avoid massive copying time and genisoimage's |
|---|
| 586 | # warning about -f (we're almost certainly on the same filesystem): |
|---|
| 587 | ln "${DEBIRF_BUILDD}/${KERNEL}" "${ISODIR}/" || failure "Failed to link kernel into iso" |
|---|
| 588 | ln "${DEBIRF_BUILDD}/${INITRAMFS}" "${ISODIR}/" || failure "Failed to link initramfs into iso" |
|---|
| 589 | |
|---|
| 590 | # make grub.cfg |
|---|
| 591 | mkdir -p "$ISODIR"/boot/grub/ |
|---|
| 592 | cp /usr/share/debirf/grub2-terminal-chooser "$ISODIR"/boot/grub/grub.cfg |
|---|
| 593 | cat >>"$ISODIR"/boot/grub/grub.cfg <<EOF |
|---|
| 594 | |
|---|
| 595 | ## MENU |
|---|
| 596 | |
|---|
| 597 | if test \$chosen_console == "console" ; then |
|---|
| 598 | console=tty0 |
|---|
| 599 | fi |
|---|
| 600 | if test \$chosen_console == "serial" ; then |
|---|
| 601 | console=ttyS0,115200n8 |
|---|
| 602 | fi |
|---|
| 603 | set default=0 |
|---|
| 604 | set timeout=4 |
|---|
| 605 | menuentry "Debirf: $DEBIRF_LABEL, console=\$console (created $(date -R))" { |
|---|
| 606 | linux /$KERNEL console=\$console |
|---|
| 607 | initrd /$INITRAMFS |
|---|
| 608 | } |
|---|
| 609 | EOF |
|---|
| 610 | |
|---|
| 611 | # make the grub iso |
|---|
| 612 | grub-mkrescue --image-type=cdrom --overlay="$ISODIR" "${DEBIRF_BUILDD}/${ISO}" |
|---|
| 613 | ;; |
|---|
| 614 | |
|---|
| 615 | isolinux) |
|---|
| 616 | msg "using isolinux as bootloader..." |
|---|
| 617 | |
|---|
| 618 | # use hard links to avoid massive copying time and genisoimage's |
|---|
| 619 | # warning about -f (we're almost certainly on the same filesystem): |
|---|
| 620 | ln "${DEBIRF_BUILDD}/${KERNEL}" "${ISODIR}/vmlinuz" || failure "Failed to link kernel into iso" |
|---|
| 621 | ln "${DEBIRF_BUILDD}/${INITRAMFS}" "${ISODIR}/debirf.cfg" || failure "Failed to link initramfs into iso" |
|---|
| 622 | |
|---|
| 623 | # insert isolinux eltorito image |
|---|
| 624 | cp /usr/lib/syslinux/isolinux.bin "$ISODIR"/ |
|---|
| 625 | |
|---|
| 626 | local ELTORITO=isolinux.bin |
|---|
| 627 | |
|---|
| 628 | # make isolinux menu |
|---|
| 629 | cat >"$ISODIR"/isolinux.cfg <<EOF |
|---|
| 630 | serial 0 115200 |
|---|
| 631 | prompt 1 |
|---|
| 632 | timeout 0 |
|---|
| 633 | display menu |
|---|
| 634 | default video |
|---|
| 635 | |
|---|
| 636 | # serial console |
|---|
| 637 | label serial |
|---|
| 638 | kernel vmlinuz |
|---|
| 639 | append initrd=debirf.cfg console=tty0 console=ttyS0,115200n8 |
|---|
| 640 | |
|---|
| 641 | # video console |
|---|
| 642 | label video |
|---|
| 643 | kernel vmlinuz |
|---|
| 644 | append initrd=debirf.cfg console=ttyS0,115200n8 console=tty0 |
|---|
| 645 | |
|---|
| 646 | EOF |
|---|
| 647 | cat >"$ISODIR"/menu <<EOF |
|---|
| 648 | |
|---|
| 649 | Welcome to debirf: DEBian on InitRamFs |
|---|
| 650 | |
|---|
| 651 | label: $DEBIRF_LABEL |
|---|
| 652 | kernel: $KERNEL |
|---|
| 653 | initramfs: $INITRAMFS |
|---|
| 654 | created: $(date -R) |
|---|
| 655 | |
|---|
| 656 | type 'video' for video console i/o (default) |
|---|
| 657 | type 'serial' for serial console i/o |
|---|
| 658 | |
|---|
| 659 | EOF |
|---|
| 660 | |
|---|
| 661 | # generate the iso |
|---|
| 662 | genisoimage -R -b "$ELTORITO" \ |
|---|
| 663 | -no-emul-boot -boot-load-size 4 -boot-info-table \ |
|---|
| 664 | -o "${DEBIRF_BUILDD}/${ISO}" \ |
|---|
| 665 | "$ISODIR" |
|---|
| 666 | ;; |
|---|
| 667 | |
|---|
| 668 | *) |
|---|
| 669 | |
|---|
| 670 | failure "unknown iso build method '$DEBIRF_ELTORITO'" |
|---|
| 671 | ;; |
|---|
| 672 | esac |
|---|
| 673 | |
|---|
| 674 | # do we need to clean up the iso/ directory so that this can be run again? |
|---|
| 675 | |
|---|
| 676 | msg "debirf iso created." |
|---|
| 677 | msg "iso: ${DEBIRF_BUILDD}/${ISO}" |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | |
|---|
| 681 | ############################################################### |
|---|
| 682 | ### MAIN |
|---|
| 683 | |
|---|
| 684 | COMMAND="$1" |
|---|
| 685 | [ "$COMMAND" ] || failure "Type '$CMD help' for usage." |
|---|
| 686 | shift |
|---|
| 687 | |
|---|
| 688 | case $COMMAND in |
|---|
| 689 | 'make'|'m') |
|---|
| 690 | make "$@" |
|---|
| 691 | ;; |
|---|
| 692 | 'makeiso'|'i') |
|---|
| 693 | makeiso "$@" |
|---|
| 694 | ;; |
|---|
| 695 | 'enter'|'e') |
|---|
| 696 | enter "$@" |
|---|
| 697 | ;; |
|---|
| 698 | 'help'|'h'|'?'|'-h'|'--help') |
|---|
| 699 | usage |
|---|
| 700 | ;; |
|---|
| 701 | *) |
|---|
| 702 | failure "Unknown command: '$COMMAND' |
|---|
| 703 | Type '$CMD help' for usage." |
|---|
| 704 | ;; |
|---|
| 705 | esac |
|---|