2016年1月6日 星期三

使用虛擬機器測試 secure boot (二) virt tool 的介紹

上文中使用到的 virt-install 是 virt-tools 中的其中一個,也是 virt-manager 專案下的一個功能。下面簡單說明幾個常被用但是稍微有點被混淆的名詞:

OpenStack - 一個軟體專案。但這個軟體專案並不是典型開發或是維護一個軟體的那種專案,比較接近是「組合或是開發各種工具以提供某種 IaaS (infrastructure as a service) 服務的解決方案」。libvirt 是它支援的工具之一。

libvirt - 一套用 c 語言寫成的 API

virt tools - libvirt 與其週邊相關的工具、產品。包括 virt-manager 。

virt-manager - 這個詞有兩個意思,一個意思是有個指令叫做 virt-manager,可以提供 GUI 來管理基於 virt-manager 這個專案下支援的工具(例如 virt-install)所建立的虛擬機器。第二個意思就是這是一個專案的名稱,集成很多基於 libvirt 開發出的工具,例如 virt-install。

virsh - 可以想成是指令列版本的 virt-manager 工具。

下一篇:virsh 的使用!






使用虛擬機器測試 secure boot (一)

最近工作上開始需要自動化測試有開啟 secure boot 的機器,鴕鳥了好多年終於開始要好好面對這件事情了 XD

下面是跟著這篇 wiki 的資訊所作的測試。有些指令我有根據自己的狀況(host machine:Ubuntu Trusty)調整。覺得太繁瑣的話可以直接跳到幾乎是最後面的小結 XDD

因為我的系統中沒有 uvt 這個指令,找了一下沒有看到 uvt 這個指令從哪來,所以採用 wiki 中提供的另外一種作法:

virt-install --connect=qemu:///system --name=sb-saucy-amd64 --arch=x86_64 --ram=768 \
--disk=path=<path to>/sb-saucy-amd64.qcow2,size=8,format=qcow2,bus=ide,sparse=True \
--virt-type=kvm --accelerate --hvm --cdrom=<path to>/saucy-desktop-amd64.iso \
--os-type=linux --os-variant=generic26 --graphics=vnc --network=network=default,model=virtio \
--video=cirrus --noreboot --boot=loader=OVMF.fd

但會得到:

ERROR    Format cannot be specified for unmanaged storage.

這看起來似乎是要事先先建立好給虛擬機器使用的 storage file。因為急著想要看到結果,我使用 --nodisk 來取代 --disk [1]

virt-install --connect=qemu:///system --name=sb-trusty-amd64-02 --arch=x86_64 --ram=768 --nodisk --virt-type=kvm --accelerate --hvm --cdrom=./uuu/ubuntu-14.04.2-desktop-amd64.iso --os-type=linux --os-variant=generic26 --graphics=vnc --network=network=default,model=virtio --video=cirrus --noreboot --boot=loader=OVMF.fd
如果遇到這個訊息

Starting install...
Creating domain...                                                                                                                                                                    |    0 B     00:00  
WARNING  Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

表示要安裝 virt-veiwer package 來顯示開機過程的畫面:

sudo apt-get install virt-viewer

接下來就會看到開機畫面啦!

下一篇打算來解釋細節,敬請期待。









要印螢幕的時候按了 alt ,鍵盤的訊號同時被 host machine 和 guest machine 捕捉到的一瞬間 XD




















根據這封 virt-tool mailing list 中的 email,這是因為我們沒有事先告訴 virt-manager (or virtsh) 使用的 disk storage 存放位置(專有名詞叫做「pool」,一個 pool 下面可以有好幾個 storage file。),所以 virt-manager 認為這個地方它不敢亂動。

首先綁定資料夾 /tmp 成為 pool 之一,名字叫做 tmppool 的 pool

virsh pool-define-as --name tmppool --type dir --target /tmp

啟動 pool

virsh pool-start tmppool

接下來把上面一長串中 --disk 選項後面指定的位置改為 /tmp (新的 pool 位置)就可以用了:

$ virt-install --connect=qemu:///system --name=thho-trusty-amd64-01 --arch=x86_64 --ram=768 --disk=path=/tmp/thho-trusty-amd64.qcow2,size=8,format=qcow2,bus=ide,sparse=True --virt-type=kvm --accelerate --hvm --cdrom=./uuu/ubuntu-14.04.2-desktop-amd64.iso --os-type=linux --os-variant=generic26 --graphics=vnc --network=network=default,model=virtio --video=cirrus --noreboot --boot=loader=OVMF.fd
Starting install...
Allocating 'thho-trusty-amd64.qcow2'                                                                                                                                                  | 8.0 GB     00:00  
Creating domain...  

