@@ -676,7 +676,7 @@ func (s *sPodGuestInstance) umountRootFs(ctrId string, rootFs *hostapi.Container
676676 return nil
677677}
678678
679- func (s * sPodGuestInstance ) getRootFsMountPath (ctrId string ) (string , error ) {
679+ func (s * sPodGuestInstance ) GetRootFsMountPath (ctrId string ) (string , error ) {
680680 ctr := s .GetContainerById (ctrId )
681681 if ctr == nil {
682682 return "" , errors .Wrapf (httperrors .ErrNotFound , "not found container %s" , ctrId )
@@ -694,10 +694,6 @@ func (s *sPodGuestInstance) getRootFsMountPath(ctrId string) (string, error) {
694694 return hostPath , nil
695695}
696696
697- func (s * sPodGuestInstance ) GetRootFsMountPath (ctx context.Context , ctrId string ) (string , error ) {
698- return s .getRootFsMountPath (ctrId )
699- }
700-
701697func (s * sPodGuestInstance ) mountPodVolumes () error {
702698 for _ , ctr := range s .GetDesc ().Containers {
703699 if ctr .Spec .Rootfs == nil {
@@ -1778,6 +1774,14 @@ func (s *sPodGuestInstance) createContainer(ctx context.Context, userCred mcclie
17781774 HostPath : shmPath ,
17791775 })
17801776 }
1777+ // inject /etc/hosts to hide host storage
1778+ if spec .Rootfs != nil {
1779+ if etcFilesMount , err := s .getEtcFilesMount (ctrId ); err != nil {
1780+ return "" , errors .Wrapf (err , "get etc hosts mount" )
1781+ } else {
1782+ mounts = append (mounts , etcFilesMount ... )
1783+ }
1784+ }
17811785
17821786 var cpuSetCpus string
17831787 var cpuSetMems string
@@ -1999,6 +2003,75 @@ func (s *sPodGuestInstance) createContainer(ctx context.Context, userCred mcclie
19992003 return criId , nil
20002004}
20012005
2006+ // copyEtcFile 复制主机上的 etc 文件到容器根文件系统
2007+ func (s * sPodGuestInstance ) copyEtcFile (hostPath , etcFilePath string ) (* runtimeapi.Mount , error ) {
2008+ hostEtcFilePath := filepath .Join (hostPath , etcFilePath )
2009+
2010+ // 确保目录存在
2011+ if err := volume_mount .EnsureDir (filepath .Dir (hostEtcFilePath )); err != nil {
2012+ return nil , errors .Wrapf (err , "ensure dir %s" , filepath .Dir (hostEtcFilePath ))
2013+ }
2014+
2015+ // 复制文件
2016+ if err := volume_mount .CopyFile (etcFilePath , hostEtcFilePath ); err != nil {
2017+ return nil , errors .Wrapf (err , "copy file %s to %s" , etcFilePath , hostEtcFilePath )
2018+ }
2019+
2020+ // 创建挂载点
2021+ return & runtimeapi.Mount {
2022+ ContainerPath : etcFilePath ,
2023+ HostPath : hostEtcFilePath ,
2024+ }, nil
2025+ }
2026+
2027+ // generateEtcFile 生成 etc 文件内容到容器根文件系统
2028+ func (s * sPodGuestInstance ) generateEtcFile (hostPath , etcFilePath , content string ) (* runtimeapi.Mount , error ) {
2029+ hostEtcFilePath := filepath .Join (hostPath , etcFilePath )
2030+
2031+ // 确保目录存在
2032+ if err := volume_mount .EnsureDir (filepath .Dir (hostEtcFilePath )); err != nil {
2033+ return nil , errors .Wrapf (err , "ensure dir %s" , filepath .Dir (hostEtcFilePath ))
2034+ }
2035+
2036+ // 生成文件内容
2037+ if err := fileutils2 .FilePutContents (hostEtcFilePath , content , false ); err != nil {
2038+ return nil , errors .Wrapf (err , "put file %s to %s" , etcFilePath , hostEtcFilePath )
2039+ }
2040+
2041+ // 创建挂载点
2042+ return & runtimeapi.Mount {
2043+ ContainerPath : etcFilePath ,
2044+ HostPath : hostEtcFilePath ,
2045+ }, nil
2046+ }
2047+
2048+ func (s * sPodGuestInstance ) getEtcFilesMount (ctrId string ) ([]* runtimeapi.Mount , error ) {
2049+ hostPath , err := s .GetRootFsMountPath (ctrId )
2050+ if err != nil {
2051+ return nil , errors .Wrapf (err , "get container root fs path of %s" , ctrId )
2052+ }
2053+
2054+ // 复制 /etc/hosts 文件
2055+ etcHostsMount , err := s .copyEtcFile (hostPath , "/etc/hosts" )
2056+ if err != nil {
2057+ return nil , errors .Wrap (err , "copy /etc/hosts" )
2058+ }
2059+
2060+ // 生成 /etc/hostname 文件
2061+ etcHostnameMount , err := s .generateEtcFile (hostPath , "/etc/hostname" , s .GetDesc ().Hostname )
2062+ if err != nil {
2063+ return nil , errors .Wrap (err , "generate /etc/hostname" )
2064+ }
2065+
2066+ // 复制 /etc/resolv.conf 文件
2067+ etcResolvConfMount , err := s .copyEtcFile (hostPath , "/etc/resolv.conf" )
2068+ if err != nil {
2069+ return nil , errors .Wrap (err , "copy /etc/resolv.conf" )
2070+ }
2071+
2072+ return []* runtimeapi.Mount {etcHostsMount , etcHostnameMount , etcResolvConfMount }, nil
2073+ }
2074+
20022075func filterContainerDevices (devs []* hostapi.ContainerDevice ) ([]* hostapi.ContainerDevice , []* hostapi.ContainerDevice ) {
20032076 envDevs := []* hostapi.ContainerDevice {}
20042077 restDevs := []* hostapi.ContainerDevice {}
@@ -2075,6 +2148,10 @@ func (s *sPodGuestInstance) getIsolatedDeviceExtraConfig(spec *hostapi.Container
20752148}
20762149
20772150func (s * sPodGuestInstance ) getContainerSystemCpusDir (ctrId string ) string {
2151+ rootFsPath , _ := s .GetRootFsMountPath (ctrId )
2152+ if rootFsPath != "" {
2153+ return filepath .Join (rootFsPath , "cpus" , ctrId )
2154+ }
20782155 return filepath .Join (s .HomeDir (), "cpus" , ctrId )
20792156}
20802157
0 commit comments