Podman unshare ทำไมต้องไม่แบ่งปันหละ

สำหรับ Feature นี้ใช้ได้เฉพาะกรณีที่ run container แบบ rootless mode ครับ ถ้าใช้ root คำสั่งนี้จะไม่สามารถทำงานได้นะ แต่งาน Run Container แบบ Non-Root มีข้อดี และข้่อจำกัด ตาม Blog นีครับ

ในเมื่อมันมีข้อจำกัดขนาดนี้ แล้ว unshare มาข่วยอะไรหละ ?

  • unshare จริงๆแล้วงเป็น Feature ที่มากับตัว Linux Kernel ที่ช่วยให้เราแยก process ลูกให้มีอิสระจาก process แม่ (Parent Process) ภายใต้พื้นที่ที่กำหนด ซึ่งเรียกว่า namespace ครับ อารมณ์เหมือนตั้งห้องย่อยใหม่ แล้วเราเป็น admin แหละครับ แต่ไม่ใหญ่ไปกว่า root

แล้วที่นี้ตัว podman มันเอาความสามารถ unshare มาใช้งานต่อ โดยเหมือนเป็นกุญแจเอาไว้เข้า user namespace ของ container หรือ rootless environment ที่ Podman สร้างขึ้นได้

ใน environment นี้ เราจะมีสิทธิ์เหมือนกับ root ภายใน namespace (แม้จะไม่ใช่ root จริง ๆ ของระบบ) และแน่นอนต้องรันบน Server นะ ถ้าใช้ Podman client มันจะทำไม่ได้ โดยประโยชน์ของมัน

  • เหมาะสำหรับใช้รันคำสั่งที่ปกติจะต้องใช้สิทธิ์ root (เช่น mount, chown, ... ) กับไฟล์หรือโฟลเดอร์ที่อยู่ใน namespace ของ Podman
# chown example
podman unshare chown -R 1000:1000 /path/to/volume

# mount example
podman unshare mount -t tmpfs none /path/to/mountpoint
  • หรือ ใช้ในการแก้ไข permission ของไฟล์/volume ที่ถูก map ระหว่าง container กับ host
  • หรือ troubleshooting unprivileged operations เป็นต้น
# 1 Check Container Id
podman ps

# 2 Get Path for troubleshooting
container_mount=$(podman mount <container-id>)

# 3 Set new room for debugging / troubleshooting
podman unshare

# 4 Go to new room
cd $container_mount

# 5 Check bla bla
ls -l /path/to/file/or/directory

# 6 restore 
podman umount <container-id>

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.