]> git.ipfire.org Git - people/jschlag/ipfire-3.x-image.git/blame - generate_image.sh
Improve how we execute commands in chroot
[people/jschlag/ipfire-3.x-image.git] / generate_image.sh
CommitLineData
5bbf7721
JS
1#!/bin/bash
2###############################################################################
3# IPFire.org - An Open Source Firewall Solution #
4# Copyright (C) - IPFire Development Team <info@ipfire.org> #
5###############################################################################
6
9c529a3a
JS
7#Path of the script
8
9SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
10
5bbf7721 11
358ea1df
JS
12# INCLUDE SETTINGS
13. "${SCRIPT_PATH}/settings.sh"
424aa4ee 14
358ea1df
JS
15# INCLUDE FUNCTIONS
16. "${SCRIPT_PATH}/functions.sh"
5bbf7721 17#
358ea1df 18# Scripts starts here
5bbf7721
JS
19#
20
e04a0de0 21log DEBUG "Working dir is ${WORKING_DIR}"
5bbf7721 22
0ca7b8a5 23
424aa4ee
JS
24#Parse cmdline
25parse_cmdline $@
26
5bbf7721
JS
27# Check that pakfire is working
28check_for_pakfire
29
c38c098c
JS
30if ! check_for_free_space 10000 "${WORKING_DIR}"; then
31 exit ${EXIT_ERROR}
32fi
5bbf7721
JS
33
34# Check that the image does not exist yet
e04a0de0 35if [ -f ${IMAGE_BASE_FILE} ]; then
5bbf7721
JS
36 log ERROR "Image file does already exists"
37 exit 1
38fi
39
40# Check that the local repo file does not exists yet.
41# We do not want to override custom user configurations.
42if [ -f "${LOCAL_REPO_FILE}" ]; then
43 log ERROR "Config file ${LOCAL_REPO_FILE} for the local repo does already exists"
44 exit 1
45fi
46
e04a0de0
JS
47# cd into working directory
48cd ${WORKING_DIR} || exit ${EXIT_ERROR}
49
5bbf7721
JS
50#
51## Create the disk image.
52#
7d368c65 53dd if=/dev/zero of=${IMAGE_BASE_FILE} seek=${IMAGE_SIZE}M count=1k bs=1
5bbf7721
JS
54
55# Setup the loopback device.
e04a0de0 56outlo=`losetup -f --show ${IMAGE_BASE_FILE}`
5bbf7721 57
16178a1b 58log INFO "Loop device is ${outlo}"
5bbf7721
JS
59log INFO "Create partions and filesystem"
60
61# Create and msdos compatible table on the image.
62parted ${outlo} mklabel msdos
63
64# Add a new partition to the image.
65parted ${outlo} mkpart primary ${FILESYSTEM} 2048k 100% -a minimal
66
67# Make the primary partition bootable.
68parted ${outlo} set 1 boot on
69
70# Notify the kernel about the new partition.
71partx -a ${outlo}
72
73#
74## Create the filesystem.
75#
76mkfs.${FILESYSTEM} ${outlo}p1
77
78#
79## Mount the filesystem.
80#
81
82log INFO "Mount partion in ${IMAGE_MOUNT_DIR}"
83
5bbf7721
JS
84# Afterwards mount the image.
85mount -t ${FILESYSTEM} ${outlo}p1 ${IMAGE_MOUNT_DIR}
86
87#
88## Install IPFire 3.x.
89#
90
91# Add grub on x86_64 to the package list.
92if [ "${ARCH}" == "x86_64" ] || [ "${ARCH}" == "i686" ]; then
93 PACKAGES="${PACKAGES} grub"
94
95 # Store, that grub is present.
96 HAVE_GRUB="True"
97fi
98
99# Check if the git network stack should be installed.
100if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then
101 GIT_REPOS="${GIT_REPOS} git://git.ipfire.org/network.git"
102
103 # Add build dependencies of network package.
104 PACKAGES="${PACKAGES} ${NETWORK_BUILD_DEPS}"
105fi
106
107# Add develoment packes to the package list, if required.
108if [ "${INSTALL_DEV_PACKAGES}" == "True" ] || [ ! -z "${GIT_REPOS}" ]; then
109 PACKAGES="${PACKAGES} ${DEVELOPMENT_PACKAGES}"
110fi
111
112log INFO "Create local respository"
113
114
115# Check if the local repo should be used.
116if [ "${USE_LOCAL_REPO}" == "True" ]; then
117 # Create local repository.
118 mkdir -pv "${LOCAL_REPO_DIR}"
119
120 # Master repository.
121 if ! pakfire-server repo create ${LOCAL_REPO_DIR} ${LOCAL_REPO_SOURCE_PATH}; then
122 log ERROR "Failed to create a local respository"
91a03596 123 cleanup
5bbf7721
JS
124 exit 1
125 fi
126
127 # Create temporary pakfire repo file.
128 echo "[repo:local]" >> "${LOCAL_REPO_FILE}"
129 echo "description = Local repository." >> "${LOCAL_REPO_FILE}"
130 echo "enabled = 0" >> "${LOCAL_REPO_FILE}"
131 echo "baseurl = ${LOCAL_REPO_DIR}" >> "${LOCAL_REPO_FILE}"
132
133 ENABLE_LOCAL="--enable-repo=local"
134fi
135
136# Install IPFire 3.x in the created image.
137yes | pakfire --root=${IMAGE_MOUNT_DIR} ${ENABLE_LOCAL} install @Base ${PACKAGES}
138
139#
140# Enable serial console
141#
142
143
144#echo "GRUB_TERMINAL=\"serial console\"" >> "${IMAGE_MOUNT_DIR}/etc/default/grub"
145#echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=115200\"" >> "${IMAGE_MOUNT_DIR}/etc/default/grub"
146
147#Hack to install a /etc/default/grub file
148
9c529a3a 149cmd cp -f "${SCRIPT_PATH}/grub" "${IMAGE_MOUNT_DIR}/etc/default"
5bbf7721 150
5bbf7721
JS
151#
152## Generate fstab
153#
154
155# Gather the uuid of the partition.
156FS_UUID=$(blkid -o value -s UUID ${outlo}p1)
157
158# Write fstab.
879862d2
JS
159echo "UUID=${FS_UUID} / ${FILESYSTEM} defaults 0 0" > "${IMAGE_MOUNT_DIR}/etc/fstab"
160
161cat "${IMAGE_MOUNT_DIR}/etc/fstab"
5bbf7721
JS
162
163#
164## Remove the password for user root.
165#
166
167reset_root_password "${IMAGE_MOUNT_DIR}"
168
169#
170## Setup git repositories.
171#
172
173clone_git_repos "${IMAGE_MOUNT_DIR}/build" ${GIT_REPOS}
174
6dd651ff 175
5bbf7721
JS
176#
177## Prepare chrooting into the image.
178#
179
6dd651ff 180chroot_script_init
5bbf7721 181
5bbf7721 182if [ "${HAVE_GRUB}" == "True" ]; then
6dd651ff 183 chroot_script_add_cmd "grub-install --boot-directory=/boot/ ${outlo}" "grub-mkconfig -o /boot/grub/grub.cfg"
5bbf7721
JS
184fi
185
840a293b 186
6dd651ff
JS
187
188# ENABLE Serial Console
189chroot_script_add_cmd "/bin/systemctl enable getty@.service"
5bbf7721
JS
190
191
6dd651ff
JS
192# Check if the network stack should be build.
193if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then
194 chroot_script_add_cmd "cd /build/network" "./autogen.sh" "./configure" "make" "make install"
195fi
5bbf7721 196
6dd651ff 197chroot_script_exec
5bbf7721
JS
198
199
200# Insert the UUID because grub-mkconfig often fails to
201# detect that correctly
202
840a293b 203sed -i "${IMAGE_MOUNT_DIR}/boot/grub/grub.cfg" \
5bbf7721
JS
204 -e "s/root=[A-Za-z0-9\/=-]*/root=UUID=${FS_UUID}/g"
205
840a293b 206cat "${IMAGE_MOUNT_DIR}/boot/grub/grub.cfg"
879862d2
JS
207
208cat "${IMAGE_MOUNT_DIR}/etc/fstab"
209
210
5bbf7721
JS
211#
212## Tidy up.
213#
214
215# Wait a second.
216sleep 5
217
218# Check filesystem for damage.
219fsck.${FILESYSTEM} ${outlo}p1
220
879862d2
JS
221cleanup_stage_1
222
bdeefd44 223publish "${IMAGE_DIR_PUBLISH}" "${IMAGE_BASE_FILE}"
5bbf7721 224
c5c9ff39 225# Cleanup
879862d2 226cleanup_stage_2