# Desktop版-透過 QEMU/KVM 來建構 Debian/Ubuntu Linux 為基礎的開發和實驗環境 ###### tags: `linux2020` `C` `docker` contributed by <```owlfox```> :::info 測試方式: 1. 依據網頁說明,逐步安裝 KVM 和 Ubuntu: https://graspingtech.com/ubuntu-desktop-18.04-virtual-machine-macos-qemu/ 2. 確保 QEMU 正確使用加速器,即 -accel hvf 選項; 3. 確認在 KVM 裡頭執行的 Ubuntu Linux 可透過 macOS (即 host 端) 存取網際網路; 將 Ubuntu Desktop 換為 Ubuntu Server Image 19.10, 並重複上述第 1 到 3 步驟; => https://ubuntu.com/download/server ::: :::info 以下操作重現 GRASPINGTECH 的 [tutorial](https://graspingtech.com/ubuntu-desktop-18.04-virtual-machine-macos-qemu/) ::: >我的實驗環境: MacBookPro >macOS Mojave Version 10.14.6 [name=unknowntpo] ### 步驟 1 : 透過 [Homebrew](https://brew.sh/) 安裝 [QEMU](https://www.qemu.org/) ```shell $ brew install qemu ``` :::info :notebook: 本文以 `$` 開頭的字串皆表示由使用者輸入到 macOS 或 GNU/Linux 終端機的命令 ::: ### 步驟 2: 下載 Ubuntu Linux - [Ubuntu_Desktop_18.04] 映像檔 [Ubuntu_Desktop_18.04]: http://releases.ubuntu.com/18.04/ 在 ```~``` 目錄下建立 QEMU 目錄 ```shell $ mkdir ~/QEMU ``` 並將事先下載的映像檔搬動到 QEMU 目錄中 #### 步驟 3: 確認 QEMU 正確安裝 ```shell $ qemu-system-x86_64 --version ``` 預期 QEMU 程式輸出: ``` QEMU emulator version 4.2.0 Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers ``` > 目前 (2020.1.18) 版本為 4.2.0 版 ### 步驟 4: 建立 QEMU 可讀取的磁碟映像檔 ```shell $ qemu-img create -f qcow2 ~/QEMU/ubuntu-desktop-18.04.qcow2 10G ``` :::info 10G 的部分 可依需求自行調整成 15G, 20G 等等 ::: 確認本步驟後已現稍早建立的 qcow2 格式檔案: ```shell $ ls ubuntu-18.04.3-desktop-amd64.iso ubuntu-desktop-18.04.qcow2 ``` ### 步驟 5: 執行 QEMU 並掛載 Ubuntu ISO 檔案 ```shell $ qemu-system-x86_64 \ -m 2048 \ -vga virtio \ -show-cursor \ -usb \ -device usb-tablet \ -enable-kvm \ -cdrom ~/QEMU/ubuntu-18.04.3-desktop-amd64.iso \ -drive file=~/QEMU/ubuntu-desktop-18.04.qcow2,if=virtio \ -accel hvf \ -cpu host ``` 參數說明 ([qemu 文件](https://qemu.weilnetz.de/doc/qemu-doc.html)): * `-m`: 記憶體大小 * `-accel hvf`: 要求 QEMU 使用 macOS 的 [hypervisor 加速器](https://developer.apple.com/documentation/hypervisor) * 硬體需求是支援 VT-x 特徵的 Intel 處理器上 * 可在 macOS 終端機內執行 `$ sysctl kern.hv_support` 命令,若看到 `kern.hv_support: 1` 輸出,就表示 hvf 可用 * `-cdrom`: 指定以 CD-ROM 的方式掛載 ISO 檔 ### 步驟 5-1: 安裝 Ubuntu Linux 以下操作在 QEMU 所建立的虛擬機器環境中: 1. Select your language and keyboard layout. 2. Select Minimal installation and then click Continue. 3. Select Erase disk and install Ubuntu then click Install Now. 4. Specify your username, machine name and password then click Continue. 5. Wait for the installation to complete then click Restart Now. 6. When asked to remove the installation medium, power off the machine and in the next step we’ll adjust the command to power on the VM without the CD-ROM attached. 7. 當螢幕顯示 Please remove your installation medium, then press ENTER 時,將電腦重新開機(不是只關掉Virtual Machine!是整台電腦重開機),並接續步驟 6. ### 步驟 6: 重新執行 QEMU (不掛載 ISO 檔案) > 也就是重做步驟 3,但是命令列選項移去 `-cdrom`,因為 Ubuntu Linux 已安裝在指定的儲存空間 ```shell $ qemu-system-x86_64 \ -m 2048 \ -vga virtio \ -show-cursor \ -usb \ -device usb-tablet \ -enable-kvm \ -drive file=~/QEMU/ubuntu-desktop-18.04.qcow2,if=virtio \ -accel hvf \ -cpu host ``` ### 步驟 7: 透過 SSH 登入稍早安裝的 Ubuntu Linux 參考 [How to Enable SSH on Ubuntu 18.04](https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-18-04/) 設定並啟用 SSH 後,即可關閉 QEMU 所啟動的虛擬機器實例 (instance),隨後重新執行 QEMU 並在命令列最後面加上 `-net user,hostfwd=tcp::2222-:22 -net nic` 也就是以下命令: ```shell $ qemu-system-x86_64 \ -m 2048 \ -vga virtio \ -show-cursor \ -usb \ -device usb-tablet \ -enable-kvm \ -drive file=~/QEMU/ubuntu-desktop-18.04.qcow2,if=virtio \ -accel hvf \ -cpu host \ -net user,hostfwd=tcp::2222-:22 -net nic ``` 這時就可使用以下命令 `$ ssh username@localhost -p 2222` 讓 macOS 上的 ssh (客戶端) 登入到位於虛擬機器環境中的 Ubuntu Linux (提供 ssh 伺服器) > :notes: 這裡的 `username` 請換為步驟 5-1 安裝 Ubuntu Linux 時所選用的使用者帳號名稱 --- ## Using ubuntu-cloud-server image [(download)](https://cloud-images.ubuntu.com/releases/) cloud-image 可以透過 cloud-init 自動化安裝的一些瑣事,而因為 MacOSX 沒有產出 cloud-init 需要的 image 的工具,我們底下利用 docker image 來做。 > 也可以 `$ docker run -i -t --rm -v "$PWD":/usr/src/image -w /usr/src/image ubuntu bash` 然後手動 apt 裝 `cloud-image-utils` 啦 `$ cat Dockerfile` ```Dockerfile # Start from ubuntu LTS FROM ubuntu:18.04 RUN apt-get update && apt-get install -y \ cloud-image-utils ``` * 由 Dockerfile 產出 叫 tool 的 docker image `$ docker build -t tool .` * 由剛才的 image 跑個 container 並掛載 `$pwd` 為工作目錄 `$ docker run -i -t --rm -v "$PWD":/usr/src/image -w /usr/src/image tool bash` 底下是在 docker 的環境執行(#) cloud-init 的`設定檔.txt`,目前就設 root 權限跟放 ssh key 進去而已: `# cat user.txt` ```txt #cloud-config users: - name: sysprog ssh-authorized-keys: - ssh-rsa *** 你的ssh公鑰 *** sudo: ['ALL=(ALL) NOPASSWD:ALL'] groups: sudo shell: /bin/bash ``` 產出 cloud-init 需要的 image(在docker 環境執行): `# cloud-localds user.img user.txt` ```shell qemu-system-x86_64 -accel hvf \ -smp 2 \ -m 2048 \ -nographic \ -hda ubuntu-19.10-server-cloudimg-amd64.img \ -hdb user.img \ -net user,hostfwd=tcp::2222-:22 \ -net nic ``` 開機完應該會看到 cloud-init 設定的 log,然後就可以登入了 ```shell $ ssh sysprog@localhost -p 2222 sysprog@ubuntu:~$ uname -a Linux ubuntu 5.3.0-26-generic #28-Ubuntu SMP Wed Dec 18 05:37:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux ``` > * 不知道為什麼 cloud-init 用密碼一直設不成功。 > * minimal image 同樣方法無法登入,沒心力研究 XD。 > * ⚠️這裏 server-image 會被修改請自行備份 > * -fsdev -virtfs 在 macport/homebrew 提供的 qemu 裡都沒有支援,可能要自己 build from source, 不知道 Macosx 到底能不能用(while building qemu with ./configure --enable-virtfs `ERROR: VirtFS is supported only on Linux`) > [name=owlfox] >感謝 owlfox 提供 Docker 方面的資訊 (之後會將 ubuntu-cloud-image的方法另外建立一篇文章進行彙整) [name=unknowntpo] # 參考資料記錄 [在kvm中使用ubuntu cloud image][ubuntu_cloud_image]: [ubuntu_cloud_image]: https://www.jianshu.com/p/aad72f1ab267 -------------- ### 待編輯項目: 因為目前 Mac-os 沒有 cloud-localds(cloud-image-utils) 套件 所以替代方案: 使用 docker 開啟ubuntu container 來生成 cloud-init 設定 [docker container 使用教學] [docker container 使用教學]: https://ithelp.ithome.com.tw/articles/10192397 [ubuntu_18.04_live_server版本](/UugCF9xRQaeXdUlyTt8GKA):