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