filesystem.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Copyright (C) 2020 The Android Open Source Project
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package filesystem
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "android/soong/android"
  20. "github.com/google/blueprint"
  21. "github.com/google/blueprint/proptools"
  22. )
  23. func init() {
  24. registerBuildComponents(android.InitRegistrationContext)
  25. }
  26. func registerBuildComponents(ctx android.RegistrationContext) {
  27. ctx.RegisterModuleType("android_filesystem", filesystemFactory)
  28. }
  29. type filesystem struct {
  30. android.ModuleBase
  31. android.PackagingBase
  32. properties filesystemProperties
  33. output android.OutputPath
  34. installDir android.InstallPath
  35. }
  36. type symlinkDefinition struct {
  37. Target *string
  38. Name *string
  39. }
  40. type filesystemProperties struct {
  41. // When set to true, sign the image with avbtool. Default is false.
  42. Use_avb *bool
  43. // Path to the private key that avbtool will use to sign this filesystem image.
  44. // TODO(jiyong): allow apex_key to be specified here
  45. Avb_private_key *string `android:"path"`
  46. // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
  47. Avb_algorithm *string
  48. // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
  49. Partition_name *string
  50. // Type of the filesystem. Currently, ext4, cpio, and compressed_cpio are supported. Default
  51. // is ext4.
  52. Type *string
  53. // file_contexts file to make image. Currently, only ext4 is supported.
  54. File_contexts *string `android:"path"`
  55. // Base directory relative to root, to which deps are installed, e.g. "system". Default is "."
  56. // (root).
  57. Base_dir *string
  58. // Directories to be created under root. e.g. /dev, /proc, etc.
  59. Dirs []string
  60. // Symbolic links to be created under root with "ln -sf <target> <name>".
  61. Symlinks []symlinkDefinition
  62. }
  63. // android_filesystem packages a set of modules and their transitive dependencies into a filesystem
  64. // image. The filesystem images are expected to be mounted in the target device, which means the
  65. // modules in the filesystem image are built for the target device (i.e. Android, not Linux host).
  66. // The modules are placed in the filesystem image just like they are installed to the ordinary
  67. // partitions like system.img. For example, cc_library modules are placed under ./lib[64] directory.
  68. func filesystemFactory() android.Module {
  69. module := &filesystem{}
  70. module.AddProperties(&module.properties)
  71. android.InitPackageModule(module)
  72. android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
  73. return module
  74. }
  75. var dependencyTag = struct {
  76. blueprint.BaseDependencyTag
  77. android.PackagingItemAlwaysDepTag
  78. }{}
  79. func (f *filesystem) DepsMutator(ctx android.BottomUpMutatorContext) {
  80. f.AddDeps(ctx, dependencyTag)
  81. }
  82. type fsType int
  83. const (
  84. ext4Type fsType = iota
  85. compressedCpioType
  86. cpioType // uncompressed
  87. unknown
  88. )
  89. func (f *filesystem) fsType(ctx android.ModuleContext) fsType {
  90. typeStr := proptools.StringDefault(f.properties.Type, "ext4")
  91. switch typeStr {
  92. case "ext4":
  93. return ext4Type
  94. case "compressed_cpio":
  95. return compressedCpioType
  96. case "cpio":
  97. return cpioType
  98. default:
  99. ctx.PropertyErrorf("type", "%q not supported", typeStr)
  100. return unknown
  101. }
  102. }
  103. func (f *filesystem) installFileName() string {
  104. return f.BaseModuleName() + ".img"
  105. }
  106. var pctx = android.NewPackageContext("android/soong/filesystem")
  107. func (f *filesystem) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  108. switch f.fsType(ctx) {
  109. case ext4Type:
  110. f.output = f.buildImageUsingBuildImage(ctx)
  111. case compressedCpioType:
  112. f.output = f.buildCpioImage(ctx, true)
  113. case cpioType:
  114. f.output = f.buildCpioImage(ctx, false)
  115. default:
  116. return
  117. }
  118. f.installDir = android.PathForModuleInstall(ctx, "etc")
  119. ctx.InstallFile(f.installDir, f.installFileName(), f.output)
  120. }
  121. // root zip will contain stuffs like dirs or symlinks.
  122. func (f *filesystem) buildRootZip(ctx android.ModuleContext) android.OutputPath {
  123. rootDir := android.PathForModuleGen(ctx, "root").OutputPath
  124. builder := android.NewRuleBuilder(pctx, ctx)
  125. builder.Command().Text("rm -rf").Text(rootDir.String())
  126. builder.Command().Text("mkdir -p").Text(rootDir.String())
  127. // create dirs and symlinks
  128. for _, dir := range f.properties.Dirs {
  129. // OutputPath.Join verifies dir
  130. builder.Command().Text("mkdir -p").Text(rootDir.Join(ctx, dir).String())
  131. }
  132. for _, symlink := range f.properties.Symlinks {
  133. name := strings.TrimSpace(proptools.String(symlink.Name))
  134. target := strings.TrimSpace(proptools.String(symlink.Target))
  135. if name == "" {
  136. ctx.PropertyErrorf("symlinks", "Name can't be empty")
  137. continue
  138. }
  139. if target == "" {
  140. ctx.PropertyErrorf("symlinks", "Target can't be empty")
  141. continue
  142. }
  143. // OutputPath.Join verifies name. don't need to verify target.
  144. dst := rootDir.Join(ctx, name)
  145. builder.Command().Text("mkdir -p").Text(filepath.Dir(dst.String()))
  146. builder.Command().Text("ln -sf").Text(proptools.ShellEscape(target)).Text(dst.String())
  147. }
  148. zipOut := android.PathForModuleGen(ctx, "root.zip").OutputPath
  149. builder.Command().
  150. BuiltTool("soong_zip").
  151. FlagWithOutput("-o ", zipOut).
  152. FlagWithArg("-C ", rootDir.String()).
  153. Flag("-L 0"). // no compression because this will be unzipped soon
  154. FlagWithArg("-D ", rootDir.String()).
  155. Flag("-d") // include empty directories
  156. builder.Command().Text("rm -rf").Text(rootDir.String())
  157. builder.Build("zip_root", fmt.Sprintf("zipping root contents for %s", ctx.ModuleName()))
  158. return zipOut
  159. }
  160. func (f *filesystem) buildImageUsingBuildImage(ctx android.ModuleContext) android.OutputPath {
  161. depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
  162. f.CopyDepsToZip(ctx, depsZipFile)
  163. builder := android.NewRuleBuilder(pctx, ctx)
  164. depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
  165. rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
  166. builder.Command().
  167. BuiltTool("zip2zip").
  168. FlagWithInput("-i ", depsZipFile).
  169. FlagWithOutput("-o ", rebasedDepsZip).
  170. Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
  171. rootDir := android.PathForModuleOut(ctx, "root").OutputPath
  172. rootZip := f.buildRootZip(ctx)
  173. builder.Command().
  174. BuiltTool("zipsync").
  175. FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
  176. Input(rootZip).
  177. Input(rebasedDepsZip)
  178. propFile, toolDeps := f.buildPropFile(ctx)
  179. output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
  180. builder.Command().BuiltTool("build_image").
  181. Text(rootDir.String()). // input directory
  182. Input(propFile).
  183. Implicits(toolDeps).
  184. Output(output).
  185. Text(rootDir.String()) // directory where to find fs_config_files|dirs
  186. // rootDir is not deleted. Might be useful for quick inspection.
  187. builder.Build("build_filesystem_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
  188. return output
  189. }
  190. func (f *filesystem) buildFileContexts(ctx android.ModuleContext) android.OutputPath {
  191. builder := android.NewRuleBuilder(pctx, ctx)
  192. fcBin := android.PathForModuleOut(ctx, "file_contexts.bin")
  193. builder.Command().BuiltTool("sefcontext_compile").
  194. FlagWithOutput("-o ", fcBin).
  195. Input(android.PathForModuleSrc(ctx, proptools.String(f.properties.File_contexts)))
  196. builder.Build("build_filesystem_file_contexts", fmt.Sprintf("Creating filesystem file contexts for %s", f.BaseModuleName()))
  197. return fcBin.OutputPath
  198. }
  199. func (f *filesystem) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
  200. type prop struct {
  201. name string
  202. value string
  203. }
  204. var props []prop
  205. var deps android.Paths
  206. addStr := func(name string, value string) {
  207. props = append(props, prop{name, value})
  208. }
  209. addPath := func(name string, path android.Path) {
  210. props = append(props, prop{name, path.String()})
  211. deps = append(deps, path)
  212. }
  213. // Type string that build_image.py accepts.
  214. fsTypeStr := func(t fsType) string {
  215. switch t {
  216. // TODO(jiyong): add more types like f2fs, erofs, etc.
  217. case ext4Type:
  218. return "ext4"
  219. }
  220. panic(fmt.Errorf("unsupported fs type %v", t))
  221. }
  222. addStr("fs_type", fsTypeStr(f.fsType(ctx)))
  223. addStr("mount_point", "/")
  224. addStr("use_dynamic_partition_size", "true")
  225. addPath("ext_mkuserimg", ctx.Config().HostToolPath(ctx, "mkuserimg_mke2fs"))
  226. // b/177813163 deps of the host tools have to be added. Remove this.
  227. for _, t := range []string{"mke2fs", "e2fsdroid", "tune2fs"} {
  228. deps = append(deps, ctx.Config().HostToolPath(ctx, t))
  229. }
  230. if proptools.Bool(f.properties.Use_avb) {
  231. addStr("avb_hashtree_enable", "true")
  232. addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
  233. algorithm := proptools.StringDefault(f.properties.Avb_algorithm, "SHA256_RSA4096")
  234. addStr("avb_algorithm", algorithm)
  235. key := android.PathForModuleSrc(ctx, proptools.String(f.properties.Avb_private_key))
  236. addPath("avb_key_path", key)
  237. addStr("avb_add_hashtree_footer_args", "--do_not_generate_fec")
  238. partitionName := proptools.StringDefault(f.properties.Partition_name, f.Name())
  239. addStr("partition_name", partitionName)
  240. }
  241. if proptools.String(f.properties.File_contexts) != "" {
  242. addPath("selinux_fc", f.buildFileContexts(ctx))
  243. }
  244. propFile = android.PathForModuleOut(ctx, "prop").OutputPath
  245. builder := android.NewRuleBuilder(pctx, ctx)
  246. builder.Command().Text("rm").Flag("-rf").Output(propFile)
  247. for _, p := range props {
  248. builder.Command().
  249. Text("echo").
  250. Flag(`"` + p.name + "=" + p.value + `"`).
  251. Text(">>").Output(propFile)
  252. }
  253. builder.Build("build_filesystem_prop", fmt.Sprintf("Creating filesystem props for %s", f.BaseModuleName()))
  254. return propFile, deps
  255. }
  256. func (f *filesystem) buildCpioImage(ctx android.ModuleContext, compressed bool) android.OutputPath {
  257. if proptools.Bool(f.properties.Use_avb) {
  258. ctx.PropertyErrorf("use_avb", "signing compresed cpio image using avbtool is not supported."+
  259. "Consider adding this to bootimg module and signing the entire boot image.")
  260. }
  261. if proptools.String(f.properties.File_contexts) != "" {
  262. ctx.PropertyErrorf("file_contexts", "file_contexts is not supported for compressed cpio image.")
  263. }
  264. depsZipFile := android.PathForModuleOut(ctx, "deps.zip").OutputPath
  265. f.CopyDepsToZip(ctx, depsZipFile)
  266. builder := android.NewRuleBuilder(pctx, ctx)
  267. depsBase := proptools.StringDefault(f.properties.Base_dir, ".")
  268. rebasedDepsZip := android.PathForModuleOut(ctx, "rebased_deps.zip").OutputPath
  269. builder.Command().
  270. BuiltTool("zip2zip").
  271. FlagWithInput("-i ", depsZipFile).
  272. FlagWithOutput("-o ", rebasedDepsZip).
  273. Text("**/*:" + proptools.ShellEscape(depsBase)) // zip2zip verifies depsBase
  274. rootDir := android.PathForModuleOut(ctx, "root").OutputPath
  275. rootZip := f.buildRootZip(ctx)
  276. builder.Command().
  277. BuiltTool("zipsync").
  278. FlagWithArg("-d ", rootDir.String()). // zipsync wipes this. No need to clear.
  279. Input(rootZip).
  280. Input(rebasedDepsZip)
  281. output := android.PathForModuleOut(ctx, f.installFileName()).OutputPath
  282. cmd := builder.Command().
  283. BuiltTool("mkbootfs").
  284. Text(rootDir.String()) // input directory
  285. if compressed {
  286. cmd.Text("|").
  287. BuiltTool("lz4").
  288. Flag("--favor-decSpeed"). // for faster boot
  289. Flag("-12"). // maximum compression level
  290. Flag("-l"). // legacy format for kernel
  291. Text(">").Output(output)
  292. } else {
  293. cmd.Text(">").Output(output)
  294. }
  295. // rootDir is not deleted. Might be useful for quick inspection.
  296. builder.Build("build_cpio_image", fmt.Sprintf("Creating filesystem %s", f.BaseModuleName()))
  297. return output
  298. }
  299. var _ android.AndroidMkEntriesProvider = (*filesystem)(nil)
  300. // Implements android.AndroidMkEntriesProvider
  301. func (f *filesystem) AndroidMkEntries() []android.AndroidMkEntries {
  302. return []android.AndroidMkEntries{android.AndroidMkEntries{
  303. Class: "ETC",
  304. OutputFile: android.OptionalPathForPath(f.output),
  305. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  306. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  307. entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String())
  308. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
  309. },
  310. },
  311. }}
  312. }
  313. var _ android.OutputFileProducer = (*filesystem)(nil)
  314. // Implements android.OutputFileProducer
  315. func (f *filesystem) OutputFiles(tag string) (android.Paths, error) {
  316. if tag == "" {
  317. return []android.Path{f.output}, nil
  318. }
  319. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  320. }
  321. // Filesystem is the public interface for the filesystem struct. Currently, it's only for the apex
  322. // package to have access to the output file.
  323. type Filesystem interface {
  324. android.Module
  325. OutputPath() android.Path
  326. // Returns the output file that is signed by avbtool. If this module is not signed, returns
  327. // nil.
  328. SignedOutputPath() android.Path
  329. }
  330. var _ Filesystem = (*filesystem)(nil)
  331. func (f *filesystem) OutputPath() android.Path {
  332. return f.output
  333. }
  334. func (f *filesystem) SignedOutputPath() android.Path {
  335. if proptools.Bool(f.properties.Use_avb) {
  336. return f.OutputPath()
  337. }
  338. return nil
  339. }