]> git.pld-linux.org Git - projects/geninitrd.git/blobdiff - geninitrd
- swsusp support from mis@pld-linux
[projects/geninitrd.git] / geninitrd
index 39073870dc4c4943295d1390600b3d7e585ab9d6..ea7d438e56abd1086cc8fec876b239626602f9fe 100755 (executable)
--- a/geninitrd
+++ b/geninitrd
@@ -17,6 +17,7 @@ PROGRAM=${0##*/}
 # list of geninitrd modules which need setup routine after commandline args parsing
 GENINITRD_MODS=""
 COMPRESS=yes
+LILO_MICROCODE=yes
 STRIP=/usr/bin/strip
 target=""
 kernel=""
@@ -46,7 +47,7 @@ proc_partitions=no
 usage() {
        echo "Usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
        echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
-       echo "       [--compress=yes|xz|lzma|bzip2|gzip|lzo]"
+       echo "       [--compress=yes|zstd|xz|lzma|bzip2|gzip|lzo]"
        echo "       [--nostrip ] [--strip PATH/strip] [--strip=PATH/strip]"
        echo "       [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]"
        echo "       [--with-bootsplash] [--without-bootsplash]"
@@ -115,7 +116,7 @@ die() {
 # find program from specified paths
 find_tool() {
        local x p b n
-       local paths="$initrd_dir /bin /sbin /usr/bin /usr/sbin"
+       local paths="$initrd_dirs /bin /sbin /usr/bin /usr/sbin"
        for x in "$@"; do
                debug "find_tool: checking $x"
                if [ -x "$x" ]; then
@@ -273,7 +274,7 @@ mount_run() {
        fi
 
        run_mounted=yes
-       echo "mount -t tmpfs run /run" | add_linuxrc
+       echo "mount -t tmpfs run /run -o mode=0755,noexec,nosuid,nodev" | add_linuxrc
 }
 
 # unmount all mountpoints mounted by geninitrd
@@ -287,8 +288,7 @@ umount_all() {
 
        if is_yes "$run_mounted"; then
                add_linuxrc <<-EOF
-               mount --bind /run /newroot/run
-               umount /run
+               mount -n --move /run /newroot/run
                EOF
                run_mounted=no
        fi
@@ -432,6 +432,10 @@ find_depmod() {
                        smodule=$(basename_module $modpath)
                        case "$smodule" in
                                btrfs)
+                                       warn "mounting multidevice btrfs volume requires rootfsflags=device=/dev/...,device=/dev/... kernel option"
+                                       find_depmod "-libcrc32c"
+                                       ;;
+                               ext4)
                                        find_depmod "-libcrc32c"
                                        ;;
                                crc-t10dif)
@@ -505,7 +509,7 @@ inst() {
                mkdir -p $parentDir
        fi
        verbose "+ cp $* DESTDIR$dest"
-       cp -HR "$@" "$DESTDIR$dest"
+       cp -HRp "$@" "$DESTDIR$dest"
 }
 
 inst_d() {
@@ -534,15 +538,20 @@ inst_exec() {
 
        inst "$@" $dest
 
-       local obj lib libs
+       local obj lib libs libs_additional libdir
        for obj in "$@"; do
                case "$obj" in
                        /lib/ld-linux.so.2 | /lib64/ld-linux-x86-64.so.2 | /libx32/ld-linux-x32.so.2)
                        continue
+                       ;;
+                   /lib/libpthread.so* | /lib64/libpthread.so* | /libx32/libpthread.so*)
+                               libs_additional="${obj%/libpthread*}/libgcc_s.so.1"
+                       ;;
                esac
 
-               libs=$(ldd "$obj" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
-               for lib in $libs; do
+
+               libs=$(ldd "$obj" 2> /dev/null | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
+               for lib in $libs $libs_additional; do
                        libdir=$(cd $(dirname "$lib"); pwd)
                        if [ ! -f "$DESTDIR/$lib" ]; then
                                inst_d $libdir
@@ -553,17 +562,20 @@ inst_exec() {
 
        # hack for uclibc linked binaries requiring this fixed path
        # XXX: shouldn't rpath be used here instead so th
-       if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
-               lib=$DESTDIR/$_lib/libc.so.0
-               lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
-               libdir=$(cd $(dirname "$lib"); pwd)
-               if [ ! -e $DESTDIR$libdir ]; then
-                       libdir=$(dirname "$libdir")
-                       inst_d $libdir
-                       verbose "+ ln -s /$_lib $DESTDIR$libdir"
-                       ln -s /$_lib $DESTDIR$libdir
+       for _lib in $(get_libdir LIBDIR); do
+               if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
+                       lib=$DESTDIR/$_lib/libc.so.0
+                       lib=$(ldd "$lib" 2> /dev/null | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
+                       libdir=$(cd $(dirname "$lib"); pwd)
+                       if [ ! -e $DESTDIR$libdir ]; then
+                               libdir=$(dirname "$libdir")
+                               inst_d $libdir
+                               verbose "+ ln -s /$_lib $DESTDIR$libdir"
+                               ln -s /$_lib $DESTDIR$libdir
+                               break
+                       fi
                fi
-       fi
+       done
 }
 
 # output modules.conf / modprobe.conf
@@ -660,6 +672,11 @@ find_modules_for_devpath() {
                return
        fi
 
+       if [[ "$devpath" == /dev/nvme* ]]; then
+               find_module "nvme"
+               return
+       fi
+
        if [[ "$devpath" == /dev/bcache* ]]; then
                find_modules_bcache "$devpath"
                return
@@ -934,62 +951,43 @@ initrd_gen_initramfs_switchroot() {
        add_linuxrc <<-'EOF'
                device=
                eval "$(busybox awk -v root="$ROOT" '
-                       # http://9fans.net/archive/2006/09/261
-                       function h2d(str, hstr, res, num, n, digit, i) {
-                               hstr = "0123456789abdcef";
-                               res = 0;
+                       function h2d(str, hstr, res, num, n, digit, i) {        # http://9fans.net/archive/2006/09/261
+                               hstr = "0123456789abdcef"; res = 0;
                                n = split(tolower(str), digit, "");
 
                                for (i = 1; i <= n; i++) {
                                        num = index(hstr, digit[i]) - 1;
                                        res = res + (num * 16 ^ (n - i));
                                }
-
                                return res;
                        }
                        BEGIN {
-
                                num_pattern_short = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]";
                                num_pattern = "[0-9a-fA-F]" num_pattern_short;
                                dev_pattern = "[hms][a-z][a-z]([0-9])+";
-                               partition = "";
-                               min = -1; maj = -1;
-
-                               # see if we have /dev/hdX or hdX, we can just take partition name
-                               if (root ~ "^\/dev\/" dev_pattern "$" || root ~ "^" dev_pattern "$") {
-                                       partition = root
-                                       sub("^/dev/", "", partition);
-
-                               } else {
-                                       # unify values first
-                                       if (root ~ "^" num_pattern_short "$")  {
-                                               # change "303" => "0x0303"
+                               partition = ""; min = -1; maj = -1;
+
+                               if (root ~ "^\/dev\/" dev_pattern "$" || root ~ "^" dev_pattern "$") {  # see if we have /dev/hdX or hdX, we can just take partition name
+                                       partition = root; sub("^/dev/", "", partition);
+                               } else {        # unify values first
+                                       if (root ~ "^" num_pattern_short "$")  {        # change "303" => "0x0303"
                                                root = "0x0" root
-                                       } else if (root ~ "^" num_pattern "$")  {
-                                               # change "0303" => "0x0303"
+                                       } else if (root ~ "^" num_pattern "$")  {       # change "0303" => "0x0303"
                                                root = "0x" root
                                        }
-
                                        maj = h2d(substr(root, 3, 2));
                                        min = h2d(substr(root, 5, 2));
                                }
                        }
-
                        partition && $4 == partition { maj = $1; min = $2; }
                        $1 == maj && $2 == min { partition = $4; }
-
                        END {
-                               if (maj >= 0 && min >= 0) {
-                                       printf("maj=%s; min=%s;\n", maj, min);
-                               }
-                               if (partition) {
-                                       printf("device=/dev/%s;\n", partition);
-                               }
-                       }
-                       ' /proc/partitions)"
+                               if (maj >= 0 && min >= 0) {     printf("maj=%s; min=%s;\n", maj, min);  }
+                               if (partition) {                printf("device=/dev/%s;\n", partition); }
+                       }' /proc/partitions)"
 
                if [ -z "$device" ]; then
-                       if [ "$DEBUGINITRD" ]; then
+                       if [ "$DEBUGINITRD" -a "$DEBUGINITRD" != 'sh' ]; then
                                cat /proc/partitions
                        fi
                        device=$ROOT
@@ -999,6 +997,16 @@ initrd_gen_initramfs_switchroot() {
                        mknod -m 660 $device b $maj $min
                fi
 
+               # XXX hack, fallback to rootdev from geninitrd time
+               if [ ! -e "$device" ]; then
+       EOF
+       add_linuxrc <<-EOF
+                       device="$rootdev"
+       EOF
+       add_linuxrc <<-'EOF'
+                       echo "DEVICE set to $device based on fstab entry from initrd gen time"
+               fi
+
                # XXX hack, if no device, try to parse it from /proc/partitions using /proc/sys/kernel/real-root-dev
                if [ ! -e "$device" ]; then
                        rrd=$(cat /proc/sys/kernel/real-root-dev)
@@ -1028,7 +1036,7 @@ initrd_gen_initramfs_switchroot() {
        busybox_applet dmesg
        busybox_applet tail
        add_linuxrc <<-'EOF'
-               if [ "$DEBUGINITRD" ]; then
+               if [ "$DEBUGINITRD" -a "$DEBUGINITRD" != 'sh' ]; then
                        echo "Last 20 lines of dmesg:"
                        dmesg | tail -n 20
                fi
@@ -1052,6 +1060,14 @@ initrd_gen_initramfs_switchroot() {
                        [ "$DEBUGINITRD" ] || usleep 10000000
                fi
 
+               # systemd[1]: /usr appears to be on its own filesytem and is not
+               # already mounted. This is not a supported setup. Some things will
+               # probably break (sometimes even silently) in mysterious ways. Consult
+               # http://freedesktop.org/wiki/Software/systemd/separate-usr-is-broken
+               # for more information.
+               echo trying to mount /usr
+               chroot /newroot mount -n /usr
+
                exec switch_root /newroot $init ${1:+"$@"}
 
                # FIXME: this code is never executed, as "exec" does not return!
@@ -1080,14 +1096,13 @@ sym_exists() {
                return 1
        fi
 
-       awk -vc=1 -vsymbol="$symbol" '$2 == "T" && $3 == symbol {c = 0} END {exit c}' $mapfile
+       awk -vc=1 -vsymbol="$symbol" '(tolower($2) == "t" || tolower($2) == "d") && $3 == symbol {c = 0} END {exit c}' $mapfile
 }
 
 # find best compressor (or forced one) for initrd
 find_compressor() {
        local mode="$1"
-       # fastest initrd decompression speed is first
-       local compressors='lzo gzip xz lzma bzip2'
+       local compressors='zstd xz lzma bzip2 gzip lzo'
 
        # a specified one, take it
        if ! is_yes "$mode"; then
@@ -1119,6 +1134,10 @@ find_compressor() {
                        sym=unlzo
                        prog=/usr/bin/lzop
                        ;;
+               zstd)
+                       sym=zstd
+                       prog=/usr/bin/zstd
+                       ;;
                none|no)
                        # any existing sym will work
                        sym=initrd_load
@@ -1161,6 +1180,9 @@ compress_image() {
        lzo)
                lzop -9 < "$IMAGE" > "$tmp" || return $?
                ;;
+       zstd)
+               zstd -9 < "$IMAGE" > "$tmp" || return $?
+               ;;
        none|no)
                cat < "$IMAGE" > "$tmp" || return $?
                ;;
@@ -1169,6 +1191,16 @@ compress_image() {
        mv -f "$tmp" "$target"
 }
 
+# prepend file to image
+prepend_file_to_image() {
+       local file="$1" target="$2" tmp
+       tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed"
+
+       cat "$file" "$target" > "$tmp" || return $?
+
+       mv -f "$tmp" "$target"
+}
+
 if [ -r /etc/sysconfig/geninitrd ]; then
        . /etc/sysconfig/geninitrd
 fi
@@ -1177,7 +1209,7 @@ if [ ! -f /proc/mounts ]; then
        warn "/proc filesystem not mounted, may cause wrong results or failure."
 fi
 
-geninitrd_load_mods ide luks multipath dmraid lvm md blkid udev tuxonice suspend fbsplash condecor bootsplash uvesafb nfs sata scsi usbkbd bcache
+geninitrd_load_mods ide luks multipath dmraid lvm md blkid udev swsusp tuxonice suspend fbsplash condecor bootsplash uvesafb nfs sata scsi usbkbd bcache
 
 while [ $# -gt 0 ]; do
        case $1 in
@@ -1213,6 +1245,9 @@ while [ $# -gt 0 ]; do
        --without-fbcondecor)
                FB_CON_DECOR=no
                ;;
+       --without-swsusp)
+               USE_SWSUSP=no
+               ;;
        --with-suspend)
                USE_SUSPEND=yes
                ;;
@@ -1348,18 +1383,17 @@ if [ "$(id -u)" != 0 ]; then
        die "You need to be root to generate initrd"
 fi
 
-if [ -d /lib64 -a -d /usr/lib64 ]; then
-       _lib=lib64
-else
-       _lib=lib
-fi
+for dir in libx32 lib64 lib; do
+       initrd_dir=/usr/$dir/initrd
+       if [ -d "$initrd_dir" ]; then
+               initrd_dirs="$initrd_dirs $initrd_dir"
+       fi
+done
 
-initrd_dir=/usr/$_lib/initrd
 kernel_version=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d",$1,$2)}')
 kernel_version_long=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d%03d",$1,$2,$3)}')
 
 verbose "# geninitrd $VERSION"
-debug "Using _lib: $_lib"
 debug "Using initrd_dir: $initrd_dir"
 
 busybox=$(find_tool $initrd_dir/busybox $initrd_dir/initrd-busybox /bin/initrd-busybox) || die "Couldn't find busybox suitable for initrd"
@@ -1385,24 +1419,46 @@ if [ -z "$INITRDFS" ]; then
        fi
 fi
 
-case "$INITRDFS" in
-  ext2)
-       [ -x /sbin/mke2fs ] || die "/sbin/mke2fs is missing"
-       ;;
-  rom|romfs)
-       [ -x /sbin/genromfs ] || die "/sbin/genromfs is missing"
-       ;;
-  cram|cramfs)
-       [ -x /sbin/mkcramfs ] || die "/sbin/mkcramfs is missing"
-       ;;
-  initramfs)
-       [ -x /bin/cpio ] || die "/bin/cpio is missing"
-       [ -x /usr/bin/find ] || die "/usr/bin/find is missing"
-       ;;
-  *)
-       die "Filesystem $INITRDFS on initrd is not supported"
-       ;;
-esac
+check_initrd_fs() {
+       local s sfound sym p prog map=/boot/System.map-$kernel
+       case "$INITRDFS" in
+               ext2)
+                       # TODO: symbols to check in case of ext2 used via ext3/4 subsystem
+                       sym=init_ext2_fs
+                       prog=/sbin/mke2fs
+                       ;;
+               rom|romfs)
+                       sym=init_romfs_fs
+                       prog=/sbin/genromfs
+                       ;;
+               cram|cramfs)
+                       sym=init_cramfs_fs
+                       prog=/sbin/mkcramfs
+                       ;;
+               initramfs)
+                       sym=__initramfs_start
+                       prog="/bin/cpio /usr/bin/find"
+                       ;;
+               *)
+                       die "Filesystem $INITRDFS on initrd is not supported by geninitrd"
+                       ;;
+       esac
+
+       # only one is needed (for cases like ext2 via ext2 or via ext3 or via ext4 subsysytem)
+       sfound=0
+       for s in $sym; do
+               sym_exists $map $s && sfound=1
+               break
+       done
+       if [ "$sfound" -eq "0" ]; then
+               die "Filesystem $INITRDFS on initrd is not supported by kernel"
+       fi
+
+       for p in $prog; do
+               [ ! -x "$p" ] && die "$prog is missing"
+       done
+}
+check_initrd_fs
 
 if [ -L "$target" ]; then
        target=$(readlink -f "$target")
