2011年10月30日 星期日

執行lucid32.box 使用 jRuby + vagrant在Windows XP

image

最近朋友有在Windows環境中使用vagrant的需求,
而筆者有時要用windows xp來使用vagrant,
故寫了本篇,便是介紹如何安裝vagrant及其環境。

安裝 Ruby
  • 實驗使用cygwin+ruby,但最後有相容性臭蟲問題而成殘念。
/usr/lib/ruby/gems/1.8/gems/virtualbox-0.9.2/lib/virtualbox/com/mscom_interface.rb:28:in `initialize': failed to create WIN32OLE object from `VirtualBox.VirtualBox' (WIN32OLERuntimeError)
    HRESULT error code:0x80004005
C:\Documents and Settings\vagrant>set PATH=C:\jruby-1.6.5\bin;%PATH%
C:\Documents and Settings\vagrant>gem install vagrant
Fetching: archive-tar-minitar-0.5.2.gem (100%)
Fetching: erubis-2.7.0.gem (100%)
Fetching: json-1.5.4-java.gem (100%)
Fetching: net-ssh-2.1.4.gem (100%)
Fetching: net-scp-1.0.4.gem (100%)
Fetching: i18n-0.6.0.gem (100%)
Fetching: thor-0.14.6.gem (100%)
Fetching: ffi-1.0.10-java.gem (100%)
Fetching: virtualbox-0.9.2.gem (100%)
Fetching: vagrant-0.8.7.gem (100%)
Successfully installed archive-tar-minitar-0.5.2
Successfully installed erubis-2.7.0
Successfully installed json-1.5.4-java
Successfully installed net-ssh-2.1.4
Successfully installed net-scp-1.0.4
Successfully installed i18n-0.6.0
Successfully installed thor-0.14.6
Successfully installed ffi-1.0.10-java
Successfully installed virtualbox-0.9.2
Successfully installed vagrant-0.8.7
10 gems installed
C:\Documents and Settings\vagrant>cd SRC
C:\Documents and Settings\vagrant\SRC>cd vagrantTest
C:\Documents and Settings\vagrant\SRC\vagrantTest>vagrant up
JRuby limited openssl loaded. http://jruby.org/openssl
gem install jruby-openssl for full support.
[default] Importing base box 'lucid32'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
Guest Additions Version: 4.1.0
VirtualBox Version: 4.1.4
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] -- web: 80 => 8080 (adapter 1)
[default] Creating shared folders metadata...
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
[default] Destroying unused networking interface...
LoadError: OpenSSL::BN requires the jruby-openssl gem
出現OpenSSL錯誤,在安裝下表的jruby-openssl後,此問題已排除。
C:\Documents and Settings\vagrant\SRC\vagrantTest>gem install jruby-openssl
Fetching: bouncy-castle-java-1.5.0146.1.gem (100%)
Fetching: jruby-openssl-0.7.4.gem (100%)
Successfully installed bouncy-castle-java-1.5.0146.1
Successfully installed jruby-openssl-0.7.4
2 gems installed

啟動lucid32版本
此版本由http://files.vagrantup.com/lucid32.box下載:
(使用vagrant add lucid32 下載,
便已發生無法正在驗證的問題,
筆者直接將box檔案用tar xvf lucid32.box解壓縮至.vagrant.d\boxes\lucid32\目錄中)
C:\Documents and Settings\vagrant\SRC\vagrantTest>vagrant up
[default] Importing base box 'lucid32'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
Guest Additions Version: 4.1.0
VirtualBox Version: 4.1.4
[default] Matching MAC address for NAT networking...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- ssh: 22 => 2222 (adapter 1)
[default] -- web: 80 => 8080 (adapter 1)
[default] Creating shared folders metadata...
[default] Running any VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] Failed to connect to VM!
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.
由最後發現啟動lucid32已成功,但SSH連線失敗,對照本文一開始的左圖(vagrantTest_1319904659, Ubuntu)
接著使用vagrant ssh 命令查看相訊息。
C:\Documents and Settings\vagrant\SRC\vagrantTest>vagrant ssh
`vagrant ssh` isn't available on the Windows platform. The
vagrant.ppk file for use with Putty is available at:
  C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/vagrant-0.8.7/keys/vagrant.ppk
