Mkinitramfs script file analysis, location/usr/sbin/mkinitramfs, mkinitramfssbin

Source: Internet
Author: User
Tags builtin

Mkinitramfs script file analysis, location/usr/sbin/mkinitramfs, mkinitramfssbin

Mkinitramfs-o/boot/initrd. img-version

The following is a script file:

#! /Bin/sh


Umask 0022
Export PATH = '/usr/bin:/sbin:/bin'

# Defaults
Keep = "n"
CONFDIR = "/etc/initramfs-tools"
Verbose = "n"
Test-e/bin/busybox & BUSYBOXDIR =/bin
Test-e/usr/lib/initramfs-tools/bin/busybox & BUSYBOXDIR =/usr/lib/initramfs-tools/bin
Export BUSYBOXDIR

OPTIONS = 'getopt-o c: d: ko: r: v-n "$0" -- "$ @"'

# Check for non-GNU getopt
If [$? ! = 0]; then echo "W: non-GNU getopt"> & 2; exit 1; fi

Eval set -- "$ OPTIONS"

While true; do
Case "$1" in
-C)
Compress = "$2"
Shift 2
;;
-D)
CONFDIR = "$2"
Shift 2
If [! -D "$ {CONFDIR}"]; then
Echo "$ {0 }:: {CONFDIR}: Not a directory"> & 2
Exit 1
Fi
;;
-O)
Outfile = "$2"
Shift 2
;;
-K)
Keep = "y"
Shift
;;
-R)
ROOT = "$2"
Shift 2
;;
-V)
Verbose = "y"
Shift
;;
--)
Shift
Break
;;
*)
Echo "Internal error! "> & 2
Exit 1
;;
Esac
Done

# For dependency ordered mkinitramfs hook scripts.
./Usr/share/initramfs-tools/scripts/functions
./Usr/share/initramfs-tools/hook-functions

. "$ {CONFDIR}/initramfs. conf"
EXTRA_CONF =''
For I in/usr/share/initramfs-tools/conf. d/* $ {CONFDIR}/conf. d/*; do
[-E $ I] & EXTRA_CONF = "$ {EXTRA_CONF} $ (basename $ I \
| Grep '^ [[: alnum:] [[: alnum:] \. _-] * $ '| grep-v '\. dpkg -. * $ ')";
Done
# FIXME: deprecated those settings on mkinitramfs run
# These conf dirs are for boot scripts and land on initramfs
For I in $ {EXTRA_CONF}; do
If [-e $ {CONFDIR}/conf. d/$ {I}]; then
. $ {CONFDIR}/conf. d/$ {I}
Elif [-e/usr/share/initramfs-tools/conf. d/$ {I}]; then
./Usr/share/initramfs-tools/conf. d/$ {I}
Fi
Done

# Source package confs
For I in/usr/share/initramfs-tools/conf-hooks.d/*; do
If [-e "$ {I}"]; then
. "$ {I }"
Fi
Done

If [-n "$ {UMASK:-}"]; then
Umask "$ {UMASK }"
Fi

If [-z "$ {outfile}"]; then
Usage
Fi

Touch "$ outfile"
Outfile = "$ (readlink-f" $ outfile ")"

# And by "version" we really mean path to kernel modules
# This is braindead, and exists to preserve the interface with mkinitrd
If [$ {#}-ne 1]; then
Version = "$ (uname-r )"
Else
Version = "$ {1 }"
Fi

# Check that we're using a new enough kernel version, first for ourselves,
# Then for each of the hooks, which can have a MINKVER variable defined
Check_minkver $ {version}
Check_minkver $ {version}/usr/share/initramfs-tools/hooks
Check_minkver $ {version }$ {CONFDIR}/hooks

Case "$ {version}" in
/Lib/modules /*/[! /] *)
;;
/Lib/modules /[! /] *)
Version = "$ {version #/lib/modules /}"
Version = "$ {version % /*}"
;;
Esac

