LinuxInstaller: Remove dependency with FerrochromeApp

This is an intermediate step to remove FerrochromeApp.

Bug: 370372551
Test: T/H
Change-Id: Iff984b74a847021b67fc1a5b2e8d81b0cd4e2507
diff --git a/android/LinuxInstaller/Android.bp b/android/LinuxInstaller/Android.bp
index f70452d..aef4174 100644
--- a/android/LinuxInstaller/Android.bp
+++ b/android/LinuxInstaller/Android.bp
@@ -2,13 +2,22 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+java_defaults {
+    name: "LinuxVmPayloadInstaller",
+    init_rc: [":linux_vm_setup.rc"],
+    required: ["linux_vm_setup"],
+    system_ext_specific: true,
+    platform_apis: true,
+    privileged: true,
+}
+
 android_app {
     name: "LinuxInstallerApp",
     srcs: ["java/**/*.java"],
     resource_dirs: ["res"],
     asset_dirs: ["assets"],
     manifest: "AndroidManifest.xml",
-    defaults: ["VmPayloadInstaller"],
+    defaults: ["LinuxVmPayloadInstaller"],
     overrides: ["LinuxInstallerAppStub"],
     required: [
         "privapp-permissions-linuxinstaller.xml",
@@ -39,3 +48,15 @@
     name: "com.android.virtualization.linuxinstaller_certificate",
     certificate: "com_android_virtualization_linuxinstaller",
 }
+
+filegroup {
+    name: "linux_vm_setup.rc",
+    srcs: ["linux_vm_setup.rc"],
+}
+
+sh_binary {
+    name: "linux_vm_setup",
+    src: "linux_vm_setup.sh",
+    system_ext_specific: true,
+    host_supported: false,
+}
diff --git a/android/LinuxInstaller/java/com/android/virtualization/linuxinstaller/MainActivity.java b/android/LinuxInstaller/java/com/android/virtualization/linuxinstaller/MainActivity.java
index 1d875cb..0351f97 100644
--- a/android/LinuxInstaller/java/com/android/virtualization/linuxinstaller/MainActivity.java
+++ b/android/LinuxInstaller/java/com/android/virtualization/linuxinstaller/MainActivity.java
@@ -136,10 +136,10 @@
             throw new RuntimeException("Internal error: destDir shouldn't be null");
         }
 
-        SystemProperties.set("debug.custom_vm_setup.path", destDir);
-        SystemProperties.set("debug.custom_vm_setup.done", "false");
-        SystemProperties.set("debug.custom_vm_setup.start", "true");
-        while (!SystemProperties.getBoolean("debug.custom_vm_setup.done", false)) {
+        SystemProperties.set("debug.linux_vm_setup.path", destDir);
+        SystemProperties.set("debug.linux_vm_setup.done", "false");
+        SystemProperties.set("debug.linux_vm_setup.start", "true");
+        while (!SystemProperties.getBoolean("debug.linux_vm_setup.done", false)) {
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
diff --git a/android/LinuxInstaller/linux_vm_setup.rc b/android/LinuxInstaller/linux_vm_setup.rc
new file mode 100644
index 0000000..9264d96
--- /dev/null
+++ b/android/LinuxInstaller/linux_vm_setup.rc
@@ -0,0 +1,23 @@
+# Copyright 2024 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+service linux_vm_setup /system_ext/bin/linux_vm_setup
+    user shell
+    group shell media_rw
+    disabled
+    oneshot
+    seclabel u:r:shell:s0
+
+on property:debug.linux_vm_setup.start=true
+    start linux_vm_setup
diff --git a/android/LinuxInstaller/linux_vm_setup.sh b/android/LinuxInstaller/linux_vm_setup.sh
new file mode 100644
index 0000000..6a93f6f
--- /dev/null
+++ b/android/LinuxInstaller/linux_vm_setup.sh
@@ -0,0 +1,32 @@
+#!/system/bin/sh
+
+function round_up() {
+  num=$1
+  div=$2
+  echo $((( (( ${num} / ${div} ) + 1) * ${div} )))
+}
+
+function install() {
+  src_dir=$(getprop debug.linux_vm_setup.path)
+  src_dir=${src_dir/#\/storage\/emulated\//\/data\/media\/}
+  dst_dir=/data/local/tmp/
+
+  cat $(find ${src_dir} -name "images.tar.gz*" | sort) | tar xz -C ${dst_dir}
+  cp -u ${src_dir}/vm_config.json ${dst_dir}
+  chmod 666 ${dst_dir}/*
+
+  if [ -f ${dst_dir}state.img ]; then
+    # increase the size of state.img to the multiple of 4096
+    num_blocks=$(du -b -K ${dst_dir}state.img | cut -f 1)
+    required_num_blocks=$(round_up ${num_blocks} 4)
+    additional_blocks=$((( ${required_num_blocks} - ${num_blocks} )))
+    dd if=/dev/zero bs=512 count=${additional_blocks} >> ${dst_dir}state.img
+  fi
+  rm ${src_dir}/images.tar.gz*
+  rm ${src_dir}/vm_config.json
+}
+
+setprop debug.linux_vm_setup.done false
+install
+setprop debug.linux_vm_setup.start false
+setprop debug.linux_vm_setup.done true