Skip to content

Commit 4247af8

Browse files
authored
Merge pull request #276 from open-edge-platform/dev_feiyu
Make chroot work with host env & enable auto login for initramfs
2 parents 2f4574f + b2c8087 commit 4247af8

11 files changed

Lines changed: 177 additions & 48 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Service]
2+
ExecStart=
3+
ExecStart=-/sbin/agetty --noclear --autologin root --keep-baud %I $TERM
4+
5+
# Default to tty1 but allow other choices
6+
[Install]
7+
Alias=getty.target.wants/getty@tty1.service
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Service]
2+
ExecStart=
3+
ExecStart=-/sbin/agetty --noclear --autologin root --keep-baud %I $TERM
4+
5+
# Default to ttyS0 but allow other choices
6+
[Install]
7+
Alias=getty.target.wants/serial-getty@ttyS0.service

config/osv/azure-linux/azl3/imageconfigs/defaultconfigs/default-initrd-x86_64.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ systemConfig:
8888
additionalFiles:
8989
- local: ../additionalfiles/99-dhcp-en.network
9090
final: /etc/systemd/network/99-dhcp-en.network
91+
- local: ../additionalfiles/getty@.service
92+
final: /usr/lib/systemd/system/getty@.service
93+
- local: ../additionalfiles/serial-getty@.service
94+
final: /usr/lib/systemd/system/serial-getty@.service
95+
96+
users:
97+
- name: root
98+
passwordMaxAge: 99999
99+
password: ""
91100

92101
kernel:
93102
name: kernel
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Service]
2+
ExecStart=
3+
ExecStart=-/sbin/agetty --noclear --autologin root --keep-baud %I $TERM
4+
5+
# Default to tty1 but allow other choices
6+
[Install]
7+
Alias=getty.target.wants/getty@tty1.service
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Service]
2+
ExecStart=
3+
ExecStart=-/sbin/agetty --noclear --autologin root --keep-baud %I $TERM
4+
5+
# Default to ttyS0 but allow other choices
6+
[Install]
7+
Alias=getty.target.wants/serial-getty@ttyS0.service

config/osv/edge-microvisor-toolkit/emt3/imageconfigs/defaultconfigs/default-initrd-x86_64.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ systemConfig:
8888
additionalFiles:
8989
- local: ../additionalfiles/99-dhcp-en.network
9090
final: /etc/systemd/network/99-dhcp-en.network
91+
- local: ../additionalfiles/getty@.service
92+
final: /usr/lib/systemd/system/getty@.service
93+
- local: ../additionalfiles/serial-getty@.service
94+
final: /usr/lib/systemd/system/serial-getty@.service
95+
96+
users:
97+
- name: root
98+
passwordMaxAge: 99999
99+
password: ""
91100

92101
kernel:
93102
name: kernel
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Service]
2+
ExecStart=
3+
ExecStart=-/sbin/agetty --noclear --autologin root --keep-baud %I $TERM
4+
5+
# Default to tty1 but allow other choices
6+
[Install]
7+
Alias=getty.target.wants/getty@tty1.service
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Service]
2+
ExecStart=
3+
ExecStart=-/sbin/agetty --noclear --autologin root --keep-baud %I $TERM
4+
5+
# Default to ttyS0 but allow other choices
6+
[Install]
7+
Alias=getty.target.wants/serial-getty@ttyS0.service

config/osv/wind-river-elxr/elxr12/imageconfigs/defaultconfigs/default-initrd-x86_64.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ systemConfig:
4444
final: /etc/systemd/network/dhcp.network
4545
- local: ../additionalfiles/elxr-aria.list
4646
final: /etc/apt/sources.list.d/elxr-aria.list
47+
- local: ../additionalfiles/getty@.service
48+
final: /usr/lib/systemd/system/getty@.service
49+
- local: ../additionalfiles/serial-getty@.service
50+
final: /usr/lib/systemd/system/serial-getty@.service
51+
52+
users:
53+
- name: root
54+
passwordMaxAge: 99999
55+
password: ""
4756

4857
kernel:
4958
name: kernel

internal/chroot/chrootenv.go