Case "$ {version}" in
*/*)
Echo "$ PROG :$ {version} is not a valid kernel version"> & 2
Exit 1
;;
Esac

# Check userspace and kernel support for compressed initramfs images
If [-z "$ {compress:-}"]; then
Compress =$ {COMPRESS}
Else
COMPRESS =$ {compress}
Fi

If! Command-v "$ {compress}">/dev/null 2> & 1; then
Compress = gzip
["$ {Verbose}" = y] & \
Echo "No $ {COMPRESS} in $ {PATH}, using gzip"
COMPRESS = gzip
Fi

If! 'Grep-q-I ^ config_rd _ $ {COMPRESS % p}/boot/config-$ {version} '; then
Compress = gzip
["$ {Verbose}" = y] & \
Echo "linux-2.6 misses $ {COMPRESS} support, using gzip"
Fi

["$ {Compress}" = lzop] & compress = "lzop-9"
["$ {Compress}" = xz] & compress = "xz-8 -- check = crc32"

If [-d "$ {outfile}"]; then
Echo "$ {outfile} is a directory"> & 2
Exit 1
Fi

MODULESDIR = "/lib/modules/$ {version }"

If [! -E "$ {MODULESDIR}"]; then
Echo "WARNING: missing $ {MODULESDIR }"
Echo "Device driver support needs thus be built-in linux image! "
Fi
If [! -E "$ {MODULESDIR}/modules. dep"]; then
Depmod $ {version}
Fi

DESTDIR = "$ (mktemp-d $ {TMPDIR:-/tmp}/mkinitramfs_XXXXXX)" | exit 1
Chmod 755 "$ {DESTDIR }"

# Do not execute cache_run_scripts () if mounted with noexec
NOEXEC = ""
Fs = $ (df-P $ DESTDIR | tail-1 | awk '{print $6 }')
If [-n "$ fs"] & mount | grep-q "on $ fs. * noexec"; then
NOEXEC = 1
Fi

_ TMPCPIOGZ = "$ (mktemp $ {TMPDIR:-/tmp}/mkinitramfs-OL_XXXXXX)" | exit 1
_ TMPEARLYCPIO = "$ (mktemp $ {TMPDIR:-/var/tmp}/mkinitramfs-FW_XXXXXX)" | exit 1

DPKG_ARCH = 'dpkg -- print-System'

# Export environment for hook scripts.
#
Export MODULESDIR
Export version
Export CONFDIR
Export DESTDIR
Export DPKG_ARCH
Export verbose
Export MODULES
Export BUSYBOX
Export COMPCACHE_SIZE

# Private, used by 'catenate _ cpiogz '.
Export _ TMPCPIOGZ

# Private, used by 'prepend _ earlyinitramfs '.
Export _ TMPEARLYCPIO

For d in bin conf/conf. d etc lib/modules run sbin scripts $ {MODULESDIR}; do
Mkdir-p "$ {DESTDIR}/$ {d }"
Done

# Copy in modules. builtin and modules. order (not generated by depmod)
For x in modules. builtin modules. order; do
If [-f "$ {MODULESDIR}/$ {x}"]; then
Cp-p "$ {MODULESDIR}/$ {x}" "$ {DESTDIR }$ {MODULESDIR}/$ {x }"
Fi
Done

# MODULES = list case. Always honor.
For x in "$ {CONFDIR}/modules"/usr/share/initramfs-tools/modules. d/*; do
If [-f "$ {x}"]; then
Add_modules_from_file "$ {x }"
Fi
Done

# MODULES = most is default
Case "$ {MODULES}" in
Dep)
Dep_add_modules
;;
Most)
Auto_add_modules
;;
Netboot)
Auto_add_modules base
Auto_add_modules net
;;
List)
# Nothing to add
;;
*)
Echo "W: mkinitramfs: unsupported MODULES setting :$ {MODULES }."
Echo "W: mkinitramfs: Falling back to MODULES = most ."
Auto_add_modules
;;
Esac

# Resolve hidden dependencies
Hidden_dep_add_modules

# First file executed by linux-2.6
Cp-p/usr/share/initramfs-tools/init $ {DESTDIR}/init

# Add existant boot scripts
For B in $ (cd/usr/share/initramfs-tools/scripts/& find .\
-Regextype posix-extended-regex '. */[[: alnum:] \. _-] + $'-type f); do
Option = $ (sed '/^ OPTION = /! D; $ d; s/^ OPTION = //; s/[[: space:] * $ // '"/usr/share/initramfs-tools/scripts/$ {B }")
[-Z "$ {option}"] | eval test-n \ "\$ {$ option} \"-a \ "\$ {$ option }\"! = \ "N \" | continue

