@@ -18,6 +18,8 @@ import (
1818
1919const (
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
222236func (chrootEnv * ChrootEnv ) updateChrootLocalRPMRepo (chrootRepoDir string ) error {
@@ -286,36 +300,46 @@ func (chrootEnv *ChrootEnv) initChrootLocalRepo(targetArch string) error {
286300}
287301
288302func (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
335359func (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