To use this create a new Putty session for `vagrant@localhost`
on port `2222`, in the Connection>SSH>Auth
configuration section navigate to the vagrant.ppk file,
select it, save the session for later use, and connect.
For a more detailed guide please consult:
  http://vagrantup.com/docs/getting-started/setup/windows.html
(註:在Mac OS X 中,因terminal環境有ssh 指令,所以可直接啟動ssh連線至虛擬機器)

使用PuTTY 


啟動PuTTY
(筆者愛用Pietty,由PuTTY改版以支援中文)

Auto-login username
在Connection->Data中,Auto-login username一欄,輸入使用者名稱 vagrant,如下:
image

輸入私密金鑰
在Connection->SSH->Auth中,Private key file for authentication一欄,輸入私密金鑰ppk所在路徑及檔名,如下:
C:\jruby-1.6.5\lib\ruby\gems\1.8\gems\vagrant-0.8.7\keys\vagrant.ppk
image

主機名稱及Port
使用主機名稱localhost 及port 2222 來連接,卻無法使用ssh 來連接 lucid32,有未知的問題存在,網上此問題討論。
image
筆者使用另一個朋友所製作gold.box, 使用主機名稱localhost 及port 2200 來連接成功,如下圖:
image image
如欲按儲存以保留設定,輸入 Saved sessions 名稱: <自訂名>。

結論
就筆者使用vagrant在windows來說,設定環境很多小問題,需要花時間去調整。整體而言,在Mac OS X 環境,設定vagrant就比windows 容易許多。
記得使用完要關機,在啟動目錄中,輸入vagrant halt,便會關閉虛擬機。一點小經驗,望有所助益。
如有網友成功直接在cygwin 中成功設定vagrant 環境,請不吝分享。

附註
  • 請先行安裝VirtualBox
  • 為求方便使用,可將jruby安裝路徑(如:C:\jruby-1.6.5\bin)加入系統環境變數
  • 請確定有將C:\Program Files\Oracle\VirtualBox 路徑加入至系統環境變數中。(與cygwin 環境整合使用)
  • 若有使用Python,也可將環境變數加入,以便在Windows命令提示字元終端中使用。
setup_globalvarsetup_globalvar_selectpathsetup_globalvar_edit  


  • 成功關閉gold vm 的訊息


C:\Documents and Settings\vagrant\SRC\gold>vagrant halt
[default] Attempting graceful shutdown of linux...

C:\Documents and Settings\vagrant\SRC\gold>

2011年10月14日 星期五

在Mac OS X初次使用Arduino實驗板

今天在HackingThursday 聚會,朋友借了一塊Arduino Mega 2560實驗板,現場測試。本文便是將開發設定相關心得做個紀錄。
當然可參考原文的說明手冊
首先至http://arduino.cc/en/Main/Software此下載Mac OS X安裝檔http://arduino.googlecode.com/files/arduino-0022.dmg
arduino_install1
開啟dmg檔案後,便會出現上列,將Arduino 圖示,拖曳到Applications(應用程式目錄),再安裝USB 序列埠驅動程式,如下,依指示安裝。
  arduino_Install_FTD_Driver
先使用USB連接線,連接Mac及2560實驗板,會出現偵測到新網路介面,按cancel 取消即可,不需進行此項設定。
IMG_1442Arduino Mega 2560 Modem in usb detected
再來到Applications 目錄中,找到並執行Arduino整合開發應用程式。
Arduino_in_Applications_dir
Arduino 整合開發環境是以Java開發而成,加上AVR gcc/g++ …的跨平台編譯器及相關設定所組成。
在Arduino設定,注意事項中,先由選單,選擇[Tools] –> [Board] –> [Arduino Mega 2560] (請依網友們的開發板型號,挑選)
arduino_select_hardware
緊接著設定USB 序列埠,先由選單,選擇[Tools] –>[Serial Port]->[/dev/tty.usbmodemfd121] (請依網友們的開發板型號,挑選)
 Arduino_select_the_serial_port
好戲上場,在Arduino中,已準備許多範例程式可供參考,選取視窗上方按鈕[Open]->[1.Basics]->[Blink],
 Arduino_selected_1.Basics_Blink.sample