檢查 pool 相關的指令有哪些
virsh help pool

驗證一下是不是真的多了 tmppool 這個 pool

$ virsh pool-list
 Name                 State      Autostart
-------------------------------------------
 default              active     yes    
 tmppool              active     no      
 uvtool               active     yes  

使用 virt-install 建立新的 virtual machine instance (domain) 之後,我們只選 try ubuntu 但是不安裝,然後關機 or reboot。下一次只要直接使用 connect 和 start 就可以開啟虛擬機器了(這兩個動作請見下篇解釋)

virsh --connect qemu:///system start thho-trusty-amd64-01

啟動後因為不像 virt-install 一樣有自己啟動 virt-viewer ,所以要自己開:

virt-viewer thho-trusty-amd64-01

(thho-trusty-amd64-01 是 domain name)

開啟了之後就會進入 UEFI shell 如圖。





因為 storage (可以想成是虛擬硬碟)中我們還沒有安裝任何作業系統(上面只選 try ubuntu 但沒有安裝),所以開機後會進入 UEFI shell。這個 shell 是 ovmf 提供的。




比較:
所有指令和選項都一樣,但把  --boot=loader=OVMF.fd 拿掉。
也就是說我們預期不使用 OVMF.fd 這個 bootloader 開機,所以應該要直接進入使用 cdrom 開機。

virt-install --connect=qemu:///system --name=thho-trusty-amd64-02 --arch=x86_64 --ram=768 --disk=path=/tmp/thho-trusty-amd64-02.qcow2,size=8,format=qcow2,bus=ide,sparse=True --virt-type=kvm --accelerate --hvm --cdrom=./uuu/ubuntu-14.04.2-desktop-amd64.iso --os-type=linux --os-variant=generic26 --graphics=vnc --network=network=default,model=virtio --video=cirrus --noreboot

果然直接進入 cdrom iso






小結:

  • 使用 virt-install 建立虛擬機器。並且是建立一個擁有 ovmf 這個韌體的虛擬機器。
  • 使用 virsh 啟動 virt-install 建立的虛擬機器,開始操作 ovmf 提供的 UEFI shell。





因為我有打算在不久的將來自動化這些指令,所以刻意選擇使用 virsh 而不是 virt-manager。virt-manger 應該也是可以作到上面說的這些事情,只是在自動化上大概就會遇到問題。

2016年1月5日 星期二

初探 ipython parallel

最近為了開發 SOLVCON 平行運算的功能,開始評估可能的解決方案。這一塊自己的知識趨近於零 XD 有點亂槍打鳥地亂看,想看看別的專案怎麼設計的,看是要模仿、參考或是甚至直接拿來用的可能性如何。

下面簡單摘要這個頁面關於 ipython parallel 的介紹:

指令列輸入這個指令,建立四個 ipython kernel 給接下來的運算使用:
ipcluster start -n 4 

然後開始玩 hello world:

In [1]: from IPython.parallel import Client
In [2]: c = Client()
In [4]: c.ids
Out[4]: set([0, 1, 2, 3])
In [5]: c[:].apply_sync(lambda : "Hello, World")
Out[5]: [ 'Hello, World', 'Hello, World', 'Hello, World', 'Hello, World' ]
ipython parallel 中很多組態設定和通訊方式是採用 JSON (或是可以轉成 JSON 的 data object)的格式,例如這資料夾下面的東西:

~/.ipython/profile_default/security/




給 kvm 虛擬機器增加 USB 儲存裝置

最近為了開發給在 Snappy 架構下的 Ubuntu Core 用的、可以自動化 USB storage 讀寫測試的工具,採用了 kvm 虛擬機器作為開發環境。因為要測試標的是 USB ,所以花了一點時間了解了一下如何連結實體的 USB storage 裝置到使用 kvm 啟動的 image 。

在 Ubuntu desktop 下使用指令:

sudo kvm -m 512 -redir :8090::80 -redir :8022::22 the-image-you-want-to-test.img -usb -device usb-host,hostbus=2,hostaddr=11

要注意的是指定最後面兩個參數的值時比較麻煩,因為該值會根據 host machine 的狀態而改變。我自己是這樣找對應的值:

  1. 插入 USB storage
  2. 使用指令 lsusb 去看上面的 Bus and Device number ,把這兩個數字記下
  3. 退出 USB storage
  4. 啟動 kvm ,啟動時,根據上面的 Bus and Device number 分別填數 hostbus and hostaddr 。要注意的是這時候 hostaddr 要加 1 (因為重新插拔了一次。現在的 Linux kernel 會給裝置編號加一表示是新找到的裝置,用來和前一次區隔)
  5. 這時候再插入 USB storage ,原本在 host machine 會自動掛載的行為不會發生(推測 event signal 被 kvm 攔下來了);稍候也可以看到在 guest machine 中的 dmesg 指令指出有新的 USB storage 插入了。

