summaryrefslogtreecommitdiffstats
path: root/dracut-catimages.sh
blob: 949e89b37a64b1d766a941d1db6cc3acb976ce51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash --norc
#
# Copyright 2009 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

dinfo() {
    [[ $beverbose ]] && echo "$@" >&2
}

derror() {
    echo "Error: $*" >&2
}

usage() {
    #                                                   80x25 linebreak here ^
    cat << EOF
Usage: $0 [OPTION]... <initramfs> <base image> [<image>...]
Creates initial ramdisk image by concatenating several images from the command
line and /boot/dracut/

  -f, --force           Overwrite existing initramfs file.
  -i, --imagedir        Directory with additional images to add
                        (default: /boot/dracut/)
  -o, --overlaydir      Overlay directory, which contains files that
                        will be used to create an additional image
  --nooverlay           Do not use the overlay directory
  --noimagedir          Do not use the additional image directory
  -h, --help            This message
  --debug               Output debug information of the build process
  -v, --verbose         Verbose output during the build process
EOF
}

imagedir=/boot/dracut/
overlay=/var/lib/dracut/overlay

while (($# > 0)); do
    case $1 in
        -f | --force) force=yes ;;
        -i | --imagedir)
            imagedir=$2
            shift
            ;;
        -o | --overlaydir)
            overlay=$2
            shift
            ;;
        --nooverlay)
            no_overlay=yes
            shift
            ;;
        --noimagedir)
            no_imagedir=yes
            shift
            ;;
        -h | --help)
            usage
            exit 1
            ;;
        --debug) export debug="yes" ;;
        -v | --verbose) beverbose="yes" ;;
        -*)
            printf "\nUnknown option: %s\n\n" "$1" >&2
            usage
            exit 1
            ;;
        *) break ;;
    esac
    shift
done

outfile=$1
shift

if [[ -z $outfile ]]; then
    derror "No output file specified."
    usage
    exit 1
fi

baseimage=$1
shift

if [[ -z $baseimage ]]; then
    derror "No base image specified."
    usage
    exit 1
fi

if [[ -f $outfile && ! $force ]]; then
    derror "Will not override existing initramfs ($outfile) without --force"
    exit 1
fi

if [[ ! $no_imagedir && ! -d $imagedir ]]; then
    derror "Image directory $overlay is not a directory"
    exit 1
fi

if [[ ! $no_overlay && ! -d $overlay ]]; then
    derror "Overlay $overlay is not a directory"
    exit 1
fi

if [[ ! $no_overlay ]]; then
    ofile="$imagedir/90-overlay.img"
    dinfo "Creating image $ofile from directory $overlay"
    type pigz &> /dev/null && gzip=pigz || gzip=gzip
    (
        cd "$overlay" || return 1
        find . | cpio --quiet -H newc -o | $gzip -9 > "$ofile"
    )
fi

if [[ ! $no_imagedir ]]; then
    for i in "$imagedir/"*.img; do
        [[ -f $i ]] && images+=("$i")
    done
fi

images+=("$@")

dinfo "Using base image $baseimage"
cat -- "$baseimage" > "$outfile"

for i in "${images[@]}"; do
    dinfo "Appending $i"
    cat -- "$i" >> "$outfile"
done

dinfo "Created $outfile"

exit 0