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