Version 2.8.
[febootstrap.git] / febootstrap-to-initramfs.sh
1 #!/bin/bash -
2 # febootstrap-to-initramfs
3 # (C) Copyright 2009 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # Written by Richard W.M. Jones <rjones@redhat.com>
20
21 unset CDPATH
22
23 TEMP=`getopt \
24         -o '' \
25         --long files:,nocompress,help \
26         -n febootstrap-to-initramfs -- "$@"`
27 if [ $? != 0 ]; then
28     echo "febootstrap-to-initramfs: problem parsing the command line arguments"
29     exit 1
30 fi
31 eval set -- "$TEMP"
32
33 compress=yes
34 files=
35
36 usage ()
37 {
38     echo "Usage: febootstrap-to-initramfs [--files=filelist] [--nocompress] DIR"
39     echo "Please read febootstrap-to-initramfs(8) man page for more information."
40 }
41
42 while true; do
43     case "$1" in
44         --files)
45             files=$2
46             shift 2;;
47         --help)
48             usage
49             exit 0;;
50         --nocompress)
51             compress=no
52             shift;;
53         --)
54             shift
55             break;;
56         *)
57             echo "Internal error!"
58             exit 1;;
59     esac
60 done
61
62 if [ $# -ne 1 ]; then
63     usage
64     exit 1
65 fi
66
67 cd "$1" > /dev/null
68
69 if [ ! -f fakeroot.log -a $(id -u) -ne 0 ]; then
70     echo "no fakeroot.log and not running as root"
71     exit 1
72 fi
73
74 set -e
75
76 (
77 if [ -f fakeroot.log ]; then
78     if [ -z "$files" ]; then
79         fakeroot -i fakeroot.log \
80         sh -c 'find -not -name fakeroot.log -a -print0 | cpio --quiet -o -0 -H newc'
81     else
82         fakeroot -i fakeroot.log \
83         sh -c 'cpio --quiet -o -H newc' < $files
84     fi
85 else
86     if [ -z "$files" ]; then
87         find -not -name fakeroot.log -a -print0 | cpio --quiet -o -0 -H newc
88     else
89         cpio --quiet -o -H newc < $files
90     fi
91 fi
92 ) | (
93 if [ "$compress" = "yes" ]; then
94     gzip --best
95 else
96     cat
97 fi
98 )