| 1 | #!/bin/bash -e |
|---|
| 2 | |
|---|
| 3 | # make-debirf: script to build debirf system. |
|---|
| 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, |
|---|
| 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 | export FAKECHROOT_BUILD=false |
|---|
| 22 | STAGE_ROOT=true |
|---|
| 23 | STAGE_PLUGINS=true |
|---|
| 24 | STAGE_INITRD=true |
|---|
| 25 | |
|---|
| 26 | ############################################################### |
|---|
| 27 | ### FUNCTIONS |
|---|
| 28 | |
|---|
| 29 | usage() { |
|---|
| 30 | cat <<EOF |
|---|
| 31 | Usage: $CMD [options] profile [kernel.deb] |
|---|
| 32 | Create a debirf system. The 'profile' is a path to a debirf profile |
|---|
| 33 | directory. If no kernel is specified, make-debirf will use the |
|---|
| 34 | latest kernel for the specified suite. If a path to a kernel deb is |
|---|
| 35 | specified on the command line, that will be installed instead. |
|---|
| 36 | |
|---|
| 37 | options: |
|---|
| 38 | -h|--help this help message |
|---|
| 39 | -f|--fakechroot fakechroot build (other wise requires being root) |
|---|
| 40 | -c|--check-vars check variables before make |
|---|
| 41 | -n|--new create new root, even if old one exists |
|---|
| 42 | -o|--overwrite debootstrap on top of old root if it exists |
|---|
| 43 | -s|--skip skip debootstrap step if old root exists |
|---|
| 44 | -i|--initrd-only do not create root, just create initramfs |
|---|
| 45 | EOF |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | usage_profile() { |
|---|
| 49 | cat <<EOF |
|---|
| 50 | It looks like your profile is not correctly formed. You should begin |
|---|
| 51 | by copying and modifying one of the example debirf profiles. |
|---|
| 52 | On Debian, you should run: |
|---|
| 53 | cp -r /usr/share/doc/debirf/example-profiles/<profile-name> . |
|---|
| 54 | EOF |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | failure() { |
|---|
| 58 | echo "$1" >&2 |
|---|
| 59 | exit ${2:-'1'} |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | create_debootstrap() { |
|---|
| 63 | mkdir -p "$DEBIRF_ROOT" |
|---|
| 64 | if [ "$FAKECHROOT_BUILD" = 'true' ] ; then |
|---|
| 65 | fakeroot -s "$DEBIRF_FAKEROOT_STATE" fakechroot /usr/sbin/debootstrap --variant=fakechroot "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR" |
|---|
| 66 | else |
|---|
| 67 | /usr/sbin/debootstrap "$DEBIRF_DISTRO" "$DEBIRF_ROOT" "$DEBIRF_MIRROR" |
|---|
| 68 | fi |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | run_plugins() { |
|---|
| 72 | # export all the DEBIRF_* environment variables: |
|---|
| 73 | for var in ${!DEBIRF_*}; do |
|---|
| 74 | export $var |
|---|
| 75 | done |
|---|
| 76 | if [ "$FAKECHROOT_BUILD" = 'true' ] ; then |
|---|
| 77 | fakeroot -i "$DEBIRF_FAKEROOT_STATE" -s "$DEBIRF_FAKEROOT_STATE" run-parts --verbose "$DEBIRF_PLUGINS" |
|---|
| 78 | else |
|---|
| 79 | run-parts --verbose "$DEBIRF_PLUGINS" |
|---|
| 80 | fi |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | ## create_initrd functions take the name of the targeted initrd as |
|---|
| 84 | ## their first argument. |
|---|
| 85 | # stupid simple method |
|---|
| 86 | create_initrd_stupid_simple() { |
|---|
| 87 | #ln sbin/init to /init |
|---|
| 88 | ln -sf /sbin/init "$DEBIRF_ROOT/init" |
|---|
| 89 | |
|---|
| 90 | if [ "$FAKECHROOT_BUILD" = 'true' ] ; then |
|---|
| 91 | # create a proper dev tree, based on the host systems dev tree |
|---|
| 92 | fakeroot -i "$DEBIRF_FAKEROOT_STATE" -s "$DEBIRF_FAKEROOT_STATE" bash -c 'rm -rf '"$DEBIRF_ROOT"'/dev; (cd / && find dev | cpio --create) | (cd '"$DEBIRF_ROOT"'/ && cpio --extract)' |
|---|
| 93 | #fakeroot -i "$DEBIRF_FAKEROOT_STATE" -s "$DEBIRF_FAKEROOT_STATE" bash -c 'rm -rf '"$DEBIRF_ROOT"'/dev; mkdir '"$DEBIRF_ROOT"'/dev' |
|---|
| 94 | |
|---|
| 95 | # create root image |
|---|
| 96 | ( cd "$DEBIRF_ROOT" && find * | fakeroot cpio --create -H newc | gzip ) > "$1" |
|---|
| 97 | else |
|---|
| 98 | # create root image |
|---|
| 99 | ( cd "$DEBIRF_ROOT" && find * | cpio --create -H newc | gzip ) > "$1" |
|---|
| 100 | fi |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | # nested cpio archives |
|---|
| 104 | create_initrd_nested() { |
|---|
| 105 | local util lib |
|---|
| 106 | local NEST_ROOT="$DEBIRF_BUILDD"/nest |
|---|
| 107 | |
|---|
| 108 | # make the nested root |
|---|
| 109 | rm -rf "$NEST_ROOT" |
|---|
| 110 | mkdir -p "$NEST_ROOT"/{bin,lib} |
|---|
| 111 | |
|---|
| 112 | # copy needed executables into nest |
|---|
| 113 | cp -f /bin/{busybox,cpio} "$NEST_ROOT"/bin/ |
|---|
| 114 | for util in sh mkdir mount ls gunzip ; do |
|---|
| 115 | ln "$NEST_ROOT"/bin/busybox "$NEST_ROOT"/bin/"$util" |
|---|
| 116 | done |
|---|
| 117 | cp -f /usr/lib/klibc/bin/run-init "$NEST_ROOT"/bin/ |
|---|
| 118 | |
|---|
| 119 | # copy in needed libraries |
|---|
| 120 | for lib in $(ldd "$NEST_ROOT"/bin/* | egrep '*.so.[[:digit:]]+ \(0x[[:xdigit:]]{8}\)$' | sed -r 's|.*[[:space:]](/[^[[:space:]]*)[[:space:]]\(0x[[:xdigit:]]{8}\)$|\1|' | sort -u) ; do |
|---|
| 121 | # pull libraries from most basic place libraries can live |
|---|
| 122 | # (avoid arch change between build env and debirf) |
|---|
| 123 | lib=/lib/$(basename "$lib") |
|---|
| 124 | echo -e "$lib\n$(readlink -f $lib)" | cpio --pass-through --make-directories "$NEST_ROOT"/ |
|---|
| 125 | done |
|---|
| 126 | cp -f /lib/klibc-* "$NEST_ROOT"/lib/ |
|---|
| 127 | |
|---|
| 128 | # create nest init |
|---|
| 129 | cat > "$NEST_ROOT"/init <<EOF |
|---|
| 130 | #!/bin/sh |
|---|
| 131 | mkdir /newroot |
|---|
| 132 | mount -t tmpfs tmpfs /newroot |
|---|
| 133 | cd /newroot |
|---|
| 134 | # specify /bin/cpio so that it gets used instead of the busybox builtin |
|---|
| 135 | gunzip - </rootfs.cgz | /bin/cpio -i |
|---|
| 136 | echo unpacking rootfs... |
|---|
| 137 | exec /bin/run-init . /sbin/init <./dev/console >./dev/console |
|---|
| 138 | EOF |
|---|
| 139 | chmod a+x "$NEST_ROOT"/init |
|---|
| 140 | |
|---|
| 141 | msg "creating rootfs.cgz..." |
|---|
| 142 | create_initrd_stupid_simple "$NEST_ROOT"/rootfs.cgz |
|---|
| 143 | |
|---|
| 144 | msg "creating wrapper cgz..." |
|---|
| 145 | ( cd "$NEST_ROOT" && find * | fakeroot cpio --create -H newc | gzip ) > "$1" |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | # tweak initrd method: untested, probably doesn't work right yet. soon! |
|---|
| 149 | create_initrd_tweak_initrd() { |
|---|
| 150 | cat "$DEBIRF_ROOT/scripts/debirf" <<EOF |
|---|
| 151 | mountroot() { |
|---|
| 152 | rootmnt="/" |
|---|
| 153 | } |
|---|
| 154 | EOF |
|---|
| 155 | cat > "$DEBIRF_ROOT/bin/run-init" <<EOF |
|---|
| 156 | #!/bin/sh |
|---|
| 157 | if [ "$1" != "/" ] ; then |
|---|
| 158 | exec /usr/lib/klibc/bin/run-init "$@" |
|---|
| 159 | else |
|---|
| 160 | MOUNT_POINT="$1" |
|---|
| 161 | INIT="$2" |
|---|
| 162 | shift 2 |
|---|
| 163 | exec "$INIT" "$@" |
|---|
| 164 | fi |
|---|
| 165 | EOF |
|---|
| 166 | # create root image |
|---|
| 167 | cp initrd.img* "$1" |
|---|
| 168 | ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) >> "$1" |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | # unionfs method: also untested and probably not working. |
|---|
| 172 | create_initrd_unionfs() { |
|---|
| 173 | #ln sbin/init to /init |
|---|
| 174 | ln -sf /sbin/init "$DEBIRF_ROOT/init" |
|---|
| 175 | |
|---|
| 176 | # add unionfs kernel module and init script |
|---|
| 177 | debirf_exec apt-get update |
|---|
| 178 | debirf_exec apt-get install unionfs-modules-"$KERNAVAIL" unionfs-tools |
|---|
| 179 | |
|---|
| 180 | cat >"$DEBIRF_ROOT/etc/init.d/root-unionfs" <<EOF |
|---|
| 181 | #!/bin/bash |
|---|
| 182 | |
|---|
| 183 | mount -t tmpfs tmpfs /tmp |
|---|
| 184 | mount -t unionfs dirs=/=ro:/tmp=rw unionfs / |
|---|
| 185 | EOF |
|---|
| 186 | |
|---|
| 187 | ln -sf ../init.d/root-unionfs /etc/rcS.d/S00root-unionfs |
|---|
| 188 | |
|---|
| 189 | # create root image |
|---|
| 190 | ( cd "$DEBIRF_ROOT" && find * | cpio -H newc --create | gzip ) > "$1" |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | ############################################################### |
|---|
| 194 | ### MAIN |
|---|
| 195 | |
|---|
| 196 | # option parsing |
|---|
| 197 | TEMP=$(getopt --options -hfcnosi --longoptions help,fakechroot,check-vars,new,overwrite,skip,initrd-only -n "$CMD" -- "$@") |
|---|
| 198 | |
|---|
| 199 | if [ $? != 0 ] ; then |
|---|
| 200 | echo "Invalid options." >&2 |
|---|
| 201 | usage |
|---|
| 202 | exit 1 |
|---|
| 203 | fi |
|---|
| 204 | |
|---|
| 205 | # Note the quotes around `$TEMP': they are essential! |
|---|
| 206 | eval set -- "$TEMP" |
|---|
| 207 | |
|---|
| 208 | while true ; do |
|---|
| 209 | case "$1" in |
|---|
| 210 | -h|--help) |
|---|
| 211 | usage |
|---|
| 212 | exit 0 |
|---|
| 213 | ;; |
|---|
| 214 | -f|--fakechroot) |
|---|
| 215 | echo "Using fakechroot build." |
|---|
| 216 | FAKECHROOT_BUILD=true |
|---|
| 217 | shift 1 |
|---|
| 218 | ;; |
|---|
| 219 | -c|--check-vars) |
|---|
| 220 | CHECK_VARS=true |
|---|
| 221 | shift 1 |
|---|
| 222 | ;; |
|---|
| 223 | -n|--new) |
|---|
| 224 | WRITE_MODE=rewrite |
|---|
| 225 | shift 1 |
|---|
| 226 | ;; |
|---|
| 227 | -o|--overwrite) |
|---|
| 228 | WRITE_MODE=overwrite |
|---|
| 229 | shift 1 |
|---|
| 230 | ;; |
|---|
| 231 | -s|--skip) |
|---|
| 232 | WRITE_MODE=skip |
|---|
| 233 | shift 1 |
|---|
| 234 | ;; |
|---|
| 235 | -i|--initrd-only) |
|---|
| 236 | STAGE_ROOT=false |
|---|
| 237 | STAGE_PLUGINS=false |
|---|
| 238 | shift 1 |
|---|
| 239 | ;; |
|---|
| 240 | --) |
|---|
| 241 | shift |
|---|
| 242 | ;; |
|---|
| 243 | *) |
|---|
| 244 | if (( $# < 1 )) ; then |
|---|
| 245 | echo "Improper number of input arguments." |
|---|
| 246 | usage |
|---|
| 247 | exit 1 |
|---|
| 248 | fi |
|---|
| 249 | DEBIRF_PROFILE="$1" |
|---|
| 250 | if [ "$2" ] ; then |
|---|
| 251 | DEBIRF_KERNEL_PACKAGE="$2" |
|---|
| 252 | fi |
|---|
| 253 | break |
|---|
| 254 | ;; |
|---|
| 255 | esac |
|---|
| 256 | done |
|---|
| 257 | |
|---|
| 258 | # source debirf.conf defaults |
|---|
| 259 | source /usr/share/debirf/debirf.conf.defaults |
|---|
| 260 | |
|---|
| 261 | # check profile |
|---|
| 262 | if [ -d "$DEBIRF_PROFILE" ] ; then |
|---|
| 263 | echo "Loading profile '$DEBIRF_PROFILE'..." |
|---|
| 264 | DEBIRF_CONF="$DEBIRF_PROFILE/debirf.conf" |
|---|
| 265 | DEBIRF_PLUGINS="$DEBIRF_PROFILE/plugins" |
|---|
| 266 | else |
|---|
| 267 | echo "$DEBIRF_PROFILE not found." |
|---|
| 268 | usage_profile |
|---|
| 269 | exit 1 |
|---|
| 270 | fi |
|---|
| 271 | |
|---|
| 272 | # source profile debirf.conf |
|---|
| 273 | if [ -f "$DEBIRF_CONF" ] ; then |
|---|
| 274 | source "$DEBIRF_CONF" |
|---|
| 275 | else |
|---|
| 276 | echo "$DEBIRF_CONF not found." |
|---|
| 277 | usage_profile |
|---|
| 278 | exit 1 |
|---|
| 279 | fi |
|---|
| 280 | |
|---|
| 281 | # check plugins directory |
|---|
| 282 | if [ ! -d "$DEBIRF_PLUGINS" ] ; then |
|---|
| 283 | echo "$DEBIRF_PLUGINS does not exist." |
|---|
| 284 | usage_profile |
|---|
| 285 | exit 1 |
|---|
| 286 | fi |
|---|
| 287 | if [ $(ls "$DEBIRF_PLUGINS" -1 | wc -l) = 0 ] ; then |
|---|
| 288 | echo "$DEBIRF_PLUGINS is empty." |
|---|
| 289 | usage_profile |
|---|
| 290 | exit 1 |
|---|
| 291 | fi |
|---|
| 292 | |
|---|
| 293 | # check buildd |
|---|
| 294 | if [ -z "$DEBIRF_BUILDD" ] ; then |
|---|
| 295 | failure "DEBIRF_BUILDD is not set." |
|---|
| 296 | fi |
|---|
| 297 | |
|---|
| 298 | # check debirf root |
|---|
| 299 | if [ -z "$DEBIRF_ROOT" ] ; then |
|---|
| 300 | failure "DEBIRF_ROOT is not set." |
|---|
| 301 | fi |
|---|
| 302 | |
|---|
| 303 | # set fakechroot save file |
|---|
| 304 | DEBIRF_FAKEROOT_STATE="$DEBIRF_BUILDD/.fakechroot-$DEBIRF_LABEL.save" |
|---|
| 305 | |
|---|
| 306 | # check variables |
|---|
| 307 | if [ "$CHECK_VARS" ] ; then |
|---|
| 308 | echo "Debirf variables:" |
|---|
| 309 | for var in ${!DEBIRF_*}; do |
|---|
| 310 | if [ $var ] ; then |
|---|
| 311 | export $var |
|---|
| 312 | else |
|---|
| 313 | failure "Variable '$var' not properly set." |
|---|
| 314 | fi |
|---|
| 315 | done |
|---|
| 316 | env | /bin/grep "^DEBIRF_" |
|---|
| 317 | read -p "enter to continue: " OK |
|---|
| 318 | fi |
|---|
| 319 | |
|---|
| 320 | ### BUILD ROOT |
|---|
| 321 | if [ "$STAGE_ROOT" = 'true' ] ; then |
|---|
| 322 | |
|---|
| 323 | # check if specified kernel package exists |
|---|
| 324 | if [ ! -f "$DEBIRF_KERNEL_PACKAGE" ] ; then |
|---|
| 325 | failure "Kernel package '$DEBIRF_KERNEL_PACKAGE' not found." |
|---|
| 326 | fi |
|---|
| 327 | |
|---|
| 328 | # determine write mode |
|---|
| 329 | if [ -d "$DEBIRF_ROOT" ] ; then |
|---|
| 330 | echo "Directory $DEBIRF_ROOT already exists." |
|---|
| 331 | if [ -z "$WRITE_MODE" ] ; then |
|---|
| 332 | echo "Select one of the following:" |
|---|
| 333 | select foo in 'new' 'overwrite' 'skip' 'exit' ; do |
|---|
| 334 | case "$foo" in |
|---|
| 335 | 'new') |
|---|
| 336 | WRITE_MODE=rewrite |
|---|
| 337 | ;; |
|---|
| 338 | 'overwrite') |
|---|
| 339 | WRITE_MODE=overwrite |
|---|
| 340 | ;; |
|---|
| 341 | 'skip') |
|---|
| 342 | WRITE_MODE=skip |
|---|
| 343 | ;; |
|---|
| 344 | *) |
|---|
| 345 | failure "aborting." |
|---|
| 346 | ;; |
|---|
| 347 | esac |
|---|
| 348 | break |
|---|
| 349 | done |
|---|
| 350 | fi |
|---|
| 351 | else |
|---|
| 352 | WRITE_MODE=new |
|---|
| 353 | fi |
|---|
| 354 | case "$WRITE_MODE" in |
|---|
| 355 | 'new') |
|---|
| 356 | msg "creating debirf root..." |
|---|
| 357 | create_debootstrap |
|---|
| 358 | ;; |
|---|
| 359 | 'rewrite') |
|---|
| 360 | msg "clearing old debirf root..." |
|---|
| 361 | rm -rf "$DEBIRF_ROOT" |
|---|
| 362 | msg "creating debirf root..." |
|---|
| 363 | create_debootstrap |
|---|
| 364 | ;; |
|---|
| 365 | 'overwrite') |
|---|
| 366 | msg "overwriting old debirf root..." |
|---|
| 367 | create_debootstrap |
|---|
| 368 | ;; |
|---|
| 369 | 'skip') |
|---|
| 370 | msg "skipping debootstrap..." |
|---|
| 371 | ;; |
|---|
| 372 | *) |
|---|
| 373 | failure "aborting." |
|---|
| 374 | ;; |
|---|
| 375 | esac |
|---|
| 376 | else |
|---|
| 377 | echo "Not building root." |
|---|
| 378 | fi |
|---|
| 379 | ### END BUILD ROOT |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | ### RUN PLUGINS |
|---|
| 383 | if [ "$STAGE_PLUGINS" = 'true' ] ; then |
|---|
| 384 | setup_debirf_info |
|---|
| 385 | msg "executing plugins..." |
|---|
| 386 | run_plugins |
|---|
| 387 | msg "plugins complete." |
|---|
| 388 | else |
|---|
| 389 | echo "Not running plugins." |
|---|
| 390 | fi |
|---|
| 391 | ### END RUN PLUGINS |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | ### BUILD INITRD |
|---|
| 395 | if [ "$STAGE_INITRD" = 'true' ] ; then |
|---|
| 396 | if [ ! -d "$DEBIRF_ROOT" ] ; then |
|---|
| 397 | failure "Root not found." |
|---|
| 398 | fi |
|---|
| 399 | # determine initrd name |
|---|
| 400 | KERNEL_VERS=$(ls -1 "$DEBIRF_ROOT/lib/modules" | head -n1) |
|---|
| 401 | DEBIRF_INITRD="${DEBIRF_LABEL}_${DEBIRF_DISTRO}_${KERNEL_VERS}.cgz" |
|---|
| 402 | |
|---|
| 403 | msg "creating debirf initrd ('$DEBIRF_METHOD')..." |
|---|
| 404 | create_initrd_${DEBIRF_METHOD} "$DEBIRF_BUILDD/$DEBIRF_INITRD" |
|---|
| 405 | |
|---|
| 406 | # final output |
|---|
| 407 | DEBIRF_KERNEL=$(ls "$DEBIRF_BUILDD" | grep "vmlinu" | grep "$KERNEL_VERS$") |
|---|
| 408 | msg "debirf initrd created." |
|---|
| 409 | if [ "$DEBIRF_BUILDD/$DEBIRF_KERNEL" ] ; then |
|---|
| 410 | msg "kernel: $DEBIRF_BUILDD/$DEBIRF_KERNEL" |
|---|
| 411 | fi |
|---|
| 412 | msg "initrd: $DEBIRF_BUILDD/$DEBIRF_INITRD" |
|---|
| 413 | else |
|---|
| 414 | echo "Not creating initramfs." |
|---|
| 415 | fi |
|---|
| 416 | ### END BUILD INITRD |
|---|