From 76341cfc5f71fb3658535bc21b001399a84f8816 Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@nbd.name>
Date: Fri, 22 Jul 2016 14:27:52 +0200
Subject: [PATCH] build: add support for per-device rootfs based on device
 profile packges

Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 include/image-legacy.mk    | 22 +++++++++++++-
 include/image.mk           | 60 ++++++++++++++++++++++++++++++++------
 scripts/target-metadata.pl | 32 ++++++++++++++++++--
 3 files changed, 101 insertions(+), 13 deletions(-)

diff --git a/include/image-legacy.mk b/include/image-legacy.mk
index 9ace98fbe5..9633500fd9 100644
--- a/include/image-legacy.mk
+++ b/include/image-legacy.mk
@@ -45,6 +45,15 @@ define LegacyDevice/Check
   endif
 endef
 
+ifdef TARGET_PER_DEVICE_ROOTFS
+  define Image/Build/Profile/Filesystem
+	cp $(KDIR)/root.$(2)+pkg=$(3) $(KDIR)/root.$(2)
+	$(call Image/Build/Profile,$(1),$(2))
+  endef
+else
+  Image/Build/Profile/Filesystem = $(Image/Build/Profile)
+endif
+
 define LegacyDevice/Build
   $$(_TARGET): legacy-image-$(1)
   $$(_TARGET_PREPARE): legacy-image-prepare-$(1)
@@ -53,10 +62,21 @@ define LegacyDevice/Build
   legacy-image-prepare-$(1):
 	$$(call Image/Prepare/Profile,$(1))
 
+  ifndef IB
+    ifdef CONFIG_TARGET_PER_DEVICE_ROOTFS
+      ROOTFS/$(1) := $(foreach fs,$(TARGET_FILESYSTEMS), \
+        $(KDIR)/root.$(fs)$$(strip $(if $(CONFIG_TARGET_PER_DEVICE_ROOTFS),+pkg=$$(ROOTFS_ID/$(1))))
+      )
+
+      $$(ROOTFS/$(1)): target-dir-$$(ROOTFS_ID/$(1))
+      legacy-images-make: $$(if $$(_PROFILE_SET),$$(ROOTFS/$(1)))
+    endif
+  endif
+
   legacy-image-$(1):
 	$$(call Image/BuildKernel/Profile,$(1))
 	$(foreach fs,$(TARGET_FILESYSTEMS),
-		$$(call Image/Build/Profile,$(1),$(fs))
+		$$(call Image/Build/Profile/Filesystem,$(1),$(fs),$$(ROOTFS_ID/$(1)))
 	)
 
 endef
diff --git a/include/image.mk b/include/image.mk
index 5970e58e1a..42aa3f0284 100644
--- a/include/image.mk
+++ b/include/image.mk
@@ -11,8 +11,19 @@ include $(INCLUDE_DIR)/kernel.mk
 include $(INCLUDE_DIR)/host.mk
 include $(INCLUDE_DIR)/version.mk
 include $(INCLUDE_DIR)/image-commands.mk
+
+ifndef IB
+  ifdef CONFIG_TARGET_PER_DEVICE_ROOTFS
+    TARGET_PER_DEVICE_ROOTFS := 1
+  endif
+endif
+
 include $(INCLUDE_DIR)/image-legacy.mk
 
+ifdef TARGET_PER_DEVICE_ROOTFS
+  include $(INCLUDE_DIR)/rootfs.mk
+endif
+
 override MAKE:=$(_SINGLE)$(SUBMAKE)
 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
 
@@ -21,7 +32,8 @@ param_get = $(patsubst $(1)=%,%,$(filter $(1)=%,$(2)))
 param_mangle = $(subst $(space),_,$(strip $(1)))
 param_unmangle = $(subst _,$(space),$(1))
 
-mkfs_target_dir = $(TARGET_DIR)
+mkfs_packages_id = $(shell echo $(sort $(1)) | md5sum | head -c 8)
+mkfs_target_dir = $(if $(call param_get,pkg,$(1)),$(KDIR)/target-dir-$(call param_get,pkg,$(1)),$(TARGET_DIR))
 
 KDIR=$(KERNEL_BUILD_DIR)
 KDIR_TMP=$(KDIR)/tmp
@@ -240,15 +252,15 @@ endef
 
 define Image/mkfs/prepare/default
 	# Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
