source: branches/contrib/debian/rootfs/debian.mk @ 667

Last change on this file since 667 was 667, checked in by ehem, 6 years ago

Replace direct invocations of sudo, su and chroot with usage of the
appropriate variables. While not much more than a search and replace
operation, cleaning this up is an important precursor for subsequent
steps.

File size: 9.9 KB
Line 
1# DebWrt - Debian on Embedded devices
2#
3# Copyright (C) 2010 Johan van Zoomeren <amain@debwrt.net>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18DEBIAN_ROOTFS_INCLUDE_PACKAGES:=$(call qstrip,$(CONFIG_DEBIAN_ROOTFS_INCLUDE_PACKAGES))
19DEBIAN_ROOTFS_QEMU_2ND_STAGE:=$(call qstrip, $(CONFIG_DEBIAN_ROOTFS_QEMU_2ND_STAGE))
20DEBWRT_EXTRA_ROOTFS_FILES_DIR:=$(TOPDIR)/debian/rootfs/files
21DEBWRT_MODULES_ARCHIVE=$(shell ls $(INSTALL_DIR)/debwrt-modules-*.tar.gz 2>/dev/null)
22MODULES_VERSION=$(shell echo `basename $(DEBWRT_MODULES_ARCHIVE) 2>/dev/null` | awk -F '-' '{print $$6}')
23DEBIAN_ROOTFS_INCLUDE_PACKAGES_DEBIAN_VERSION:=$(wildcard debian/rootfs/include_packages.list.$(DEBIAN_BUILD_VERSION))
24
25### temporary during work towards functioning fakeroot/fakechroot
26CHROOT:=$(SUROOT) chroot $(ROOTFS_BUILD_DIR)
27CHROOT_USER:=$(CHROOT) su - $(USER)
28### temporary during work towards functioning fakeroot/fakechroot
29
30define qemu-prepare
31        @if [ ! -x $(QEMU_BIN_STATIC) ]; then echo "E: can't find qemu ($(QEMU_BIN_STATIC))"; fi
32        @if [ ! -e /proc/sys/fs/binfmt_misc/qemu-$(TARGET_ARCH) ]; then echo "E: can't find binfmt_misc qemu-$(TARGET_ARCH) ( /proc/sys/fs/binfmt_misc/qemu-$(TARGET_ARCH)).\nE: install qemu-user-static, or\nE: disable second stage with qemu with menuconfig (DEBIAN_ROOTFS_QEMU_2ND_STAGE)."; fi
33        @if [ ! -x $(QEMU_BIN_STATIC) -o ! -e /proc/sys/fs/binfmt_misc/qemu-$(TARGET_ARCH) ]; then echo "E: abort - qemu not available in chroot"; exit 1; fi
34        @if [ ! -e $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER) ]; then $(SUROOT) cp $(QEMU_BIN_STATIC) $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER); fi
35        @if ! $(CHROOT) /bin/ls >/dev/null; then echo "E: qemu can't execute binaries for $(TARGET_ARCH)"; echo "E: abort"; exit 1; fi
36        @if ! (mount | grep -q $(ROOTFS_BUILD_DIR)/proc); then $(CHROOT) mount -t proc none /proc; fi
37        @if ! (mount | grep -q $(ROOTFS_BUILD_DIR)/sys ); then $(CHROOT) mount -t sysfs none /sys; fi
38        @mount
39endef
40
41define qemu-cleanup
42        @if (mount | grep -q $(ROOTFS_BUILD_DIR)/proc); then $(SUROOT) umount $(ROOTFS_BUILD_DIR)/proc; fi
43        @if (mount | grep -q $(ROOTFS_BUILD_DIR)/sys ); then $(SUROOT) umount $(ROOTFS_BUILD_DIR)/sys ; fi
44        @$(SUROOT) rm -f $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER)
45endef
46
47define rootfs-package-list
48$(shell echo "$(DEBWRT_ROOTFS_PKGLST_DEPS)" \
49         | tr ' ' '\n' \
50         >$(TMP_DIR)/include_packages_list.debwrt-package-deps; \
51        grep -hv '^#' \
52                 $(DEBIAN_ROOTFS_INCLUDE_PACKAGES) \
53                 $(DEBIAN_ROOTFS_INCLUDE_PACKAGES_DEBIAN_VERSION) \
54                 $(TMP_DIR)/include_packages_list.debwrt-package-deps \
55          | sort -u | tr '\n' ',' | sed 's/,$$//')
56endef
57
58chr:
59        $(call qemu-prepare)
60        @$(SUROOT) bash -c "echo "debwrt-$(TARGET_ARCH)" > $(ROOTFS_BUILD_DIR)/etc/debian_chroot"
61        @$(SUROOT) cp $(ROOTFS_BUILD_DIR)/etc/resolv.conf $(ROOTFS_BUILD_DIR)/etc/resolv.conf_debwrt
62        @$(SUROOT) bash -c "cat /etc/resolv.conf | grep nameserver >$(ROOTFS_BUILD_DIR)/etc/resolv.conf"
63        @$(CHROOT)
64        @$(SUROOT) rm -f $(ROOTFS_BUILD_DIR)/etc/debian_chroot
65        @$(SUROOT) cp $(ROOTFS_BUILD_DIR)/etc/resolv.conf_debwrt $(ROOTFS_BUILD_DIR)/etc/resolv.conf
66        $(call qemu-cleanup)
67
68debian/rootfs: debian/rootfs/bootstrap            \
69               debian/rootfs/unpack               \
70               debian/rootfs/files-install        \
71               debian/rootfs/debwrt-packages      \
72               debian/rootfs/modules-install      \
73               debian/rootfs/post-setup           \
74               debian/rootfs/second-stage-qemu    \
75               debian/rootfs/verify               \
76               debian/rootfs/save
77        @echo "I: root filesystem $(DEBIAN_BUILD_VERSION) $(TARGET_ARCH) prepared in $(ROOTFS_BUILD_DIR)"
78        @echo "I: if the DebWrt kernel wasn't build, then no kernel modules have been installed, install them by:"
79        @echo "I: - make openwrt/all"
80        @echo "I: - make debian/rootfs/modules-install"
81        @echo "I: - or: install manually from alternative source"
82        @echo "I:"
83        @echo "I: install the image to USB-Disk/USB-Stick/SD-Card (make sure it is already mounted at /media/DEBWRT_ROOTFS)"
84        @echo "I: - make debian/rootfs/install"
85        @echo "I:"
86        @echo "I: enter the change root(with qemu) and perform additional post install steps:"
87        @echo "I: - make chr"
88        @echo "I: - apt-get update # example statement ;-) - networking works!"
89        touch $@
90
91debian/rootfs/install: debian/rootfs
92        if [ -d /media/DEBWRT_ROOT ]; then \
93                $(SUROOT) rm -rf /media/DEBWRT_ROOT/*; \
94                $(SUROOT) bash -c "tar cf - -C $(ROOTFS_BUILD_DIR) . | tar xf - -C /media/DEBWRT_ROOT"; \
95        fi
96        #touch $@
97
98debian/rootfs/verify:
99        @if [ -f $(ROOTFS_BUILD_DIR)/debootstrap/debootstrap.log ]; then \
100                echo "E: Second stage install was not completed successfully" ;\
101                echo "E: Please check debootstrap.log to inspect the situation:" ;\
102                echo "E:    make chr" ;\
103                echo "E:    vi /debootstrap/debootstrap.log" ;\
104                exit 1 ;\
105        fi
106        touch $@
107
108debian/rootfs/save:
109ifeq ($(DEBIAN_ROOTFS_QEMU_2ND_STAGE),y)
110        $(SUROOT) bash -c "tar cjf $(INSTALL_DIR)/$(ROOTFS_TAR_BZ2) -C $(ROOTFS_BUILD_DIR) ."
111else
112        if [ -d /media/DEBWRT_ROOT ]; then \
113                $(SUROOT) bash -c "tar cjf $(ROOTFS_TAR_BZ2) -C /media/DEBWRT_ROOT ."; \
114        fi
115endif
116
117debian/rootfs/files-install: debian/rootfs/bootstrap
118        if [ ! -f $(ROOTFS_BUILD_DIR)/etc/init.d/rcS.debian -a -e $(ROOTFS_BUILD_DIR)/etc/init.d/rcS ]; then \
119                $(SUROOT) mv -v $(ROOTFS_BUILD_DIR)/etc/init.d/rcS $(ROOTFS_BUILD_DIR)/etc/init.d/rcS.debian; \
120        fi
121        if [ ! -p $(ROOTFS_BUILD_DIR)/dev/initctl ]; then  \
122                $(SUROOT) mkdir -p $(ROOTFS_BUILD_DIR)/dev;         \
123                $(SUROOT) mkfifo   $(ROOTFS_BUILD_DIR)/dev/initctl; \
124        fi
125        chmod 600 $(DEBWRT_EXTRA_ROOTFS_FILES_DIR)/etc/ssh/ssh_host_rsa_key
126        chmod 600 $(DEBWRT_EXTRA_ROOTFS_FILES_DIR)/etc/ssh/ssh_host_dsa_key
127        $(SUROOT) bash -c "tar cf - --exclude=".svn" -C $(DEBWRT_EXTRA_ROOTFS_FILES_DIR) . | tar -xovf - -C $(ROOTFS_BUILD_DIR)"
128        $(SUROOT) cat $(ROOTFS_BUILD_DIR)/etc/securetty \
129                $(TOPDIR)/debian/rootfs/securetty.tail \
130                >> $(TOPDIR)/securetty.temp
131        $(SUROOT) mv    $(TOPDIR)/securetty.temp $(ROOTFS_BUILD_DIR)/etc/securetty
132        touch $@
133
134debian/rootfs/modules-install: debian/rootfs/bootstrap
135ifneq ($(DEBWRT_MODULES_ARCHIVE),)
136        $(SUROOT) tar xof $(DEBWRT_MODULES_ARCHIVE) -C $(ROOTFS_BUILD_DIR)
137        #$(SUROOT) depmod -a -b $(ROOTFS_BUILD_DIR) $(MODULES_VERSION)
138else
139        @echo "W: Can't install kernel module to the change root. Kernel modules archive missing. ($(DEBWRT_MODULES_ARCHIVE))"
140endif
141        touch $@
142
143# Fow now: Include debwrt-packages dependencies staticly. This
144#          list(DEBWRT_ROOTFS_PKGLST_DEPS) is managed in debian/package-host/debian.mk.
145debian/rootfs/bootstrap: debian/rootfs/clean-rootfs-dir
146        $(SUROOT) debootstrap --arch=$(TARGET_ARCH)\
147                     --foreign \
148                     --include=$(call rootfs-package-list) \
149                     $(DEBIAN_BUILD_VERSION) \
150                     $(ROOTFS_BUILD_DIR) \
151                     $(CONFIG_DEBIAN_BUILDENV_REPOSITORY)
152        # complete ROOTFS/dev/ with additional device files
153        $(SUROOT) bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v consoleonly"
154        $(SUROOT) bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v sda"
155        touch $@
156
157debian/rootfs/unpack: debian/rootfs/bootstrap
158        find $(ROOTFS_BUILD_DIR) -name "*.deb" | while read deb; do \
159                n=`basename $$deb`; \
160                echo -n "I: Extracting $${n}..."; \
161                $(SUROOT) bash -c "dpkg --extract "$$deb" $(ROOTFS_BUILD_DIR)" ; \
162                echo "done"; \
163        done
164        touch $@
165
166# install all available cross-compiled debwrt debian packages, except for the kernel-headers package and the cross packages
167debian/rootfs/debwrt-packages: debian/rootfs/bootstrap debian/package-host/rootfs
168        ls ${INSTALL_DIR_DEBIAN_PACKAGES}/*.deb | grep -v -e"debwrt-kernel-headers" -e"cross" | while read package; do \
169                pfname=`basename $$package`; \
170                pname=`echo $$pfname | sed 's/_.*//'`; \
171                echo "Installing DebWrt package: $$pname"; \
172                $(SUROOT) cp $$package $(ROOTFS_BUILD_DIR)/var/cache/apt/archives; \
173                $(SUROOT) dpkg-deb -x $$package $(ROOTFS_BUILD_DIR); \
174                $(SUROOT) bash -c "echo \"$$pname /var/cache/apt/archives/$$pfname\" >>$(ROOTFS_BUILD_DIR)/debootstrap/debpaths" ;\
175                $(SUROOT) sed -i "s/$$/$$pname /" $(ROOTFS_BUILD_DIR)/debootstrap/base ;\
176        done
177        touch $@
178       
179debian/rootfs/clean-rootfs-dir:
180        mkdir -p $(BUILD_DIR_BASE)
181        $(SUROOT) rm -rf $(ROOTFS_BUILD_DIR)
182        touch $@
183
184debian/rootfs/post-setup: debian/rootfs/bootstrap
185        $(SUROOT) mkdir -p $(ROOTFS_BUILD_DIR)/etc/apt
186        # note: unfortunately /etc/apt/sources.list is cleaned after running second stage install
187        $(SUROOT) bash -c "echo \"deb http://ftp.debian.org/debian $(DEBIAN_BUILD_VERSION) main\" > $(ROOTFS_BUILD_DIR)/etc/apt/sources.list"
188        $(SUROOT) bash -c "echo \"$(DEBWRTVERSION)\" >$(ROOTFS_BUILD_DIR)/etc/debwrt_version"
189        $(SUROOT) bash -c "echo \"$(DEBIAN_BUILD_VERSION)\" >$(ROOTFS_BUILD_DIR)/etc/debian_release"
190        touch $@
191
192ifeq ($(DEBIAN_ROOTFS_QEMU_2ND_STAGE),y)
193debian/rootfs/second-stage-qemu: debian/rootfs/bootstrap debian/buildenv/qemu-build
194        $(call qemu-prepare)
195        $(CHROOT) /usr/sbin/PostInstall && $(SUROOT) rm -f $(ROOTFS_BUILD_DIR)/usr/sbin/PostInstall
196        $(call qemu-cleanup)
197else
198debian/rootfs/second-stage-qemu: debian/rootfs/bootstrap
199        @echo "I: Qemu 2nd stage install disabled. Second stage needs now to be performend on the target device itself."
200        @echo "I: Qemu 2nd stage install can be enabled using menuconfig (DEBIAN_ROOTFS_QEMU_2ND_STAGE)."
201endif
202        touch $@
203
204debian/rootfs/clean:
205        $(SUROOT) rm -rf $(ROOTFS_BUILD_DIR)
206        rm -f debian/rootfs/debwrt-packages
207        rm -f debian/rootfs/bootstrap
208        rm -f debian/rootfs/install
209        rm -f debian/rootfs/files-install
210        rm -f debian/rootfs/modules-install
211
Note: See TracBrowser for help on using the repository browser.