@@ -1455,6 +1511,10 @@ if is_yes "$USE_SUSPEND"; then
        find_modules_suspend
 fi
 
+if is_yes "$USE_SWSUSP"; then
+       find_modules_swsusp
+fi
+
 find_root "$fstab" || exit
 verbose "Using $rootdev as device for rootfs"
 
@@ -1492,7 +1552,7 @@ chmod a+rx "$RCFILE"
 ln -s linuxrc $DESTDIR/init
 
 # create dirs that we really need
-inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,run,sys}
+inst_d /{lib,bin,sbin,etc,dev{,/pts,/shm},loopfs,var,proc,run,sys,tmp}
 
 modules_install "$MODULES"
 
@@ -1521,7 +1581,11 @@ mount_proc
 
 kmsg "geninitrd/$VERSION starting"
 
+inst_d /lib/geninitrd/
+inst /lib/geninitrd/functions.initrd /lib/geninitrd/functions.initrd
+
 add_linuxrc <<-EOF
+       . /lib/geninitrd/functions.initrd
        # builtin defaults from geninitrd
        ROOT=$rootdev
        ROOTFS=$rootFs
@@ -1539,6 +1603,9 @@ add_linuxrc <<-'EOF'
                if [ "${arg##root=}" != "${arg}" ]; then
                        ROOT=${arg##root=}
                fi
+               if [ "${arg##rootfs=}" != "${arg}" ]; then
+                       ROOTFS=${arg##rootfs=}
+               fi
                if [ "${arg##rootflags=}" != "${arg}" ]; then
                        ROOTFLAGS=${arg##rootflags=}
                fi
@@ -1590,6 +1657,10 @@ add_linuxrc <<-'EOF'
        fi
 EOF
 
+# mount early
+mount_tmp
+mount_run
+
 modules_add_linuxrc $MODULES
 
 # TODO: rewrite for busybox
@@ -1639,6 +1710,7 @@ initrd_gen_stop_uvesafb
 # resume after killing local processes
 initrd_gen_tuxonice
 initrd_gen_suspend
+initrd_gen_swsusp
 
 # clean up env
 add_linuxrc <<-'EOF'
@@ -1723,14 +1795,16 @@ case "$INITRDFS" in
        die "Filesystem $INITRDFS not supported by $PROGRAM"
 esac
 
-CONFIG_BLK_DEV_RAM_SIZE=$(ikconfig | awk -F= '/^CONFIG_BLK_DEV_RAM_SIZE/{print $2}')
-if [ -z "$CONFIG_BLK_DEV_RAM_SIZE" ]; then
-       CONFIG_BLK_DEV_RAM_SIZE=4096
-       warn "No CONFIG_BLK_DEV_RAM_SIZE detected, fallback to $CONFIG_BLK_DEV_RAM_SIZE"
-fi
+if [ "$INITRDFS" != "initramfs" ]; then
+       CONFIG_BLK_DEV_RAM_SIZE=$(ikconfig | awk -F= '/^CONFIG_BLK_DEV_RAM_SIZE/{print $2}')
+       if [ -z "$CONFIG_BLK_DEV_RAM_SIZE" ]; then
+               CONFIG_BLK_DEV_RAM_SIZE=4096
+               warn "No CONFIG_BLK_DEV_RAM_SIZE detected, fallback to $CONFIG_BLK_DEV_RAM_SIZE"
+       fi
 
-if [ "$IMAGESIZE" -gt $CONFIG_BLK_DEV_RAM_SIZE ]; then
-       warn "Your image size is larger than $CONFIG_BLK_DEV_RAM_SIZE, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
+       if [ "$IMAGESIZE" -gt $CONFIG_BLK_DEV_RAM_SIZE ]; then
+               warn "Your image size is larger than $CONFIG_BLK_DEV_RAM_SIZE, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
+       fi
 fi
 
 if ! is_no "$COMPRESS"; then
@@ -1751,6 +1825,14 @@ else
        cp -a "$IMAGE" "$target"
 fi
 
+# microcode support for lilo
+if ! is_no "$LILO_MICROCODE"; then
+       if [ -x /sbin/lilo -a -f "/boot/intel-ucode.img" ]; then
+               verbose "Prepending $target with microcode image /boot/intel-ucode.img for LILO"
+               prepend_file_to_image "/boot/intel-ucode.img" "$target"
+       fi
+fi
+
 # XXX. check if bootsplash can output data to tmp dir not directly to initramfs image.
 initrd_gen_bootsplash "$target"
 
This page took 0.094355 seconds and 4 git commands to generate.