Lines changed: 101 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
const (
2020
ChrootRepoDir = "/cdrom/cache-repo"
21+
RPMRepoConfigFile = "local.repo"
22+
DEBRepoConfigFile = "local.list"
2123
RPMRepoConfigDir = "/etc/yum.repos.d/"
2224
DEBRepoConfigDir = "/etc/apt/sources.list.d/"
2325
ResolvConfPath = "/etc/resolv.conf"
@@ -183,9 +185,13 @@ func (chrootEnv *ChrootEnv) MountChrootPath(hostFullPath, chrootPath, mountFlags
183185
if err != nil {
184186
return fmt.Errorf("failed to get chroot host path for %s: %w", chrootPath, err)
185187
}
186-
if _, err := os.Stat(chrootHostPath); os.IsNotExist(err) {
187-
if _, err = shell.ExecCmd("mkdir -p "+chrootHostPath, true, shell.HostPath, nil); err != nil {
188-
return fmt.Errorf("failed to create directory %s: %w", chrootHostPath, err)
188+
if hostFullPath == chrootHostPath {
189+
return nil
190+
} else {
191+
if _, err := os.Stat(chrootHostPath); os.IsNotExist(err) {
192+
if _, err = shell.ExecCmd("mkdir -p "+chrootHostPath, true, shell.HostPath, nil); err != nil {
193+
return fmt.Errorf("failed to create directory %s: %w", chrootHostPath, err)
194+
}
189195
}
190196
}
191197
return mount.MountPath(hostFullPath, chrootHostPath, mountFlags)
@@ -207,7 +213,11 @@ func (chrootEnv *ChrootEnv) CopyFileFromHostToChroot(hostFilePath, chrootPath st
207213
if err != nil {
208214
return fmt.Errorf("failed to get chroot host path for %s: %w", chrootPath, err)
209215
}
210-
return file.CopyFile(hostFilePath, chrootHostPath, "-f", true)
216+
if hostFilePath == chrootHostPath {
217+
return nil
218+
} else {
219+
return file.CopyFile(hostFilePath, chrootHostPath, "-f", true)
220+
}
211221
}
212222

213223
// CopyFileFromChrootToHost copies a file from the chroot environment to the host
@@ -216,7 +226,11 @@ func (chrootEnv *ChrootEnv) CopyFileFromChrootToHost(hostFilePath, chrootPath st
216226
if err != nil {
217227
return fmt.Errorf("failed to get chroot host path for %s: %w", chrootPath, err)
218228
}
219-
return file.CopyFile(chrootHostPath, hostFilePath, "-f", true)
229+
if hostFilePath == chrootHostPath {
230+
return nil
231+
} else {
232+
return file.CopyFile(chrootHostPath, hostFilePath, "-f", true)
233+
}
220234
}
221235

222236
func (chrootEnv *ChrootEnv) updateChrootLocalRPMRepo(chrootRepoDir string) error {
@@ -286,36 +300,46 @@ func (chrootEnv *ChrootEnv) initChrootLocalRepo(targetArch string) error {
286300
}
287301

288302
func (chrootEnv *ChrootEnv) createChrootRepo(targetOs, targetDist string) error {
303+
var repoConfigDir string
304+
var repoConfigFile string
305+
289306
targetOsConfigDir := chrootEnv.GetTargetOsConfigDir()
290307
pkgType := chrootEnv.GetTargetOsPkgType()
291308
if pkgType == "rpm" {
292-
chrootRepoCongfigPath := filepath.Join(targetOsConfigDir, "chrootenvconfigs", "local.repo")
293-
if _, err := os.Stat(chrootRepoCongfigPath); os.IsNotExist(err) {
294-
return fmt.Errorf("chroot repo config file does not exist: %s", chrootRepoCongfigPath)
295-
}
296-
297-
if err := chrootEnv.CopyFileFromHostToChroot(chrootRepoCongfigPath, RPMRepoConfigDir); err != nil {
298-
return fmt.Errorf("failed to copy local.repo: %w", err)
299-
}
309+
repoConfigDir = RPMRepoConfigDir
310+
repoConfigFile = RPMRepoConfigFile
300311
} else if pkgType == "deb" {
301-
chrootRepoCongfigPath, err := chrootEnv.GetChrootEnvHostPath(DEBRepoConfigDir)
302-
if err != nil {
303-
return fmt.Errorf("failed to get chroot host path for local repo config: %w", err)
304-
}
305-
if _, err := shell.ExecCmd("rm -f "+chrootRepoCongfigPath+"/*", true, shell.HostPath, nil); err != nil {
306-
return fmt.Errorf("failed to remove existing local repo config files: %w", err)
307-
}
312+
repoConfigDir = DEBRepoConfigDir
313+
repoConfigFile = DEBRepoConfigFile
314+
} else {
315+
return fmt.Errorf("unsupported package type: %s", pkgType)
316+
}
308317

309-
RepoCongfigPath := filepath.Join(targetOsConfigDir, "chrootenvconfigs", "local.list")
310-
if _, err := os.Stat(RepoCongfigPath); os.IsNotExist(err) {
311-
return fmt.Errorf("chroot repo config file does not exist: %s", RepoCongfigPath)
318+
// Backup existing local repo config files in chroot environment
319+
chrootRepoCongfigPath, err := chrootEnv.GetChrootEnvHostPath(repoConfigDir)
320+
if err != nil {
321+
return fmt.Errorf("failed to get chroot host path for local repo config: %w", err)
322+
}
323+
if _, err := os.Stat(chrootRepoCongfigPath); err == nil {
324+
if files, _ := os.ReadDir(chrootRepoCongfigPath); len(files) != 0 {
325+
repoConfigBackupPath := filepath.Join(chrootEnv.ChrootEnvRoot, "repo-config-backup")
326+
if err := file.CopyDir(chrootRepoCongfigPath, repoConfigBackupPath, "-f", true); err != nil {
327+
return fmt.Errorf("failed to backup existing repo config files: %w", err)
328+
}
329+
if _, err := shell.ExecCmd("rm -f "+chrootRepoCongfigPath+"/*", true, shell.HostPath, nil); err != nil {
330+
return fmt.Errorf("failed to remove existing local repo config files: %w", err)
331+
}
312332
}
333+
}
313334

314-
if err := chrootEnv.CopyFileFromHostToChroot(RepoCongfigPath, DEBRepoConfigDir); err != nil {
315-
return fmt.Errorf("failed to copy local.repo: %w", err)
316-
}
317-
} else {
318-
return fmt.Errorf("unsupported package type: %s", pkgType)
335+
// Copy local repo config file to chroot environment
336+
localRepoCongfigPath := filepath.Join(targetOsConfigDir, "chrootenvconfigs", repoConfigFile)
337+
if _, err := os.Stat(localRepoCongfigPath); os.IsNotExist(err) {
338+
return fmt.Errorf("chroot repo config file does not exist: %s", localRepoCongfigPath)
339+
}
340+
341+
if err := chrootEnv.CopyFileFromHostToChroot(localRepoCongfigPath, repoConfigDir); err != nil {
342+
return fmt.Errorf("failed to copy local.repo: %w", err)
319343
}
320344

321345
return nil
@@ -333,10 +357,7 @@ func (chrootEnv *ChrootEnv) initChrootWorkspace() error {
333357
}
334358

335359
func (chrootEnv *ChrootEnv) InitChrootEnv(targetOs, targetDist, targetArch string) (err error) {
336-
var chrootRootfsExist bool = true
337-
338360
if files, _ := os.ReadDir(chrootEnv.ChrootEnvRoot); len(files) == 0 {
339-
chrootRootfsExist = false
340361
chrootBuildDir := chrootEnv.ChrootBuilder.GetChrootBuildDir()
341362
chrootEnvTarPath := filepath.Join(chrootBuildDir, "chrootenv.tar.gz")
342363
if _, err := os.Stat(chrootEnvTarPath); os.IsNotExist(err) {
@@ -362,26 +383,26 @@ func (chrootEnv *ChrootEnv) InitChrootEnv(targetOs, targetDist, targetArch strin
362383
return fmt.Errorf("failed to initialize chroot workspace: %w", err)
363384
}
364385

365-
// Mount sysfs to the chroot environment
366-
err = chrootEnv.MountChrootSysfs("/")
367-
if err != nil {
368-
return fmt.Errorf("failed to mount sysfs for chroot environment: %w", err)
369-
}
370-
371-
defer func() {
386+
if chrootEnv.ChrootEnvRoot != shell.HostPath {
387+
// Mount sysfs to the chroot environment
388+
err = chrootEnv.MountChrootSysfs("/")
372389
if err != nil {
373-
if umountErr := chrootEnv.UmountChrootSysfs("/"); umountErr != nil {
374-
log.Errorf("Failed to unmount sysfs for chroot environment: %v", umountErr)
375-
err = fmt.Errorf("operation failed: %w, cleanup errors: %v", err, umountErr)
376-
}
390+
return fmt.Errorf("failed to mount sysfs for chroot environment: %w", err)
377391
}
378-
}()
379392

380-
if !chrootRootfsExist {
381-
// Create chroot repository
382-
if err = chrootEnv.createChrootRepo(targetOs, targetDist); err != nil {
383-
return fmt.Errorf("failed to create chroot repository: %w", err)
384-
}
393+
defer func() {
394+
if err != nil {
395+
if umountErr := chrootEnv.UmountChrootSysfs("/"); umountErr != nil {
396+
log.Errorf("Failed to unmount sysfs for chroot environment: %v", umountErr)
397+
err = fmt.Errorf("operation failed: %w, cleanup errors: %v", err, umountErr)
398+
}
399+
}
400+
}()
401+
}
402+
403+
// Create chroot local repository
404+
if err = chrootEnv.createChrootRepo(targetOs, targetDist); err != nil {
405+
return fmt.Errorf("failed to create chroot repository: %w", err)
385406
}
386407

387408
if err = chrootEnv.initChrootLocalRepo(targetArch); err != nil {
@@ -400,6 +421,38 @@ func (chrootEnv *ChrootEnv) CleanupChrootEnv(targetOs, targetDist, targetArch st
400421
if err := mount.UmountSubPath(chrootEnv.ChrootEnvRoot); err != nil {
401422
return fmt.Errorf("failed to unmount path for chroot environment: %w", err)
402423
}
424+
425+
// Restore existing local repo config files in chroot environment
426+
repoConfigBackupPath := filepath.Join(chrootEnv.ChrootEnvRoot, "repo-config-backup")
427+
if _, err := os.Stat(repoConfigBackupPath); err == nil {
428+
var repoConfigDir string
429+
pkgType := chrootEnv.GetTargetOsPkgType()
430+
if pkgType == "rpm" {
431+
repoConfigDir = RPMRepoConfigDir
432+
} else if pkgType == "deb" {
433+
repoConfigDir = DEBRepoConfigDir
434+
} else {
435+
return fmt.Errorf("unsupported package type: %s", pkgType)
436+
}
437+
438+
chrootRepoCongfigPath, err := chrootEnv.GetChrootEnvHostPath(repoConfigDir)
439+
if err != nil {
440+
return fmt.Errorf("failed to get chroot host path for local repo config: %w", err)
441+
}
442+
if _, err := os.Stat(chrootRepoCongfigPath); err == nil {
443+
if files, _ := os.ReadDir(chrootRepoCongfigPath); len(files) != 0 {
444+
if _, err := shell.ExecCmd("rm -f "+chrootRepoCongfigPath+"/*", true, shell.HostPath, nil); err != nil {
445+
return fmt.Errorf("failed to remove existing local repo config files: %w", err)
446+
}
447+
if err := file.CopyDir(repoConfigBackupPath, chrootRepoCongfigPath, "-f", true); err != nil {
448+
return fmt.Errorf("failed to backup existing repo config files: %w", err)
449+
}
450+
if _, err := shell.ExecCmd("rm -rf "+repoConfigBackupPath, true, shell.HostPath, nil); err != nil {
451+
return fmt.Errorf("failed to remove repo config backup directory %s: %w", repoConfigBackupPath, err)
452+
}
453+
}
454+
}
455+
}
403456
} else {
404457
log.Infof("Chroot environment root %s does not exist, skipping cleanup", chrootEnv.ChrootEnvRoot)
405458
}

0 commit comments

Comments
 (0)