2015年11月9日 星期一

使用構樹皮製作布料

入門的話,
選 3 歲以下的構樹(纖維較嫩,比較好剝皮,熟悉之後可以挑更粗更大的構樹)。

沿樹幹長軸用刀具劃開樹皮後,把樹皮撥下來。
接著將樹皮最外層的角質(咖啡色的那些東西)刮除,

刮除方式自由發揮,
反正不要傷到樹皮白色纖維的部份(會被當作布料使用的部份)就好。


刮乾淨後,使用木槌不斷敲擊樹皮,直到面積變為原來的 1.5~2倍,
想要布料越柔軟就要敲越久,變為原來兩倍左右就會頗軟了。
不可使用鐵鎚,因為攻擊力太高了,一敲樹皮就破了 XD

敲好之後,把樹皮放在冷水裡(蓋滿) , 煮到滾。

之後轉小火繼續熬煮約20分鐘,然後拿出來放到冷水泡3天之後陰乾。
陰乾完後就是可以幫做布料的構樹皮了,
可以拿針線來縫。

製作不難,幾乎不會失敗 XD
只是真的很費工 XD


括除表面角質中






剛剝下並除去樹皮角質後的構樹皮。深綠色與褐色部份為沒有把角質去乾淨的部份。

煮構樹皮

煮完、陰乾後的構樹皮。


2015年10月29日 星期四

開發 ubiquity - 環境架設


首先先確定系統中有裝 ubiquity (程式核心) 和 ubiquity-slideshow-ubuntu-gnome (gnome 桌面環境下播放的 ubiquity slides),如果沒裝,要先裝
sudo apt-get install ubiquity ubiquity-slideshow-ubuntu-gnome ubiquity-frontend-gtk
因為 Ubuntu 桌面環境預設使用由 gnome 開發,所以這邊選擇安裝 -ubuntu-gnome。如果你想開發的桌面環境是其他種類,例如 kubuntu 或是 lubuntu,請分別選擇不同的 suffix,例如 ubiquity-slideshow-kubuntu 。

frontend 的部份也是一樣的意思,如果使用 kde ,請裝 ubiquity-frontedn-kde 。

因為我想開發 oem 安裝模式下的 ubiquity 行為,所以我的系統還要先有安裝 oem-config
sudo apt-get install oem-config
接著創建一個用來放開發用程式碼的資料夾
mkdir -p src/ubiquity
進到這個資料夾中[1],
然後取得架設環境用的 script (你可能要事先安裝 bzr),
sudo apt-get install bzr 
bzr branch lp:~taihsiangho/+junk/ubiquity-scripts scripts
或是從這裡也可以取得(我是從這 copy 的,免得原作者有天把來源拿掉)
bzr branch lp:~agateau/+junk/ubiquity-scripts scripts
取得 ubiquity 中播放的 slides
bzr branch lp:ubiquity-slideshow-ubuntu slideshow
取得 ubiquity 原始碼(到時要開發這一份)
bzr branch lp:ubiquity code
整個環境架設的核心概念是把下載下來的部份原始碼,使用 symbolic link 去代替在系統中的程式碼(這種作法可以生效是因為 ubiquity 核心本身是 python 這個直譯式語言寫成的關係),所以開發時直接改 code 就會好像直接改系統中的 ubiquity 程式一樣。

上面的步驟可以整合為
sudo apt-get install ubiquity ubiquity-slideshow-ubuntu-gnome ubiquity-frontend-gtk oem-config && mkdir -p src/ubiquity && cd src/ubiquity && sudo apt-get install bzr && bzr branch lp:~taihsiangho/+junk/ubiquity-scripts scripts && bzr branch lp:ubiquity-slideshow-ubuntu slideshow && bzr branch lp:ubiquity code
使用這個指令去準備開發環境[2]
./scripts/setup setup
done!! 至此為止所有東西都準備好了。所以我們切換到開發狀態
./scripts/setup dev
修改好程式後,接著啟動 ubiquity 。我在這裡想開發的是 oem 模式下的行為,所以啟動方式是
sudo oem-config
而不是標準的 ubiquity
sudo ubiquity



本篇主要參考
http://agateau.com/2013/hacking-on-ubiquity-the-setup/
我已經將虛擬機器的部份拿掉,著重在最核心的概念:使用原始碼替換系統中的程式碼。讀者可依照自己的需求,將此環境設置方式自行搭配必要的虛擬環境,免得把自己的主系統弄髒。

