3 # Copyright (c) 2010, 2018
4 # The DragonFly Project. All rights reserved.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
16 # 3. Neither the name of The DragonFly Project nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific, prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 . /etc/defaults/mkinitrd.conf
36 if [ -r /etc/mkinitrd.conf ]; then
38 echo "Loaded configuration from /etc/mkinitrd.conf"
43 # Calculate the total size of the given directory, taking care of the
48 awk '{ print $7,$1 }' | \
51 awk '{ sum+=$1 } END { print sum }' # byte
56 echo "Contents directories:" >&2
58 for dir in ${CONTENT_DIRS}; do
59 csize=$(calc_size ${dir})
60 echo "* ${dir}: ${csize} bytes" >&2
61 isize=$((${isize} + ${csize}))
63 # Round initrd size up by MB
64 isize_mb=$(echo "${isize}" | awk '
67 return (x>y ? y+1 : y);
74 echo $((${isize_mb} + 1))
79 if [ ! -d "$BUILD_DIR" ]; then
81 echo "Created build directory $BUILD_DIR"
83 vnconfig -c -S ${INITRD_SIZE}m -Z -T vn ${TMP_DIR}/initrd.img \
84 > ${TMP_DIR}/vndev.mkinitrd || {
85 echo "Failed to configure vn device"
89 VN_DEV=`cat ${TMP_DIR}/vndev.mkinitrd | cut -f 2 -d ' '`
90 rm ${TMP_DIR}/vndev.mkinitrd
92 echo "Configured $VN_DEV"
93 newfs -i 131072 -m 0 /dev/${VN_DEV}s0
94 echo "Formatted initrd image with UFS"
95 mount -t ufs /dev/${VN_DEV}s0 $BUILD_DIR
96 echo "Mounted initrd image on ${BUILD_DIR}"
101 umount /dev/${VN_DEV}s0
102 echo "Unmounted initrd image"
104 echo "Unconfigured $VN_DEV"
109 for dir in ${INITRD_DIRS}; do
110 mkdir -p ${BUILD_DIR}/${dir}
113 echo "Created directory structure"
118 for dir in ${CONTENT_DIRS}; do
119 cpdup -o -u ${dir}/ ${BUILD_DIR}/ || {
120 echo "Failed to copy ${dir} to ${BUILD_DIR}"
134 echo "usage: ${0##*/} [-b bootdir] [-c contentsdir] [-t tmpdir]" \
135 "[-s size] [-S max_size]"
139 args=`getopt b:c:s:S:t: $*`
140 test $? -ne 0 && usage
145 -b) BOOT_DIR="$2"; shift; shift;;
146 -c) CONTENT_DIR="$2"; shift; shift;;
147 -s) INITRD_SIZE="$2"; shift; shift;;
148 -S) INITRD_SIZE_MAX="$2"; shift; shift;;
149 -t) TMP_DIR="$2"; shift; shift;;
153 test ! -d ${BOOT_DIR} && usage
154 test ! -d ${CONTENT_DIR} && usage
155 test ! -d ${TMP_DIR} && usage
156 test ! -z "$1" && usage
158 BUILD_DIR="${TMP_DIR}/initrd"
159 INITRD_SIZE=${INITRD_SIZE%[mM]} # MB
160 INITRD_SIZE_MAX=${INITRD_SIZE_MAX%[mM]} # MB
162 CSIZE=$(calc_initrd_size)
163 echo "Required initrd image size: ${CSIZE} MB"
164 if [ -n "${INITRD_SIZE}" -a "${INITRD_SIZE}" != "0" ]; then
165 if [ ${CSIZE} -gt ${INITRD_SIZE} ]; then
166 echo "Specified initrd size (${INITRD_SIZE} MB) too small"
172 echo "Initrd size: ${INITRD_SIZE} MB"
174 if [ -n "${INITRD_SIZE_MAX}" -a "${INITRD_SIZE_MAX}" != "0" ] && \
175 [ ${INITRD_SIZE} -gt ${INITRD_SIZE_MAX} ]; then
176 echo "Exceeded the specified maximum size (${INITRD_SIZE_MAX} MB)"
185 echo -n "Compressing ${TMP_DIR}/initrd.img ..."
186 gzip -9 ${TMP_DIR}/initrd.img
188 echo -n "Copying ${TMP_DIR}/initrd.img.gz to ${BOOT_DIR}/kernel/initrd.img.gz ..."
189 mv ${TMP_DIR}/initrd.img.gz ${BOOT_DIR}/kernel/initrd.img.gz