[-D "$ {DESTDIR}/scripts/$ (dirname" $ {B} ")"] \
| Mkdir-p "$ {DESTDIR}/scripts/$ (dirname" $ {B }")"
Cp-p "/usr/share/initramfs-tools/scripts/$ {B }"\
"$ {DESTDIR}/scripts/$ (dirname" $ {B }")/"
Done
For B in $ (cd "$ {CONFDIR}/scripts" & find .\
-Regextype posix-extended-regex '. */[[: alnum:] \. _-] + $'-type f); do
Option = $ (sed '/^ OPTION = /! D; $ d; s/^ OPTION = //; s/[[: space:] * $ // '"$ {CONFDIR}/scripts/$ {B }")
[-Z "$ {option}"] | eval test-n \ "\$ {$ option} \"-a \ "\$ {$ option }\"! = \ "N \" | continue

[-D "$ {DESTDIR}/scripts/$ (dirname" $ {B} ")"] \
| Mkdir-p "$ {DESTDIR}/scripts/$ (dirname" $ {B }")"
Cp-p "$ {CONFDIR}/scripts/$ {B}" "$ {DESTDIR}/scripts/$ (dirname" $ {B }")/"
Done

Echo "DPKG_ARCH =$ {DPKG_ARCH}" >$ {DESTDIR}/conf/arch. conf
Cp-p "$ {CONFDIR}/initramfs. conf" $ {DESTDIR}/conf
For I in $ {EXTRA_CONF}; do
If [-e "$ {CONFDIR}/conf. d/$ {I}"]; then
Copy_exec "$ {CONFDIR}/conf. d/$ {I}"/conf. d
Elif [-e "/usr/share/initramfs-tools/conf. d/$ {I}"]; then
Copy_exec "/usr/share/initramfs-tools/conf. d/$ {I}"/conf. d
Fi
Done

# ROOT hardcoding
If [-n "$ {ROOT:-}"]; then
Echo "ROOT =$ {ROOT}" >$ {DESTDIR}/conf. d/root
Fi

If! Command-v ldd>/dev/null 2> & 1; then
Echo "WARNING: no ldd around-install libc-bin"> & 2
Exit 1
Fi

Copy_exec/usr/lib/initramfs-tools/bin/wait-for-root/sbin

