### -*-makefile-*- ### ### Main maintenance script for chroots ### ### (c) 2018 Mark Wooding ### ###----- Licensing notice --------------------------------------------------- ### ### This file is part of the distorted.org.uk chroot maintenance tools. ### ### distorted-chroot 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 2 of the ### License, or (at your option) any later version. ### ### distorted-chroot 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 distorted-chroot. If not, write to the Free Software ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ### USA. all:: clean:: .PHONY: all clean .SECONDEXPANSION: #sorry ###-------------------------------------------------------------------------- ### Configuration. CONFIG_VARS = ## Volume group from which to allocate chroot volumes and snapshots. CONFIG_VARS += VG LVPREFIX VG = vg-$(shell hostname) LVPREFIX = ## Logical volume size, as an LVM option. CONFIG_VARS += LVSZ LVSZ = -L8G ## Debian mirror. CONFIG_VARS += DEBMIRROR DEBMIRROR = http://deb.debian.org/debian/ ## APT sources configurations. (Also, $($d_APTSRC) for each $d in $(DISTS).) CONFIG_VARS += APTSRC APTSRC = etc/aptsrc.conf $(wildcard etc/aptsrc.local.conf) ## APT configuration fragment names. These will be linked into ## `/etc/apt/apt.conf.d' in each chroot. To put a fragment f in a surprising ## place, set $($f_APTCONFSRC). CONFIG_VARS += APTCONF APTCONF_DIR = etc/apt-conf.d APTCONF = $(notdir $(wildcard $(APTCONF_DIR)/[0-9]*[!~])) ## Proxy setting. CONFIG_VARS += PROXY PROXY := $(shell \ eval $$(apt-config $(foreach a,$(APTCONF), -c$(APTCONF_DIR)/$a) \ shell proxy Acquire::http::proxy); \ case $${proxy+t} in (t) echo "$$proxy" ;; (*) echo nil ;; esac) ## Debian distributions to support. CONFIG_VARS += PRIMARY_DIST DISTS PRIMARY_DIST = stretch DISTS = $(PRIMARY_DIST) buster bullseye sid ## Host's native architecture(s). CONFIG_VARS += MYARCH NATIVE_ARCHS MYARCH = $(shell dpkg --print-architecture) OTHERARCHS = $(shell dpkg --print-foreign-architectures) NATIVE_ARCHS = $(MYARCH) $(OTHERARCHS) ## Foreign (emulated) architectures to support. CONFIG_VARS += FOREIGN_ARCHS FOREIGN_ARCHS = ## Master lists of chroots to build and maintain. NATIVE_CHROOTS = $(foreach a,$(NATIVE_ARCHS), \ $(foreach d,$(or $($a_DISTS) $(DISTS)), \ $d-$a)) FOREIGN_CHROOTS = $(foreach a,$(FOREIGN_ARCHS), \ $(foreach d,$(or $($a_DISTS) $(DISTS)), \ $d-$a)) ## Which host architecture to use for foreign architectures. It turns out ## that it's best to use a Qemu with the same host bitness as the target ## architecture; otherwise it has to do a difficult job of converting ## arguments and results between kernel ABI flavours. 32BIT_QEMUHOST = $(or $(filter i386,$(NATIVE_ARCHS)),$(TOOLSARCH)) 64BIT_QEMUHOST = $(or $(filter amd64,$(NATIVE_ARCHS)),$(TOOLSARCH)) CONFIG_VARS += $(foreach a,$(FOREIGN_ARCHS), $a_QEMUHOST) armel_QEMUHOST = $(32BIT_QEMUHOST) armhf_QEMUHOST = $(32BIT_QEMUHOST) arm64_QEMUHOST = $(64BIT_QEMUHOST) i386_QEMUHOST = $(32BIT_QEMUHOST) amd64_QEMUHOST = $(64BIT_QEMUHOST) ## Qemu architecture names. These tell us which Qemu binary to use for a ## particular Debian architecture. CONFIG_VARS += $(foreach a,$(FOREIGN_ARCHS), $a_QEMUARCH) armel_QEMUARCH = arm armhf_QEMUARCH = arm arm64_QEMUARCH = aarch64 i386_QEMUARCH = i386 amd64_QEMUARCH = x86_64 ## Alias mapping for chroots. CONFIG_VARS += $(foreach d,$(DISTS), $d_ALIASES) stretch_ALIASES = oldstable buster_ALIASES = stable bullseye_ALIASE = testing sid_ALIASES = unstable ## Which host architecture to use for commonly used tools in foreign chroots. CONFIG_VARS += TOOLSARCH TOOLSARCH = $(MYARCH) ## A directory in which we can maintain private files and state. CONFIG_VARS += STATE STATE = state ## A directory which will be spliced into chroots as `/usr/local.schroot/'. ## This will be our primary point of contact with the chroot. CONFIG_VARS += LOCAL LOCAL = local.schroot ## How to run a command as a privileged user. ROOTLY = sudo ## Files to be copied into a chroot from the host. SCHROOT_COPYFILES = /etc/resolv.conf SCHROOT_NSSDATABASES = passwd shadow group gshadow ## Local configuration hook. -include local.mk ## All chroot names. CONFIG_VARS += ALL_CHROOTS ALL_CHROOTS = $(NATIVE_CHROOTS) $(FOREIGN_CHROOTS) ###-------------------------------------------------------------------------- ### Utilities. ## $(call chroot-dist,D-A) -> D ## $(call chroot-arch,D-A) -> A ## ## Parse chroot descriptions. chroot-dist = $(patsubst %/,%,$(dir $(subst -,/,$1))) chroot-arch = $(notdir $(subst -,/,$1)) ## $(call native-chroot-p,D-A) -> D | ## ## Answer whether D-A is a native chroot. native-chroot-p = $(filter $(call chroot-arch,$1), $(NATIVE_ARCHS)) ## $(call chroot-qemuhost,D-A) -> AA ## ## Answer the apporopriate Qemu host architecture for foreign chroot D-A. chroot-qemuhost = $($(call chroot-arch,$1)_QEMUHOST) ## $(call chroot-deps,PRE,D-A) -> PRE/DD-AA ... | ## ## Answer a list of additional dependencies for the chroot D-A: specifically, ## if D-A is foreign then include PRE/DD-AA entries for the tools ## architecture, and Qemu host architecture (if that's different). chroot-deps = $(if $(call native-chroot-p,$2),, \ $(addprefix $1/$(call chroot-dist,$2)-,\ $(sort $(call chroot-toolsarch,$2) \ $(call chroot-qemuhost,$2)))) ## Substituting placeholders in files. SUBSTITUTIONS := local=$(shell realpath $(LOCAL)) SUBSTITUTIONS += primary-dist=$(PRIMARY_DIST) subst-file = { \ echo "$1 GENERATED by distorted-chroot: do not edit"; \ substs=""; \ for v in $(SUBSTITUTIONS); do \ var=$${v%%=*} value=$${v\#*=}; \ substs="$$substs s@$$var@$$valueg;"; \ done; \ sed "$$substs"; \ } ## Silent-rules machinery. V = 0 V_AT = $(V_AT_$V) V_AT_0 = @ v_print = $(call v_print_$V,$1,$2) v_print_0 = printf " %-8s %s\n" "$1" "$2"; v_tag = $(V_AT)$(call v_print,$1,$@) v_log = $(call v_log_$V,$1) v_log_ = $(call v_log_1,$1) v_log_0 = >$1.log 2>&1 v_log_1 = 2>&1 | tee $1.log CLEANFILES += *.log ###-------------------------------------------------------------------------- ### APT configuration. ## In a chroot, `/etc/apt/sources.list' links to ## `/usr/local.schroot/etc/apt/sources.$d' for the appropriate distribution. APT_SOURCES = $(foreach d,$(DISTS), $(LOCAL)/etc/apt/sources.$d) CLEANFILES += $(APT_SOURCES) all:: $(APT_SOURCES) $(foreach d,$(DISTS), $(STATE)/etc/apt/aptsrc.$d): $(STATE)/etc/apt/aptsrc.%: $(V_AT)mkdir -p $(dir $@) $(call v_tag,GEN){ \ echo "### -*-conf-*- GENERATED by distorted-chroot: do not edit"; \ echo "subscribe"; \ echo " debian : $*"; \ } >$@.new && mv $@.new $@ $(APT_SOURCES): $(LOCAL)/etc/apt/sources.%: \ $$(APTSRC) $$($$*_APTSRC) $$(STATE)/etc/apt/aptsrc.$$* $(V_AT)mkdir -p $(dir $@) $(call v_tag,GEN)bin/mkaptsrc \ $(APTSRC) $($*_APTSRC) $(STATE)/etc/apt/aptsrc.$* \ >$@.new && mv $@.new $@ ## In a chroot, a link `/etc/apt/apt.conf.d/FOO' is created for each file in ## `/usr/local.schroot/etc/apt/apt.conf.d/FOO'. APT_CONFIGS = $(addprefix $(LOCAL)/etc/apt/apt.conf.d/,$(APTCONF)) all:: $(APT_CONFIGS) $(APT_CONFIGS): $(LOCAL)/etc/apt/apt.conf.d/%: \ $$(or $$($$*_APTCONFSRC) $$(APTCONF_DIR)/$$*) $(V_AT)mkdir -p $(dir $@) $(call v_tag,COPY)cp $< $@.new && mv $@.new $@ ###-------------------------------------------------------------------------- ### `schroot' and `sbuild' configuration. all:: schroot-config schroot-config:: .PHONY: schroot-config %print-varlist = { \ echo "\#\#\# -*-sh-*- GENERATED by distorted-chroot: do not edit"; \ $(foreach v,$1, echo $v=\''$(subst ','\'\\\'\'',$($v))'\';) \ } #' schroot-config_HASH := \ $(shell $(call %print-varlist,$(CONFIG_VARS)) | \ sha256sum | cut -c1-32) schroot-config_FILE = $(STATE)/config.sh-$(schroot-config_HASH) $(schroot-config_FILE): $(V_AT)mkdir -p $(STATE) $(V_AT)rm -f $(STATE)/config.sh-* $(call v_tag,STAMP)$(call %print-varlist,$(CONFIG_VARS)) \ >$@.new && mv $@.new $@ schroot-config:: $(STATE)/config.sh $(STATE)/config.sh: $(schroot-config_FILE) $(call v_tag,SYMLINK)ln -sf $(notdir $<) $@ schroot-config:: $(STATE)/etc/schroot/sbuild.schroot $(STATE)/etc/schroot/sbuild.schroot: $(STATE)/config.sh bin/mkchrootconf $(V_AT)mkdir -p $(dir $@) $(call v_tag,GEN)bin/mkchrootconf >$@.new && mv $@.new $@ schroot-config:: $(STATE)/etc/schroot/sbuild.profile/copyfiles $(STATE)/etc/schroot/sbuild.profile/copyfiles: $(schroot-config_STAMP) $(V_AT)mkdir -p $(dir $@) $(call v_tag,GEN){ \ echo "### -*-conf-*- GENERATED by distorted-chroot: do not edit"; \ for i in $(SCHROOT_COPYFILES); do echo "$$i"; done; \ } >$@.new && mv $@.new $@ schroot-config:: $(STATE)/etc/schroot/sbuild.profile/nssdatabases $(STATE)/etc/schroot/sbuild.profile/nssdatabases: $(schroot-config_STAMP) $(V_AT)mkdir -p $(dir $@) $(call v_tag,GEN){ \ echo "### -*-conf-*- GENERATED by distorted-chroot: do not edit"; \ for i in $(SCHROOT_NSSDATABASES); do echo "$$i"; done; \ } >$@.new && mv $@.new $@ schroot-config:: $(STATE)/etc/schroot/sbuild.profile/fstab $(STATE)/etc/schroot/sbuild.profile/fstab: \ etc/sbuild.fstab.in $(schroot-config_STAMP) $(V_AT)mkdir -p $(dir $@) $(call v_tag,SUBST)$(call subst-file,### -*-conf-*-) \ <$< >$@.new && mv $@.new $@ schroot-config:: $(STATE)/etc/sbuild.conf $(STATE)/etc/sbuild.conf: etc/sbuild.conf.in $(schroot-config_STAMP) $(V_AT)mkdir -p $(dir $@) $(call v_tag,SUBST)$(call subst-file,### -*-perl-*-) \ <$< >$@.new && mv $@.new $@ ###-------------------------------------------------------------------------- ### Constructing chroots. BUILD_CHROOTS = $(addprefix chroot/,$(ALL_CHROOTS)) CHROOT_STAMPS = $(addprefix $(STATE)/stamp/chroot.,$(ALL_CHROOTS)) all:: setup-chroots setup-chroots: $(BUILD_CHROOTS) $(BUILD_CHROOTS): chroot/%: $(STATE)/stamp/chroot.% .PHONY: setup-chroots $(BUILD_CHROOTS) $(CHROOT_STAMPS): $(STATE)/stamp/chroot.%: $(STATE)/config.sh bin/mkbuildchroot $(call v_tag,CHROOT)bin/mkbuildchroot \ $(call chroot-dist,$*) $(call chroot-arch,$*) \ $(call v_log,setup-chroot.$*) ###-------------------------------------------------------------------------- ### Installing basic custom software. ###-------------------------------------------------------------------------- ### Other maintenance targets. show:; : $x clean:: rm -f $(CLEANFILES) rm -rf $(STATE) realclean:: clean rm -rf $(LOCAL) ###----- That's all, folks --------------------------------------------------