android.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2019 Yunion
  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 fsdriver
  15. import (
  16. "fmt"
  17. "sort"
  18. "strings"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  20. deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
  21. )
  22. func ParsePropStr(lines string) map[string]string {
  23. ret := map[string]string{}
  24. for _, l := range strings.Split(lines, "\n") {
  25. if len(l) > 0 && l[0] != '#' {
  26. pos := strings.Index(l, "=")
  27. if pos > 0 {
  28. key := strings.TrimSpace(l[:pos])
  29. val := strings.TrimSpace(l[pos+1:])
  30. ret[key] = val
  31. }
  32. }
  33. }
  34. return ret
  35. }
  36. func BuildPropStr(prop map[string]string) string {
  37. keys := []string{}
  38. for k := range prop {
  39. keys = append(keys, k)
  40. }
  41. sort.Strings(keys)
  42. ret := ""
  43. for _, k := range keys {
  44. ret += fmt.Sprintf("%s=%s\n", k, prop[k])
  45. }
  46. return ret
  47. }
  48. type sBaseAndroidRootFs struct {
  49. *sGuestRootFsDriver
  50. rootDir string
  51. distroKey string
  52. versionKey string
  53. }
  54. func (m *sBaseAndroidRootFs) IsFsCaseInsensitive() bool {
  55. return false
  56. }
  57. func (m *sBaseAndroidRootFs) RootSignatures() []string {
  58. return []string{
  59. m.rootDir, "/grub",
  60. }
  61. }
  62. func (m *sBaseAndroidRootFs) GetLoginAccount(rootFs IDiskPartition, user string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
  63. return "", nil
  64. }
  65. func (m *sBaseAndroidRootFs) DeployPublicKey(rootfs IDiskPartition, uname string, pubkeys *deployapi.SSHKeys) error {
  66. return nil
  67. }
  68. func (m *sBaseAndroidRootFs) ChangeUserPasswd(part IDiskPartition, account, gid, publicKey, password string, isRandomPassword bool) (string, error) {
  69. return "", nil
  70. }
  71. func (m *sBaseAndroidRootFs) DeployHostname(part IDiskPartition, hostname, domain string) error {
  72. return nil
  73. }
  74. func (m *sBaseAndroidRootFs) DeployHosts(part IDiskPartition, hn, domain string, ips []string) error {
  75. return nil
  76. }
  77. func (m *sBaseAndroidRootFs) GetOs() string {
  78. return "Android"
  79. }
  80. func (m *sBaseAndroidRootFs) GetReleaseInfo(IDiskPartition) *deployapi.ReleaseInfo {
  81. spath := fmt.Sprintf("/%s/system/build.prop", m.rootDir)
  82. lines, _ := m.rootFs.FileGetContents(spath, false)
  83. prop := ParsePropStr(string(lines))
  84. distro, _ := prop[m.distroKey]
  85. version, _ := prop[m.versionKey]
  86. arch, _ := prop["ro.product.cpu.abi"]
  87. return &deployapi.ReleaseInfo{
  88. Distro: distro,
  89. Version: version,
  90. Arch: arch,
  91. }
  92. }
  93. func (m *sBaseAndroidRootFs) PrepareFsForTemplate(IDiskPartition) error {
  94. return nil
  95. }
  96. func (m *sBaseAndroidRootFs) DeployNetworkingScripts(rootfs IDiskPartition, nics []*types.SServerNic) error {
  97. return nil
  98. }
  99. func (m *sBaseAndroidRootFs) ConfigSshd(loginAccount, loginPassword string, sshPort int) error {
  100. return nil
  101. }
  102. func (m *sBaseAndroidRootFs) CommitChanges(part IDiskPartition) error {
  103. spath := fmt.Sprintf("/%s/system/build.prop", m.rootDir)
  104. lines, _ := m.rootFs.FileGetContents(spath, false)
  105. prop := ParsePropStr(string(lines))
  106. prop["ro.setupwizard.mode"] = "DISABLED"
  107. prop["persist.sys.timezone"] = "Asia/Shanghai"
  108. return m.rootFs.FilePutContents(spath, BuildPropStr(prop), false, false)
  109. }
  110. type SAndroidRootFs struct {
  111. *sBaseAndroidRootFs
  112. }
  113. func NewAndroidRootFs(part IDiskPartition) IRootFsDriver {
  114. return &SAndroidRootFs{
  115. &sBaseAndroidRootFs{
  116. sGuestRootFsDriver: newGuestRootFsDriver(part),
  117. rootDir: "android-*",
  118. distroKey: "ro.product.model",
  119. versionKey: "ro.build.version.release",
  120. },
  121. }
  122. }
  123. func (m *SAndroidRootFs) GetName() string {
  124. return "Android"
  125. }
  126. func (m *SAndroidRootFs) String() string {
  127. return "AndroidRootFs"
  128. }
  129. type SPhoenixOSRootFs struct {
  130. *sBaseAndroidRootFs
  131. }
  132. func NewPhoenixOSRootFs(part IDiskPartition) IRootFsDriver {
  133. return &SPhoenixOSRootFs{
  134. &sBaseAndroidRootFs{
  135. sGuestRootFsDriver: newGuestRootFsDriver(part),
  136. rootDir: "PhoenixOS",
  137. distroKey: "ro.phoenix.os.branch",
  138. versionKey: "ro.phoenix.version.codename",
  139. },
  140. }
  141. }
  142. func (m *SPhoenixOSRootFs) GetName() string {
  143. return "PhoenixOS"
  144. }
  145. func (m *SPhoenixOSRootFs) String() string {
  146. return "PhoenixOSRootFs"
  147. }