This repo contains software to build linux from scratch and package it into rpm files.
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.
 
 
 
 

310 lines
10 KiB

  1. #-----------------------------------------------------------------------------
  2. Summary: mkinitramfs creates images for use as initial ramdisk (initrd) images.
  3. Name: mkinitramfs
  4. Version: 9.1
  5. Release: 1
  6. License: Any
  7. URL: www.linuxfromscratch.org/blfs
  8. Group: LFS/Base
  9. Requires: filesystem
  10. Requires: cpio
  11. %description
  12. The only purpose of an initramfs is to mount the root filesystem.
  13. The initramfs is a complete set of directories that you would find on a normal
  14. root filesystem. It is bundled into a single cpio archive and compressed
  15. with one of several compression algorithms.
  16. At boot time, the boot loader loads the kernel and the initramfs image into
  17. memory and starts the kernel. The kernel checks for the presence of the
  18. initramfs and, if found, mounts it as / and runs /init. The init program is
  19. typically a shell script. Note that the boot process takes longer, possibly
  20. significantly longer, if an initramfs is used.
  21. For most distributions, kernel modules are the biggest reason to have an
  22. initramfs. In a general distribution, there are many unknowns such as
  23. file system types and disk layouts. In a way, this is the opposite of LFS
  24. where the system capabilities and layout are known and a custom kernel is
  25. normally built. In this situation, an initramfs is rarely needed.
  26. There are only four primary reasons to have an initramfs in the
  27. LFS environment: loading the rootfs from a network, loading it from an
  28. LVM logical volume, having an encrypted rootfs where a password is required,
  29. or for the convenience of specifying the rootfs as a LABEL or UUID.
  30. Anything else usually means that the kernel was not configured properly.
  31. #-----------------------------------------------------------------------------
  32. %prep
  33. %build
  34. %install
  35. #-----------------------------------------------------------------------------
  36. install -vdm 755 %{buildroot}/sbin
  37. cat > %{buildroot}/sbin/mkinitramfs <<- "EOF"
  38. #!/bin/bash
  39. # This file based in part on the mkinitramfs script for the LFS LiveCD
  40. # written by Alexander E. Patrakov and Jeremy Huntwork.
  41. # set -o errexit # exit if error...insurance ;)
  42. # set -o nounset # exit if variable not initalized
  43. # set +h # disable hashall
  44. copy() {
  45. local file
  46. if [ "$2" == "lib" ]; then
  47. file=$(PATH=/lib:/usr/lib type -p $1)
  48. else
  49. file=$(type -p $1)
  50. fi
  51. if [ -n $file ] ; then
  52. cp $file $WDIR/$2
  53. else
  54. echo "Missing required file: $1 for directory $2"
  55. rm -rf $WDIR
  56. exit 1
  57. fi
  58. }
  59. if [ -z $1 ] ; then
  60. INITRAMFS_FILE=initrd.img-no-kmods
  61. else
  62. KERNEL_VERSION=$1
  63. INITRAMFS_FILE=initrd.img-$KERNEL_VERSION
  64. fi
  65. if [ -n "$KERNEL_VERSION" ] && [ ! -d "/lib/modules/$1" ] ; then
  66. echo "No modules directory named $1"
  67. exit 1
  68. fi
  69. printf "Creating $INITRAMFS_FILE... "
  70. binfiles="sh cat cp dd killall ls mkdir mknod mount "
  71. binfiles="$binfiles umount sed sleep ln rm uname"
  72. binfiles="$binfiles readlink basename"
  73. #
  74. # Systemd installs udevadm in /bin. Other udev implementations have it in /sbin
  75. if [ -x /bin/udevadm ] ; then binfiles="$binfiles udevadm"; fi
  76. sbinfiles="modprobe blkid switch_root"
  77. #
  78. #Optional files and locations
  79. for f in mdadm mdmon udevd udevadm; do
  80. if [ -x /sbin/$f ] ; then sbinfiles="$sbinfiles $f"; fi
  81. done
  82. unsorted=$(mktemp /tmp/unsorted.XXXXXXXXXX)
  83. DATADIR=/usr/share/mkinitramfs
  84. INITIN=init.in
  85. #
  86. # Create a temporary working directory
  87. WDIR=$(mktemp -d /tmp/initrd-work.XXXXXXXXXX)
  88. #
  89. # Create base directory structure
  90. mkdir -p $WDIR/{bin,dev,lib/firmware,run,sbin,sys,proc,usr}
  91. mkdir -p $WDIR/etc/{modprobe.d,udev/rules.d}
  92. touch $WDIR/etc/modprobe.d/modprobe.conf
  93. ln -s lib $WDIR/lib64
  94. ln -s ../bin $WDIR/usr/bin
  95. #
  96. # Create necessary device nodes
  97. mknod -m 640 $WDIR/dev/console c 5 1
  98. mknod -m 664 $WDIR/dev/null c 1 3
  99. #
  100. # Install the udev configuration files
  101. if [ -f /etc/udev/udev.conf ]; then cp /etc/udev/udev.conf $WDIR/etc/udev/udev.conf; fi
  102. for file in $(find /etc/udev/rules.d/ -type f) ; do
  103. cp $file $WDIR/etc/udev/rules.d
  104. done
  105. #
  106. # Install any firmware present
  107. cp -a /lib/firmware $WDIR/lib
  108. #
  109. # Copy the RAID configuration file if present
  110. if [ -f /etc/mdadm.conf ]; then cp /etc/mdadm.conf $WDIR/etc; fi
  111. #
  112. # Install the init file
  113. install -m0755 $DATADIR/$INITIN $WDIR/init
  114. if [ -n "$KERNEL_VERSION" ] ; then
  115. if [ -x /bin/kmod ] ; then
  116. binfiles="$binfiles kmod"
  117. else
  118. binfiles="$binfiles lsmod"
  119. sbinfiles="$sbinfiles insmod"
  120. fi
  121. fi
  122. #
  123. # Install basic binaries
  124. for f in $binfiles ; do
  125. if [ -e /bin/$f ]; then d="/bin"; else d="/usr/bin"; fi
  126. ldd $d/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  127. copy $d/$f bin
  128. done
  129. #
  130. # Add lvm if present
  131. if [ -x /sbin/lvm ] ; then sbinfiles="$sbinfiles lvm dmsetup"; fi
  132. for f in $sbinfiles ; do
  133. ldd /sbin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  134. copy $f sbin
  135. done
  136. #
  137. # Add udevd libraries if not in /sbin
  138. if [ -x /lib/udev/udevd ] ; then
  139. ldd /lib/udev/udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  140. elif [ -x /lib/systemd/systemd-udevd ] ; then
  141. ldd /lib/systemd/systemd-udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
  142. fi
  143. #
  144. # Add module symlinks if appropriate
  145. if [ -n "$KERNEL_VERSION" ] && [ -x /bin/kmod ] ; then
  146. ln -s kmod $WDIR/bin/lsmod
  147. ln -s kmod $WDIR/bin/insmod
  148. fi
  149. #
  150. # Add lvm symlinks if appropriate
  151. # Also copy the lvm.conf file
  152. if [ -x /sbin/lvm ] ; then
  153. ln -s lvm $WDIR/sbin/lvchange
  154. ln -s lvm $WDIR/sbin/lvrename
  155. ln -s lvm $WDIR/sbin/lvextend
  156. ln -s lvm $WDIR/sbin/lvcreate
  157. ln -s lvm $WDIR/sbin/lvdisplay
  158. ln -s lvm $WDIR/sbin/lvscan
  159. ln -s lvm $WDIR/sbin/pvchange
  160. ln -s lvm $WDIR/sbin/pvck
  161. ln -s lvm $WDIR/sbin/pvcreate
  162. ln -s lvm $WDIR/sbin/pvdisplay
  163. ln -s lvm $WDIR/sbin/pvscan
  164. ln -s lvm $WDIR/sbin/vgchange
  165. ln -s lvm $WDIR/sbin/vgcreate
  166. ln -s lvm $WDIR/sbin/vgscan
  167. ln -s lvm $WDIR/sbin/vgrename
  168. ln -s lvm $WDIR/sbin/vgck
  169. # Conf file(s)
  170. cp -a /etc/lvm $WDIR/etc
  171. fi
  172. #
  173. # Install libraries
  174. sort $unsorted | uniq | while read library ; do
  175. if [ "$library" == "linux-vdso.so.1" ] ||
  176. [ "$library" == "linux-gate.so.1" ]; then
  177. continue
  178. fi
  179. copy $library lib
  180. done
  181. if [ -d /lib/udev ]; then cp -a /lib/udev $WDIR/lib; fi
  182. if [ -d /lib/systemd ]; then cp -a /lib/systemd $WDIR/lib; fi
  183. #
  184. # Install the kernel modules if requested
  185. if [ -n "$KERNEL_VERSION" ]; then
  186. find \
  187. /lib/modules/$KERNEL_VERSION/kernel/{crypto,fs,lib} \
  188. /lib/modules/$KERNEL_VERSION/kernel/drivers/{block,ata,md,firewire} \
  189. /lib/modules/$KERNEL_VERSION/kernel/drivers/{scsi,message,pcmcia,virtio} \
  190. /lib/modules/$KERNEL_VERSION/kernel/drivers/usb/{host,storage} \
  191. -type f 2> /dev/null | cpio --make-directories -p --quiet $WDIR
  192. cp /lib/modules/$KERNEL_VERSION/modules.{builtin,order} $WDIR/lib/modules/$KERNEL_VERSION
  193. depmod -b $WDIR $KERNEL_VERSION
  194. fi
  195. ( cd $WDIR ; find . | cpio -o -H newc --quiet | gzip -9 ) > $INITRAMFS_FILE
  196. #
  197. # Remove the temporary directory and file
  198. rm -rf $WDIR $unsorted
  199. printf "done.\n"
  200. EOF
  201. chmod 0755 %{buildroot}/sbin/mkinitramfs
  202. #-----------------------------------------------------------------------------
  203. install -vdm 755 %{buildroot}/usr/share/mkinitramfs
  204. cat > %{buildroot}/usr/share/mkinitramfs/init.in <<- "EOF"
  205. #!/bin/sh
  206. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  207. export PATH
  208. problem() {
  209. printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
  210. sh
  211. }
  212. no_device() {
  213. printf "The device %s, which is supposed to contain the\n" $1
  214. printf "root file system, does not exist.\n"
  215. printf "Please fix this problem and exit this shell.\n\n"
  216. }
  217. no_mount() {
  218. printf "Could not mount device %s\n" $1
  219. printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
  220. printf "Maybe the device is formatted with an unsupported file system?\n\n"
  221. printf "Or maybe filesystem type autodetection went wrong, in which case\n"
  222. printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
  223. printf "Available partitions:\n"
  224. }
  225. do_mount_root() {
  226. mkdir /.root
  227. [ -n "$rootflags" ] && rootflags="$rootflags,"
  228. rootflags="$rootflags$ro"
  229. case "$root" in
  230. /dev/* ) device=$root ;;
  231. UUID=* ) eval $root; device="/dev/disk/by-uuid/$UUID" ;;
  232. LABEL=*) eval $root; device="/dev/disk/by-label/$LABEL" ;;
  233. "" ) echo "No root device specified." ; problem ;;
  234. esac
  235. while [ ! -b "$device" ] ; do
  236. no_device $device
  237. problem
  238. done
  239. if ! mount -n -t "$rootfstype" -o "$rootflags" "$device" /.root ; then
  240. no_mount $device
  241. cat /proc/partitions
  242. while true ; do sleep 10000 ; done
  243. else
  244. echo "Successfully mounted device $root"
  245. fi
  246. }
  247. init=/sbin/init
  248. root=
  249. rootdelay=
  250. rootfstype=auto
  251. ro="ro"
  252. rootflags=
  253. device=
  254. mount -n -t devtmpfs devtmpfs /dev
  255. mount -n -t proc proc /proc
  256. mount -n -t sysfs sysfs /sys
  257. mount -n -t tmpfs tmpfs /run
  258. read -r cmdline < /proc/cmdline
  259. for param in $cmdline ; do
  260. case $param in
  261. init=* ) init=${param#init=} ;;
  262. root=* ) root=${param#root=} ;;
  263. rootdelay=* ) rootdelay=${param#rootdelay=} ;;
  264. rootfstype=*) rootfstype=${param#rootfstype=} ;;
  265. rootflags=* ) rootflags=${param#rootflags=} ;;
  266. ro ) ro="ro" ;;
  267. rw ) ro="rw" ;;
  268. esac
  269. done
  270. #
  271. # udevd location depends on version
  272. if [ -x /sbin/udevd ]; then
  273. UDEVD=/sbin/udevd
  274. elif [ -x /lib/udev/udevd ]; then
  275. UDEVD=/lib/udev/udevd
  276. elif [ -x /lib/systemd/systemd-udevd ]; then
  277. UDEVD=/lib/systemd/systemd-udevd
  278. else
  279. echo "Cannot find udevd nor systemd-udevd"
  280. problem
  281. fi
  282. ${UDEVD} --daemon --resolve-names=never
  283. udevadm trigger
  284. udevadm settle
  285. if [ -f /etc/mdadm.conf ]; then mdadm -As; fi
  286. if [ -x /sbin/vgchange ]; then /sbin/vgchange -a y > /dev/null; fi
  287. if [ -n "$rootdelay" ]; then sleep "$rootdelay"; fi
  288. do_mount_root
  289. killall -w ${UDEVD##*/}
  290. exec switch_root /.root "$init" "$@"
  291. EOF
  292. #-----------------------------------------------------------------------------
  293. # Create file list
  294. find "${RPM_BUILD_ROOT}" -not -type d -print > filelist.list
  295. sed -i "s|^${RPM_BUILD_ROOT}||" filelist.list
  296. #-----------------------------------------------------------------------------
  297. %files -f filelist.list
  298. %defattr(-,root,root)
  299. #-----------------------------------------------------------------------------
  300. %post
  301. %postun
  302. #-----------------------------------------------------------------------------
  303. %changelog
  304. * Mon Jun 01 2020 Chris Cromer <chris@cromer.cl> 9.1-1
  305. * Sun Sep 09 2018 baho-utot <baho-utot@columbus.rr.com> 8.2-1
  306. - Initial build. First version