]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - lfs/flash-images
Enhance the flash image to support EFI
[people/pmueller/ipfire-2.x.git] / lfs / flash-images
1 ###############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2007-2018 IPFire Team <info@ipfire.org> #
5 # #
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. #
10 # #
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. #
15 # #
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/>. #
18 # #
19 ###############################################################################
20
21 ###############################################################################
22 # Definitions
23 ###############################################################################
24
25 include Config
26
27 VER = ipfire
28
29 THISAPP = flash-image
30 TARGET = $(DIR_INFO)/$(THISAPP)
31
32 ifeq "$(MACHINE)" "i586"
33 EFI = 1
34 endif
35
36 DEVICE = $(shell losetup -f)
37 PART_BOOT = /dev/mapper/$(patsubst /dev/%,%,$(DEVICE))p1
38 ifeq "$(EFI)" "1"
39 PART_EFI = /dev/mapper/$(patsubst /dev/%,%,$(DEVICE))p2
40 endif
41 PART_ROOT = /dev/mapper/$(patsubst /dev/%,%,$(DEVICE))p3
42
43 IMAGE_FILE = /install/images/$(SNAME)-$(VERSION).2gb-ext4.$(BUILD_ARCH)-full-core$(CORE).img.xz
44
45 FSTAB_FMT = UUID=%s %-8s %-4s %-10s %d %d\n
46
47 ###############################################################################
48 # Top-level Rules
49 ###############################################################################
50
51 install : $(TARGET)
52
53 check :
54
55 download :
56
57 md5 :
58
59 ###############################################################################
60 # Installation Details
61 ###############################################################################
62 MNThdd := /install/harddisk
63 IMG := /install/images/image.img
64
65 # All sizes in blocks
66 ifeq "$(BUILD_PLATFORM)" "arm"
67 BOOTLOADER =
68 S_OFFSET = 8192
69
70 # FAT32
71 PART_TYPE = c
72 else
73 BOOTLOADER = grub
74 S_OFFSET = 8192
75
76 # Logical
77 PART_TYPE = L
78 endif
79
80 GRUB_CFGS = /boot/grub/grub.cfg
81
82 ifeq "$(EFI)" "1"
83 S_EFI = 204800
84 GRUB_CFGS += /boot/efi/EFI/ipfire/grub-efi/grub.cfg
85 else
86 S_EFI = 0
87 endif
88
89 # /boot: 128MB - OFFSET
90 # / : 1200 MB
91 S_BOOT := $(shell echo $$(( 262144 - $(S_OFFSET) )))
92 S_ROOT := 2477600
93
94 PADDING = 100 # MB
95
96 ifeq "$(EFI)" "1"
97 SFDISK_EFI = $(S_OFFSET),$(S_EFI),c,*\n
98 SFDISK = $(SFDISK_BOOT)$(SFDISK_EFI)$(SFDISK_ROOT)
99 else
100 SFDISK_SWAP = ,0,0\n
101 SFDISK = $(SFDISK_BOOT)$(SFDISK_SWAP)$(SFDISK_ROOT)
102 endif
103
104 SFDISK_BOOT = $(shell echo $$(( $(S_OFFSET) + $(S_EFI) ))),$(S_BOOT),$(PART_TYPE)\n
105 SFDISK_ROOT = $(shell echo $$(( $(S_OFFSET) + $(S_EFI) + $(S_BOOT) ))),$(S_ROOT),L\n
106
107 $(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
108 # Stop if $(MNThdd) is still mounted
109 mountpoint $(MNThdd) && exit 1 || exit 0
110
111 rm -rf $(IMG) $(MNThdd) && mkdir -p $(MNThdd)
112
113 # Allocate image on disk
114 dd if=/dev/zero of=$(IMG) bs=512 count=$$(( $(S_OFFSET) + $(S_BOOT) + $(S_EFI) + $(S_ROOT) ))
115 losetup $(DEVICE) $(IMG)
116
117 # Write Partition table
118 echo -e "$(SFDISK)" | sfdisk -uS $(DEVICE)
119
120 # Remove 2nd empty partition
121 ifneq "$(EFI)" "1"
122 -echo -e "d\n2\nw\n\q\n" | fdisk $(DEVICE)
123 endif
124
125 kpartx -v -a $(DEVICE)
126
127 # Format them
128 ifeq "$(PART_TYPE)" "c"
129 mkfs.vfat $(PART_BOOT)
130 else
131 mkfs.ext2 -F $(PART_BOOT)
132 endif
133 ifeq "$(EFI)" "1"
134 mkfs.vfat $(PART_EFI)
135 endif
136 mkfs.ext4 -O ^has_journal,extent -F $(PART_ROOT)
137
138 # Most systems that use Flashimages has no RTC at boot
139 # so the interval check should disables
140 tune2fs -i0 $(PART_ROOT)
141
142 # Mount root partition
143 mount $(PART_ROOT) $(MNThdd)
144
145 # Mount boot partition
146 mkdir -pv $(MNThdd)/boot
147 mount $(PART_BOOT) $(MNThdd)/boot
148
149 ifeq "$(EFI)" "1"
150 mkdir -pv $(MNThdd)/boot/efi
151 mount $(PART_EFI) $(MNThdd)/boot/efi
152 endif
153
154 # Install Pandaboard MLO and uboot first
155 ifeq "$(BUILD_ARCH)" "armv5tel"
156 cp -v /boot/MLO $(MNThdd)/boot/
157 cp -v /boot/u-boot.img $(MNThdd)/boot/
158 sync
159 umount $(MNThdd)/boot
160 mount $(PART_BOOT) $(MNThdd)/boot
161 endif
162
163 # Install IPFire
164 ifneq "$(BUILD_PLATFORM)" "arm"
165 tar -x --xz -C $(MNThdd)/ -f /install/cdrom/distro.img
166 else
167 tar -x -C $(MNThdd)/ -f /install/cdrom/distro.img
168 endif
169 -touch $(MNThdd)/lib/modules/$(KVER)-ipfire/modules.dep
170 mkdir $(MNThdd)/proc
171 mount --bind /proc $(MNThdd)/proc
172 mount --bind /dev $(MNThdd)/dev
173 mount --bind /sys $(MNThdd)/sys
174
175 chroot $(MNThdd) /usr/bin/perl -e "require '/var/ipfire/lang.pl'; &Lang::BuildCacheLang"
176
177 # Create /etc/fstab
178 printf "$(FSTAB_FMT)" "$$(blkid -o value -s UUID $(PART_BOOT))" "/boot" \
179 "auto" "defaults" 1 2 > $(MNThdd)/etc/fstab
180 ifeq "$(EFI)" "1"
181 printf "$(FSTAB_FMT)" "$$(blkid -o value -s UUID $(PART_EFI))" "/boot/efi" \
182 "auto" "defaults" 1 2 >> $(MNThdd)/etc/fstab
183 endif
184 printf "$(FSTAB_FMT)" "$$(blkid -o value -s UUID $(PART_ROOT))" "/" \
185 "auto" "defaults" 1 1 >> $(MNThdd)/etc/fstab
186
187 ifeq "$(BOOTLOADER)" "grub"
188 # backup defaults file
189 cp $(MNThdd)/etc/default/grub $(MNThdd)/etc/default/grub.backup
190
191 # Enable also serial console on GRUB
192 echo "GRUB_TERMINAL=\"serial console\"" >> $(MNThdd)/etc/default/grub
193 echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=115200\"" >> $(MNThdd)/etc/default/grub
194
195 # Add additional entry for Serial console
196 cp $(DIR_SRC)/config/flash-images/grub/11_linux_scon \
197 $(MNThdd)/etc/grub.d/
198
199 # Create configuration
200 for i in $(GRUB_CFGS); do \
201 mkdir -pv $$(dirname $(MNThdd)$(GRUB_CFG)); \
202 chroot $(MNThdd) grub-mkconfig -o $$i || exit 1; \
203 done
204
205 # Boot the first kernel by default
206 chroot $(MNThdd) grub-set-default 0
207
208 # Insert the UUID because grub-mkconfig often fails to
209 # detect that correctly
210 sed -i $(addprefix $(MNThdd),$(GRUB_CFGS)) \
211 sed -i $(MNThdd)/boot/grub/grub.cfg \
212 -e "s/root=[A-Za-z0-9\/=-]*/root=UUID=$$(blkid -o value -s UUID $(PART_ROOT))/g"
213
214 # Install GRUB
215 grub-install --force --recheck --no-floppy --target=i386-pc \
216 --root-directory=$(MNThdd) $(DEVICE)
217
218 # restore orginal defaults
219 mv -f $(MNThdd)/etc/default/grub.backup $(MNThdd)/etc/default/grub
220 rm -f $(MNThdd)/etc/grub.d/11_linux_scon
221
222 ifeq "$(EFI)" "1"
223 mkdir -pv $(MNThdd)/boot/efi/EFI/boot
224 cp -vf $(MNThdd)/boot/efi/EFI/ipfire/grub-efi/grub.efi \
225 $(MNThdd)/boot/efi/EFI/boot/bootia32.efi
226 endif
227 endif
228
229 # Set ramdisk mode to automatic
230 echo RAMDISK_MODE=2 > $(MNThdd)/etc/sysconfig/ramdisk
231
232 # Automatically resize the root partition to its maximum size at first boot
233 touch $(MNThdd)/.partresize
234
235 # Unmount
236 umount $(MNThdd)/proc
237 umount $(MNThdd)/sys
238 umount $(MNThdd)/dev
239 ifeq "$(EFI)" "1"
240 umount $(MNThdd)/boot/efi
241 endif
242 umount $(MNThdd)/boot
243 umount $(MNThdd)
244
245 # zerofree the ext2 images to get better compression
246 ifneq "$(PART_TYPE)" "c"
247 zerofree $(PART_BOOT)
248 -fsck.ext2 -f -y $(PART_BOOT)
249 fsck.ext2 -f -y $(PART_BOOT)
250 endif
251 zerofree $(PART_ROOT)
252 -fsck.ext4 -f -y $(PART_ROOT)
253 fsck.ext4 -f -y $(PART_ROOT)
254
255 sleep 10 #Ubuntu compiling: allow time to automount/dismount
256 kpartx -d -v $(DEVICE)
257 losetup -d $(DEVICE)
258
259 # Add padding at the end of the image (to fix alignment issues if the image is
260 # not copied to a block device)
261 dd if=/dev/zero bs=1M count=$(PADDING) >> $(IMG)
262
263 ifeq "$(BUILD_ARCH)" "armv5tel"
264 # Install u-boot for Orangepi Zero/Nanopi DUO into image 8KB
265 dd if=/usr/share/u-boot/orangepi_zero/u-boot-sunxi-with-spl.bin of=$(IMG) bs=1K seek=8 conv=notrunc
266 endif
267
268 # Compress Image
269 xz $(XZ_OPT) < $(IMG) > $(IMAGE_FILE)
270 rm -rf $(IMG) $(MNThdd)