如何在 Ubuntu Linux 上使用 KVM 雲鏡像
如何下載並使用運行在 Ubuntu Linux 伺服器上的 KVM 雲鏡像?如何在 Ubuntu Linux 16.04 LTS 伺服器上無需完整安裝即可創建虛擬機?如何在 Ubuntu Linux 上使用 KVM 雲鏡像?
基於內核的虛擬機(KVM)是 Linux 內核的虛擬化模塊,可將其轉變為虛擬機管理程序。你可以在命令行使用 Ubuntu 為 libvirt 和 KVM 提供的虛擬化前端通過 KVM 創建 Ubuntu 雲鏡像。
這個快速教程展示了如何安裝和使用 uvtool,它為 Ubuntu 雲鏡像下載,libvirt 和 clout_int 提供了統一的集成虛擬機前端。
步驟 1 - 安裝 KVM
你必須安裝並配置 KVM。使用 [apt 命令](https://www.cyberciti.biz/faq/ubuntu-lts-debian-linux-apt-command-examples/ "See Linux/Unix apt command examples for more info")/[apt-get 命令](https://www.cyberciti.biz/tips/linux-debian-package-management-cheat-sheet.html "See Linux/Unix apt-get command examples for more info"),如下所示:
$ sudo apt install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker
$ kvm-ok
## [configure bridged networking as described here][3]
$ sudo vi /etc/network/interfaces
$ sudo systemctl restart networking
$ sudo brctl show
參閱如何在 Ubuntu 16.04 LTS Headless 伺服器上安裝 KVM 以獲得更多信息。(LCTT 譯註:Headless 伺服器是指沒有本地介面的計算設備,專用於向其他計算機及其用戶提供服務。)
步驟 2 - 安裝 uvtool
鍵入以下 [apt 命令](https://www.cyberciti.biz/faq/ubuntu-lts-debian-linux-apt-command-examples/ "See Linux/Unix apt command examples for more info")/[apt-get 命令](https://www.cyberciti.biz/tips/linux-debian-package-management-cheat-sheet.html "See Linux/Unix apt-get command examples for more info"):
$ sudo apt install uvtool
示例輸出:
[sudo] password for vivek:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gksu libgksu2-0 libqt5designer5 libqt5help5 libqt5printsupport5 libqt5sql5 libqt5sql5-sqlite libqt5xml5 python3-dbus.mainloop.pyqt5 python3-notify2 python3-pyqt5 python3-sip
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
cloud-image-utils distro-info python-boto python-pyinotify python-simplestreams socat ubuntu-cloudimage-keyring uvtool-libvirt
Suggested packages:
cloud-utils-euca shunit2 python-pyinotify-doc
The following NEW packages will be installed:
cloud-image-utils distro-info python-boto python-pyinotify python-simplestreams socat ubuntu-cloudimage-keyring uvtool uvtool-libvirt
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,211 kB of archives.
After this operation, 6,876 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu artful/main amd64 distro-info amd64 0.17 [20.3 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 python-boto all 2.44.0-1ubuntu2 [740 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu artful/main amd64 python-pyinotify all 0.9.6-1 [24.6 kB]
Get:4 http://in.archive.ubuntu.com/ubuntu artful/main amd64 ubuntu-cloudimage-keyring all 2013.11.11 [4,504 B]
Get:5 http://in.archive.ubuntu.com/ubuntu artful/main amd64 cloud-image-utils all 0.30-0ubuntu2 [17.2 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 python-simplestreams all 0.1.0~bzr450-0ubuntu1 [29.7 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 socat amd64 1.7.3.2-1 [342 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 uvtool all 0~git122-0ubuntu1 [6,498 B]
Get:9 http://in.archive.ubuntu.com/ubuntu artful/universe amd64 uvtool-libvirt all 0~git122-0ubuntu1 [26.9 kB]
Fetched 1,211 kB in 3s (393 kB/s)
Selecting previously unselected package distro-info.
(Reading database ... 199933 files and directories currently installed.)
Preparing to unpack .../0-distro-info_0.17_amd64.deb ...
Unpacking distro-info (0.17) ...
Selecting previously unselected package python-boto.
Preparing to unpack .../1-python-boto_2.44.0-1ubuntu2_all.deb ...
Unpacking python-boto (2.44.0-1ubuntu2) ...
Selecting previously unselected package python-pyinotify.
Preparing to unpack .../2-python-pyinotify_0.9.6-1_all.deb ...
Unpacking python-pyinotify (0.9.6-1) ...
Selecting previously unselected package ubuntu-cloudimage-keyring.
Preparing to unpack .../3-ubuntu-cloudimage-keyring_2013.11.11_all.deb ...
Unpacking ubuntu-cloudimage-keyring (2013.11.11) ...
Selecting previously unselected package cloud-image-utils.
Preparing to unpack .../4-cloud-image-utils_0.30-0ubuntu2_all.deb ...
Unpacking cloud-image-utils (0.30-0ubuntu2) ...
Selecting previously unselected package python-simplestreams.
Preparing to unpack .../5-python-simplestreams_0.1.0~bzr450-0ubuntu1_all.deb ...
Unpacking python-simplestreams (0.1.0~bzr450-0ubuntu1) ...
Selecting previously unselected package socat.
Preparing to unpack .../6-socat_1.7.3.2-1_amd64.deb ...
Unpacking socat (1.7.3.2-1) ...
Selecting previously unselected package uvtool.
Preparing to unpack .../7-uvtool_0~git122-0ubuntu1_all.deb ...
Unpacking uvtool (0~git122-0ubuntu1) ...
Selecting previously unselected package uvtool-libvirt.
Preparing to unpack .../8-uvtool-libvirt_0~git122-0ubuntu1_all.deb ...
Unpacking uvtool-libvirt (0~git122-0ubuntu1) ...
Setting up distro-info (0.17) ...
Setting up ubuntu-cloudimage-keyring (2013.11.11) ...
Setting up cloud-image-utils (0.30-0ubuntu2) ...
Setting up socat (1.7.3.2-1) ...
Setting up python-pyinotify (0.9.6-1) ...
Setting up python-boto (2.44.0-1ubuntu2) ...
Setting up python-simplestreams (0.1.0~bzr450-0ubuntu1) ...
Processing triggers for doc-base (0.10.7) ...
Processing 1 added doc-base file...
Setting up uvtool (0~git122-0ubuntu1) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up uvtool-libvirt (0~git122-0ubuntu1) ...
步驟 3 - 下載 Ubuntu 雲鏡像
你需要使用 uvt-simplestreams-libvirt
命令。它維護一個 libvirt 容量存儲池,作為一個 簡單流 源的鏡像子集的本地鏡像,比如 Ubuntu 雲鏡像。要使用當前所有 amd64 鏡像更新 uvtool 的 libvirt 容量存儲池,運行:
$ uvt-simplestreams-libvirt sync arch=amd64
要更新/獲取 Ubuntu 16.04 LTS (xenial/amd64) 鏡像,運行:
$ uvt-simplestreams-libvirt --verbose sync release=xenial arch=amd64
示例輸出:
Adding: com.ubuntu.cloud:server:16.04:amd64 20171121.1
通過 query 選項查詢本地鏡像:
$ uvt-simplestreams-libvirt query
示例輸出:
release=xenial arch=amd64 label=release (20171121.1)
現在,我為 Ubuntu xenial 創建了一個鏡像,接下來我會創建虛擬機。
步驟 4 - 創建 SSH 密鑰
你需要使用 SSH 密鑰才能登錄到 KVM 虛擬機。如果你根本沒有任何密鑰,請使用 ssh-keygen
命令創建一個新的密鑰。
$ ssh-keygen
參閱「如何在 Linux / Unix 系統上設置 SSH 密鑰」 和 「Linux / UNIX: 生成 SSH 密鑰」 以獲取更多信息。
步驟 5 - 創建 VM
是時候創建虛擬機了,它叫 vm1,即創建一個 Ubuntu Linux 16.04 LTS 虛擬機:
$ uvt-kvm create vm1
默認情況下 vm1 使用以下配置創建:
- 內存:512M
- 磁碟大小:8GiB
- CPU:1 vCPU core
要控制內存、磁碟、CPU 和其他配置,使用以下語法:
$ uvt-kvm create vm1
--memory MEMORY
--cpu CPU
--disk DISK
--bridge BRIDGE
--ssh-public-key-file /path/to/your/SSH_PUBLIC_KEY_FILE
--packages PACKAGES1, PACKAGES2, ..
--run-script-once RUN_SCRIPT_ONCE
--password PASSWORD
其中
--password PASSWORD
:設置 ubuntu 用戶的密碼和允許使用 ubuntu 的用戶登錄(不推薦,使用 ssh 密鑰)。--run-script-once RUN_SCRIPT_ONCE
: 第一次啟動時,在虛擬機上以 root 身份運行RUN_SCRIPT_ONCE
腳本,但再也不會運行。這裡給出完整的路徑。這對於在虛擬機上運行自定義任務時非常有用,例如設置安全性或其他內容。--packages PACKAGES1, PACKAGES2, ..
: 在第一次啟動時安裝以逗號分隔的軟體包。
要獲取幫助,運行:
$ uvt-kvm -h
$ uvt-kvm create -h
如何刪除虛擬機?
要銷毀/刪除名為 vm1 的虛擬機,運行(請小心使用以下命令,因為沒有確認框):
$ uvt-kvm destroy vm1
獲取 vm1 的 IP 地址,運行:
$ uvt-kvm ip vm1
192.168.122.52
列出所有運行的虛擬機
$ uvt-kvm list
示例輸出:
vm1
freebsd11.1
步驟 6 - 如何登錄 vm1
語法是:
$ uvt-kvm ssh vm1
示例輸出:
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-101-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
0 packages can be updated.
0 updates are security updates.
Last login: Thu Dec 7 09:55:06 2017 from 192.168.122.1
另一個選擇是從 macOS/Linux/Unix/Windows 客戶端使用常規的 ssh 命令:
$ ssh ubuntu@192.168.122.52
$ ssh -i ~/.ssh/id_rsa ubuntu@192.168.122.52
示例輸出:
一旦創建了 vim,你可以照常使用 virsh
命令:
$ virsh list
via: https://www.cyberciti.biz/faq/how-to-use-kvm-cloud-images-on-ubuntu-linux/
作者:Vivek Gite 譯者:MjSeven 校對:wxy
本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive