1 ###############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 ###############################################################################
21 ###############################################################################
23 ###############################################################################
30 TARGET = $(DIR_INFO)/$(THISAPP)
32 DEVICE = $(shell losetup -f)
33 PART_BOOT = /dev/mapper/$(patsubst /dev/%,%,$(DEVICE))p1
35 PART_EFI = /dev/mapper/$(patsubst /dev/%,%,$(DEVICE))p2
37 PART_ROOT = /dev/mapper/$(patsubst /dev/%,%,$(DEVICE))p3
39 IMAGE_FILE = /install/images/$(SNAME)-$(VERSION)-core$(CORE)-$(BUILD_ARCH).img.xz
41 FSTAB_FMT = UUID=%s %-8s %-4s %-10s %d %d\n
43 ###############################################################################
45 ###############################################################################
55 ###############################################################################
56 # Installation Details
57 ###############################################################################
58 MNThdd := $(DIR_TMP)/harddisk
59 IMG := $(DIR_TMP)/image.img
62 ifeq "$(BUILD_PLATFORM)" "arm"
68 ifeq "$(BUILD_ARCH)" "aarch64"
80 # /boot: 256 MB - OFFSET
82 S_BOOT := $(shell echo $$(( 524288 - $(S_OFFSET) )))
94 SFDISK = $(SFDISK_BOOT)$(SFDISK_EFI)$(SFDISK_ROOT)
96 SFDISK = $(SFDISK_BOOT),0,0\n$(SFDISK_ROOT)
99 SFDISK_BOOT = $(S_OFFSET),$(S_BOOT),$(PART_TYPE),*\n
100 SFDISK_EFI = $(shell echo $$(( $(S_OFFSET) + $(S_BOOT) ))),$(S_EFI),U\n
101 SFDISK_ROOT = $(shell echo $$(( $(S_OFFSET) + $(S_BOOT) + $(S_EFI) ))),$(S_ROOT),L\n
103 $(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
104 # Stop if $(MNThdd) is still mounted
105 mountpoint $(MNThdd) && exit 1 || exit 0
107 rm -rf $(IMG) $(MNThdd) && mkdir -p $(MNThdd)
109 # Allocate image on disk
110 dd if=/dev/zero of=$(IMG) bs=512 count=$$(( $(S_OFFSET) + $(S_BOOT) + $(S_EFI) + $(S_ROOT) ))
111 losetup $(DEVICE) $(IMG)
113 # Write Partition table
114 echo -e "$(SFDISK)" | sfdisk -uS $(DEVICE)
116 kpartx -v -a $(DEVICE)
119 ifeq "$(PART_TYPE)" "c"
120 mkfs.vfat $(PART_BOOT)
122 mkfs.ext2 -F $(PART_BOOT)
125 mkfs.vfat $(PART_EFI)
127 mkfs.ext4 -O ^has_journal,extent -F $(PART_ROOT)
129 # Most systems that use Flashimages has no RTC at boot
130 # so the interval check should disables
131 tune2fs -i0 $(PART_ROOT)
133 # Mount root partition
134 mount $(PART_ROOT) $(MNThdd)
136 # Mount boot partition
137 mkdir -pv $(MNThdd)/boot
138 mount $(PART_BOOT) $(MNThdd)/boot
141 mkdir -pv $(MNThdd)/boot/efi
142 mount $(PART_EFI) $(MNThdd)/boot/efi
145 # Install Pandaboard MLO and uboot first
146 ifeq "$(BUILD_ARCH)" "armv6l"
147 cp -v /boot/MLO $(MNThdd)/boot/
148 cp -v /boot/u-boot.img $(MNThdd)/boot/
150 umount $(MNThdd)/boot
151 mount $(PART_BOOT) $(MNThdd)/boot
155 ifneq "$(BUILD_PLATFORM)" "arm"
156 tar $(TAR_OPTIONS) -x --zstd -C $(MNThdd)/ -f $(DIR_TMP)/cdrom/distro.img
158 tar $(TAR_OPTIONS) -x -C $(MNThdd)/ -f $(DIR_TMP)/cdrom/distro.img
160 -touch $(MNThdd)/lib/modules/$(KVER)-ipfire/modules.dep
162 mount --bind /proc $(MNThdd)/proc
163 mount --bind /dev $(MNThdd)/dev
164 mount --bind /sys $(MNThdd)/sys
166 chroot $(MNThdd) /usr/bin/perl -e "require '/var/ipfire/lang.pl'; &Lang::BuildCacheLang"
169 printf "$(FSTAB_FMT)" "$$(blkid -o value -s UUID $(PART_BOOT))" "/boot" \
170 "auto" "defaults,nodev,noexec,nosuid" 1 2 > $(MNThdd)/etc/fstab
172 printf "$(FSTAB_FMT)" "$$(blkid -o value -s UUID $(PART_EFI))" "/boot/efi" \
173 "auto" "defaults" 1 2 >> $(MNThdd)/etc/fstab
175 printf "$(FSTAB_FMT)" "$$(blkid -o value -s UUID $(PART_ROOT))" "/" \
176 "auto" "defaults" 1 1 >> $(MNThdd)/etc/fstab
178 ifeq "$(BOOTLOADER)" "grub"
179 # backup defaults file
180 cp $(MNThdd)/etc/default/grub $(MNThdd)/etc/default/grub.backup
182 # Enable also serial console on GRUB
183 echo "GRUB_TERMINAL=\"serial console\"" >> $(MNThdd)/etc/default/grub
184 echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=115200\"" >> $(MNThdd)/etc/default/grub
186 # Add additional entry for Serial console
187 cp $(DIR_SRC)/config/flash-images/grub/11_linux_scon \
188 $(MNThdd)/etc/grub.d/
190 # Create configuration
191 mkdir -pv $(MNThdd)/boot/grub
192 GRUB_FIRST_BOOT=true KERNEL_RELEASE="$(KVER)-ipfire" chroot $(MNThdd) \
193 grub-mkconfig -o /boot/grub/grub.cfg
195 # Boot the first kernel by default
196 chroot $(MNThdd) grub-set-default 0
198 # Insert the UUID because grub-mkconfig often fails to
199 # detect that correctly
200 sed -i $(MNThdd)/boot/grub/grub.cfg \
201 -e "s/root=[A-Za-z0-9\/=-]*/root=UUID=$$(blkid -o value -s UUID $(PART_ROOT))/g"
203 ifeq "$(BUILD_PLATFORM)" "x86"
205 grub-install --force --recheck --no-floppy --target=i386-pc \
206 --root-directory=$(MNThdd) $(DEVICE)
210 # Install GRUB for EFI
211 grub-install --target=$(GRUB_ARCH)-efi --removable --no-nvram \
212 --boot-directory=$(MNThdd)/boot --efi-directory=$(MNThdd)/boot/efi
215 # restore orginal defaults
216 mv -f $(MNThdd)/etc/default/grub.backup $(MNThdd)/etc/default/grub
217 rm -f $(MNThdd)/etc/grub.d/11_linux_scon
220 ifeq "$(BUILD_PLATFORM)" "arm"
221 # Insert the UUID to uENV.txt
222 sed -i $(MNThdd)/boot/uENV.txt \
223 -e "s/^root_dev=.*/root_dev=UUID=$$(blkid -o value -s UUID $(PART_ROOT))/g"
226 # Set ramdisk mode to automatic
227 echo RAMDISK_MODE=2 > $(MNThdd)/etc/sysconfig/ramdisk
229 # Automatically resize the root partition to its maximum size at first boot
230 touch $(MNThdd)/.partresize
233 umount $(MNThdd)/proc
237 umount $(MNThdd)/boot/efi
239 umount $(MNThdd)/boot
242 # zerofree the ext2 images to get better compression
243 ifneq "$(PART_TYPE)" "c"
244 zerofree $(PART_BOOT)
245 -fsck.ext2 -f -y $(PART_BOOT)
246 fsck.ext2 -f -y $(PART_BOOT)
248 zerofree $(PART_ROOT)
249 -fsck.ext4 -f -y $(PART_ROOT)
250 fsck.ext4 -f -y $(PART_ROOT)
252 sleep 10 #Ubuntu compiling: allow time to automount/dismount
253 kpartx -d -v $(DEVICE)
256 # Add padding at the end of the image (to fix alignment issues if the image is
257 # not copied to a block device)
258 dd if=/dev/zero bs=1M count=$(PADDING) >> $(IMG)
260 ifeq "$(BUILD_ARCH)" "armv6l"
261 # Install u-boot for Orangepi Zero/Nanopi DUO into image 8KB
262 dd if=/usr/share/u-boot/orangepi_zero/u-boot-sunxi-with-spl.bin of=$(IMG) bs=1K seek=8 conv=notrunc
264 ifeq "$(BUILD_ARCH)" "aarch64"
265 # Install u-boot for NanoPi R2S into image 8KB
266 dd if=/usr/share/u-boot/nanopi_r2s/u-boot-rockchip.bin of=$(IMG) bs=1K seek=32 conv=notrunc
270 xz $(XZ_OPT) < $(IMG) > $(IMAGE_FILE)
271 rm -rf $(IMG) $(MNThdd) $(DIR_TMP)/cdrom