-	- $(FIND) $(TARGET_DIR) -type f -not -perm /0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
-	- $(FIND) $(TARGET_DIR) -type f -perm /0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
-	- $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
-	$(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
-	chmod 1777 $(TARGET_DIR)/tmp
+	- $(FIND) $(1) -type f -not -perm /0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
+	- $(FIND) $(1) -type f -perm /0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
+	- $(FIND) $(1) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
+	$(INSTALL_DIR) $(1)/tmp $(1)/overlay
+	chmod 1777 $(1)/tmp
 endef
 
 define Image/mkfs/prepare
-	$(call Image/mkfs/prepare/default)
+	$(call Image/mkfs/prepare/default,$(1))
 endef
 
 
@@ -272,6 +284,23 @@ ifdef CONFIG_TARGET_ROOTFS_CPIOGZ
   endef
 endif
 
+mkfs_packages = $(filter-out @%,$(PACKAGES_$(call param_get,pkg,pkg=$(target_params))))
+mkfs_packages_add = $(filter-out -%,$(mkfs_packages))
+mkfs_packages_remove = $(patsubst -%,%,$(filter -%,$(mkfs_packages)))
+mkfs_cur_target_dir = $(call mkfs_target_dir,pkg=$(target_params))
+
+target-dir-%: FORCE
+	rm -rf $(mkfs_cur_target_dir)
+	$(CP) $(TARGET_DIR) $(mkfs_cur_target_dir)
+	$(if $(mkfs_packages_add), \
+		$(call opkg,$(mkfs_cur_target_dir)) install \
+			$(call opkg_package_files,$(mkfs_packages_add)))
+	$(if $(mkfs_packages_remove), \
+		$(call opkg,$(mkfs_cur_target_dir)) remove \
+			$(mkfs_packages_remove))
+	$(call Image/mkfs/prepare,$(mkfs_cur_target_dir))
+	$(call prepare_rootfs,$(mkfs_cur_target_dir))
+
 $(KDIR)/root.%: kernel_prepare
 	$(call Image/mkfs/$(word 1,$(target_params)),$(target_params))
 
@@ -347,6 +376,10 @@ endif
 
 define Device/Check/Common
   _PROFILE_SET = $$(strip $$(foreach profile,$$(PROFILES) DEVICE_$(1),$$(call DEVICE_CHECK_PROFILE,$$(profile))))
+  ifdef TARGET_PER_DEVICE_ROOTFS
+    ROOTFS_ID/$(1) := $$(if $$(_PROFILE_SET),$$(call mkfs_packages_id,$$(DEVICE_PACKAGES)))
+    PACKAGES_$$(ROOTFS_ID/$(1)) := $$(DEVICE_PACKAGES)
+  endif
 endef
 
 define Device/Check
@@ -409,7 +442,16 @@ endef
 define Device/Build/image
   $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
   $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
-  $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $$(KDIR_KERNEL_IMAGE) $(KDIR)/root.$(1)$$(if $$(FS_OPTIONS/$(1)),+fs=$$(call param_mangle,$$(FS_OPTIONS/$(1))))
+  ROOTFS/$(1)/$(3) := \
+	$(KDIR)/root.$(1)$$(strip \
+		$$(if $$(FS_OPTIONS/$(1)),+fs=$$(call param_mangle,$$(FS_OPTIONS/$(1)))) \
+	)$$(strip \
+		$(if $(TARGET_PER_DEVICE_ROOTFS),+pkg=$$(ROOTFS_ID/$(3))) \
+	)
+  ifndef IB
+    $$(ROOTFS/$(1)/$(3)): $(if $(TARGET_PER_DEVICE_ROOTFS),target-dir-$$(ROOTFS_ID/$(3)))
+  endif
+  $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $$(KDIR_KERNEL_IMAGE) $$(ROOTFS/$(1)/$(3))
 	@rm -f $$@
 	[ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
 	$$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
@@ -493,7 +535,7 @@ define BuildImage
   endif
 
   mkfs_prepare: image_prepare
-	$(call Image/mkfs/prepare)
+	$(call Image/mkfs/prepare,$(TARGET_DIR))
 
   kernel_prepare: mkfs_prepare
 	$(call Image/Build/targz)
diff --git a/scripts/target-metadata.pl b/scripts/target-metadata.pl
index ca9940e9d9..7dd782a6c2 100755
--- a/scripts/target-metadata.pl
+++ b/scripts/target-metadata.pl
@@ -151,6 +151,18 @@ sub gen_target_config() {
 		target_name($a) cmp target_name($b);
 	} @target;
 
+	foreach my $target (@target_sort) {
+		next if @{$target->{subtargets}} > 0;
+		print <<EOF;
+config DEFAULT_TARGET_$target->{conf}
+	bool
+	depends on TARGET_PER_DEVICE_ROOTFS
+	default y if TARGET_$target->{conf}
+EOF
+		foreach my $pkg (@{$target->{packages}}) {
+			print "\tselect DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
+		}
+	}
 
 	print <<EOF;
 choice
@@ -238,6 +250,9 @@ endchoice
 menu "Target Devices"
 	depends on TARGET_MULTI_PROFILE
 
+	config TARGET_PER_DEVICE_ROOTFS
+		bool "Use a per-device root filesystem that adds profile packages"
+
 EOF
 	foreach my $target (@target) {
 		my $profiles = $target->{profiles};
@@ -250,7 +265,8 @@ config TARGET_DEVICE_$target->{conf}_$profile->{id}
 EOF
 			my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
 			foreach my $pkg (@pkglist) {
-				print "\tselect DEFAULT_$pkg\n";
+				print "\tselect DEFAULT_$pkg if !TARGET_PER_DEVICE_ROOTFS\n";
+				print "\tselect MODULE_DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
 				$defaults{$pkg} = 1;
 			}
 		}
@@ -340,8 +356,18 @@ config LINUX_$v
 EOF
 	}
 	foreach my $def (sort keys %defaults) {
-		print "\tconfig DEFAULT_".$def."\n";
-		print "\t\tbool\n\n";
+		print <<EOF;
+	config DEFAULT_$def
+		bool
+
+	config MODULE_DEFAULT_$def
+		tristate
+		depends on TARGET_PER_DEVICE_ROOTFS
+		depends on m
+		default m if DEFAULT_$def
+		select PACKAGE_$def
+
+EOF
 	}
 }
 
-- 
2.30.2