Skip to content

Commit 47370e2

Browse files
authored
Use supervisor mount namespace for mount (#20144)
1 parent 25b84bc commit 47370e2

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

components/ws-daemon/pkg/iws/iws.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,15 @@ func (wbs *InWorkspaceServiceServer) MountSysfs(ctx context.Context, req *api.Mo
709709

710710
func (wbs *InWorkspaceServiceServer) MountNfs(ctx context.Context, req *api.MountNfsRequest) (resp *api.MountNfsResponse, err error) {
711711
var (
712-
reqPID = req.Pid
713-
nfsPID uint64
712+
reqPID = req.Pid
713+
supervisorPID uint64
714714
)
715715
defer func() {
716716
if err == nil {
717717
return
718718
}
719719

720-
log.WithError(err).WithFields(wbs.Session.OWI()).WithField("procPID", nfsPID).WithField("reqPID", reqPID).WithFields(wbs.Session.OWI()).Error("cannot mount nfs")
720+
log.WithError(err).WithFields(wbs.Session.OWI()).WithField("procPID", supervisorPID).WithField("reqPID", reqPID).WithFields(wbs.Session.OWI()).Error("cannot mount nfs")
721721
if _, ok := status.FromError(err); !ok {
722722
err = status.Error(codes.Internal, "cannot mount nfs")
723723
}
@@ -737,9 +737,9 @@ func (wbs *InWorkspaceServiceServer) MountNfs(ctx context.Context, req *api.Moun
737737
return nil, xerrors.Errorf("cannot find container PID for containerID %v: %w", wscontainerID, err)
738738
}
739739

740-
nfsPID, err = wbs.Uidmapper.findHostPID(containerPID, uint64(req.Pid))
740+
supervisorPID, err = wbs.Uidmapper.findSupervisorPID(containerPID)
741741
if err != nil {
742-
return nil, xerrors.Errorf("cannot map in-container PID %d (container PID: %d): %w", req.Pid, containerPID, err)
742+
return nil, xerrors.Errorf("cannot map supervisor PID %d (container PID: %d): %w", req.Pid, containerPID, err)
743743
}
744744

745745
nodeStaging, err := os.MkdirTemp("", "nfs-staging")
@@ -774,7 +774,7 @@ func (wbs *InWorkspaceServiceServer) MountNfs(ctx context.Context, req *api.Moun
774774
}
775775
}
776776

777-
err = moveMount(wbs.Session.InstanceID, int(nfsPID), nodeStaging, req.Target)
777+
err = moveMount(wbs.Session.InstanceID, int(supervisorPID), nodeStaging, req.Target)
778778
if err != nil {
779779
return nil, err
780780
}

components/ws-daemon/pkg/iws/uidmap.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ func (m *Uidmapper) findHostPID(containerPID, inContainerPID uint64) (uint64, er
174174
seen[p] = struct{}{}
175175

176176
p = filepath.Join(m.Config.ProcLocation, p)
177-
178177
pid, nspid, err := readStatusFile(filepath.Join(p, "status"))
179178
if err != nil {
180179
log.WithField("file", filepath.Join(p, "status")).WithError(err).Error("findHostPID: cannot read PID file")
@@ -201,6 +200,54 @@ func (m *Uidmapper) findHostPID(containerPID, inContainerPID uint64) (uint64, er
201200
}
202201
}
203202

203+
func (m *Uidmapper) findSupervisorPID(containerPID uint64) (uint64, error) {
204+
paths := []string{fmt.Sprint(containerPID)}
205+
seen := make(map[string]struct{})
206+
207+
for {
208+
if len(paths) == 0 {
209+
return 0, xerrors.Errorf("cannot find supervisor PID for container %v", containerPID)
210+
}
211+
212+
p := paths[0]
213+
paths = paths[1:]
214+
215+
if _, ok := seen[p]; ok {
216+
continue
217+
}
218+
seen[p] = struct{}{}
219+
220+
procPath := filepath.Join(m.Config.ProcLocation, p)
221+
cmdline, err := os.ReadFile(filepath.Join(procPath, "cmdline"))
222+
if err != nil {
223+
log.WithField("file", filepath.Join(procPath, "cmdline")).WithError(err).Error("cannot read cmdline")
224+
continue
225+
}
226+
227+
if strings.HasPrefix(string(cmdline), "supervisor") {
228+
pid, err := strconv.ParseUint(p, 10, 64)
229+
if err != nil {
230+
return 0, err
231+
}
232+
233+
return pid, nil
234+
}
235+
236+
taskfn := filepath.Join(procPath, "task")
237+
tasks, err := os.ReadDir(taskfn)
238+
if err != nil {
239+
continue
240+
}
241+
for _, task := range tasks {
242+
cldrn, err := os.ReadFile(filepath.Join(taskfn, task.Name(), "children"))
243+
if err != nil {
244+
continue
245+
}
246+
paths = append(paths, strings.Fields(string(cldrn))...)
247+
}
248+
}
249+
}
250+
204251
func readStatusFile(fn string) (pid uint64, nspid []uint64, err error) {
205252
f, err := os.Open(fn)
206253
if err != nil {

0 commit comments

Comments
 (0)