# Module-init-tools
Copy_exec/sbin/modprobe/sbin
Copy_exec/sbin/rmmod/sbin
Mkdir-p "$ {DESTDIR}/etc/modprobe. d"
Cp-a/etc/modprobe. d/* "$ {DESTDIR}/etc/modprobe. d /"

# Util-linux
Copy_exec/sbin/blkid/sbin

# Workaround: libgcc always needed on old-abi arm
If ["$ DPKG_ARCH" = arm] | ["$ DPKG_ARCH" = armeb]; then
Cp-a/lib/libgcc_s.so.1 "$ {DESTDIR}/lib /"
Fi

Run_scripts/usr/share/initramfs-tools/hooks optional
Run_scripts "$ {CONFDIR}"/hooks optional

# Cache boot run order
If [-n "$ NOEXEC"]; then
Echo "W: TMPDIR is mounted noexec, will not cache run scripts ."
Else
For B in $ (cd "$ {DESTDIR}/scripts" & find.-mindepth 1-type d); do
Cache_run_scripts "$ {DESTDIR}" "/scripts/$ {B #./}"
Done
Fi

# Generate module deps
Depmod-a-B "$ {DESTDIR}" $ {version}
Rm-f "$ {DESTDIR}/lib/modules/$ {version}"/modules. * map

# Make sure that library search path is up to date
Cp-ar/etc/ld. so. conf * "$ DESTDIR"/etc/
If! Ldconfig-r "$ DESTDIR"; then
[$ (Id-u )! = "0"] \
& Echo "ldconfig might need uid = 0 (root) for chroot ()"> & 2
Fi

# Apply DSDT to initramfs
If [-e "$ {CONFDIR}/DSDT. aml"]; then
Copy_exec "$ {CONFDIR}/DSDT. aml "/
Fi

# Remove any looping or broken symbolic links, since they break cpio.
["$ {Verbose}" = y] & xargs_verbose = "-t"
(Cd "$ {DESTDIR}" & find. -type l-printf' % p % Y \ n' | sed-n's/[LN] $ // P '\
| Xargs ${xargs_verbose:-}-rL1 rm-f)

# Dirty hack for armhf's double-linker situation; if we have one
# The two known eglibc linkers, nuke both and re-create sanity
If ["$ DPKG_ARCH" = armhf]; then
If [-e "$ {DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"] | \
[-E "$ {DESTDIR}/lib/ld-linux-armhf.so.3"]; then
Rm-f "$ {DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"
Rm-f "$ {DESTDIR}/lib/ld-linux-armhf.so.3"
Cp-aL/lib/ld-linux-armhf.so.3 "$ {DESTDIR}/lib /"
Ln-sf/lib/ld-linux-armhf.so.3 "$ {DESTDIR}/lib/arm-linux-gnueabihf/ld-linux.so.3"
Fi
Fi

["$ {Verbose}" = y] & echo "Building cpio $ {outfile} initramfs"

If [-s "$ {__ TMPEARLYCPIO}"]; then
Cat "$ {__ TMPEARLYCPIO}"> "$ {outfile}" | exit 1
Else
# Truncate
> "$ {Outfile }"
Fi

(
# Work around lack of "set-o pipefail" for the following pipe:
# Cd "$ {DESTDIR}" & find. | cpio -- quiet-R 0: 0-o-H newc | gzip> "$ {outfile}" | exit 1
Exec 3> & 1
Eval'
# Http://cfaj.freeshell.org/shell/cus-faq-2.html
Exec 4> & 1> & 3 3> &-
Cd "$ {DESTDIR }"
{
Find. 4> &-; echo "ec1 =$ ?; "> & 4
} | {
Cpio -- quiet-R 0: 0-o-H newc 4> &-; echo "ec2 =$ ?; "> & 4
} | $ {Compress }>> "$ {outfile }"
Echo "ec3 = $ ?; "> & 4
`
If ["$ ec1"-ne 0]; then
Echo "E: mkinitramfs failure find $ ec1 cpio $ ec2 $ compress $ ec3"
Exit "$ ec1"
Fi
If ["$ ec2"-ne 0]; then
Echo "E: mkinitramfs failure cpio $ ec2 $ compress $ ec3"
Exit "$ ec2"
Fi
If ["$ ec3"-ne 0]; then
Echo "E: mkinitramfs failure $ compress $ ec3"
Exit "$ ec3"
Fi
) | Exit 1

If [-s "$ {__ TMPCPIOGZ}"]; then
Cat "$ {__ TMPCPIOGZ}" >>" $ {outfile} "| exit 1
Fi

If ["$ {keep}" = "y"]; then
Echo "Working files in $ {DESTDIR}, early initramfs in $ {__ TMPEARLYCPIO} and overlay in $ {__ TMPCPIOGZ }"
Else
Rm-rf "$ {DESTDIR }"
Rm-rf "$ {__ TMPCPIOGZ }"
Rm-rf "$ {__ TMPEARLYCPIO }"
Fi

Exit 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.