วัน: 8 เมษายน 2017

WSL: mount linux diskWSL: mount linux disk

วิธีทำให้ WSL: Windows Subsystem for Linux มองเห็น hard disk ที่สร้างไว้เป็น Linux partition และสามารถใช้ได้เหมือนกับว่าเป็น disk ของตัว windows เอง

  1. เริ่มจาก list hard disk โดยใช้ Windows PowerShell as an Administrator

    GET-CimInstance -query "SELECT * from Win32_DiskDrive"

    จะเห็นรายชื่อ hard disk ที่มีอยู่ในเครื่อง เช่น \\.\PHYSICALDRIVE1 WD My Book 25EE USB Device 1 0659787734 WD My Book 25EE USB Device . ให้ดูที่ DeviceID ในบรรทัดนี้คือ \\.\PHYSICALDRIVE1 คือ <DiskPath> ที่จะนำมาใช้ อ้างอิง
  2. mount disk โดยรูปแบบ wsl –mount <DiskPath> เช่น

    wsl --mount \\.\PHYSICALDRIVE1 --bare

    บางครั้ง จะมี error
    The disk ‘\\.\PHYSICALDRIVE1‘ is already attached.
    Error code: Wsl/Service/AttachDisk/WSL_E_DISK_ALREADY_ATTACHED
    เกิดจาการที่ hard disk ลูกนี้เคย mount เอาไว้แล้ว
  3. ทดลอง list hard disk และ partition ใน Linux command

    lsblk

    จะเห็น partition name เช่น sda, sdb, sdc, sdd
  4. mount partition แรกใน Windows PowerShell as an Administrator โดยคำสั่ง

    wsl --mount \\.\PHYSICALDRIVE1 --partition 1

    จะมีข้อความตอบกลับมา เช่น

    The disk was successfully mounted as ‘/mnt/wsl/PHYSICALDRIVE1p1‘.
    Note: The location will be different if you have modified the automount.root setting in /etc/wsl.conf.
    To unmount and detach the disk, run ‘wsl.exe –unmount \\.\PHYSICALDRIVE1‘.
  5. ถ้า wsl ของเราชื่อ Ubuntu และ path ที่ mount ไว้คือ
    /mnt/wsl/PHYSICALDRIVE1p1
    เข้าไปดู ไปใช้ไฟล์ได้ใน
    \\wsl.localhost\Ubuntu\mnt\wsl\PHYSICALDRIVE1p1
  6. เมื่อใช้เสร็จแล้ว ควรเอาออกไปโดยใช้คำสั่ง wsl –unmount <DiskPath> เช่น

    wsl --unmount \\.\PHYSICALDRIVE1

ในครั้งต่อไป ถ้าทราบ แล้ว สามารถเริ่มที่ ขั้นตอนที่ 4 ได้เลยครับ หรือถ้าใช้บ่อย ๆ ก็ระบุชื่อโดยใช้

wsl --mount --name myDisk
* ใช่ – – สองตัวทุกจุดนะครับ ไม่ใช่ – เดียว
อ่านเพิ่มเติม