diff options
author | Thayne Harbaugh <tharbaugh@lnxi.com> | 2005-01-07 21:43:14 -0800 |
---|---|---|
committer | Linus Torvalds <torvalds@evo.osdl.org> | 2005-01-07 21:43:14 -0800 |
commit | a363b37930df6678d816b3711b07acbc0e566b04 (patch) | |
tree | f6f8d0f5b1308bd6f64841af0ea41bf79d5f563b /scripts | |
parent | 2f0f10bb16dcc550feee88cab0fb5a2257688e59 (diff) | |
download | history-a363b37930df6678d816b3711b07acbc0e566b04.tar.gz |
[PATCH] initramfs: unprivileged image creation
This patch makes several tweaks so that an initramfs image can be
completely created by an unprivileged user. It should maintain
compatibility with previous initramfs early userspace cpio/image creation
and it updates documentation.
There are a few very important tweaks:
CONFIG_INITRAMFS_SOURCE is now either a single cpio archive that is
directly used or a list of directories and files for building a cpio
archive for the initramfs image. Making the cpio archive listable in
CONFIG_INITRAMFS_SOURCE makes the cpio step more official and automated so
that it doesn't have to be copied by hand to usr/initramfs_data.cpio (I
think this was broken anyway and would be overwritten). The alternative
list of directories *and* files means that files can be install in a "root"
directory and device-special files can be listed in a file list.
CONFIG_ROOT_UID and CONFIG_ROOT_GID are now available for doing simple
user/group ID translation. That means that user ID 500, group ID 500 can
create all the files in the "root" directory, but that they can all be
owned by user ID 0, group ID 0 in the cpio image.
Various documentation updates to pull it all together.
Removal of old cruft that was unused/misleading.
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/gen_initramfs_list.sh | 129 |
1 files changed, 101 insertions, 28 deletions
diff --git a/scripts/gen_initramfs_list.sh b/scripts/gen_initramfs_list.sh index 79ccbd7cf27fa4..6d411169bfa62b 100644 --- a/scripts/gen_initramfs_list.sh +++ b/scripts/gen_initramfs_list.sh @@ -2,17 +2,17 @@ # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org> # Released under the terms of the GNU GPL # -# Generate a newline separated list of entries from the file/directory pointed -# out by the environment variable: CONFIG_INITRAMFS_SOURCE +# Generate a newline separated list of entries from the file/directory +# supplied as an argument. # -# If CONFIG_INITRAMFS_SOURCE is non-existing then generate a small dummy file. +# If a file/directory is not supplied then generate a small dummy file. # -# The output is suitable for gen_init_cpio as found in usr/Makefile. +# The output is suitable for gen_init_cpio built from usr/gen_init_cpio.c. # -simple_initramfs() { +default_initramfs() { cat <<-EOF - # This is a very simple initramfs + # This is a very simple, default initramfs dir /dev 0755 0 0 nod /dev/console 0600 0 0 c 5 1 @@ -63,6 +63,9 @@ parse() { local uid="$3" local gid="$4" local ftype=$(filetype "${location}") + # remap uid/gid to 0 if necessary + [ "$uid" -eq "$root_uid" ] && uid=0 + [ "$gid" -eq "$root_gid" ] && gid=0 local str="${mode} ${uid} ${gid}" [ "${ftype}" == "invalid" ] && return 0 @@ -101,30 +104,100 @@ parse() { return 0 } -if [ -z "$1" ]; then - simple_initramfs -elif [ -f "$1" ]; then - print_mtime "$1" - cat "$1" -elif [ -d "$1" ]; then - srcdir=$(echo "$1" | sed -e 's://*:/:g') - dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null) - - # If $dirlist is only one line, then the directory is empty - if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then - print_mtime "$1" +usage() { + printf "Usage:\n" + printf "$0 [ [-u <root_uid>] [-g <root_gid>] [-d | <cpio_source>] ] . . .\n" + printf "\n" + printf -- "-u <root_uid> User ID to map to user ID 0 (root).\n" + printf " <root_uid> is only meaningful if <cpio_source>\n" + printf " is a directory.\n" + printf -- "-g <root_gid> Group ID to map to group ID 0 (root).\n" + printf " <root_gid> is only meaningful if <cpio_source>\n" + printf " is a directory.\n" + printf "<cpio_source> File list or directory for cpio archive.\n" + printf " If <cpio_source> is not provided then a\n" + printf " a default list will be output.\n" + printf -- "-d Output the default cpio list. If no <cpio_source>\n" + printf " is given then the default cpio list will be output.\n" + printf "\n" + printf "All options may be repeated and are interpreted sequentially\n" + printf "and immediately. -u and -g states are preserved across\n" + printf "<cpio_source> options so an explicit \"-u 0 -g 0\" is required\n" + printf "to reset the root/group mapping.\n" +} + +build_list() { + printf "\n#####################\n# $cpio_source\n" + + if [ -f "$cpio_source" ]; then + print_mtime "$cpio_source" + cat "$cpio_source" + elif [ -d "$cpio_source" ]; then + srcdir=$(echo "$cpio_source" | sed -e 's://*:/:g') + dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null) + + # If $dirlist is only one line, then the directory is empty + if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then + print_mtime "$cpio_source" - echo "${dirlist}" | \ - while read x; do - parse ${x} - done + echo "${dirlist}" | \ + while read x; do + parse ${x} + done + else + # Failsafe in case directory is empty + default_initramfs + fi else - # Failsafe in case directory is empty - simple_initramfs + echo " $0: Cannot open '$cpio_source'" >&2 + exit 1 fi -else - echo " $0: Cannot open '$1' (CONFIG_INITRAMFS_SOURCE)" >&2 - exit 1 -fi +} + + +root_uid=0 +root_gid=0 + +while [ $# -gt 0 ]; do + arg="$1" + shift + case "$arg" in + "-u") + root_uid="$1" + shift + ;; + "-g") + root_gid="$1" + shift + ;; + "-d") + default_list="$arg" + default_initramfs + ;; + "-h") + usage + exit 0 + ;; + *) + case "$arg" in + "-"*) + printf "ERROR: unknown option \"$arg\"\n" >&2 + printf "If the filename validly begins with '-', then it must be prefixed\n" >&2 + printf "by './' so that it won't be interpreted as an option." >&2 + printf "\n" >&2 + usage >&2 + exit 1 + ;; + *) + cpio_source="$arg" + build_list + ;; + esac + ;; + esac +done + +# spit out the default cpio list if a source hasn't been specified +[ -z "$cpio_source" -a -z "$default_list" ] && default_initramfs exit 0 |