วิธี shrink/expand disks ใน VirtualBox - Tutorial
อัปเดต: 16 พฤศจิกายน 2009
เราเคยมี tutorials 2 เรื่องเกี่ยวกับ VirtualBox disk management เรียนรู้การ clone disks และเพิ่ม disks ให้ VMs วันนี้เราจะเรียนรู้ shrink disks ที่佔ที่เกินไป หรือ expand disks ที่ free space ต่ำกว่ากลัว

นี่คือ task แรก สมมติคุณสร้าง virtual disk 40GB และ pre-allocated เมื่อสร้าง new virtual storage device คุณมี choice ระหว่าง dynamically expanding disk และ fixed-size disk
หากสร้าง fixed-size disk คุณจะ shrink ได้ หากสร้าง dynamically expanding disk โอกาสที่จะ shrink ได้ต่ำกว่า มาดูก่อนว่าต้องทำอะไรก่อนเปลี่ยนขนาด disk
Tasks ก่อน shrinking
Defragment
Task นี้มีประโยชน์ อาจย้าย dispersed chunks of data ให้เป็น continuous blocks ให้คุณ resize disk ได้ Defragmentation อาจไม่ apply กับบาง filesystems ที่ VM ใช้
Remove junk
ก็มีประโยชน์เช่นกัน Clean temporary files, temporary Internet files, trash bin, installation leftovers, orphaned packages แต่ละ OS ต้องการ approach ต่างกัน
Shrink command
อีกครั้ง เราจะใช้ VBoxManage utility ที่รุ่งเรือง:
VBoxManage modifyhd disk.vdi compact
อะไรคืออะไร?
modifyhd command บอก VBoxManage พยายาม alter disk
disk.vdi คือ target disk ที่คุณอยากทำงาน ก่อนที่จะพยายามเปลี่ยน disk โปรดแน่ใจว่า critical data ภายใน VM ถูก backup อย่างปลอดภัย จริงๆ คุณอาจอยาก clone disk และ test shrinking operation บน copy ก่อน commit changes ไป production setup
compact คือ function ที่บอก VBoxManage พยายาม shrink disk อาจสำเร็จหรือไม่ ขึ้นกับ data ใน disk และการกระจายบน filesystem ตัวอย่างเช่น หาก VM มี 6GB TrueCrypt container ซึ่งถูกมองว่าเป็น raw random data โดย encapsulating filesystems segment นี้ของ disk space ไม่สามารถ compact ได้
มีอีกหนึ่งสิ่งที่ควรระวัง...
Dynamically expanding disks เป็น sparse files ที่ empty storage ใช้พื้นที่น้อยมาก การแปลง virtual disks เป็นรูปแบบอื่นอาจ inflate กลับไปขนาดเดิม ตัวอย่าง หากแปลง disks เป็น RAW format ด้วย utility เช่น QEMU ตามที่แสดงใน Amazon EC2 tutorial พวกมันจะโตกลับไปขนาดเต็ม
จำไว้ เพราะแม้ virtual disk อาจใช้แค่ 1MB ขณะที่ควรเป็น 100GB การ manipulate และ convert จะเปิดไฟล์ให้เต็มขนาด นั่นคือทั้งหมด ผมไม่มี screenshots น่าตื่นเต้น ขออภัย
Expand disks
การขยายดิสก์เป็นงานที่ง่ายกว่า แตซับซ้อนกว่า ง่ายกว่าเพราะคุณจะสำเร็จหากมุ่งมั่น ในขณะที่ shrinking เป็นการเดิมพัน ส่วน expanding ไม่ได้เกิดขึ้นอย่างเวทมนตร์
เพื่อขยาย disk คุณต้อง image มันก่อน หมายถึงสร้าง image ของ OS ในส่วนใหญ่ไม่สามารถทำ in-vivo ได้ ต้องใช้ third-party software เช่น CloneZilla running live session

ขั้นตอนต่อไป สร้าง new disk ใหญ่กว่า และเพิ่มเข้า VM โชคดีที่เราเรียนรู้วิธีทำในบทความล่าสุด

