遠程服務器QEMU+Ubuntu+GRUB+VNC最佳實踐
- 1. 準備
- 2. QEMU啟動安裝Ubuntu
- 2.1 服務器端
- 2.2 本地端
- 3. 從服務器終端控制虛擬機GRUB與虛擬機終端
這段時間參與大量內核切換測試工作,實體機需要硬件自檢太過笨重,因此主要通過QEMU驗證正確性。有一個很大的問題是QEMU運行起來后終端看不到系統的狀態,只能通過VNC連接,這無疑是比較低效的方法。更為重要的是,QEMU搭載在遠程服務器上。下面介紹本人的最佳實踐。
1. 準備
- 遠程服務器:(ip) a.b.c.d
- QEMU
- Ubuntu鏡像
- 本地機器
- ssh
- VNCViewer
2. QEMU啟動安裝Ubuntu
2.1 服務器端
OS_IMG=./ubuntu20.img
qemu-system-x86_64 -m 4G\-hda ${OS_IMG} \-enable-kvm \-cpu host \-device e1000,netdev=net \-netdev user,id=net,hostfwd=tcp::2333-:22 \-smp cpus=20 \-serial stdio \-vnc :2
-netdev ... hostfwd=tcp::2333-:22
:此時a.b.c.d:2333
端口被轉發到虛擬Ubuntu的22端口,意味著可以通過ssh a.b.c.d:2333
連接虛擬機。不過需要虛擬機上安裝openssh-serial stdio
:虛擬機的串口使用服務器的標準輸出(也就是能夠在終端顯示)-vnc :2
:暴露VNC口a.b.c.d:5902
2.2 本地端
此時只能使用VNC連接安裝,如何快速連接遠端VNC口呢?參考
-
在服務器上用qemu制作虛擬機
-
vnc通過ssh隧道連接到Linux服務器
總的來說,首先在本地建立起 ssh隧道
:
ssh -L 5902:localhost:5902 username@a.b.c.d
這代表本地端的5902端口與a.b.c.d:5902
建立起了一條隧道,將本地機的5902端口轉發到遠端機器的5902端口
然后打開VNCReviewer,連接localhost:5902
即可
3. 從服務器終端控制虛擬機GRUB與虛擬機終端
安裝完畢后,重新運行QEMU,終端上仍然在等待VNC連接,沒有任何輸出。參考
- UbuntuHelp:SerialConsoleHowto
遠程連接虛擬機
ssh username@a.b.c.d -p 2333
修改虛擬機配置/etc/default/grub
如下:
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'GRUB_DEFAULT=saved
# GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="maybe-ubiquity"
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"# Uncomment to disable graphical terminal (grub-pc only)
GRUB_TERMINAL="serial"# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"
關鍵點如下:
GRUB_TERMINAL="serial"
,GRUB的輸出通過串口輸出GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
:Ubuntu Linux的輸出通過串口輸出
sudo reboot
后就能夠在服務器終端上看到GRUB菜單以及Ubuntu的輸出信息,然后就可以徹底拋棄VNC了。
OK,從現在開始就可以起飛了🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫🛫