1ed2843683f5d60c07161951714acc9bc7f90326
[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 TMP_DIR="/tmp"
34 BOOT_DIR="/boot"
35 INITRD_SIZE="15m"
36 BIN_TOOLS="mkdir rm sh kill"
37 SBIN_TOOLS="mount mount_devfs mount_hammer mount_nfs mount_null mount_procfs mount_tmpfs umount iscontrol cryptsetup lvm sysctl udevd"
38 INITRD_DIRS="bin boot dev etc mnt proc sbin tmp var new_root"
39 CONTENT_DIRS="/usr/share/initrd"
40
41 if [ -e /etc/defaults/mkinitrd.conf ]; then
42         . /etc/defaults/mkinitrd.conf
43         echo "Loaded configuration from /etc/defaults/mkinitrd.conf"
44 fi
45
46
47 if [ -e /etc/mkinitrd.conf ]; then
48         . /etc/mkinitrd.conf
49         echo "Loaded configuration from /etc/mkinitrd.conf"
50 fi
51
52 VN_DEV=""
53
54 create_vn()
55 {
56         if [ ! -d  $BUILD_DIR ]; then
57                 mkdir -p $BUILD_DIR
58                 echo "Created build directory $BUILD_DIR"
59         fi
60         VN_DEV=`vnconfig -c -S ${INITRD_SIZE} -Z -T vn ${TMP_DIR}/initrd.img | cut -f 2 -d ' '`
61         echo "Configured $VN_DEV"
62         newfs /dev/${VN_DEV}s0
63         echo "Formatted initrd image with UFS"
64         mount /dev/${VN_DEV}s0 $BUILD_DIR
65         echo "Mounted initrd image on ${BUILD_DIR}"
66 }
67
68 destroy_vn()
69 {
70         umount /dev/${VN_DEV}s0
71         echo "Unmounted initrd image"
72         vnconfig -u $VN_DEV
73         echo "Unconfigured $VN_DEV"
74 }
75
76 make_hier()
77 {
78         for dir in ${INITRD_DIRS}; do
79                 mkdir -p ${BUILD_DIR}/${dir}
80         done
81
82         echo "Created directory structure"
83 }
84
85 copy_tools()
86 {
87         for tool in ${BIN_TOOLS}; do
88                 objcopy -S /bin/${tool} ${BUILD_DIR}/bin/${tool}
89         done
90
91         for tool in ${SBIN_TOOLS}; do
92                 objcopy -S /sbin/${tool} ${BUILD_DIR}/sbin/${tool}
93         done
94
95         echo "Copied essential tools"
96 }
97
98 copy_content()
99 {
100         for dir in ${CONTENT_DIRS}; do
101                 cp -R ${dir}/* ${BUILD_DIR}/
102         done
103 }
104
105 print_info()
106 {
107         lt ${BUILD_DIR}
108         df -h | head -n 1
109         df -h | grep $VN_DEV
110 }
111
112 usage()
113 {
114         echo "usage: $0 [-b bootdir] [-t tmpdir]"
115         exit 2
116 }
117
118 args=`getopt b:t: $*`
119 test $? -ne 0 && usage
120
121 set -- $args
122 for i; do
123         case "$i" in
124         -b)     BOOT_DIR="$2"; shift; shift;;
125         -t)     TMP_DIR="$2"; shift; shift;;
126         --)     shift; break;
127         esac
128 done
129 test ! -d ${BOOT_DIR} && usage
130 test ! -d ${TMP_DIR} && usage
131 test ! -z $1 && usage
132 BUILD_DIR="${TMP_DIR}/initrd"
133
134 create_vn
135 make_hier
136 copy_tools
137 copy_content
138 print_info
139 destroy_vn
140 mv ${TMP_DIR}/initrd.img ${BOOT_DIR}/kernel/initrd.img