source: trunk/debian/rootfs/debian.mk @ 899

Last change on this file since 899 was 899, checked in by amain, 2 years ago

rootfs: run qemu-clean before clean rootfs to make sure hanging mounts are umounted

File size: 10.5 KB
Line 
1# DebWrt - Debian on Embedded devices
2#
3# Copyright (C) 2010,2016 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
18QEMU_BIN=/usr/bin/qemu-$(ROOTFS_DEBIAN_ARCH)
19QEMU_BIN_STATIC=$(QEMU_BIN)-static
20QEMU_INTERPRETER=$(shell cat /proc/sys/fs/binfmt_misc/qemu-$(ROOTFS_DEBIAN_ARCH) 2>/dev/null | grep interpreter | awk '{print $$2}')
21
22ROOTFS_DEBIAN_INCLUDE_PACKAGES:=$(call qstrip,$(CONFIG_ROOTFS_DEBIAN_INCLUDE_PACKAGES))
23ROOTFS_DEBIAN_QEMU_2ND_STAGE:=$(call qstrip, $(CONFIG_ROOTFS_DEBIAN_QEMU_2ND_STAGE))
24DEBWRT_EXTRA_ROOTFS_FILES_DIR:=$(TOPDIR)/debian/rootfs/files
25MODULES_VERSION=$(shell echo `basename $(DEBWRT_MODULES_ARCHIVE) 2>/dev/null` | awk -F '-' '{print $$6}')
26ROOTFS_DEBIAN_INCLUDE_PACKAGES_DEBIAN_VERSION:=$(wildcard debian/rootfs/include_packages.list.$(ROOTFS_DEBIAN_RELEASE))
27
28include  $(TOPDIR)/debian/rootfs/debootstrap.mk
29include  $(TOPDIR)/debian/rootfs/packages.mk
30
31define qemu-prepare
32        @if [ ! -x $(QEMU_BIN_STATIC) ]; then echo "E: can't find qemu ($(QEMU_BIN_STATIC))"; fi
33        @if [ ! -e /proc/sys/fs/binfmt_misc/qemu-$(ROOTFS_DEBIAN_ARCH) ]; then echo "E: can't find binfmt_misc qemu-$(ROOTFS_DEBIAN_ARCH) ( /proc/sys/fs/binfmt_misc/qemu-$(ROOTFS_DEBIAN_ARCH)).\nE: install qemu-user-static, or\nE: disable second stage with qemu with menuconfig (ROOTFS_DEBIAN_QEMU_2ND_STAGE)."; fi
34        @if [ ! -x $(QEMU_BIN_STATIC) -o ! -e /proc/sys/fs/binfmt_misc/qemu-$(ROOTFS_DEBIAN_ARCH) ]; then echo "E: abort - qemu not available in chroot"; exit 1; fi
35        @if [ ! -e $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER) ]; then sudo cp $(QEMU_BIN_STATIC) $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER); fi
36        @if ! sudo chroot $(ROOTFS_BUILD_DIR) /bin/ls >/dev/null; then echo "E: qemu can't execute binaries for $(ROOTFS_DEBIAN_ARCH)"; echo "E: abort"; exit 1; fi
37        @if ! (mount | grep -q $(ROOTFS_BUILD_DIR)/proc); then sudo chroot $(ROOTFS_BUILD_DIR) mount -t proc none /proc; fi
38        @if ! (mount | grep -q $(ROOTFS_BUILD_DIR)/sys ); then sudo chroot $(ROOTFS_BUILD_DIR) mount -t sysfs none /sys; fi
39endef
40
41define qemu-cleanup
42        @if (mount | grep -q $(ROOTFS_BUILD_DIR)/proc); then sudo umount $(ROOTFS_BUILD_DIR)/proc; fi
43        @if (mount | grep -q $(ROOTFS_BUILD_DIR)/sys ); then sudo umount $(ROOTFS_BUILD_DIR)/sys ; fi
44        @sudo 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 -e '^#' -v -e '^$$'  \
52                 $(ROOTFS_DEBIAN_INCLUDE_PACKAGES) \
53                 $(ROOTFS_DEBIAN_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        @sudo bash -c "echo "debwrt-$(ROOTFS_DEBIAN_ARCH)" > $(ROOTFS_BUILD_DIR)/etc/debian_chroot"
61        @sudo cp $(ROOTFS_BUILD_DIR)/etc/resolv.conf $(ROOTFS_BUILD_DIR)/etc/resolv.conf_debwrt
62        @sudo bash -c "cat /etc/resolv.conf | grep nameserver >$(ROOTFS_BUILD_DIR)/etc/resolv.conf"
63        @sudo chroot $(ROOTFS_BUILD_DIR)
64        @sudo rm -f $(ROOTFS_BUILD_DIR)/etc/debian_chroot
65        @sudo cp $(ROOTFS_BUILD_DIR)/etc/resolv.conf_debwrt $(ROOTFS_BUILD_DIR)/etc/resolv.conf
66        $(call qemu-cleanup)
67
68debian/rootfs: | debian/rootfs/prepare-debwrt-packages \
69                 debian/rootfs/bootstrap               \
70                 debian/rootfs/copy-debwrt-packages    \
71                 debian/rootfs/unpack                  \
72                 debian/rootfs/files-install           \
73                 debian/rootfs/post-setup              \
74                 debian/rootfs/post-setup-emulated     \
75                 debian/rootfs/install-debwrt-packages \
76                 debian/rootfs/verify                  \
77                 debian/rootfs/save
78        @echo "I: Root filesystem $(ROOTFS_DEBIAN_RELEASE) $(ROOTFS_DEBIAN_ARCH) prepared in $(ROOTFS_BUILD_DIR) and archived in $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2)."
79        @echo "I:"
80        @echo "I: Install the image to USB-Disk/USB-Stick/SD-Card (make sure it is already mounted at /media/DEBWRT_ROOTFS)"
81        @echo "I: - make debian/rootfs/install"
82        @echo "I:"
83        @echo "I: Enter the change root(with qemu) and perform additional post install steps:"
84        @echo "I: - make chr"
85        @echo "I: - apt-get update # example statement ;-) - networking works!"
86        touch $@
87
88debian/rootfs/install: debian/rootfs
89        if [ -d /media/DEBWRT_ROOT ]; then \
90                sudo rm -rf /media/DEBWRT_ROOT/*; \
91                sudo bash -c "tar cf - -C $(ROOTFS_BUILD_DIR) . | tar xf - -C /media/DEBWRT_ROOT"; \
92        fi
93        #touch $@
94
95debian/rootfs/verify:
96        @if [ -f $(ROOTFS_BUILD_DIR)/debootstrap/debootstrap.log ]; then \
97                echo "E: Second stage install was not completed successfully" ;\
98                echo "E: Please check debootstrap.log to inspect the situation:" ;\
99                echo "E:    make chr" ;\
100                echo "E:    vi /debootstrap/debootstrap.log" ;\
101                exit 1 ;\
102        fi
103        touch $@
104
105debian/rootfs/save:
106        mkdir -p $(ROOTFS_INSTALL_DIR)
107        sudo bash -c "tar cjf $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2) -C $(ROOTFS_BUILD_DIR) ."
108        sudo chown $(USER) $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2)
109        sudo chmod 644 $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2)
110
111debian/rootfs/files-install: debian/rootfs/bootstrap
112        if [ ! -p $(ROOTFS_BUILD_DIR)/dev/initctl ]; then  \
113                sudo mkdir -p $(ROOTFS_BUILD_DIR)/dev;         \
114                sudo mkfifo   $(ROOTFS_BUILD_DIR)/dev/initctl; \
115        fi
116        @echo "Installing generic files (all):"
117        sudo bash -c "tar cpf - --exclude=".svn" -C $(DEBWRT_EXTRA_ROOTFS_FILES_DIR)/all . | tar -xpvf - -C $(ROOTFS_BUILD_DIR)"
118        @echo "Installing all targets files:"
119        sudo mkdir -p $(ROOTFS_BUILD_DIR)/target
120        sudo bash -c "tar cpf - --exclude=".svn" --exclude ./all -C $(DEBWRT_EXTRA_ROOTFS_FILES_DIR) . | tar -xpvf - -C $(ROOTFS_BUILD_DIR)/target"
121        sudo chmod 600 $(ROOTFS_BUILD_DIR)/etc/ssh/ssh_host_rsa_key
122        sudo chmod 600 $(ROOTFS_BUILD_DIR)/etc/ssh/ssh_host_dsa_key
123        sudo cat $(ROOTFS_BUILD_DIR)/etc/securetty \
124                $(TOPDIR)/debian/rootfs/securetty.tail \
125                >> $(TOPDIR)/securetty.temp
126        sudo mv $(TOPDIR)/securetty.temp $(ROOTFS_BUILD_DIR)/etc/securetty
127        touch $@
128
129# Fow now: Include debwrt-packages dependencies staticly. This
130#          list(DEBWRT_ROOTFS_PKGLST_DEPS) is managed in debian/package/debian.mk.
131debian/rootfs/bootstrap: | debian/rootfs/debootstrap-install \
132                           debian/rootfs/clean-rootfs-dir \
133                           debian/rootfs/prepare-debwrt-packages
134        mkdir -p $(ROOTFS_BUILD_DIR)
135ifeq ($(ROOTFS_DEBIAN_QEMU_2ND_STAGE),y)
136        # Debootstrap and automated qemu based 2nd stage install
137        sudo qemu-debootstrap --arch=$(ROOTFS_DEBIAN_ARCH)\
138                              --include=$(call rootfs-package-list),$(call debwrt-package-deps-list) \
139                              $(ROOTFS_DEBIAN_RELEASE) \
140                              $(ROOTFS_BUILD_DIR) \
141                              $(CONFIG_DEBIAN_BUILDENV_REPOSITORY)
142else
143        # Use debootstrap to ony extract selected packages
144        sudo debootstrap --arch=$(ROOTFS_DEBIAN_ARCH)\
145                         --foreign \
146                         --include=$(call rootfs-package-list) \
147                         $(ROOTFS_DEBIAN_RELEASE) \
148                         $(ROOTFS_BUILD_DIR) \
149                         $(CONFIG_DEBIAN_BUILDENV_REPOSITORY)
150endif
151        # complete ROOTFS/dev/ with additional device files
152        sudo bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v consoleonly"
153        sudo bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v sda"
154        sudo bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v ttyS0"
155        touch $@
156
157ifeq ($(ROOTFS_DEBIAN_QEMU_2ND_STAGE),y)
158debian/rootfs/unpack: debian/rootfs/bootstrap
159        # no action
160else
161debian/rootfs/unpack: debian/rootfs/bootstrap
162        # extract packages, to allow second stage to take place on physical
163        # machine, while being able to boot the machine with most important
164        # binaries in place.
165        find $(ROOTFS_BUILD_DIR) -name "*.deb" | while read deb; do \
166                n=`basename $$deb`; \
167                echo -n "I: Extracting $${n}..."; \
168                sudo bash -c "dpkg --extract "$$deb" $(ROOTFS_BUILD_DIR)" ; \
169                echo "done"; \
170        done
171        touch $@
172endif
173
174debian/rootfs/clean-rootfs-dir:
175        mkdir -p $(BUILD_DIR_BASE)
176        sudo rm -rf $(ROOTFS_BUILD_DIR)
177        touch $@
178
179debian/rootfs/post-setup: debian/rootfs/bootstrap
180        sudo mkdir -p $(ROOTFS_BUILD_DIR)/etc/apt
181        sudo bash -c "echo -e \"# For low memory (<64M) system, which have a hard time running apt-get update,\n# DebWrt provides a Debian mirror containing a, still very large, subset (~13.000) of all\n# Debian packages.\ndeb http://debian.debwrt.net/debian $(ROOTFS_DEBIAN_RELEASE) main\" > $(ROOTFS_BUILD_DIR)/etc/apt/sources.list"
182        sudo bash -c "echo \"deb http://packages.debwrt.net/debwrt $(ROOTFS_DEBIAN_RELEASE)-debwrt-stable main\" >> $(ROOTFS_BUILD_DIR)/etc/apt/sources.list"
183        sudo bash -c "echo \"$(DEBWRTVERSION)\" >$(ROOTFS_BUILD_DIR)/etc/debwrt_version"
184        sudo bash -c "echo \"$(ROOTFS_DEBIAN_RELEASE)\" >$(ROOTFS_BUILD_DIR)/etc/debwrt_debian_release"
185        touch $@
186
187ifeq ($(ROOTFS_DEBIAN_QEMU_2ND_STAGE),y)
188debian/rootfs/post-setup-emulated: debian/rootfs/bootstrap
189        $(call qemu-prepare)
190        sudo chroot $(ROOTFS_BUILD_DIR) /usr/sbin/PostInstall && sudo rm -f $(ROOTFS_BUILD_DIR)/usr/sbin/PostInstall
191        wget -qO - "http://buildd.debwrt.net:8066/mini_buildd/api?command=getkey&output=plain" | sudo chroot $(ROOTFS_BUILD_DIR) apt-key add -
192        wget -qO - "http://debian.debwrt.net/apt/conf/debwrt-debian-mirror-public-gpg.key" | sudo chroot $(ROOTFS_BUILD_DIR) apt-key add -
193        $(call qemu-cleanup)
194else
195debian/rootfs/post-setup-emulated: debian/rootfs/bootstrap
196        @echo "I: Qemu 2nd stage install disabled. Second stage needs now to be performend on the target device itself."
197        @echo "I: Qemu 2nd stage install can be enabled using menuconfig (ROOTFS_DEBIAN_QEMU_2ND_STAGE)."
198        @# Firstboot only works on sysvinit systems, not on newer systemd
199        sudo mv $(ROOTFS_BUILD_DIR)/etc/init.d/rcS $(ROOTFS_BUILD_DIR)/etc/init.d/rcS.Debian
200        sudo ln -s /etc/init.d/rcS.firstboot $(ROOTFS_BUILD_DIR)/etc/init.d/rcS
201
202endif
203        touch $@
204
205debian/rootfs/qemu-clean:
206        $(call qemu-cleanup)
207
208debian/rootfs/clean: | debian/rootfs/qemu-clean debian/rootfs/debwrt-packages-clean
209        sudo rm -rf $(ROOTFS_BUILD_DIR)
210        rm -f debian/rootfs/bootstrap
211        rm -f debian/rootfs/install
212        rm -f debian/rootfs/files-install
213
Note: See TracBrowser for help on using the repository browser.