kvm 虛擬化安裝 Ubuntu 18.04 server


本文續上篇 《裸金屬服務器 kvm 虛擬化安裝 win10》繼續記錄使用 KVMCentos 7.5 裸金屬服務器上安裝 Ubuntu 18.04 Server的過程及遇到的問題。

資源准備

首先要配置好 KVM 環境,上文已經詳細說明,磁盤配置類似的使用 qemu-img create -f qcow2 ubuntu.qcow2 100G 命令創建虛擬磁盤,系統鏡像可以去官網下載:

Ubuntu 官網: https://ubuntu.com/
Ubuntu Server 18.04 LTS 下載: https://ubuntu.com/download/server/thank-you?country=HK&version=18.04.3&architecture=amd64

Verify your download

# Run this command in your terminal in the directory the iso was downloaded to verify the SHA256 checksum:

$ echo "b9beac143e36226aa8a0b03fc1cbb5921cff80123866e718aaeba4edb81cfa63 *ubuntu-18.04.3-live-server-amd64.iso" | shasum -a 256 --check

# You should get the following output:

ubuntu-18.04.3-live-server-amd64.iso: OK

網絡配置繼續使用 KVM 默認的 default nat 方式。

安裝

$ virt-install \
--virt-type=kvm \
--name=nextcloud \
--hvm \
--vcpus=2 \
--memory=2048 \
--cdrom=/srv/kvm/nextcloud/ubuntu-18.04.3-live-server-amd64.iso \
--disk path=/srv/kvm/nextcloud/nextcloud.qcow2,size=500,format=qcow2 \
--network bridge=br0 \
--graphics vnc,password=kvmwin10,listen=::,port=5911 \
--autostart \
--force

安裝成功后使用任意一個可以接入互聯網的帶有桌面換的設備上的 VNC viewer 進入 YourIp:5911 輸入密碼 kvmwin10,就可以進入虛擬機,然后繼續安裝了。

Virsh 基礎命令

virsh list --all           # 查看所有運行和沒有運行的虛擬機
virsh list                 # 查看在運行的虛擬機
virsh dumpxml vm-name      # 查看kvm虛擬機配置文件
virsh start vm-name        # 啟動kvm虛擬機
virsh shutdown vm-name     # 正常關機

virsh destroy vm-name      # 非正常關機,強制關閉虛擬機(相當於物理機直接拔掉電源)
virsh undefine vm-name     # 刪除vm的配置文件

ls /etc/libvirt/qemu
# 查看刪除結果,Centos-6.6的配置文件被刪除,但磁盤文件不會被刪除

virsh define file-name.xml # 根據配置文件定義虛擬機
virsh suspend vm-name      # 掛起,終止
virsh resumed vm-name      # 恢復被掛起的虛擬機
virsh autostart vm-name    # 開機自啟動vm
virsh console <虛擬機名稱>   # 連接虛擬機

-install 常用參數說明

–name指定虛擬機名稱
–memory分配內存大小。
–vcpus分配CPU核心數,最大與實體機CPU核心數相同
–disk指定虛擬機鏡像,size指定分配大小單位為G。
–network網絡類型,此處用的是默認,一般用的應該是bridge橋接。
–accelerate加速
–cdrom指定安裝鏡像iso
–vnc啟用VNC遠程管理,一般安裝系統都要啟用。
–vncport指定VNC監控端口,默認端口為5900,端口不能重復。
–vnclisten指定VNC綁定IP,默認綁定127.0.0.1,這里改為0.0.0.0。
–os-type=linux,windows
–os-variant=rhel6

--name      指定虛擬機名稱
--ram       虛擬機內存大小,以 MB 為單位
--vcpus     分配CPU核心數,最大與實體機CPU核心數相同
–-vnc       啟用VNC遠程管理,一般安裝系統都要啟用。
–-vncport   指定VNC監控端口,默認端口為5900,端口不能重復。
–-vnclisten  指定VNC綁定IP,默認綁定127.0.0.1,這里改為0.0.0.0。
--network   虛擬機網絡配置
  # 其中子選項,bridge=br0 指定橋接網卡的名稱。

–os-type=linux,windows
–os-variant=rhel7.2

--disk 指定虛擬機的磁盤存儲位置
  # size,初始磁盤大小,以 GB 為單位。

--location 指定安裝介質路徑,如光盤鏡像的文件路徑。
--graphics 圖形化顯示配置
  # 全新安裝虛擬機過程中可能會有很多交互操作,比如設置語言,初始化 root 密碼等等。
  # graphics 選項的作用就是配置圖形化的交互方式,可以使用 vnc(一種遠程桌面軟件)進行鏈接。
  # 我們這列使用命令行的方式安裝,所以這里要設置為 none,但要通過 --extra-args 選項指定終端信息,
  # 這樣才能將安裝過程中的交互信息輸出到當前控制台。
--extra-args 根據不同的安裝方式設置不同的額外選項

Ubuntu 固定ip

由於安裝 Ubuntu 是為了做某應用的服務,因此需要外網訪問,使用 Nat 映射端口需要固定 ip。

