創建虛擬機鏡像
$ qemu-img create -f qcow2 100G.qcow2 100G
$ qemu-img info 100G.qcow2
image: 100G.qcow2
file format: qcow2
virtual size: 100 GiB (107374182400 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
創建的鏡像是一個稀疏文件,也就是說剛創建出來的文件並沒有100G,它會隨着數據的增多慢慢增加
啓動鏡像安裝系統
準備virtio鏡像
到 https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/ 下載驅動。安裝驅動可以在安裝時就指定好驅動文件,後面系統安裝成功後,再安裝下網卡驅動即可。
指定virtio驅動+安裝xp
(XP的安裝程序只支持從軟盤加載驅動程序)
$qemu-system-x86_64
-drive file=./100G.qcow2,format=qcow2,cache=none,if=virtio
-m 1512
-net nic,model=virtio -net user
-fda ./virtio-win_x86.vfd
-cdrom ./xp_professional_with_service_pack_3_x86.iso -boot d
有看到個參數,format=qcow2
,未試過,應該沒什麼問題
$qemu-system-x86_64
-drive file=./100G.qcow2,format=qcow2,cache=none,if=virtio
-m 1512
-net nic,model=virtio -net user
-fda ./virtio-win_x86.vfd
-cdrom ./xp_professional_with_service_pack_3_x86.iso -boot d
其中部分參數說明如下:
-
-cdrom file
和-boot d
指定啓動光盤,並指定啓動順序爲從光盤啓動:
-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)
-boot [order=drives][,once=drives][,menu=on|off] [,splash=sp_name][,splash-time=sp_time][,reboot-timeout=rb_time][,strict=on|off] 'drives': floppy (a), hard disk (c), CD-ROM (d), network (n) 'sp_name': the file's name that would be passed to bios as logo picture, if menu=on 'sp_time': the period that splash picture last if menu=on, unit is ms 'rb_timeout': the timeout before guest reboot when boot failed, unit is ms
-
-m 1512
指定內存的大小
-m [size=]megs[,slots=n,maxmem=size] configure guest RAM size: initial amount of guest memory slots: number of hotplug slots (default: none) maxmem: maximum amount of guest memory (default: none)
啓動後到安裝界面時按F6:
按提示選擇,我們是第一個
…
後面會有個警告提示,按提示操作
安裝後,進入桌面,我的電腦-》設備管理
看下C盤的屬性
可以看到網卡的驅動還是問號,繼續安裝下:
平時啓動
$qemu-system-x86_64
-drive file=./100G.qcow2,format=qcow2,cache=none,if=virtio
-m 1512
-net nic,model=virtio -net user
使用遠程桌面連接
主要使用參數-nographic -vnc :0
注:-vnc與:0之間有空格
$qemu-system-x86_64
-drive file=./100G.qcow2,format=qcow2,cache=none,if=virtio
-m 1512
-net nic,model=virtio -net user
-nographic -vnc :0
使用vncviewer連接就可以了,下圖是我的設置,就一個0,如果上面的命令是1,這邊也是1
遠程桌面連接鼠標位置不準
需要兩個操作
1.將提高指針精確度關閉
2.啓動虛擬機時加上參數-usbdevice tablet
現在啓動命令是
$qemu-system-x86_64
-drive file=./100G.qcow2,format=qcow2,cache=none,if=virtio
-m 1512
-net nic,model=virtio -net user
-nographic -vnc :0
-usbdevice tablet
會好一些,但還有一些偏移就是了
虛擬機關機還原
如果是想試驗某個操作,比如當某個軟件的小白鼠,使用-snapshot
參數:
-snapshot write to temporary files instead of disk image files
$qemu-system-x86_64
-drive file=./100G.qcow2,format=qcow2,cache=none,if=virtio
-m 1512
-net nic,model=virtio -net user
-nographic -vnc :0
-usbdevice tablet
-snapshot
注意,重新啓動後,變化還是在的,是在完全關機後再用命令啓動,就還原成之前的狀態了。
參考
1.https://www.cnblogs.com/jusonalien/p/4772618.html
qemu-kvm磁盤讀寫的緩衝(cache)的五種模式
2.https://blog.csdn.net/jinguangliu/article/details/50661982
磁盤格式