mkinitramfs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/bash
  2. # This file based in part on the mkinitrafms script for the LFS LiveCD
  3. # written by Alexander E. Patrakov and Jeremy Huntwork.
  4. # adapted by sham
  5. copy()
  6. {
  7. local file
  8. if [ "$2" == "lib" ]; then
  9. file=$(PATH=/lib:/usr/lib type -p $1)
  10. else
  11. file=$(type -p $1)
  12. fi
  13. if [ -n $file ] ; then
  14. cp $file $WDIR/$2
  15. else
  16. echo "Missing required file: $1 for directory $2"
  17. rm -rf $WDIR
  18. exit 1
  19. fi
  20. }
  21. if [ -z $1 ] ; then
  22. INITRAMFS_FILE=initrd
  23. else
  24. KERNEL_VERSION=$1
  25. INITRAMFS_FILE=initrd-$KERNEL_VERSION
  26. sbinfiles="udevd udevadm ip"
  27. fi
  28. if [ -n "$KERNEL_VERSION" ] && [ ! -d "/lib/modules/$1" ] ; then
  29. echo "No modules directory named $1"
  30. exit 1
  31. fi
  32. printf "Creating $INITRAMFS_FILE... "
  33. binfiles="sh cat clear cut date grep cp dd hostname killall ls mkdir mknod more mount mountpoint chmod"
  34. binfiles="$binfiles umount stty sed sleep ln rm uname"
  35. sbinfiles="$sbinfiles blkid switch_root"
  36. #Optional files and locations
  37. if [ -x /sbin/mdadm ] ; then sbinfiles="$sbinfiles mdadm"; fi
  38. unsorted=$(mktemp /tmp/unsorted.XXXXXXXXXX)
  39. DATADIR=/lib/services
  40. INITIN=init.in
  41. # Create a temporrary working directory
  42. WDIR=$(mktemp -d /tmp/initrd-work.XXXXXXXXXX)
  43. # Create base directory structure
  44. # mkdir -p $WDIR/{bin,dev,lib/firmware,run,sbin,sys,proc}
  45. mkdir -p $WDIR/{bin,dev,lib/{lsb,firmware},run,sbin,sys,proc,tmp,etc,usr/share}
  46. mkdir -p $WDIR/etc/{sysconfig,modprobe.d,udev/{rules.d,hwdb.d}}
  47. touch $WDIR/etc/modprobe.d/modprobe.conf
  48. ln -s lib $WDIR/lib64
  49. # Create necessary device nodes
  50. # mknod -m 640 $WDIR/dev/console c 5 1
  51. # mknod -m 664 $WDIR/dev/null c 1 3
  52. # Install the init-functions
  53. if [ -f /lib/lsb/init-functions ]; then
  54. cp /lib/lsb/init-functions $WDIR/lib/lsb/init-functions
  55. fi
  56. if [ -n "$KERNEL_VERSION" ] ; then
  57. cp /etc/group $WDIR/etc
  58. fi
  59. cat > $WDIR/etc/fstab << "EOF"
  60. proc /proc proc nosuid,noexec,nodev 0 0
  61. sysfs /sys sysfs nosuid,noexec,nodev 0 0
  62. devpts /dev/pts devpts gid=5,mode=620 0 0
  63. tmpfs /run tmpfs defaults 0 0
  64. devtmpfs /dev devtmpfs mode=0755,nosuid 0 0
  65. EOF
  66. # echo and false are a bit wird
  67. cp /bin/echo $WDIR/bin/echo
  68. cp /bin/false $WDIR/bin/false
  69. # terminal stuff
  70. mkdir -p $WDIR/lib/ncurses/terminfo/l
  71. cp /lib/ncurses/terminfo/l/linux \
  72. $WDIR/lib/ncurses/terminfo/l
  73. ln -s ../../lib/ncurses/terminfo $WDIR/usr/share/terminfo
  74. # Install the udev configuration files
  75. if [ -f /etc/udev/udev.conf ]; then
  76. cp /etc/udev/udev.conf $WDIR/etc/udev/udev.conf
  77. fi
  78. for file in $(find /etc/udev/rules.d/ -type f) ; do
  79. cp $file $WDIR/etc/udev/rules.d
  80. done
  81. for file in $(find /etc/udev/hwdb.d/ -type f) ; do
  82. cp $file $WDIR/etc/udev/hwdb.d
  83. done
  84. cat > $WDIR/etc/udev/rules.d/55-lfs.rules << "EOF"
  85. # /etc/udev/rules.d/55-lfs.rules: Rule definitions for LFS.
  86. # Core kernel devices
  87. # This causes the system clock to be set as soon as /dev/rtc becomes available.
  88. SUBSYSTEM=="rtc", ACTION=="add", MODE="0644", RUN="do_start_clock"
  89. KERNEL=="rtc", ACTION=="add", MODE="0644", RUN="do_start_clock"
  90. # Comms devices
  91. KERNEL=="ippp[0-9]*", GROUP="dialout"
  92. KERNEL=="isdn[0-9]*", GROUP="dialout"
  93. KERNEL=="isdnctrl[0-9]*", GROUP="dialout"
  94. KERNEL=="dcbri[0-9]*", GROUP="dialout"
  95. EOF
  96. # Hostname
  97. if [ -f /etc/hostname ] ; then
  98. cp /etc/hostname $WDIR/etc/hostname
  99. else
  100. echo "milis" > $WDIR/etc/hostname
  101. fi
  102. # Clock setting
  103. if [ -f /etc/sysconfig/clock ] ; then
  104. cp /etc/sysconfig/clock $WDIR/etc/sysconfig
  105. fi
  106. # Install any firmware present
  107. # cp -a /lib/firmware $WDIR/lib
  108. # Copy the RAID configureation file if present
  109. # if [ -f /etc/mdadm.conf ] ; then
  110. # cp /etc/mdadm.conf $WDIR/etc
  111. # fi
  112. # Install the init file
  113. install -m0755 $DATADIR/$INITIN $WDIR/init
  114. binfiles="$binfiles groups"
  115. if [ -x /bin/kmod ] ; then
  116. binfiles="$binfiles kmod"
  117. else
  118. binfiles="$binfiles lsmod"
  119. sbinfiles="$sbinfiles insmod"
  120. fi
  121. # Install basic binaries
  122. for f in $binfiles ; do
  123. ldd /bin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  124. copy $f bin
  125. done
  126. # Add lvm if present
  127. if [ -x /sbin/lvm ] ; then sbinfiles="$sbinfiles lvm dmsetup"; fi
  128. for f in $sbinfiles ; do
  129. ldd /sbin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  130. copy $f sbin
  131. done
  132. # Add udevd libraries if not in /sbin
  133. if [ -x /lib/udev/udevd ] ; then
  134. ldd /lib/udev/udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  135. fi
  136. if [ -x /bin/kmod ] ; then
  137. ln -s kmod $WDIR/bin/lsmod
  138. ln -s ../bin/kmod $WDIR/sbin/insmod
  139. ln -s ../bin/kmod $WDIR/sbin/modprobe
  140. ln -s ../bin/kmod $WDIR/sbin/depmod
  141. fi
  142. # Add lvm symlinks if appropriate
  143. # Also copy the lvm.conf file
  144. if [ -x /sbin/lvm ] ; then
  145. ln -s lvm $WDIR/sbin/lvchange
  146. ln -s lvm $WDIR/sbin/lvrename
  147. ln -s lvm $WDIR/sbin/lvextend
  148. ln -s lvm $WDIR/sbin/lvcreate
  149. ln -s lvm $WDIR/sbin/lvdisplay
  150. ln -s lvm $WDIR/sbin/lvscan
  151. ln -s lvm $WDIR/sbin/pvchange
  152. ln -s lvm $WDIR/sbin/pvck
  153. ln -s lvm $WDIR/sbin/pvcreate
  154. ln -s lvm $WDIR/sbin/pvdisplay
  155. ln -s lvm $WDIR/sbin/pvscan
  156. ln -s lvm $WDIR/sbin/vgchange
  157. ln -s lvm $WDIR/sbin/vgcreate
  158. ln -s lvm $WDIR/sbin/vgscan
  159. ln -s lvm $WDIR/sbin/vgrename
  160. ln -s lvm $WDIR/sbin/vgck
  161. # Conf file(s)
  162. cp -a /etc/lvm $WDIR/etc
  163. fi
  164. # Install libraries
  165. sort $unsorted | uniq | while read library ; do
  166. if [ "$library" == "linux-vdso.so.1" ] ||
  167. [ "$library" == "linux-gate.so.1" ]; then
  168. continue
  169. fi
  170. copy $library lib
  171. done
  172. cp -a /lib/udev $WDIR/lib
  173. # The Hardware clock setting maybe could be faster done
  174. cat > $WDIR/lib/udev/do_start_clock << "EOF"
  175. #!/bin/sh -e
  176. . /lib/lsb/init-functions
  177. do_start_clock
  178. EOF
  179. chmod 755 $WDIR/lib/udev/do_start_clock
  180. # Install the kernel modules if requested
  181. if [ -n "$KERNEL_VERSION" ]; then
  182. find \
  183. /lib/modules/$KERNEL_VERSION/kernel/{arch,crypto,fs,kernel,lib,mm,security} \
  184. /lib/modules/$KERNEL_VERSION/kernel/drivers/{ata,base,block,cdrom,hid,md,firewire} \
  185. /lib/modules/$KERNEL_VERSION/kernel/drivers/{scsi,message,pcmcia,virtio,net} \
  186. /lib/modules/$KERNEL_VERSION/kernel/drivers/usb/{core,common,host,storage} \
  187. -type f 2> /dev/null | cpio --make-directories -p --quiet $WDIR
  188. cp /lib/modules/$KERNEL_VERSION/modules.{builtin,order} \
  189. $WDIR/lib/modules/$KERNEL_VERSION
  190. depmod -b $WDIR $KERNEL_VERSION
  191. cp -a /lib/firmware/rtlwifi \
  192. $WDIR/lib/firmware > /dev/null 2>&1
  193. cp -a /lib/firmware/ath6k \
  194. $WDIR/lib/firmware > /dev/null 2>&1
  195. cp -a /lib/firmware/RTL8192E \
  196. $WDIR/lib/firmware > /dev/null 2>&1
  197. cp -a /lib/firmware/bnx2x-* \
  198. $WDIR/lib/firmware > /dev/null 2>&1
  199. cp -a /lib/firmware/brcm \
  200. $WDIR/lib/firmware > /dev/null 2>&1
  201. cp -a /lib/firmware/iwlwifi-* \
  202. $WDIR/lib/firmware > /dev/null 2>&1
  203. cp -a /lib/firmware/radeon \
  204. $WDIR/lib/firmware > /dev/null 2>&1
  205. cp -a /lib/firmware/ar3k \
  206. $WDIR/lib/firmware > /dev/null 2>&1
  207. fi
  208. rm -f /boot/$INITRAMFS_FILE
  209. # cp -a /boot $WDIR
  210. # rm -v $WDIR/boot/kernel*
  211. ( cd $WDIR ; find . | cpio -o -H newc --quiet | gzip -9 ) > $INITRAMFS_FILE
  212. # Remove the temporary direc
  213. rm -rf $WDIR $unsorted
  214. mv $INITRAMFS_FILE /boot/$INITRAMFS_FILE
  215. # in case of iso
  216. # mv $INITRAMFS_FILE /ISO/isolinux/$INITRAMFS_FILE
  217. printf "done.\n"