首先,您需要確定要配置的網絡接口。 您可以使用ifconfig命令列出系統中所有連接的網絡接口。

$ ifconfig -a

Ubuntu設置靜態IP地址

$ sudo vim /etc/netplan/xxxx.ymal

修改這個文件:

network:
  ethernets:
    ens33:
      addresses:
      - 192.168.122.3/24
      dhcp4: false
      gateway4: 192.168.122.1
      nameservers:
        addresses:
        - 192.168.122.1
        search: []
  version: 2

說明:

# ens33:網絡接口名稱
# dhcp4:接收IPV4接口的dhcp屬性
# dhcp6:接收IPV6接口的dhcp屬性
# addresses:接口的靜態地址序列
# gateway4:默認網關的IPV4地址
# Nameservers:DNS服務器地址,以,號分割

小提示

如果在 vim 中誤觸,可以使用 u撤銷一次操作.
u   撤銷上一步的操作
Ctrl+r 恢復上一步被撤銷的操作

保存該文件並退出。然后使用以下netplan命令應用最近的網絡更改。

$ sudo netplan apply

現在再次驗證所有可用的網絡接口,ens33以太網接口現在應連接到本地網絡,並具有IP地址.

ifconfig -a

至此,Ubuntu 虛擬機安裝完成, 進入 VNC 繼續后續操作就好。

彩蛋

瀏覽參考文獻時發現一個有意思的文章《使用virt-install安裝虛擬機,發行版安裝代碼直接復制運行》,在這里記錄一下,有時間試試。

Debian 8
virt-install \
--name debian8 \
--ram 1024 \
--disk path=./debian8.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.nl.debian.org/debian/dists/jessie/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
Debian 7
virt-install \
--name debian7 \
--ram 1024 \
--disk path=./debian7.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant debian7 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.nl.debian.org/debian/dists/jessie/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
Debian 6
virt-install \
--name debian6 \
--ram 1024 \
--disk path=./debian6.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant debian6 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.nl.debian.org/debian/dists/squeeze/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
CentOS 7
virt-install \
--name centos7 \
--ram 1024 \
--disk path=./centos7.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant centos7 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirror.i3d.net/pub/centos/7/os/x86_64/' \
--extra-args 'console=ttyS0,115200n8 serial'
CentOS 6
virt-install \
--name centos6 \
--ram 1024 \
--disk path=./centos6.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant centos6 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirror.i3d.net/pub/centos/6/os/x86_64/' \
--extra-args 'console=ttyS0,115200n8 serial'
CentOS 5
virt-install \
--name centos5 \
--ram 1024 \
--disk path=./centos5.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant centos5 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirror.i3d.net/pub/centos/5/os/x86_64/' \
--extra-args 'console=ttyS0,115200n8 serial'
Ubuntu 14.04
virt-install \
--name ubuntu1404 \
--ram 1024 \
--disk path=./ubuntu1404.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
Ubuntu 12.04
virt-install \
--name ubuntu1204 \
--ram 1024 \
--disk path=./ubuntu1204.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant ubuntu12.04 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://archive.ubuntu.com/ubuntu/dists/precise/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
Ubuntu 10.04
virt-install \
--name ubuntu1004 \
--ram 1024 \
--disk path=./ubuntu1004.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant ubuntu10.04 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://archive.ubuntu.com/ubuntu/dists/lucid/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
OpenSUSE 13
virt-install \
--name opensuse13 \
--ram 1024 \
--disk path=./opensuse13.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://download.opensuse.org/distribution/13.2/repo/oss/' \
--extra-args 'console=ttyS0,115200n8 serial'
OpenSUSE 12
virt-install \
--name opensuse12 \
--ram 1024 \
--disk path=./opensuse12.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://download.opensuse.org/distribution/12.3/repo/oss/' \
--extra-args 'console=ttyS0,115200n8 serial'
OpenSUSE 11
virt-install \
--name opensuse11 \
--ram 1024 \
--disk path=./opensuse11.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://download.opensuse.org/distribution/11.4/repo/oss/' \
--extra-args 'console=ttyS0,115200n8 serial'
Generic ISO
Download an ISO file and give the filename to the --cdrom= parameter. This is used instead of --location. A VNC console is available on localhost, port 5999 for you to use.

An example for FreeBSD 10. First download the ISO:

wget http://ftp.freebsd.org/pub/FreeBSD/releases/ISO-IMAGES/10.1/FreeBSD-10.1-RELEASE-amd64-dvd1.iso
Then start virt-install:

 virt-install \
--name freebsd10 \
--ram 1024 \
--disk path=./freebsd10.qcow2,size=8 \
--vcpus 1 \
--os-type generic \
--os-variant generic \
--network bridge=virbr0 \
--graphics vnc,port=5999 \
--console pty,target_type=serial \
--cdrom ./FreeBSD-10.1-RELEASE-amd64-dvd1.iso \
You need to start up a VNC client to do the installation.

參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM