devpts.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. To support containers, we now allow multiple instances of devpts filesystem,
  2. such that indices of ptys allocated in one instance are independent of indices
  3. allocated in other instances of devpts.
  4. To preserve backward compatibility, this support for multiple instances is
  5. enabled only if:
  6. - CONFIG_DEVPTS_MULTIPLE_INSTANCES=y, and
  7. - '-o newinstance' mount option is specified while mounting devpts
  8. IOW, devpts now supports both single-instance and multi-instance semantics.
  9. If CONFIG_DEVPTS_MULTIPLE_INSTANCES=n, there is no change in behavior and
  10. this referred to as the "legacy" mode. In this mode, the new mount options
  11. (-o newinstance and -o ptmxmode) will be ignored with a 'bogus option' message
  12. on console.
  13. If CONFIG_DEVPTS_MULTIPLE_INSTANCES=y and devpts is mounted without the
  14. 'newinstance' option (as in current start-up scripts) the new mount binds
  15. to the initial kernel mount of devpts. This mode is referred to as the
  16. 'single-instance' mode and the current, single-instance semantics are
  17. preserved, i.e PTYs are common across the system.
  18. The only difference between this single-instance mode and the legacy mode
  19. is the presence of new, '/dev/pts/ptmx' node with permissions 0000, which
  20. can safely be ignored.
  21. If CONFIG_DEVPTS_MULTIPLE_INSTANCES=y and 'newinstance' option is specified,
  22. the mount is considered to be in the multi-instance mode and a new instance
  23. of the devpts fs is created. Any ptys created in this instance are independent
  24. of ptys in other instances of devpts. Like in the single-instance mode, the
  25. /dev/pts/ptmx node is present. To effectively use the multi-instance mode,
  26. open of /dev/ptmx must be a redirected to '/dev/pts/ptmx' using a symlink or
  27. bind-mount.
  28. Eg: A container startup script could do the following:
  29. $ chmod 0666 /dev/pts/ptmx
  30. $ rm /dev/ptmx
  31. $ ln -s pts/ptmx /dev/ptmx
  32. $ ns_exec -cm /bin/bash
  33. # We are now in new container
  34. $ umount /dev/pts
  35. $ mount -t devpts -o newinstance lxcpts /dev/pts
  36. $ sshd -p 1234
  37. where 'ns_exec -cm /bin/bash' calls clone() with CLONE_NEWNS flag and execs
  38. /bin/bash in the child process. A pty created by the sshd is not visible in
  39. the original mount of /dev/pts.
  40. User-space changes
  41. ------------------
  42. In multi-instance mode (i.e '-o newinstance' mount option is specified at least
  43. once), following user-space issues should be noted.
  44. 1. If -o newinstance mount option is never used, /dev/pts/ptmx can be ignored
  45. and no change is needed to system-startup scripts.
  46. 2. To effectively use multi-instance mode (i.e -o newinstance is specified)
  47. administrators or startup scripts should "redirect" open of /dev/ptmx to
  48. /dev/pts/ptmx using either a bind mount or symlink.
  49. $ mount -t devpts -o newinstance devpts /dev/pts
  50. followed by either
  51. $ rm /dev/ptmx
  52. $ ln -s pts/ptmx /dev/ptmx
  53. $ chmod 666 /dev/pts/ptmx
  54. or
  55. $ mount -o bind /dev/pts/ptmx /dev/ptmx
  56. 3. The '/dev/ptmx -> pts/ptmx' symlink is the preferred method since it
  57. enables better error-reporting and treats both single-instance and
  58. multi-instance mounts similarly.
  59. But this method requires that system-startup scripts set the mode of
  60. /dev/pts/ptmx correctly (default mode is 0000). The scripts can set the
  61. mode by, either
  62. - adding ptmxmode mount option to devpts entry in /etc/fstab, or
  63. - using 'chmod 0666 /dev/pts/ptmx'
  64. 4. If multi-instance mode mount is needed for containers, but the system
  65. startup scripts have not yet been updated, container-startup scripts
  66. should bind mount /dev/ptmx to /dev/pts/ptmx to avoid breaking single-
  67. instance mounts.
  68. Or, in general, container-startup scripts should use:
  69. mount -t devpts -o newinstance -o ptmxmode=0666 devpts /dev/pts
  70. if [ ! -L /dev/ptmx ]; then
  71. mount -o bind /dev/pts/ptmx /dev/ptmx
  72. fi
  73. When all devpts mounts are multi-instance, /dev/ptmx can permanently be
  74. a symlink to pts/ptmx and the bind mount can be ignored.
  75. 5. A multi-instance mount that is not accompanied by the /dev/ptmx to
  76. /dev/pts/ptmx redirection would result in an unusable/unreachable pty.
  77. mount -t devpts -o newinstance lxcpts /dev/pts
  78. immediately followed by:
  79. open("/dev/ptmx")
  80. would create a pty, say /dev/pts/7, in the initial kernel mount.
  81. But /dev/pts/7 would be invisible in the new mount.
  82. 6. The permissions for /dev/pts/ptmx node should be specified when mounting
  83. /dev/pts, using the '-o ptmxmode=%o' mount option (default is 0000).
  84. mount -t devpts -o newinstance -o ptmxmode=0644 devpts /dev/pts
  85. The permissions can be later be changed as usual with 'chmod'.
  86. chmod 666 /dev/pts/ptmx
  87. 7. A mount of devpts without the 'newinstance' option results in binding to
  88. initial kernel mount. This behavior while preserving legacy semantics,
  89. does not provide strict isolation in a container environment. i.e by
  90. mounting devpts without the 'newinstance' option, a container could
  91. get visibility into the 'host' or root container's devpts.
  92. To workaround this and have strict isolation, all mounts of devpts,
  93. including the mount in the root container, should use the newinstance
  94. option.