d53440f7ac4d0ff30e8f6c00d7464117eda3bcd0
[dragonfly.git] / sbin / mkinitrd / mkinitrd.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2010
4 #       The DragonFly Project.  All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
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
15 #    distribution.
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.
19 #
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
31 # SUCH DAMAGE.
32 #
33
34 . /etc/defaults/mkinitrd.conf
35
36 if [ -r /etc/mkinitrd.conf ]; then
37         . /etc/mkinitrd.conf
38         echo "Loaded configuration from /etc/mkinitrd.conf"
39 fi
40
41 VN_DEV=""
42
43 create_vn()
44 {
45         if [ ! -d  $BUILD_DIR ]; then
46                 mkdir -p $BUILD_DIR
47                 echo "Created build directory $BUILD_DIR"
48         fi
49         vnconfig -c -S ${INITRD_SIZE} -Z -T vn ${TMP_DIR}/initrd.img \
50             > ${TMP_DIR}/vndev.mkinitrd
51         if [ $? -ne 0 ]; then
52             echo "Failed to configure vn device"
53             exit 1
54         fi
55
56         VN_DEV=`cat ${TMP_DIR}/vndev.mkinitrd | cut -f 2 -d ' '`
57         [ -f ${TMP_DIR}/vndev.mkinitrd ] && rm ${TMP_DIR}/vndev.mkinitrd
58
59         echo "Configured $VN_DEV"
60         newfs -i 131072 -m 0 /dev/${VN_DEV}s0
61         echo "Formatted initrd image with UFS"
62         mount /dev/${VN_DEV}s0 $BUILD_DIR
63         echo "Mounted initrd image on ${BUILD_DIR}"
64 }
65
66 destroy_vn()
67 {
68         umount /dev/${VN_DEV}s0
69         echo "Unmounted initrd image"
70         vnconfig -u $VN_DEV
71         echo "Unconfigured $VN_DEV"
72 }
73
74 make_hier()
75 {
76         for dir in ${INITRD_DIRS}; do
77                 mkdir -p ${BUILD_DIR}/${dir}
78         done
79
80         echo "Created directory structure"
81 }
82
83 copy_content()
84 {
85         for dir in ${CONTENT_DIRS}; do
86                 cpdup ${dir}/ ${BUILD_DIR}/
87         done
88 }
89
90 print_info()
91 {
92         lt ${BUILD_DIR}
93         df -h | head -n 1
94         df -h | grep $VN_DEV
95 }
96
97 usage()
98 {
99         echo "usage: $0 [-b bootdir] [-t tmpdir]"
100         exit 2
101 }
102
103 args=`getopt b:t: $*`
104 test $? -ne 0 && usage
105
106 set -- $args
107 for i; do
108         case "$i" in
109         -b)     BOOT_DIR="$2"; shift; shift;;
110         -t)     TMP_DIR="$2"; shift; shift;;
111         --)     shift; break;
112         esac
113 done
114 test ! -d ${BOOT_DIR} && usage
115 test ! -d ${TMP_DIR} && usage
116 test ! -z $1 && usage
117 BUILD_DIR="${TMP_DIR}/initrd"
118
119 create_vn
120 copy_content
121 make_hier
122 print_info
123 destroy_vn
124 mv ${TMP_DIR}/initrd.img ${BOOT_DIR}/kernel/initrd.img