選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mkinitrd-net 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. # initrd builder for network booting
  5. # Utility function to determine whether or not a filesystem is usable for
  6. # loopback mounts. Lifted verbatim from Erik Troan's mkinitrd script.
  7. #
  8. is_good_fs() {
  9. local parttype= tmpname=
  10. local dir=$1
  11. [[ -d $dir ]] || return 1
  12. [[ -w $dir ]] || return 1
  13. [[ $dir == */ ]] && dir=${dir%/}
  14. parttype=$(awk "{if (\$2 == \""$dir"\") print \$3 }" /proc/mounts)
  15. while tmpname=${dir%/*} && [[ -z $parttype ]];do
  16. [[ -z $tmpname ]] && tmpname=/
  17. parttype=$(awk "{if (\$2 == \""$tmpname"\") print \$3 }" /proc/mounts)
  18. dir=$tmpname
  19. done
  20. case $parttype in
  21. nfs|tmpfs) return 1;;
  22. *) return 0;
  23. esac
  24. }
  25. # Find a suitable temporary directory (i.e. not tmpfs or nfs)
  26. if is_good_fs $TMPDIR; then
  27. tmpdir=$TMPDIR
  28. elif is_good_fs /tmp; then
  29. tmpdir=/tmp
  30. elif is_good_fs /var/tmp; then
  31. tmpdir=/var/tmp
  32. elif is_good_fs /root/tmp; then
  33. tmpdir=/root/tmp
  34. else
  35. echo "Cannot use a tmp directory" >&2
  36. exit 1
  37. fi
  38. # Default settings (some can be overridden by command-line options)
  39. include_modules=include-modules
  40. initrd_skel=/usr/lib/mkinitrd-net/initrd-skel
  41. kernel_ver=`uname -r`
  42. use_sudo=y
  43. keep=n
  44. output_dir=/var/lib/tftpboot
  45. make_link=y
  46. quiet=
  47. # No need to use sudo if we are root
  48. if [ $UID -eq 0 ]; then
  49. use_sudo=n
  50. fi
  51. USAGE="Usage: $0 [-k|--kernel <kernel_ver>] [-n|--nolink] [-q|--quiet] [-l|--local] [--nosudo] [--keep] [--help] module_list ..."
  52. # Parse command-line options
  53. while [ $# -gt 0 ]; do
  54. case "$1" in
  55. -l|--local)
  56. shift
  57. use_local=y ;;
  58. -k|--kernel)
  59. shift
  60. kernel_ver=$1
  61. shift ;;
  62. --nosudo) shift ; use_sudo=n ;;
  63. --keep) shift ; keep=y ;;
  64. --n|--nolink)
  65. shift ; make_link=n ;;
  66. -q|--quiet) shift ; quiet=-q ;;
  67. --help) shift ; do_help=y ;;
  68. --) shift ; break ;;
  69. -*) echo "${0}: ${1}: invalid option" >&2
  70. echo $USAGE >& 2
  71. exit 2 ;;
  72. *) break ;;
  73. esac
  74. done
  75. # Build list of requested modules
  76. modules="$*"
  77. requested_modules="$modules"
  78. modules="$modules nfs" # Always require nfs for nfs mount
  79. modules="$modules af_packet" # Always require af_packet for udhcpc
  80. # --help => Print help message
  81. if [ "$do_help" == "y" ]; then
  82. echo $USAGE
  83. echo " -k, --kernel Specify kernel version"
  84. echo " -n, --nolink Do not create a matching symbolic link"
  85. echo " -l, --local Run locally from CVS (for developers only)"
  86. echo " --nosudo Do not use sudo (i.e. must run as root instead)"
  87. echo " --keep Keep temporary files instead of deleting them"
  88. exit 0;
  89. fi
  90. # --local => we are running directly from CVS, rather than
  91. # from an installed copy, so use local files and directories
  92. if [ "$use_local" == "y" ]; then
  93. include_modules=./include-modules
  94. initrd_skel=initrd-skel
  95. output_dir=tftpboot
  96. fi
  97. # If use_sudo is set, check that sudo exists
  98. sudo=/usr/bin/sudo
  99. if [ "$use_sudo" == "y" ]; then
  100. if [ ! -x $sudo ]; then
  101. use_sudo=n
  102. echo "WARNING: --nosudo not specified but $sudo not found"
  103. fi
  104. fi
  105. if [ "$use_sudo" == "n" ]; then
  106. sudo=
  107. fi
  108. # Create temporary working files
  109. initrd=`mktemp -d ${tmpdir}/initrd.XXXXXX`
  110. initrdimg=`mktemp ${tmpdir}/initrd.img.XXXXXX`
  111. initrdmnt=`mktemp -d ${tmpdir}/initrd.mnt.XXXXXX`
  112. # Copy skeleton into temporary area
  113. cp -a $initrd_skel/* $initrd/
  114. mkdir -p $initrd/lib/modules/$kernel_ver
  115. $include_modules $quiet -k $kernel_ver -d $initrd/lib/modules/$kernel_ver $modules > $initrd/bin/insert-modules || exit 1
  116. chmod 755 $initrd/bin/insert-modules
  117. # Create empty ext2fs image file
  118. dd if=/dev/zero bs=1k of=$initrdimg count=$((`du -sk $initrd | cut -f1` * 7 / 6)) 2> /dev/null
  119. /sbin/mke2fs -q -F $initrdimg 2> /dev/null
  120. # Mount image file, copy files on, create /dev entries, display free space, umount
  121. $sudo mount -o loop $initrdimg $initrdmnt
  122. cp -a $initrd/* $initrdmnt/
  123. $sudo mknod $initrdmnt/dev/console c 5 1
  124. $sudo mknod $initrdmnt/dev/null c 1 3
  125. $sudo mknod $initrdmnt/dev/ram b 1 1
  126. $sudo mknod $initrdmnt/dev/systty c 4 0
  127. for i in 1 2 3 4; do $sudo mknod $initrdmnt/dev/tty$i c 4 $i; done
  128. if [ "$quiet" == "n" ]; then
  129. df -h $initrdmnt
  130. fi
  131. $sudo umount $initrdmnt
  132. # Create output file
  133. initrd_suffix=`echo $requested_modules | tr " " .`
  134. gzip -9 -n -c $initrdimg > $output_dir/initrd-$initrd_suffix.$kernel_ver.img
  135. # Create symlink
  136. if [ "$make_link" == "y" ]; then
  137. link=$output_dir/initrd-$initrd_suffix.img
  138. [ -L $link ] && rm -f $link
  139. ln -s initrd-$initrd_suffix.$kernel_ver.img $link
  140. fi
  141. # Remove temporary files
  142. if [ "$keep" == "n" ]; then
  143. rm -rf $initrd
  144. rm -f $initrdimg
  145. rmdir $initrdmnt
  146. fi