ขั้นตอนที่สาม restore image บน disk ใหญ่กว่า ในที่สุดคุณต้องตั้ง new disk เป็น boot device ครั้งต่อไปที่คุณ power VM ขึ้นอยู่กับ imaging software image จะถูก expanded เพื่อรองรับ new disk หรือคุณจะมี extra free space ที่ปลาย new disk แต่รอ!
Important! โปรดทราบว่าคุณต้อง store image ไว้ที่ไหนสักที่! ดังนั้นคุณต้องการ THIRD storage device ที่จะ backup image ไว้และ restore ภายหลัง ค่อนข้างซับซ้อน ผมรู้
Mini summary
สรุป สำหรับการ expand virtual disks สำเร็จ:
- สร้าง new disk อย่างน้อยขนาดเดิม
- Attach new disk เข้า VM
- Boot เข้า VM และ partition/format new disk
- ใช้ imaging software ที่ชอบ สร้าง image ของ original disk ส่วนใหญ่ imaging software ทำงานจาก live CD แตบางตัว snapshot ระบบขณะใช้งานได้ หมายความว่าคุณต้อง boot VM จาก live CD หรือ ISO image ของ imaging software
- สร้าง image เก็บไว้บน temporary disk
- Power down VM
- Attach third disk เข้า VM ใหญ่กว่าเดิม เพราะอยาก expand VM disk
- Boot อีกครั้งเข้า live session ครั้งนี้ restore image จาก temporary disk ไป third larger disk โปรดทราบ all imaging software require ที่ OS ที่ restore ต้องปิด ดังนั้นต้องใช้ live session อาจจะ dual boot system ก็ได้ แตะไม่นำซับซ้อน
- หลัง restore image เสร็จ power off VM
- Detach temporary storage disk
- เปลี่ยน boot priority ระหว่าง original filled disk และ new larger disk ที่ restore image แล้ว ให้ new disk boot first
- Boot เข้า VM และตรวจสอบว่าทุกอย่างทำงาน พอใจแล้วคุณสามารถ detach first disk หรือ format ใช้เก็บ data ก็ได้
เห็นได้ว่า expand disks เป็น task ที่ serious และซับซ้อนมาก ที่ new users ไม่ควรลอง เว้นแต่ familiar กับ partitioning และ comfortable กับ imaging software หวังว่าคำอธิบายชัดเจนพอ
VirtualBox snapshots
VirtualBox snapshots อาจ interfere กับ shrinking/expanding disks หากเจอปัญหากับ operations นี้ พิจารณาลบ snapshots อีกครั้ง ขณะที่ยัง test setup ให้ทำ operations ทั้งหมดบน cloned machine
Conclusion
Shrinking และ expanding disks เป็น task ซับซ้อนที่เกี่ยวข้องกับ risks มาก เนื่องจาก botched operations อาจทำให้ data loss งานนี้ต้องการ knowledge และ experience และน่ากลัวเท่าเทียมกัน whether physical หรือ virtual disks
สิ่งที่ดีที่สุดคือ plan ahead พยายามทำ setup modular และ scalable เพื่อ exchange bits ของ whole โดยไม่ interrupt daily operation ตัวอย่าง ใช้ separate partitions สำหรับ system และ data แม้แต่ span across multiple virtual disks
พิจารณาเว้น free space หลัง system partition เพื่อ extend ได้ด้วย partitioning software เช่น GParted โดยไม่ต้อง image เหมือนกัน allocate extra space เหนือ minimum requirements ของ vendor เช่น OS ต้องการ 4GB ให้ไป 8GB หรือมากกว่า ไม่รู้ว่าจะต้อง install ใหญ่ใหม่อะไร หากใช้ dynamically expanding disks free space เพิ่มเติมไม่ได้ matter
หวังว่าคู่มือนี้มีประโยชน์ ต่างจากส่วนใหญ่ ไม่มี images มาก แต่ผมคิดไม่ออก simple formula ที่นำเสนอ material ทั้งหมดได้ง่ายโดยไม่ทำให้ article ใหญ่เกินไป หากมีคำถาม ติดต่อมาได้
ในบทความที่สี่ เราจะพูดถึง VirtualBox networking & sharing
Cheers.