Skip to content

Commit 633c8e2

Browse files
authored
feat(host): hide host files mounted to container (#23292)
1 parent b524b2f commit 633c8e2

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

pkg/hostman/container/snapshot_service/snapshot_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type IGuestManager interface {
4343
}
4444

4545
type ISnapshotContainerManager interface {
46-
GetRootFsMountPath(ctx context.Context, containerId string) (string, error)
46+
GetRootFsMountPath(containerId string) (string, error)
4747
}
4848

4949
func NewSnapshotter(guestMan IGuestManager, root string, opts ...overlay.Opt) (snapshots.Snapshotter, error) {
@@ -129,7 +129,7 @@ func (s *overlayRootFsUpperSnapshotter) changeUpper(ctx context.Context, key str
129129
if err != nil {
130130
return mounts, errors.Wrapf(err, "GetContainerManager with %s", serverId)
131131
}
132-
rootFsPath, err := ctrMan.GetRootFsMountPath(ctx, containerId)
132+
rootFsPath, err := ctrMan.GetRootFsMountPath(containerId)
133133
if err != nil {
134134
return mounts, errors.Wrapf(err, "GetRootFsMountPath with %s, %s", serverId, containerId)
135135
}

pkg/hostman/container/volume_mount/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type IPodInfo interface {
4848
GetVolumesOverlayDir() string
4949
GetDisks() []*desc.SGuestDisk
5050
GetDiskMountPoint(disk storageman.IDisk) string
51+
GetRootFsMountPath(ctrId string) (string, error)
5152
}
5253

5354
type IVolumeMount interface {

pkg/hostman/container/volume_mount/text.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ func (t text) GetRuntimeMountHostPath(pod IPodInfo, ctrId string, vm *hostapi.Co
5151
if err := EnsureDir(pod.GetVolumesDir()); err != nil {
5252
return "", errors.Wrapf(err, "mkdir %s", pod.GetVolumesDir())
5353
}
54-
mntPath := filepath.Join(pod.GetVolumesDir(), fmt.Sprintf("%s-%s", ctrId, strings.ReplaceAll(vm.MountPath, "/", "_")))
54+
dirPath := pod.GetVolumesDir()
55+
rootFsPath, _ := pod.GetRootFsMountPath(ctrId)
56+
if rootFsPath != "" {
57+
dirPath = rootFsPath
58+
}
59+
mntPath := filepath.Join(dirPath, fmt.Sprintf("%s-%s", ctrId, strings.ReplaceAll(vm.MountPath, "/", "_")))
5560
if err := t.writeContent(ti, mntPath); err != nil {
5661
return "", errors.Wrapf(err, "write content %s to %s", ti, mntPath)
5762
}

pkg/hostman/guestman/pod.go

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
701697
func (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+
20022075
func 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

20772150
func (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

Comments
 (0)