Lab 1: Virtual Technelogy: Kernel-based Virtual Machine (KVM)

實驗描述

本實驗示範如何運用 Kernel-based Virtual Machine (KVM) 建立虛擬叢集環境.

實驗環境

理學大樓 1002 教室
Cloud-A01 ~ Cloud-D12
CPU AMD Phenom™ II X6 1065T Processor
Memory 8G
Disk spaces 500G、500G
O.S. Debian squeeze

理學大樓 821 機房
CSIE-Cloud01 ~ CSIE-Cloud06
CPU AMD Opteron™ Processor 6128 * 2
(total 16 cpu cores)
Memory 32G
Disk spaces 500G、500G、1T
O.S. Debian wheezy
CSIE-Cloud07、CSIE-Cloud08
CPU AMD Opteron™ Processor 6234 * 2
(total 24 cpu cores)
Memory 32G
Disk spaces 500G、500G、1T
O.S. Debian wheezy

實習機器
CSIE-Student*、CSIE-Work*
CPU AMD Opteron™ Processor 6128
(total 4 cpu cores)
Memory 4G
Disk spaces 40G
O.S. Debian wheezy

安裝實作

本實作內容以 step-by-step 方式寫作,盡可能地讓同學們以複製貼上方式完成指令輸入。

練習完本實作之後,同學們應可以 shell script 方式建立虛擬機器。

  1. 安裝 Kernel-based virtual machine, KVM, 前的準備工作.
  2. 使用預先建置好的 image (template) 來進行佈署.
  3. 手動建立 KVM 檔案系統範本
  4. KVM 使用參數
  5. 在 Linux 環境下大多數的指令,我們可以用以下方式來查詢使用方法。

  6. Shell scripts 說明
  7. Resize image
  8. 我們所製作的檔案系統範本大小為 2G,這是許昌旺老師經過多年使用經驗所訂定下來的大小,可以將常用軟體全部放入並且不會浪費太多空間。由於是作為檔案系統範本使用,一切應以精簡為原則。真正使用虛擬機器來提供服務時,2G 就顯得太小家子氣,不敷使用了。這個時候,我們可以使用 image resize 來調整所需大小,以供存放大量資料。

    1. 複製一個映像檔來作 image resizing.
      $ cd ~/KVM/img $ cp DebSqz-Mini.img VM-resized.img
    2. 變更虛擬硬碟的大小
      $ qemu-img resize VM-resized.img 4G
      Image resized.
      $ fdisk -l VM-resized.img
      Disk VM-resized.img: 4294 MB, 4294967296 bytes 255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000d8946 Device Boot Start End Blocks Id System VM-resized.img1 * 2048 999423 498688 83 Linux VM-resized.img2 999424 4192255 1596416 83 Linux
    3. 使用 network block device, nbd 將虛擬硬碟轉換 (connect) 成 block device 形式
      $ sudo modprobe nbd max_part=8 $ ls -l /dev/nbd*
      brw-rw---T 1 root disk 43, 0 Oct 17 16:50 /dev/nbd0 brw-rw---T 1 root disk 43, 16 Oct 17 16:50 /dev/nbd1 brw-rw---T 1 root disk 43, 160 Oct 17 16:50 /dev/nbd10 brw-rw---T 1 root disk 43, 176 Oct 17 16:50 /dev/nbd11 brw-rw---T 1 root disk 43, 192 Oct 17 16:50 /dev/nbd12 brw-rw---T 1 root disk 43, 208 Oct 17 16:50 /dev/nbd13 brw-rw---T 1 root disk 43, 224 Oct 17 16:50 /dev/nbd14 brw-rw---T 1 root disk 43, 240 Oct 17 16:50 /dev/nbd15 brw-rw---T 1 root disk 43, 32 Oct 17 16:50 /dev/nbd2 brw-rw---T 1 root disk 43, 48 Oct 17 16:50 /dev/nbd3 brw-rw---T 1 root disk 43, 64 Oct 17 16:50 /dev/nbd4 brw-rw---T 1 root disk 43, 80 Oct 17 16:50 /dev/nbd5 brw-rw---T 1 root disk 43, 96 Oct 17 16:50 /dev/nbd6 brw-rw---T 1 root disk 43, 112 Oct 17 16:50 /dev/nbd7 brw-rw---T 1 root disk 43, 128 Oct 17 16:50 /dev/nbd8 brw-rw---T 1 root disk 43, 144 Oct 17 16:50 /dev/nbd9
      $ sudo qemu-nbd -c /dev/nbd0 VM-resized.img $ ls -l /dev/nbd0*
      brw-rw---T 1 root disk 43, 0 Oct 17 16:51 /dev/nbd0 brw-rw---T 1 root disk 43, 1 Oct 17 16:51 /dev/nbd0p1 brw-rw---T 1 root disk 43, 2 Oct 17 16:51 /dev/nbd0p2
    4. 調整 Partition table
      1. 基礎版,以圖形化操作介面, Gparted 來作 image resizing.
        • 安裝 Gparted
          $ sudo aptitude update $ sudo aptitude install gparted
        • 使用 Gparted 開啟虛擬硬碟來編輯
          $ gksudo gparted /dev/nbd0

          註:如果開啟時發現圖示顯示不正常,重新開啟一次就會恢復了。


          選擇第二個分割區,按下調整按鈕。


          將分割區容量拉到最大,我們將多出來的空間全部給它。


          確認無誤後按下 Resize/Move


          接著按下 Apply 將變更儲存至硬碟


          $ fdisk -l VM-resized.img
          Disk VM-resized.img: 4294 MB, 4294967296 bytes 255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000d8946 Device Boot Start End Blocks Id System VM-resized.img1 * 2048 999423 498688 83 Linux VM-resized.img2 999424 8388607 3694592 83 Linux
          $ sudo qemu-nbd -d /dev/nbd0
          /dev/nbd0 disconnected
      2. 進階版,以文字模式, sfdisk 來操作.
        • 同樣地,先複製一個 image 來擴充
          $ cp DebSqz-Mini.img VM-cmd-resized.img $ sudo qemu-nbd -c /dev/nbd1 VM-cmd-resized.img $ ls -l /dev/nbd1*
          brw-rw---T 1 root disk 43, 16 Oct 17 17:39 /dev/nbd1 brw-rw---T 1 root disk 43, 160 Oct 17 16:50 /dev/nbd10 brw-rw---T 1 root disk 43, 176 Oct 17 16:50 /dev/nbd11 brw-rw---T 1 root disk 43, 192 Oct 17 16:50 /dev/nbd12 brw-rw---T 1 root disk 43, 208 Oct 17 16:50 /dev/nbd13 brw-rw---T 1 root disk 43, 224 Oct 17 16:50 /dev/nbd14 brw-rw---T 1 root disk 43, 240 Oct 17 16:50 /dev/nbd15 brw-rw---T 1 root disk 43, 17 Oct 17 17:39 /dev/nbd1p1 brw-rw---T 1 root disk 43, 18 Oct 17 17:39 /dev/nbd1p2
        • 檢查硬碟是否有誤,若有則進行修復
          $ sudo e2fsck -f -y -v /dev/nbd1p2
          e2fsck 1.42.5 (29-Jul-2012) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information 25144 inodes used (25.18%, out of 99840) 16 non-contiguous files (0.1%) 38 non-contiguous directories (0.2%) # of inodes with ind/dind/tind blocks: 0/0/0 Extent depth histogram: 22930/4 145850 blocks used (36.54%, out of 399104) 0 bad blocks 1 large file 20517 regular files 2314 directories 12 character device files 25 block device files 2 fifos 411 links 2264 symbolic links (2160 fast symbolic links) 1 socket ------------ 25546 files
          $ echo $?
          0
          $ sudo qemu-nbd -d /dev/nbd1
          /dev/nbd1 disconnected
          $ qemu-img resize VM-cmd-resized.img 4G
        • 查詢硬碟分割資訊,Blocks 為單位顯示。
          $ sfdisk -l -uB VM-cmd-resized.img
          Disk VM-cmd-resized.img: cannot get geometry Disk VM-cmd-resized.img: 522 cylinders, 255 heads, 63 sectors/track Units = blocks of 1024 bytes, counting from 0 Device Boot Start End #blocks Id System VM-cmd-resized.img1 * 1024 499711 498688 83 Linux VM-cmd-resized.img2 499712 2096127 1596416 83 Linux VM-cmd-resized.img3 0 - 0 0 Empty VM-cmd-resized.img4 0 - 0 0 Empty
          $ sfdisk -uB --force VM-cmd-resized.img <<EOF 1024,498688,,+ 499712 0,0; 0,0; EOF
          Warning: VM-cmd-resized.img is not a block device Disk VM-cmd-resized.img: cannot get geometry Disk VM-cmd-resized.img: 522 cylinders, 255 heads, 63 sectors/track Old situation: Units = blocks of 1024 bytes, counting from 0 Device Boot Start End #blocks Id System VM-cmd-resized.img1 * 1024 499711 498688 83 Linux VM-cmd-resized.img2 499712 2096127 1596416 83 Linux VM-cmd-resized.img3 0 - 0 0 Empty VM-cmd-resized.img4 0 - 0 0 Empty New situation: Units = blocks of 1024 bytes, counting from 0 Device Boot Start End #blocks Id System VM-cmd-resized.img1 * 1024 499711 498688 83 Linux VM-cmd-resized.img2 499712 4194303 3694592 83 Linux VM-cmd-resized.img3 0 - 0 0 Empty VM-cmd-resized.img4 0 - 0 0 Empty Warning: partition 1 does not end at a cylinder boundary Warning: partition 2 does not start at a cylinder boundary Warning: partition 2 does not end at a cylinder boundary end of partition 2 has impossible value for cylinders: 522 (should be in 0-521) Successfully wrote the new partition table Re-reading the partition table ... BLKRRPART: Inappropriate ioctl for device If you created or changed a DOS partition, /dev/foo7, say, then use dd(1) to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1 (See fdisk(8).)
          $ echo $?
          0
          $ sudo qemu-nbd -c /dev/nbd1 VM-cmd-resized.img $ ls -l /dev/nbd1p*
          brw-rw---T 1 root disk 43, 17 Oct 17 17:49 /dev/nbd1p1 brw-rw---T 1 root disk 43, 18 Oct 17 17:49 /dev/nbd1p2
          $ sudo e2fsck -f -y -v /dev/nbd1p2
          e2fsck 1.42.5 (29-Jul-2012) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information 25144 inodes used (25.18%, out of 99840) 16 non-contiguous files (0.1%) 38 non-contiguous directories (0.2%) # of inodes with ind/dind/tind blocks: 0/0/0 Extent depth histogram: 22930/4 145850 blocks used (36.54%, out of 399104) 0 bad blocks 1 large file 20517 regular files 2314 directories 12 character device files 25 block device files 2 fifos 411 links 2264 symbolic links (2160 fast symbolic links) 1 socket ------------ 25546 files
        • 使用 resize2fs 調整分割區大小,雖然我們將分割表調大了,但是實際上 superblock 並沒有更新,因此需要重新調整。
          $ sudo resize2fs /dev/nbd1p2
          resize2fs 1.42.5 (29-Jul-2012) Resizing the filesystem on /dev/nbd1p2 to 923648 (4k) blocks. The filesystem on /dev/nbd1p2 is now 923648 blocks long.
          $ sudo qemu-nbd -d /dev/nbd1
          /dev/nbd1 disconnected
    5. 移除不用的 module,節約系統資源。
      $ sudo rmmod nbd

    References

    1. Debian 安裝光碟下載 (下載 debian-6.0.6-amd64-netinst.iso 或是 debian-6.0.6-amd64-xfce+lxde-CD-1.iso )
    2. HPL Note
    3. KVM-tool-Cloud
    4. KVM Main Page
    5. KVM Compilation
    6. KVM Template Creation
    7. KVM Virtio
    8. VDE - Virtual Distributed Ethernet
    9. 實習環境的建置