mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.8 KiB

  1. #!/bin/bash
  2. kernel=`uname -r`
  3. targetdir=`mktemp -d`
  4. modprobeopt="-n"
  5. [ "${kernel:0:3}" == "2.6" ] && modprobeopt="--show-depends"
  6. empty=0
  7. rootdir=""
  8. cross_compile=""
  9. initrdfs="cramfs"
  10. block_size=""
  11. ramdisk_size=8192
  12. # Successfully tested combinations: <arch> <blocksize> <initrdfs>
  13. # ARM (QEMU) 1024 ext2fs
  14. # ARM (QEMU) 4096 cramfs
  15. # x86 (QEMU) 1024 ext2fs
  16. # x86 (QEMU) 4096 cramfs
  17. while [ ${#} -gt 0 ] ; do
  18. case "${1}" in
  19. empty) empty=1 ;;
  20. -root)
  21. if [ -d "${2}/" ]; then
  22. rootdir="${2%/}" ; shift
  23. else
  24. echo "Can't open ${2}: No such directory."
  25. echo "Usage: ${0} [ kernel-version ]"
  26. exit 1
  27. fi
  28. ;;
  29. -cross) cross_compile="${2}" ; shift ;;
  30. -bs) block_size="${2}" ; shift ;;
  31. -fs)
  32. case "${2}" in
  33. ext2fs|ext3fs|cramfs|ramfs) initrdfs="${2}" ; shift ;;
  34. *)
  35. echo "Filesystem ${2} not supported as initrd filesystem."
  36. exit 1
  37. ;;
  38. esac
  39. ;;
  40. *)
  41. if [ -d "${rootdir}/lib/modules/${1}" ]; then
  42. kernel="${1}"
  43. echo "kernel ${kernel}"
  44. else
  45. echo "Can't open ${rootdir}/lib/modules/${1}: No such directory."
  46. echo "Usage: ${0} [ kernel-version ]"
  47. exit 1
  48. fi
  49. ;;
  50. esac
  51. shift
  52. done
  53. case ${initrdfs} in
  54. ext2fs|ext3fs|cramfs)
  55. initrd_img="${rootdir}/boot/initrd-${kernel}.img"
  56. ;;
  57. ramfs)
  58. initrd_img="${rootdir}/boot/initrd-${kernel}.cpio"
  59. ;;
  60. esac
  61. echo "Creating ${initrd_img} ..."
  62. mkdir -p ${targetdir}/etc/conf
  63. if [ "${empty}" = 0 ] ; then
  64. grep '^modprobe ' ${rootdir}/etc/conf/kernel | grep -v 'no-initrd' | \
  65. sed 's,[ ]#.*,,' | \
  66. while read a b ; do ${a} ${modprobeopt} -v ${b} 2> /dev/null; done |
  67. while read a b c; do
  68. [[ "${b}" = *.ko ]] && b=${b/.ko/};
  69. b="`find ${rootdir}/lib/modules/${kernel} -wholename "${b}.o" -o -wholename "${b}.ko"`"
  70. echo "Adding ${b}."
  71. mkdir -p ${targetdir}/${b%/*}
  72. cp ${b} ${targetdir}/${b}
  73. echo "/sbin/insmod ${b} ${c}" >> ${targetdir}/etc/conf/kernel
  74. done
  75. fi
  76. mkdir -p ${targetdir}/{dev,root,tmp,proc,sys}
  77. mknod ${targetdir}/dev/ram0 b 1 0
  78. mknod ${targetdir}/dev/null c 1 3
  79. mknod ${targetdir}/dev/zero c 1 5
  80. mknod ${targetdir}/dev/tty c 5 0
  81. mknod ${targetdir}/dev/console c 5 1
  82. # this copies a set of programs and the necessary libraries into a
  83. # chroot environment
  84. echo -n "Checking necessary fsck programs ... "
  85. while read dev a mnt b fs c ; do
  86. [ -e "${rootdir}/sbin/fsck.${fs}" ] && echo "/sbin/fsck.${fs} /sbin/fsck.${fs}"
  87. done < <( mount ) | sort | uniq >/etc/conf/initrd/initrd_fsck
  88. echo "/sbin/fsck /sbin/fsck" >>/etc/conf/initrd/initrd_fsck
  89. echo "done"
  90. libdirs="${rootdir}/lib `sed -e"s,^\(.*\),${rootdir}\1," ${rootdir}/etc/ld.so.conf | tr '\n' ' '`"
  91. needed_libs() {
  92. local x="${1}" library
  93. ${cross_compile}readelf -d ${x} 2>/dev/null | grep "(NEEDED)" |
  94. sed -e"s,.*Shared library: \[\(.*\)\],\1," |
  95. while read library ; do
  96. find ${libdirs} -name "${library}" 2>/dev/null |
  97. sed -e "s,^${rootdir},,g" | tr '\n' ' '
  98. done
  99. }
  100. echo -n "Copying other files ... "
  101. for x in ${rootdir}/etc/conf/initrd/initrd_* ; do
  102. [ -f ${x} ] || continue
  103. while read file target ; do
  104. file="${rootdir}/${file}"
  105. [ -e ${file} ] || continue
  106. while read f ; do
  107. tfile=${targetdir}/${target}${f#${file}}
  108. [ -e ${tfile} ] && continue
  109. if [ -d ${f} -a ! -L ${f} ] ; then
  110. mkdir -p ${tfile}
  111. continue
  112. else
  113. mkdir -p ${tfile%/*}
  114. fi
  115. # if [ -b ${f} -o -c ${f} -o -p ${f} -o -L ${f} ] ; then
  116. cp -a ${f} ${tfile}
  117. # else
  118. # cp ${f} ${tfile}
  119. # fi
  120. file -L ${f} | grep -q ELF || continue
  121. libs="${libs} `needed_libs ${f}`"
  122. done < <( find "${file}" )
  123. done < ${x}
  124. done
  125. echo "done"
  126. echo -n "Copying required libraries ... "
  127. while [ -n "${libs}" ] ; do
  128. oldlibs=${libs}
  129. libs=""
  130. for x in ${oldlibs} ; do
  131. [ -e ${targetdir}/${x} ] && continue
  132. mkdir -p ${targetdir}/${x%/*}
  133. cp ${rootdir}/${x} ${targetdir}/${x}
  134. file -L ${rootdir}/${x} | grep -q ELF || continue
  135. for y in `needed_libs ${rootdir}/${x}` ; do
  136. [ ! -e "${targetdir}/${y}" ] && libs="${libs} ${y}"
  137. done
  138. done
  139. done
  140. echo "done"
  141. echo "Creating links for identical files ..."
  142. while read ck fn
  143. do
  144. # don't link empty files...
  145. if [ "${oldck}" = "${ck}" -a -s "${fn}" ] ; then
  146. echo "\`- Found ${fn#${targetdir}} -> ${oldfn#${targetdir}}."
  147. rm ${fn} ; ln -s /${oldfn#${targetdir}} ${fn}
  148. else
  149. oldck=${ck} ; oldfn=${fn}
  150. fi
  151. done < <( find ${targetdir} -type f | xargs md5sum | sort )
  152. # though this is not clean, it helps avoid a warning from fsck about
  153. # it being unable to determine wether a filesystem is mounted.
  154. ln -s /proc/mounts ${targetdir}/etc/mtab
  155. echo "Creating initrd filesystem image (${initrdfs}): "
  156. case "${initrdfs}" in
  157. cramfs)
  158. [ "${block_size}" == "" ] && block_size=4096
  159. /sbin/mkfs.cramfs -b ${block_size} ${targetdir} ${initrd_img}
  160. ;;
  161. ramfs)
  162. # cp -a ${targetdir}/{linuxrc,init}
  163. ( cd ${targetdir} ; find | cpio -o -c > ${initrd_img} ; )
  164. ;;
  165. ext2fs|ext3fs)
  166. [ "${block_size}" == "" ] && block_size=1024
  167. block_count=$(( ( 1024 * ${ramdisk_size} ) / ${block_size} ))
  168. echo "Creating temporary files."
  169. tmpdir=`mktemp -d` ; mkdir -p ${tmpdir}
  170. dd if=/dev/zero of=${initrd_img} bs=${block_size} count=${block_count} &> /dev/null
  171. tmpdev="`losetup -f 2>/dev/null`"
  172. if [ -z "${tmpdev}" ] ; then
  173. for x in /dev/loop* /dev/loop/* ; do
  174. [ -b "${x}" ] || continue
  175. losetup ${x} 2>&1 >/dev/null || tmpdev="${x}"
  176. [ -n "${tmpdev}" ] && break
  177. done
  178. if [ -z "${tmpdev}" ] ; then
  179. echo "No free loopback device found!"
  180. rm -f ${tmpfile} ; rmdir ${tmpdir}; exit 1
  181. fi
  182. fi
  183. echo "Using loopback device ${tmpdev}."
  184. losetup "${tmpdev}" ${initrd_img}
  185. echo "Writing initrd image file."
  186. /sbin/mkfs.${initrdfs:0:4} -b ${block_size} -m 0 -N 360 -q ${tmpdev} &> /dev/null
  187. mount -t ${initrdfs:0:4} ${tmpdev} ${tmpdir}
  188. rmdir ${tmpdir}/lost+found/
  189. cp -a ${targetdir}/* ${tmpdir}
  190. umount ${tmpdir}
  191. echo "Removing temporary files."
  192. losetup -d ${tmpdev}
  193. rm -rf ${tmpdir}
  194. ;;
  195. esac
  196. echo "Compressing initrd image file."
  197. gzip -9 -c ${initrd_img} > ${initrd_img}.gz
  198. rm -rf ${targetdir}
  199. echo "Done."