@@ -204,7 +204,7 @@ func prepareRootfs(pipe *syncSocket, iConfig *initConfig) (err error) {
204204 // container. It's just cleaner to do this here (at the expense of the
205205 // operation not being perfectly split).
206206
207- if err := unix .Chdir ( config . Rootfs ); err != nil {
207+ if err := unix .Fchdir ( int ( rootFd . Fd ()) ); err != nil {
208208 return & os.PathError {Op : "chdir" , Path : config .Rootfs , Err : err }
209209 }
210210
@@ -219,7 +219,7 @@ func prepareRootfs(pipe *syncSocket, iConfig *initConfig) (err error) {
219219 if config .NoPivotRoot {
220220 err = msMoveRoot (config .Rootfs )
221221 } else if config .Namespaces .Contains (configs .NEWNS ) {
222- err = pivotRoot (config . Rootfs )
222+ err = pivotRoot (rootFd )
223223 } else {
224224 err = chroot ()
225225 }
@@ -1125,28 +1125,22 @@ func setupPtmx(config *configs.Config) error {
11251125
11261126// pivotRoot will call pivot_root such that rootfs becomes the new root
11271127// filesystem, and everything else is cleaned up.
1128- func pivotRoot (rootfs string ) error {
1128+ func pivotRoot (root * os. File ) error {
11291129 // While the documentation may claim otherwise, pivot_root(".", ".") is
11301130 // actually valid. What this results in is / being the new root but
11311131 // /proc/self/cwd being the old root. Since we can play around with the cwd
11321132 // with pivot_root this allows us to pivot without creating directories in
11331133 // the rootfs. Shout-outs to the LXC developers for giving us this idea.
11341134
1135- oldroot , err := linux .Open ("/" , unix .O_DIRECTORY | unix .O_RDONLY , 0 )
1135+ oldroot , err := linux .Open ("/" , unix .O_DIRECTORY | unix .O_RDONLY | unix . O_PATH , 0 )
11361136 if err != nil {
11371137 return err
11381138 }
11391139 defer unix .Close (oldroot )
11401140
1141- newroot , err := linux .Open (rootfs , unix .O_DIRECTORY | unix .O_RDONLY , 0 )
1142- if err != nil {
1143- return err
1144- }
1145- defer unix .Close (newroot )
1146-
11471141 // Change to the new root so that the pivot_root actually acts on it.
1148- if err := unix .Fchdir (newroot ); err != nil {
1149- return & os.PathError {Op : "fchdir " , Path : "fd " + strconv . Itoa ( newroot ), Err : err }
1142+ if err := unix .Fchdir (int ( root . Fd ()) ); err != nil {
1143+ return & os.PathError {Op : "chdir " , Path : root . Name ( ), Err : err }
11501144 }
11511145
11521146 if err := unix .PivotRoot ("." , "." ); err != nil {
0 commit comments