This repository was archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMakefile.devel
388 lines (305 loc) · 16.6 KB
/
Makefile.devel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# Copyright 2007-2008 Massimo Rimondini - Computer Networks Research Group,
# Roma Tre University.
#
# This file is part of Netkit.
#
# Netkit is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Netkit is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Netkit. If not, see <http://www.gnu.org/licenses/>.
# *********************************** WARNING ************************************
# *********************************** WARNING ************************************
# *********************************** WARNING ************************************
# *********************************** WARNING ************************************
# *********************************** WARNING ************************************
#
# As the filesystem build procedure frequently needs root access to your host, we
# provide no warranty (once more, NO WARRANTY) that using it will not break your
# system. We put all the necessary care in ensuring that no unnecessary
# components are touched, yet interactions with unexpected host settings may
# still happen and cause damage.
# The procedure is meant to be used by developers or people who know how to make
# a filesystem image but miss the necessary tweaks to make it become a Netkit
# filesystem. Standard users, in general, have no reason to execute it.
# Please *** AVOID *** using this procedure if you are not sure about what you are
# doing or are not ready to debug potential serious problems on your host.
#
# ******************************** END OF WARNING ********************************
# The following variables should contain relative paths
FS_MOUNT_DIR_RELATIVE=mounted_fs
TOOLS_SRC_BUILD_DIR_RELATIVE=tools-build
# The following variables should contain absolute paths
FS_BUILD_DIR=$(CURDIR)/fs-build
FS_MOUNT_DIR=$(CURDIR)/$(FS_MOUNT_DIR_RELATIVE)
TOOLS_SRC_DIR=$(FS_BUILD_DIR)/tools-src
TOOLS_SRC_BUILD_DIR=$(TOOLS_SRC_DIR)/$(TOOLS_SRC_BUILD_DIR_RELATIVE)
TWEAKS_DIR=$(FS_BUILD_DIR)/netkit-tweaks
# Debian distribution to be installed in the filesystem image
DEBIAN_VERSION?=sid
# Available space in the filesystem image, in MB
FS_SIZE?=1024
# Additional mkfs flags
MKFS_FLAGS?=
# Target architecture of the FS image. Valid values include:
# alpha amd64 arm armel hppa i386 ia64 mips mipsel powerpc s390 sparc
# However, note that it may not be possible to compile some tools from the
# source code if no suitable cross-architecture compiler is installed.
SUBARCH?=i386
# URL of the Debian mirror to get packages from
DEBIAN_MIRROR?=http://ftp.it.debian.org/debian
# Comma separated list of packages to be included in the base system install.
# This comes handy should the Debian distribution being installed have broken
# dependencies.
ADDITIONAL_PACKAGES=debconf-utils,locales,libept1,libxapian15
# Files where downloaded Debian packages should be put
BASE_PACKAGES_TARBALL_BASENAME=debian-base-packages-$(DEBIAN_VERSION)-$(SUBARCH)
PACKAGES_TARBALL_BASENAME=debian-packages-$(DEBIAN_VERSION)-$(SUBARCH)
BASE_PACKAGES_TARBALL_TIMESTAMP_BASENAME=$(BASE_PACKAGES_TARBALL_BASENAME)-$(shell date +%Y%m%d)
PACKAGES_TARBALL_TIMESTAMP_BASENAME=$(PACKAGES_TARBALL_BASENAME)-$(shell date +%Y%m%d)
##############################################################################
##############################################################################
##############################################################################
## Settings below these lines should never be touched. If you really need to
## alter them, be really *REALLY* careful, as changing even a single character
## may screw file paths or other critical operations, causing damage to your
## host filesystem!!
##############################################################################
##############################################################################
##############################################################################
# The CDPATH environment variable can cause problems
override CDPATH=
# Uncomment this if you prefer to use sudo (note the space at the end of the line)
SUDO_PFX=/usr/bin/sudo -E -p %u\''s password:'
SUDO_SFX=
# Uncomment this if you prefer to use su
#SUDO_PFX=/bin/su -c '
#SUDO_SFX='
.EXPORT_ALL_VARIABLES:
LC_ALL=en_US.UTF-8
LOSETUP=/sbin/losetup
DEBOOTSTRAP=/usr/sbin/debootstrap
NK_FS_RELEASE=$(shell awk '/filesystem version/ {print $$NF}' netkit-filesystem-version)
# Filesystem label must be at most 16 characters long
FS_LABEL=nkfs-$(SUBARCH)-$(NK_FS_RELEASE)
# It is very important to make the following simply expanded (':=' instead of
# '='). Also, ***DO NOT EVER*** fiddle with the value of this variable, as most
# subsequent commands use its value as the name of a raw device to write to!!!
override LOOP_DEV:=$(shell $(LOSETUP) -f)
SECTOR_SIZE=512
BLOCK_SIZE=1024
SECTORS_PER_TRACK=63
PARTITION_OFFSET_BLOCKS=32
PARTITION_OFFSET_SECTORS:=$(shell expr $(PARTITION_OFFSET_BLOCKS) \* $(BLOCK_SIZE) / $(SECTOR_SIZE))
PARTITION_OFFSET_BYTES:=$(shell expr $(PARTITION_OFFSET_SECTORS) \* $(SECTOR_SIZE))
FS_SIZE_BYTES:=$(shell expr $(FS_SIZE) \* 1024 \* 1024)
FS_SIZE_TRACKS:=$(shell expr $(FS_SIZE_BYTES) / $(SECTORS_PER_TRACK) / $(SECTOR_SIZE))
ACTUAL_FS_SIZE_BYTES:=$(shell expr $(FS_SIZE_TRACKS) \* $(SECTORS_PER_TRACK) \* $(SECTOR_SIZE) - $(PARTITION_OFFSET_BYTES))
ACTUAL_FS_SIZE_BLOCKS:=$(shell expr $(ACTUAL_FS_SIZE_BYTES) / $(BLOCK_SIZE))
define UMOUNT_FS
-$(SUDO_PFX)umount $(FS_MOUNT_DIR)/proc$(SUDO_SFX) >/dev/null 2>&1; true
-$(SUDO_PFX)umount $(FS_MOUNT_DIR)/proc$(SUDO_SFX) >/dev/null 2>&1; true
-$(SUDO_PFX)umount $(FS_MOUNT_DIR)$(SUDO_SFX) >/dev/null 2>&1; true
endef
define SETUP_LOOPDEV
-$(SUDO_PFX)$(LOSETUP) -d $(LOOP_DEV)$(SUDO_SFX) >/dev/null 2>&1; true
$(SUDO_PFX)$(LOSETUP) $(LOOP_DEV) $(CURDIR)/netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE)$(SUDO_SFX)
endef
define SETUP_LOOPFS
-$(SUDO_PFX)$(LOSETUP) -d $(LOOP_DEV)$(SUDO_SFX) >/dev/null 2>&1; true
$(SUDO_PFX)$(LOSETUP) -o $(PARTITION_OFFSET_BYTES) $(LOOP_DEV) $(CURDIR)/netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE)$(SUDO_SFX)
endef
define MOUNT_FS
$(UMOUNT_FS)
$(SETUP_LOOPFS)
mkdir -p $(FS_MOUNT_DIR)
-$(SUDO_PFX)mount $(LOOP_DEV) $(FS_MOUNT_DIR)$(SUDO_SFX)
endef
define CLEAN_MOUNTDIRS
$(UMOUNT_FS)
-$(SUDO_PFX)rm -fr $(FS_MOUNT_DIR)$(SUDO_SFX)
endef
define CLEAN_LOOPDEVS
-for LOOP in /dev/loop[0-9]*; do $(LOSETUP) $${LOOP} | grep -q "netkit-fs" >&/dev/null && $(SUDO_PFX)$(LOSETUP) -d $(SUDO_SFX)$${LOOP}; done >& /dev/null; true
endef
default: help
.PHONY: help
help:
@echo
@echo -e "\e[1mAvailable targets are:\e[0m"
@echo
@echo -e " \e[1mfilesystem\e[0m Build a Netkit filesystem image from scratch. This"
@echo " procedure requires an Internet connection to retrieve"
@echo " the required filesystem packages."
@echo
@echo -e " \e[1mpackage\e[0m Create a distributable tarball of the Netkit filesystem."
@echo
@echo -e " \e[1mclean\e[0m Remove files from previous builds."
@echo
@echo -e " \e[1mclean-all\e[0m Also remove downloaded files."
@echo
@echo -e "\e[1mAvailable variables that influence the build process are:"
@echo
@echo -e " \e[1mDEBIAN_VERSION\e[0m The name of the Debian distribution to be"
@echo " installed. Refer to http://www.debian.org/releases/"
@echo " for the available versions."
@echo
@echo -e " \e[1mFS_SIZE\e[0m The size of the filesystem image to be created"
@echo " (in terms of available space inside the image),"
@echo " expressed in MB (default: $(FS_SIZE))."
@echo
@echo -e " \e[1mSUBARCH\e[0m Packages inside the filesystem will be"
@echo " installed for this architecture. This parameter"
@echo " may be influenced by the architecture used"
@echo " while compiling the UML kernel. Possible values"
@echo " are: alpha amd64 arm armel hppa i386 ia64"
@echo " mips mipsel powerpc s390 sparc (default: $(SUBARCH))."
@echo " Note that some installed tools need to be"
@echo " compiled from source, and compilation may"
@echo " not be supported for all the above"
@echo " architectures."
@echo
@echo -e " \e[1mDEBIAN_MIRROR\e[0m The Debian mirror that is used to"
@echo " retrieve the packages. Use a nearby mirror"
@echo " (default: $(DEBIAN_MIRROR))."
@echo " A list of available mirrors is available"
@echo " at http://www.debian.org/mirror/list."
@echo
@echo -e " \e[1mADDITIONAL_PACKAGES\e[0m Additional packages that should be"
@echo " installed early in the build process. The"
@echo " default set of packages is usually fine."
@echo
@echo -e " \e[1mMKFS_FLAGS\e[0m Additional flags to pass to mkfs."
@echo
.PHONY: filesystem
.SILENT: filesystem
filesystem: netkit-fs
.SILENT: netkit-fs
netkit-fs: .sparsify
$(CLEAN_MOUNTDIRS)
$(CLEAN_LOOPDEVS)
ln -fs netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE) netkit-fs
echo
echo "Filesystem size: $(FS_SIZE) MB"
echo "Filesystem offset: $(PARTITION_OFFSET_BYTES) bytes"
.SILENT: netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE)
netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE):
echo -e "\n\e[1m\e[32m========= Creating disk image... =========\e[0m"
dd if=/dev/zero of=netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE) count=0 seek=$(FS_SIZE) bs=1M
.SILENT: .partitions_created
.partitions_created: | netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE)
echo -e "\n\e[1m\e[32m====== Creating partition table... =======\e[0m"
$(SETUP_LOOPDEV)
echo "$(PARTITION_OFFSET_SECTORS),,L,*" | $(SUDO_PFX)sfdisk -q -H 1 -S $(SECTORS_PER_TRACK) -uS -L --no-reread $(LOOP_DEV)$(SUDO_SFX)
: > .partitions_created
.SILENT: .fs_created
.fs_created: .partitions_created
echo -e "\n\e[1m\e[32m========== Creating filesystem... ========\e[0m"
$(SETUP_LOOPFS)
# Mkfs may overestimate the block count if it is not an integer number;
# therefore the block count must be explicitly specified.
$(SUDO_PFX)mkfs.ext2 -q -b $(BLOCK_SIZE) $(if $(FS_LABEL),-L $(FS_LABEL),) $(MKFS_FLAGS) $(LOOP_DEV) $(ACTUAL_FS_SIZE_BLOCKS)$(SUDO_SFX)
$(SUDO_PFX)tune2fs -c 0 -i 0 $(LOOP_DEV)$(SUDO_SFX)
: > .fs_created
.SILENT: $(BASE_PACKAGES_TARBALL_BASENAME).tgz
$(BASE_PACKAGES_TARBALL_BASENAME).tgz:
mkdir -p $(FS_MOUNT_DIR)
echo -e "\n\e[1m\e[33m======= Downloading base packages... =====\e[0m"
$(DEBOOTSTRAP) --arch=$(SUBARCH) $(if $(ADDITIONAL_PACKAGES),--include $(ADDITIONAL_PACKAGES),) --make-tarball $(CURDIR)/$(BASE_PACKAGES_TARBALL_TIMESTAMP_BASENAME).tgz $(DEBIAN_VERSION) $(FS_MOUNT_DIR) $(DEBIAN_MIRROR)
ln -fs $(BASE_PACKAGES_TARBALL_TIMESTAMP_BASENAME).tgz $(BASE_PACKAGES_TARBALL_BASENAME).tgz
.SILENT: .base_system_installed
.base_system_installed: $(BASE_PACKAGES_TARBALL_BASENAME).tgz .fs_created
echo -e "\n\e[1m\e[32m======= Installing base system... ========\e[0m"
$(MOUNT_FS)
$(SUDO_PFX)$(DEBOOTSTRAP) --arch=$(SUBARCH) $(if $(ADDITIONAL_PACKAGES),--include $(ADDITIONAL_PACKAGES),) --unpack-tarball $(CURDIR)/$(BASE_PACKAGES_TARBALL_BASENAME).tgz --no-resolve-deps $(DEBIAN_VERSION) $(FS_MOUNT_DIR) $(DEBIAN_MIRROR)
: > .base_system_installed
.SILENT: .full_system_installed
.full_system_installed: .base_system_installed .uncompressed_packages
echo -e "\n\e[1m\e[32m=== Installing additional packages... ====\e[0m"
$(MOUNT_FS)
-$(SUDO_PFX)chroot $(FS_MOUNT_DIR) mount -t proc none /proc$(SUDO_SFX)
# First of all, configure the locale installation
$(SUDO_PFX)sed -ri 's/^# +en_US/en_US/' $(FS_MOUNT_DIR)/etc/locale.gen$(SUDO_SFX)
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) locale-gen$(SUDO_SFX)
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) update-locale LANG=en_US.UTF-8 LC_MEASUREMENT=en_US.UTF-8$(SUDO_SFX)
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) apt-get update$(SUDO_SFX)
cat $(FS_BUILD_DIR)/debconf-package-selections | $(SUDO_PFX)chroot $(FS_MOUNT_DIR) debconf-set-selections$(SUDO_SFX)
# Divert some files in order to prevent services from being started
for DIVERSION in $(shell find $(TWEAKS_DIR)/diversions -type f ! -wholename "*.svn*" -printf "/%P\n"); do \
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) dpkg-divert --divert `dirname $${DIVERSION}`/orig-`basename $${DIVERSION}` --rename $${DIVERSION}$(SUDO_SFX); \
[ -s $(TWEAKS_DIR)/diversions/$${DIVERSION} ] && $(SUDO_PFX)cp $(TWEAKS_DIR)/diversions/$${DIVERSION} $(FS_MOUNT_DIR)/$${DIVERSION}$(SUDO_SFX) || true; \
done
for PACKAGE in $$(<$(FS_BUILD_DIR)/packages-list); do \
echo Installing $${PACKAGE}...; \
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) apt-get install -o APT::Get::AllowUnauthenticated=1 -fqqy $${PACKAGE}$(SUDO_SFX) || exit 1; \
done
for DIVERSION in $(shell find $(TWEAKS_DIR)/diversions -type f ! -wholename "*.svn*" -printf "/%P\n"); do \
if [ -e $(FS_MOUNT_DIR)/`dirname $${DIVERSION}`/orig-`basename $${DIVERSION}`.dpkg-new ]; then \
$(SUDO_PFX)mv $(FS_MOUNT_DIR)/`dirname $${DIVERSION}`/orig-`basename $${DIVERSION}`.dpkg-new $(FS_MOUNT_DIR)/`dirname $${DIVERSION}`/orig-`basename $${DIVERSION}`$(SUDO_SFX); \
fi; \
if [ -s $(TWEAKS_DIR)/diversions/$${DIVERSION} ]; then \
$(SUDO_PFX)rm -f $(FS_MOUNT_DIR)/$${DIVERSION}$(SUDO_SFX); \
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) dpkg-divert --rename --remove $${DIVERSION}$(SUDO_SFX); \
fi; \
done
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) apt-get clean$(SUDO_SFX)
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) debconf-get-selections$(SUDO_SFX) > $(FS_BUILD_DIR)/debconf-package-selections$(SUDO_SFX)
: > .full_system_installed
.SILENT: .installed_src_tools
.installed_src_tools: .full_system_installed
echo -e "\n\e[1m\e[32m===== Installing additional tools... =====\e[0m"
mkdir -p $(TOOLS_SRC_BUILD_DIR)
+$(MAKE) -C $(TOOLS_SRC_BUILD_DIR) -f ../Makefile.devel install-all
: > .installed_src_tools
.SILENT: .applied_tweaks
.applied_tweaks: .installed_src_tools
echo -e "\n\e[1m\e[32m=== Applying Netkit-specific tweaks... ===\e[0m"
$(MOUNT_FS)
+$(MAKE) -C $(TWEAKS_DIR) -f Makefile.devel netkit-tweaks
: > .applied_tweaks
.SILENT: $(PACKAGES_TARBALL_BASENAME).tgz
$(PACKAGES_TARBALL_BASENAME).tgz: | .base_system_installed
echo -e "\n\e[1m\e[33m=== Downloading additional packages... ===\e[0m"
$(MOUNT_FS)
$(SUDO_PFX)chroot $(FS_MOUNT_DIR) apt-get update$(SUDO_SFX)
for PACKAGE in $$(<$(FS_BUILD_DIR)/packages-list); do echo Downloading $${PACKAGE} and its dependencies...; $(SUDO_PFX)chroot $(FS_MOUNT_DIR) apt-get -fqqy --download-only install $${PACKAGE}$(SUDO_SFX); done
echo "Compressing packages..."; tar -C $(FS_MOUNT_DIR)/var/cache/apt --exclude=lock -czf $(PACKAGES_TARBALL_TIMESTAMP_BASENAME).tgz .
ln -fs $(PACKAGES_TARBALL_TIMESTAMP_BASENAME).tgz $(PACKAGES_TARBALL_BASENAME).tgz
.SILENT: .uncompressed_packages
.uncompressed_packages: $(PACKAGES_TARBALL_BASENAME).tgz
echo "Uncompressing packages..."
$(MOUNT_FS)
$(SUDO_PFX)tar -C $(FS_MOUNT_DIR)/var/cache/apt -xzf $(PACKAGES_TARBALL_BASENAME).tgz$(SUDO_SFX)
: > .uncompressed_packages
.SILENT: .sparsify
.sparsify: .applied_tweaks
echo -e "\n\e[1m\e[32m==== Sparsifying filesystem image... =====\e[0m"
$(MOUNT_FS)
-$(SUDO_PFX)dd if=/dev/zero of=$(FS_MOUNT_DIR)/zeros-mass$(SUDO_SFX) >/dev/null 2>&1; true
$(SUDO_PFX)rm -f $(FS_MOUNT_DIR)/zeros-mass$(SUDO_SFX)
$(UMOUNT_FS)
mv netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE) netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE).old
cp --sparse=always netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE).old netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE)
rm -f netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE).old
: > .sparsify
.PHONY: package
package: ../netkit-filesystem-$(SUBARCH)-$(NK_FS_RELEASE).tar.bz2
../netkit-filesystem-$(SUBARCH)-$(NK_FS_RELEASE).tar.bz2: netkit-fs
tar --exclude=".*" --exclude="CVS" --exclude="$(FS_MOUNT_DIR_RELATIVE)" --exclude="*.tar.bz2" --exclude="*.tar.gz" --exclude="*.deb" --exclude="*.tgz" --exclude="$(TOOLS_SRC_BUILD_DIR_RELATIVE)" -C ../.. -cSjf ../netkit-filesystem-$(SUBARCH)-$(NK_FS_RELEASE).tar.bz2 netkit/fs
.PHONY: clean
clean:
$(CLEAN_MOUNTDIRS)
$(CLEAN_LOOPDEVS)
-rm -fr $(BUILD_DIR) $(FS_MOUNT_DIR) .partitions_created .fs_created .base_system_installed .full_system_installed .uncompressed_packages .installed_src_tools .applied_tweaks .sparsify netkit-fs-$(SUBARCH)-$(NK_FS_RELEASE) netkit-fs installed-packages-$(SUBARCH)-$(NK_FS_RELEASE) installed-packages
-$(MAKE) -C $(TOOLS_SRC_BUILD_DIR) -f ../Makefile.devel clean
.PHONY: clean-all
clean-all: clean
-rm -f debian-*packages*
-rm -fr $(TOOLS_SRC_BUILD_DIR)