即出現下方畫面,中央為範例程式碼。最後執行[Upload]鈕(右方數來第二按鈕),立刻編譯並上傳到開發板。(在傳送過程式中,TX燈會閃爍,完成後,即啟動閃爍程式,令13腳位LED每隔一秒閃爍一次。
arduino_Blink_LED_Uploading
若無其他3.3V LED可連13及GND腳位時,可參考下圖,USB Port右方,紅色燈號亦作用與在13pin插LED相同。
 IMG_1445 IMG_1448
結論
Arduino設計非常易於使用,並且有相當多的相容擴充元件可供使用,很適合不太熟悉程式設計及硬體的人來使用。

2011年10月1日 星期六

ARM Linux 開發環境設定指南

debian-arm-booting 
最近在工作上,需要用到ARM相關平台,所以花時間研究 ARM CPU 架構的開發環境,
其中硬體商(目前開發板:Friendly ARM Tiny6410)提供的部分程式必須在Windows 上才可執行。
故筆者想能不能在windows 及ubuntu-linux 中,也可各有一個模擬arm 的開發平台,可直接編譯程式而不需要使用cross-compiler(簡言之,如在windows平台上,使用ARM虛擬機器編譯arm執行檔)。
經過搜尋網上資料後,發現有一套件debian armel可供使用,且很貼心的提供可利用qemu arm來直接載入使用,大大減少重新編譯的時間。

設定步驟


安裝QEMU
下載debian-armel
檔案所在位置:http://people.debian.org/~aurel32/qemu/armel/
 a3853606c5762d41bb245aaeb8823996  debian_lenny_armel_desktop.qcow2
  dce9f0d2855d57b79b63bcf3a9ecd51d  debian_lenny_armel_standard.qcow2
  a6d0dde54fb27254e2b8d94736889462  initrd.img-2.6.26-2-versatile
  1969e2a0ffc571ccf6024dc7c842d4dd  vmlinuz-2.6.26-2-versatile
  c5f9039b463e07852055071ac118320a  debian_squeeze_armel_desktop.qcow2
  b7807306c4c79aaa9a423119f4633ffb  debian_squeeze_armel_standard.qcow2
  f73e83b8e48bd422457dafeb8afe7fc6  initrd.img-2.6.32-5-versatile
  8b33324f99d13dc71d6ad060db2fd60a  vmlinuz-2.6.32-5-versatile

筆者所使用的檔案組合如下表:
下載檔案說明
initrd.img-2.6.32-5-versatile初始化並啟動附加檔案
vmlinuz-2.6.32-5-versatile系統啟動用linux 核心
debian_squeeze_armel_standard.qcowdebian squeeze 標準系統映像檔(可寫入更新資料)


執行debian arm linux
開機完成後,預設登入帳號及密碼皆為root。
debian-arm-login

執行腳本檔
此腳本用來執行debian-armel,及相關必要參數。
run.sh
#!/bin/sh
export PATH="$PATH:/usr/local/qemu/bin"
PLATFORM="`uname -s | cut -d'_' -f1`"
QEMUEXE="qemu-system-arm"
if [ "$PLATFORM" = "CYGWIN" ] ; then
    QEMUEXE=${QEMUEXE}.exe
fi
img=debian_squeeze_armel_standard.qcow2
$QEMUEXE -M versatilepb  -kernel vmlinuz-versatile -initrd initrd.gz -m 256 -hda $img -append 'root=/dev/sda1' -redir tcp:2200::22

使用腳本檔案前用先將,下列檔案更名:

  • initrd.img-2.6.32-5-versatile  改為 initrd.gz
  • vmlinuz-2.6.32-5-versatile 改為 vmlinuz-versatile

進入到run.sh 所在目錄,並執行下列腳本檔

sh run.sh

註一:當在使用apt-get install gcc g++ 時,發生找不到檔案的情形。
解決方法: apt-get update –fix-missing
註二:因為qemu 網路架構原設計,需使用-redir 參數用來將Guest OS(debian-arm) 的ssh 22 port 轉到本機2200port。(記得debian-arm中,執行 apt-get install openssh-server來安裝ssh server)

結論
當筆者完成所有步驟後,等於擁有一台虛擬ARM機器,且為一包含完整開發軟體的LINUX系統。
若網友們所寫的程式無特別額外ARM硬體需求,則可直接在上面開發偵錯,可減少arm程式複製到arm 板子上的時間。

2011年9月16日 星期五

自製免費軟體:超軟中文直式閱瀏器sviewer 0.27.1001更新

sviewer027


主要修正 2.7.1001
* 解決無BOM標頭UTF8檔案,顯示亂碼問題。
* 狀態列顯示檔案類型UTF8, BOM UTF8, ASCII等等
* 工具字型大小選擇: 字型加大為大24, 中20, 小16

檔案下載
由此下載sviewer-027-1001.zip檔案
a073cfa82d621593c49fc26b6972e7bf *sviewer-027-1001.zip

2011年9月14日 星期三

Go 程式語言再探

go_webs_qrcode
  從官網中範例程式找到一段Go程式碼WebServer,加入我的編譯腳本檔如下:
GOCC=6g
GOLD=6l
PROJECT=webserver
all: $(PROJECT)
$(PROJECT).6: $(PROJECT).go
        $(GOCC) $(CFLAGS) $^
$(PROJECT): $(PROJECT).6
        $(GOLD) -o $@ $^
run: $(PROJECT)
        ./$^
clean:
        rm -f $(PROJECT).6 $(PROJECT)
當你輸入,make run 時,如果設定無誤(詳設定請參閱Go語言初探),將會啟動一個Web Server 在localhost:1718。
如上圖,筆者輸入本站網址,點按[Show QR],輕輕鬆鬆產生本站QRCode圖像。
結語
短短50行程式碼,其中包含20行網頁腳本,使用掛載flag, http, io, log, template等等套件大幅提高程式生產力。
若要學習Go,不容錯過Effective Go一文。

2011年9月7日 星期三

Go 程式語言初探

from http://golang.org



最近寫多篇與OS開發相關超硬的文章,來學習Go 語言換換口味輕鬆一下吧!參考英文說明,寫了以下心得:

使用平台為Mac OS X 10.6.8 + Xcode 4.2

前言

Go 程式語言是一個開放源碼的專案,以BSD-style授權發佈。
它以通用語言作為系統設計基礎想法。在強調於型別、垃圾回收及明確定義支持Concurrent程式設計的語言功能等等。並提供大量套件 ,提高寫程式效率。目前支援x86及ARM處理器,一個64位元及32位元x86的編譯套件(6g和8g),及支援ARM的編譯套件(5g)。


開發環境設定

安裝 Mercurial (hg)
 sudo easy_install mercurial
(註:easy_install 是python所內附一個好用安裝第三方工具的python腳本)

Mercurial versions 1.7.x 需要設定檔來指定憑證授權 Certification Authorities (CAs). 在筆者機器遇到下列警示訊息:
warning: go.googlecode.com certificate with fingerprint 28:92:... not verified (check hostfingerprints or web.cacerts config setting)

這表示你所使用的Mercurial沒有正確設定CAs. 參考configure the CAs一文,進行設定。
以下是OSX 10.6以上版本的設定

openssl req -new -x509 -extensions v3_ca -keyout /dev/null -out dummycert.pem -days 3650

使用openssl命令建立替代憑證(設定內容隨意打)
接著使用下列指令將dummycert.pem複製到/etc

sudo cp dummycert.pem /etc/hg-dummy-cert.pem

最後在~/.hgrc 或go/.hg/hgrc內容加入下列兩行。

[web]
cacerts = /etc/hg-dummy-cert.pem

下載Go 正式發佈版本源碼


 hg clone -u release https://go.googlecode.com/hg/ go


編譯Go

你將需要安裝Xcode,其中包含GCC, C libraries ...

$ cd go/src
$ ./all.bash
如果沒任何錯誤發生,訊息如下:


ALL TESTS PASSED

---
Installed Go for darwin/amd64 in /Users/you/go.
Installed commands in /Users/you/Go/bin.
*** You need to add /Users/you/Go/bin to your $PATH. ***
The compiler is 6g.

最後幾行顯示你的系統資訊,硬體架構,和所安裝的目錄,你需要將此目錄加到搜尋路徑中。詳請參閱英文文章環境變數environment variables.


使用Go來編譯Hello範例
$ make
6g hello.go
hello.go:2: can't find import: fmt
錯誤發生原因沒有指定環境變數。
筆者指定下列環境變數後,總算編譯成功。

export PATH=~/Go/bin:$PATH
export GOROOT=$HOME/Go
export GOARCH=386
export GOOS=darwin
export GOBIN=$HOME/Go/bin

$ make
6g hello.go
6l -o hello hello.6


執行結果
$ make run
./hello
hello, world


hello 範例可由此下載。

歡迎留言分享心得。

2011年9月5日 星期一

啟用e100 (i82559er)網路卡在自製Bos

bos_i82559er_enabled 續上篇自製BenOS支援lspci 命令,參考MIT OSE Lab6: Network Driver 及其中各項文件,且參照相關Linux網路驅動程式設計,目前已整合實作82559er網路卡驅動至BOS v0.21中。
新增及修改下列檔案:
pcireg.h PCI相關參數標頭檔,引用自NetBSD,源自MIT OSE LAB
pci.c 移植Lab6 kern/pci.c
net_e100.h 參考Lab6相關實作資料
net_e100.c 參考Lab6相關實作資料
teset/Makefile   已修改以i82559er為參數,啟動qemu :
qemu -fda "../bos.img" -net user -net nic,model=i82559er
執行後結果,如文頭畫面。

啟用e100
其中pci.c,如何啟用e100 82559er 簡略原始碼如下:
pci_func_enable(pci_pdata_t f) {
        pci_conf_write(f, PCI_COMMAND_STATUS_REG,
                       PCI_COMMAND_IO_ENABLE |
                       PCI_COMMAND_MEM_ENABLE |
                       PCI_COMMAND_MASTER_ENABLE);
        for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END;
             bar += bar_width)
        {
                uint32_t oldv = pci_conf_read(f, bar);
                bar_width = 4;
                pci_conf_write(f, bar, 0xffffffff);
                uint32_t rv = pci_conf_read(f, bar);
                if (rv == 0)   continue;
                int regnum = PCI_MAPREG_NUM(bar);
                if (PCI_MAPREG_TYPE(rv) == PCI_MAPREG_TYPE_MEM) {
                        if (PCI_MAPREG_MEM_TYPE(rv) == PCI_MAPREG_MEM_TYPE_64BIT)
                                bar_width = 8;
                        size = PCI_MAPREG_MEM_SIZE(rv);
                        base = PCI_MAPREG_MEM_ADDR(oldv);
                } else {
                        size = PCI_MAPREG_IO_SIZE(rv);
                        base = PCI_MAPREG_IO_ADDR(oldv);
                }
       pci_conf_write(f, bar, oldv);
       f->addr_base[regnum] = base;
       f->addr_size[regnum] = size; }

net_e100.c
啟用後,這時便用使用到 io region 1: 0xc040,長度為64位元組,參考Intel 82559,來進行下列操作。
需要將e100 做軟體重置(可參e100_reset函式),因使用DMA Rings,
所以接著要掛上傳送的緩衝區Control Block List(CBL)及接收用的緩衝區Receive Frame Area(RFA),可參考cbl_init()及rfa_init()。



net 命令
在起動Bos v0.21後,輸入net 指令,可看到下列結果:
bos$ net
PCI function 00:18.0 (8086:1209) enabled
mem region 0: 4096 bytes at 0xf2020000
io region 1: 64 bytes at 0xc040
cbl avail = 9, wait = 1, slot = 0x420000, irq=11
rfa avail = 10, wait = 0, slot = 0x420050
其中黃色標記三項資訊,在對網路卡設備進行操作時,會使用到。


結語
原使用e100設備序號,並不生效;後來研究OSE資料後,發現必須在qemu參數中設定,之後再使用設備序號1209來啟用e100(i82559er),如此才能正確執行在qemu環境。
MIT OSE Lab6十分具有挑戰性,筆者經幾天的努力才把這網路卡驅動部分實作完成;但大約只完成Lab6的三分之二,其餘部分,待9/6社群活動後,再來討論後面部分旳細節。本次程式碼可由此下載。