[1] 注意不要漏掉這一步,不然後面的 ./scripts/setup 不會正確工作。
[2] 如果系統沒有安裝 qt 相關的 ubiquity 的話;setup 這個 script 中 qt 相關的動作要被移除,來保證 script 執行沒有中斷。免得行為不符合預期。


Trouble Shooting

sudo oem-config 啟動後沒反應

遠端啟動時,是否有指定環境變數 DISPLAY

/usr/sbin/oem-config 會先檢查是否有 DISPLAY 環境變數,如果有才 import Gtk module。所以不指定的話就會有如下錯誤:

Jan  3 18:41:12 201606-22347 ubiquity[23735]: Ubiquity 2.21.63.3 (oem-config)
Jan  3 18:41:13 201606-22347 ubiquity[23735]: Exception in GTK frontend (invoking crash handler):
Jan  3 18:41:13 201606-22347 ubiquity[23735]: Traceback (most recent call last):
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/sbin/oem-config", line 649, in <module>
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     main(oem_config)
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/sbin/oem-config", line 635, in main
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     install(query=options.query)
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/sbin/oem-config", line 264, in install
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     wizard = ui.Wizard(distro)
Jan  3 18:41:13 201606-22347 ubiquity[23735]:   File "/usr/lib/ubiquity/ubiquity/frontend/gtk_ui.py", line 228, in __init__
Jan  3 18:41:13 201606-22347 ubiquity[23735]:     self.builder = Gtk.Builder()
Jan  3 18:41:13 201606-22347 ubiquity[23735]: NameError: name 'Gtk' is not defined

例如可以這樣指定

export DISPLAY=:0 ; sudo oem-config

檢查 /var/log/syslog

合併 tail -f /var/log/syslog 的監控技巧,執行 oem-config。目前發現在 Xenial proposed stack 上有點問題;這應該是因為最新的 ubiquity source 和 Xenial 的環境有不相容之處。可以切回 ./scripts/setup distro 使用系統原本的試試是否可以起來。

Jan  3 18:41:30 201606-22347 ubiquity[23742]: Ubiquity 2.21.63.3 (oem-config)
Jan  3 18:41:30 201606-22347 ubiquity[23742]: Exception in GTK frontend (invoking crash handler):
Jan  3 18:41:30 201606-22347 ubiquity[23742]: Traceback (most recent call last):
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/sbin/oem-config", line 649, in <module>
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     main(oem_config)
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/sbin/oem-config", line 635, in main
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     install(query=options.query)
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/sbin/oem-config", line 264, in install
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     wizard = ui.Wizard(distro)
Jan  3 18:41:30 201606-22347 ubiquity[23742]:   File "/usr/lib/ubiquity/ubiquity/frontend/gtk_ui.py", line 278, in __init__
Jan  3 18:41:30 201606-22347 ubiquity[23742]:     ''')
Jan  3 18:41:30 201606-22347 ubiquity[23742]: GLib.GError: gtk-css-provider-error-quark: <data>:8:24'min-height' is not a valid property name (3)

2015年10月20日 星期二

除錯技巧心得 - 以 GConf 參數為例



安裝

sudo apt-get install gconf-editor

啟動

gconf-editor

如果已經知道 schema 的話,按照 schema 去找;若是不知道 gconf-editor 有 Find 功能可用。


把設定界面打開,一邊調整一邊觀察。所以就會知道該怎麼調整 gnome 設定。



gnome 的說明 https://projects.gnome.org/gconf/

gconf-editor
GNOME has a prototype-quality unfinished program called gconf-editor which is sort of the GUI equivalent of gconftool. Try it out.


dconf 是比較新的工具,先用 dconf,找沒有想要的屬性的話再用 gconf 查找。原本的回答很不錯,直接引用:
dconf is a new way for applications to store settings, and it is intended to replace gconf. dconf-editor and gconf-editor are for their respective configuration systems, and they don't talk to each other. At the moment that transition is still a work in progress, so many applications continue to use gconf. In addition, some applications still have settings left over in gconf even though they are using dconf now.
Almost all of the software that is installed by default - everything that comes from GNOME - uses dconf (if anything). Almost everything else that is available uses gconf, though this is of course changing as older software is brought up to date.
So, yes, changing a setting in gconf is likely to do something! If you did a fresh install of Ubuntu 11.10 and didn't migrate your gconf settings, it's likely that everything you see with gconf-editor is being looked at and understood by some current application. If your settings predate 11.10, there may be stuff there that isn't being used.
As a rule of thumb, check dconf first and if what you're looking for isn't there, try gconf.

https://askubuntu.com/questions/91403/when-to-use-gconf-vs-dconf