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

Last change on this file since 868 was 868, checked in by amain, 3 years ago

rootfs: auto install all debwrt packages

File size: 10.2 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/packages.mk
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-$(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
33        @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
34        @if [ ! -e $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER) ]; then sudo cp $(QEMU_BIN_STATIC) $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER); fi
35        @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
36        @if ! (mount | grep -q $(ROOTFS_BUILD_DIR)/proc); then sudo chroot $(ROOTFS_BUILD_DIR) mount -t proc none /proc; fi
37        @if ! (mount | grep -q $(ROOTFS_BUILD_DIR)/sys ); then sudo chroot $(ROOTFS_BUILD_DIR) mount -t sysfs none /sys; fi
38endef
39
40define qemu-cleanup
41        @if (mount | grep -q $(ROOTFS_BUILD_DIR)/proc); then sudo umount $(ROOTFS_BUILD_DIR)/proc; fi
42        @if (mount | grep -q $(ROOTFS_BUILD_DIR)/sys ); then sudo umount $(ROOTFS_BUILD_DIR)/sys ; fi
43        @sudo rm -f $(ROOTFS_BUILD_DIR)$(QEMU_INTERPRETER)
44endef
45
46define rootfs-package-list
47$(shell echo "$(DEBWRT_ROOTFS_PKGLST_DEPS)" \
48         | tr ' ' '\n' \
49         >$(TMP_DIR)/include_packages_list.debwrt-package-deps; \
50        grep -hv -e '^#' -v -e '^$$'  \
51                 $(ROOTFS_DEBIAN_INCLUDE_PACKAGES) \
52                 $(ROOTFS_DEBIAN_INCLUDE_PACKAGES_DEBIAN_VERSION) \
53                 $(TMP_DIR)/include_packages_list.debwrt-package-deps \
54          | sort -u | tr '\n' ',' | sed 's/,$$//')
55endef
56
57chr:
58        $(call qemu-prepare)
59        @sudo bash -c "echo "debwrt-$(ROOTFS_DEBIAN_ARCH)" > $(ROOTFS_BUILD_DIR)/etc/debian_chroot"
60        @sudo cp $(ROOTFS_BUILD_DIR)/etc/resolv.conf $(ROOTFS_BUILD_DIR)/etc/resolv.conf_debwrt
61        @sudo bash -c "cat /etc/resolv.conf | grep nameserver >$(ROOTFS_BUILD_DIR)/etc/resolv.conf"
62        @sudo chroot $(ROOTFS_BUILD_DIR)
63        @sudo rm -f $(ROOTFS_BUILD_DIR)/etc/debian_chroot
64        @sudo cp $(ROOTFS_BUILD_DIR)/etc/resolv.conf_debwrt $(ROOTFS_BUILD_DIR)/etc/resolv.conf
65        $(call qemu-cleanup)
66
67debian/rootfs: | debian/rootfs/prepare-debwrt-packages \
68                 debian/rootfs/bootstrap               \
69                 debian/rootfs/copy-debwrt-packages    \
70                 debian/rootfs/unpack                  \
71                 debian/rootfs/files-install           \
72                 debian/rootfs/post-setup              \
73                 debian/rootfs/post-setup-emulated     \
74                 debian/rootfs/install-debwrt-packages \
75                 debian/rootfs/verify                  \
76                 debian/rootfs/save
77        @echo "I: Root filesystem $(ROOTFS_DEBIAN_RELEASE) $(ROOTFS_DEBIAN_ARCH) prepared in $(ROOTFS_BUILD_DIR) and archived in $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2)."
78        @echo "I:"
79        @echo "I: Install the image to USB-Disk/USB-Stick/SD-Card (make sure it is already mounted at /media/DEBWRT_ROOTFS)"
80        @echo "I: - make debian/rootfs/install"
81        @echo "I:"
82        @echo "I: Enter the change root(with qemu) and perform additional post install steps:"
83        @echo "I: - make chr"
84        @echo "I: - apt-get update # example statement ;-) - networking works!"
85        touch $@
86
87debian/rootfs/install: debian/rootfs
88        if [ -d /media/DEBWRT_ROOT ]; then \
89                sudo rm -rf /media/DEBWRT_ROOT/*; \
90                sudo bash -c "tar cf - -C $(ROOTFS_BUILD_DIR) . | tar xf - -C /media/DEBWRT_ROOT"; \
91        fi
92        #touch $@
93
94debian/rootfs/verify:
95        @if [ -f $(ROOTFS_BUILD_DIR)/debootstrap/debootstrap.log ]; then \
96                echo "E: Second stage install was not completed successfully" ;\
97                echo "E: Please check debootstrap.log to inspect the situation:" ;\
98                echo "E:    make chr" ;\
99                echo "E:    vi /debootstrap/debootstrap.log" ;\
100                exit 1 ;\
101        fi
102        touch $@
103
104debian/rootfs/save:
105        mkdir -p $(ROOTFS_INSTALL_DIR)
106        sudo bash -c "tar cjf $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2) -C $(ROOTFS_BUILD_DIR) ."
107        sudo chown $(USER) $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2)
108        sudo chmod 644 $(ROOTFS_INSTALL_DIR)/$(ROOTFS_TAR_BZ2)
109
110debian/rootfs/files-install: debian/rootfs/bootstrap
111        if [ ! -f $(ROOTFS_BUILD_DIR)/etc/init.d/rcS.debian -a -e $(ROOTFS_BUILD_DIR)/etc/init.d/rcS ]; then \
112                sudo mv -v $(ROOTFS_BUILD_DIR)/etc/init.d/rcS $(ROOTFS_BUILD_DIR)/etc/init.d/rcS.debian; \
113        fi
114        if [ ! -p $(ROOTFS_BUILD_DIR)/dev/initctl ]; then  \
115                sudo mkdir -p $(ROOTFS_BUILD_DIR)/dev;         \
116                sudo mkfifo   $(ROOTFS_BUILD_DIR)/dev/initctl; \
117        fi
118        @echo "Installing generic files (all):"
119        sudo bash -c "tar cf - --exclude=".svn" -C $(DEBWRT_EXTRA_ROOTFS_FILES_DIR)/all . | tar -xovf - -C $(ROOTFS_BUILD_DIR)"
120        @echo "Installing all targets files:"
121        sudo mkdir -p $(ROOTFS_BUILD_DIR)/target
122        sudo bash -c "tar cf - --exclude=".svn" --exclude ./all -C $(DEBWRT_EXTRA_ROOTFS_FILES_DIR) . | tar -xovf - -C $(ROOTFS_BUILD_DIR)/target"
123        sudo chmod 600 $(ROOTFS_BUILD_DIR)/etc/ssh/ssh_host_rsa_key
124        sudo chmod 600 $(ROOTFS_BUILD_DIR)/etc/ssh/ssh_host_dsa_key
125        sudo cat $(ROOTFS_BUILD_DIR)/etc/securetty \
126                $(TOPDIR)/debian/rootfs/securetty.tail \
127                >> $(TOPDIR)/securetty.temp
128        sudo mv $(TOPDIR)/securetty.temp $(ROOTFS_BUILD_DIR)/etc/securetty
129        touch $@
130
131# Fow now: Include debwrt-packages dependencies staticly. This
132#          list(DEBWRT_ROOTFS_PKGLST_DEPS) is managed in debian/package/debian.mk.
133debian/rootfs/bootstrap: | debian/rootfs/clean-rootfs-dir 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                              --foreign \
139                              --include=$(call rootfs-package-list),$(call debwrt-package-deps-list) \
140                              $(ROOTFS_DEBIAN_RELEASE) \
141                              $(ROOTFS_BUILD_DIR) \
142                              $(CONFIG_DEBIAN_BUILDENV_REPOSITORY)
143else
144        # Use debootstrap to ony extract selected packages
145        sudo debootstrap --arch=$(ROOTFS_DEBIAN_ARCH)\
146                         --foreign \
147                         --include=$(call rootfs-package-list) \
148                         $(ROOTFS_DEBIAN_RELEASE) \
149                         $(ROOTFS_BUILD_DIR) \
150                         $(CONFIG_DEBIAN_BUILDENV_REPOSITORY)
151endif
152        # complete ROOTFS/dev/ with additional device files
153        sudo bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v consoleonly"
154        sudo bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v sda"
155        sudo bash -c "cd $(ROOTFS_BUILD_DIR)/dev ; /sbin/MAKEDEV -v ttyS0"
156        touch $@
157
158ifeq ($(ROOTFS_DEBIAN_QEMU_2ND_STAGE),y)
159debian/rootfs/unpack: debian/rootfs/bootstrap
160        # no action
161else
162debian/rootfs/unpack: debian/rootfs/bootstrap
163        # extract packages, to allow second stage to take place on physical
164        # machine, while being able to boot the machine with most important
165        # binaries in place.
166        find $(ROOTFS_BUILD_DIR) -name "*.deb" | while read deb; do \
167                n=`basename $$deb`; \
168                echo -n "I: Extracting $${n}..."; \
169                sudo bash -c "dpkg --extract "$$deb" $(ROOTFS_BUILD_DIR)" ; \
170                echo "done"; \
171        done
172        touch $@
173endif
174
175debian/rootfs/clean-rootfs-dir:
176        mkdir -p $(BUILD_DIR_BASE)
177        sudo rm -rf $(ROOTFS_BUILD_DIR)
178        touch $@
179
180debian/rootfs/post-setup: debian/rootfs/bootstrap
181        sudo mkdir -p $(ROOTFS_BUILD_DIR)/etc/apt
182        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/ $(ROOTFS_DEBIAN_RELEASE) main\" > $(ROOTFS_BUILD_DIR)/etc/apt/sources.list"
183        sudo bash -c "echo \"deb http://packages.debwrt.net/ $(ROOTFS_DEBIAN_RELEASE)-debwrt-stable main\" >> $(ROOTFS_BUILD_DIR)/etc/apt/sources.list"
184        sudo bash -c "echo \"$(DEBWRTVERSION)\" >$(ROOTFS_BUILD_DIR)/etc/debwrt_version"
185        sudo bash -c "echo \"$(ROOTFS_DEBIAN_RELEASE)\" >$(ROOTFS_BUILD_DIR)/etc/debwrt_debian_release"
186        touch $@
187
188ifeq ($(ROOTFS_DEBIAN_QEMU_2ND_STAGE),y)
189debian/rootfs/post-setup-emulated: debian/rootfs/bootstrap
190        $(call qemu-prepare)
191        sudo chroot $(ROOTFS_BUILD_DIR) /usr/sbin/PostInstall && sudo rm -f $(ROOTFS_BUILD_DIR)/usr/sbin/PostInstall
192        wget -qO - "http://buildd.debwrt.net:8066/mini_buildd/api?command=getkey&output=plain" | sudo chroot $(ROOTFS_BUILD_DIR) apt-key add -
193        wget -qO - "http://debian.debwrt.net/apt/conf/debwrt-debian-mirror-public-gpg.key" | sudo chroot $(ROOTFS_BUILD_DIR) apt-key add -
194        $(call qemu-cleanup)
195else
196debian/rootfs/post-setup-emulated: debian/rootfs/bootstrap
197        @echo "I: Qemu 2nd stage install disabled. Second stage needs now to be performend on the target device itself."
198        @echo "I: Qemu 2nd stage install can be enabled using menuconfig (ROOTFS_DEBIAN_QEMU_2ND_STAGE)."
199endif
200        touch $@
201
202debian/rootfs/clean:
203        sudo rm -rf $(ROOTFS_BUILD_DIR)
204        rm -f debian/rootfs/bootstrap
205        rm -f debian/rootfs/install
206        rm -f debian/rootfs/files-install
207
Note: See TracBrowser for help on using the repository browser.