2015年3月30日 星期一

虛擬機鏡像站:NFS、FTP、Apache

一、實體機先測試鏡像站


1. 實體機修改 /etc/apt/sources.list

sudo vi /etc/apt/sources.list
# 安全性更新不動
deb http://security.debian.org/ jessie/updates main
deb-src http://security.debian.org/ jessie/updates main

## 假設我的實體機 IP:120.117.72.64
deb ftp://120.117.72.64/debian/ wheezy-updates main

deb ftp://120.117.72.64/debian/ wheezy main contrib non-free
## 下面兩條寫法也可以連至本機
#deb ftp://127.0.0.1/debian wheezy main contrib non-free
#deb file:///home/ftp/debian/ wheezy main contrib non-free

2. 實體機測試

sudo apt-get update
sudo apt-get upgrade

二、實體機架  NFS Server

為了將載好的 /home/ftp 目錄分享給 pxeserver.img 虛擬機使用:

1. 測試有無安裝

showmount -e localhost

正確輸出:
Export list for localhost:
/home/ftp 192.168.10.0/24

若輸出下面,即是告訴我們還沒裝 NFS 伺服器:
clnt_create: RPC: Program not registered

2. 安裝

sudo apt-get install nfs-kernel-server

3. 設定

sudo vi /etc/exports

加入:
/home/ftp 192.168.10.0/24(ro,sync,no_subtree_check)

4. 重啟 NFS

/etc/init.d/nfs-kernel-server restart

5. 再測試

showmount -e localhost

正確輸出:
Export list for localhost:
/home/ftp 192.168.10.0/24

三、虛擬機架  NFS 伺服器

1. 虛擬機手動掛載 NFS

先建立掛載目錄:
mkdir /home/ftp

手動掛載:
mount -t nfs 192.168.10.1:/home/ftp /home/ftp

2. 虛擬機檢查掛載

mount
df -h
cd /home/ftp ; ls -l

會看到 /home/ftp 裡面要實體機所下載的鏡像站資料。

3. 虛擬機設定自動掛載 NFS

修改設定檔:
vi /etc/fstab

加入:
192.168.10.1:/home/ftp /home/ftp nfs defaults 1 2

4. 虛擬機測試 NFS

先將原先的掛載卸除:
umount /home/ftp

** 如果無法 umount 就先關閉 proftpd 就能關了!
/etc/init.d/proftpd stop

打指令執行 /etc/fstab 的設定:

mount -a
df -h

指令 mount -a 正確的話,不會有什麼訊息

四、虛擬機架  FTP 伺服器


基本設定同實體機的方法。

1. 虛擬機安裝

sudo apt-get install proftpd-basic

2. 虛擬機設定 /etc/proftpd/proftpd.conf

sudo vi /etc/proftpd/proftpd.conf

搜尋 Anonymous解除註解,完成如下結果:
User                         ftp
   Group                                nogroup
   # We want clients to be able to login with "anonymous" as well as "ftp"
   UserAlias                    anonymous ftp
   # Cosmetic changes, all files belongs to ftp user
   DirFakeUser  on ftp
   DirFakeGroup on ftp

   RequireValidShell            off

   # Limit the maximum number of anonymous logins
   MaxClients                   10

   # We want 'welcome.msg' displayed at login, and '.message' displayed
   # in each newly chdired directory.
   DisplayLogin                 welcome.msg
   DisplayChdir         .message

   # Limit WRITE everywhere in the anonymous chroot
   
     
       DenyAll
     
   

   # Uncomment this if you're brave.
   # 
   #   # Umask 022 is a good standard umask to prevent new files and dirs
   #   # (second parm) from being group and world writable.
   #   Umask                            022  022
   #            
   #            DenyAll
   #            
   #            
   #            AllowAll
   #            
   # 

3. 虛擬機設 /etc/passwd 之 FPT 目錄路徑

sudo vi /etc/passwd

搜尋 ftp ,將其中的目錄路徑改為 /home/ftp

4. 測試

使用實體機開瀏灠器來測試,
URL 為 pxeserver.img 的 IP: 192.168.10.254

正常會看到同虛擬機的 /home/ftp 目錄下的東西 。

若無法連上 pxeserver 之 FTP 站,請設定防火牆,於實體機執行:
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A INPUT -i br10 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE

5. 虛擬機讓 FTP 下次開機後都自動執行

systemctl status proftpd
systemctl enable proftpd




五、虛擬機架 Apache Server

apt-get install apache2

vi /etc/apache2/apache2.conf
將底下片改為 /home/ftp:
<Directory /home/ftp>
        Options Indexes
        AllowOverride None
        Require all granted
</Directory>
vi /etc/apache2/sites-available/000-default.conf
修改為:
#DocumentRoot /var/www/html
DocumentRoot /home/ftp
/etc/init.d/apache2 restart

六、於實體機連線至 pxeserver.img 鏡像站更新

vi /etc/apt/sources.list

deb ftp://192.168.10.254/debian/ wheezy main contrib non-free
deb ftp://192.168.10.254/debian/ wheezy-updates main

成功之後的 pxe server 就是我們整個虛擬教室的 ftp 站。

七、其他

做最原始的 WinXP.img 備份
cp WinXP.img WinXP_bare.img
qemu-img convert -O raw WinXP.img WinXP_ok.img









架設鏡像站( 下載鏡像站、FTP Server、異質網段、img 備份)

一、實體機下載鏡像站


1. 確認空間容量 > 210 GB

請先確認 /home 目錄的可用量容大於 210 GB
我們要下載 Wheezy, Jessie 之鏡像站資料,需要大量空間。

查看指令:
df -h 

2. 建立目錄 /home/ftp

從南臺 PM 私人鏡像站抓 mirror 檔(需要很長時間,檔案量約 200 GB):
sudo mkdir /home/ftp
sudo cd /home/ftp
sudo wget -t 0 -m ftp://120.117.72.71/debian/

3. 移動檔案位置

下載下來的資料會放在 120.117.72.71/ 這個目錄下,
我們要將裡面的 debian 目錄往上一層移動 。
(注意移 debian 沒有鈄線,要移的是整個目錄)
cd /home/ftp/120.117.72.71/
mv debian ..

確認 120.117.72.71/ 移乾淨後,可以清除:
du -sh 120.117.72.71
rm -r 120.117.72.71

二、實體機架 FTP 伺服器


1. 下載

sudo apt-get install proftpd-basic

2.設定檔 proftpd.conf

sudo vi /etc/proftpd/proftpd.conf

搜尋 Anonymous 解除註解,完成如下結果:

 <Anonymous ~ftp>
   User                         ftp
   Group                                nogroup
   # We want clients to be able to login with "anonymous" as well as "ftp"
   UserAlias                    anonymous ftp
   # Cosmetic changes, all files belongs to ftp user
   DirFakeUser  on ftp
   DirFakeGroup on ftp

   RequireValidShell            off

   # Limit the maximum number of anonymous logins
   MaxClients                   10

   # We want 'welcome.msg' displayed at login, and '.message' displayed
   # in each newly chdired directory.
   DisplayLogin                 welcome.msg
   DisplayChdir         .message

   # Limit WRITE everywhere in the anonymous chroot
   <Directory *>
     <Limit WRITE>
       DenyAll
     </Limit>
   </Directory>

   # Uncomment this if you're brave.
   # <Directory incoming>
   #   # Umask 022 is a good standard umask to prevent new files and dirs
   #   # (second parm) from being group and world writable.
   #   Umask                            022  022
   #            <Limit READ WRITE>
   #            DenyAll
   #            </Limit>
   #            <Limit STOR>
   #            AllowAll
   #            </Limit>
   # </Directory>

 </Anonymous>

3. FTP 目錄路徑

sudo vi /etc/passwd

搜尋 ftp ,將其中的目錄路徑改為 /home/ftp

4. 開啟瀏灠器測試

URL 為自己實體機的 IP,成功時要可以看到自己下載的 mirror 檔案,及 debian 目錄。

三、建立異質網段

建立一個自給自足的內網,其網段為 192.168.10.0/24,內網的主要機器為 pxe server,>在內網中要有不同作業系統,不同硬體架構之主機。例如:

WindowsXP
ARM
CentOS

等機器,這些機器可以透過我們所建立的橋彼此互相溝通

目前定義如下:
pxeserver       -> pxeserver.img        192.168.10.254
Windows XP      -> WinXp.img            192.168.10.100

四、補充: 建立 Windows XP 虛擬機:


1. iso 檔及開通序號

請自行先準備一個 windows_xp.iso 及序號,這邊不提供。

2. 建 WinXP.img

(注意這邊 MAC 網卡卡號不要跟 pxeserver.img 的一樣)
qemu-img create WinXP.img 10G

3. 接下來寫一個 buildxp.sh,並執行安裝 XP:

CPU=host
SMP=2
MEMORY=1024
HDA=WinXP.img
MAC="DE:AD:BE:CF:EC:B9"
BRIDGE=br10
CDROM=windows_xp.iso

qemu-system-x86_64 -enable-kvm -cpu $CPU -smp $SMP -m $MEMORY \
-hda $HDA \
-cdrom $CDROM -boot d \
-net nic,macaddr=$MAC -net bridge,br=$BRIDGE

4. 解決 WinXP.img 網路問題:

首先了解,進 XP 後會找不到網卡裝置,
所以需要在虛擬機設定參數給虛擬網卡,
網卡設定參考資料:
http://en.wikibooks.org/wiki/QEMU/Devices/Network

這邊我使用 Realtek Fast Ethernet ,支援度較佳,
寫一個 runxp.sh :

CPU=host
SMP=2
MEMORY=1024
HDA=WinXP.img
MAC="DE:AD:BE:CF:EC:B9"
BRIDGE=br10
CDROM=windows_xp.iso

qemu-system-x86_64 -enable-kvm -cpu $CPU -smp $SMP -m $MEMORY \
-hda $HDA \
-cdrom $CDROM -boot c \
-net nic,model=rtl8139,macaddr=$MAC -net bridge,br=$BRIDGE

5. 做 img 備份

cp WinXP.img WinXP_bare.img
qemu-img convert -O raw WinXP.img WinXP_ok.img













2015年3月20日 星期五

顯卡驅動、橋接、建虛擬機

一、驅動顯卡

承上一章 行前準備,用新編好的核心 linux-3.14.34 開機,接下來安裝顯卡驅動。

相關文章:
安裝 Nvidia 顯卡驅動

1. 查看顯卡

lspci

找到顯卡型號,輸出如下:
01:00.0 VGA compatible controller: NVIDIA Corporation GF119 [GeForce GT 620] (rev a1) (prog-if 00 [VGA controller])

2. 至 Nvidia 官方下載最新驅動:

點此下載 347.88 版驅動

3. 關閉 Linux 開放源始碼之 NV 驅動

rmmod nouveau
echo "blacklist nouveau" >> /etc/modprobe.d/fbdev-blacklist.conf

4. 重開機,並安裝 NV 347.88 驅動

reboot
sh NVIDIA-Linux-x86_64-346.47.run

之後都選 yes 下一步來進行。

5. 確認顯卡驅動

可以用指令查看
xdpyinfo
lsmod | grep nvidia

更進一步可以開繪圖軟體測試,例如:
Blender > File > User Preferences > System > CUDA > Geforce GT 620

二、橋接網路設定

相關文章:
Bridge 建立橋接網路(QEMU Debug)

1. 先設定建置目標:

實體機器 - wheezy (Debian Stable)
  • 實體網路 eth0,P503 主機上 IP
  • 建立橋接網路 br10 (IP:192.168.10.1)
  • 裝有 QEMU-Linaro 虛擬機
  • 下載鏡像站 Mirror 至 /home/$USER/ftp
  • 裝有 FTP 掛上鏡像站 (proftpd)
  • 設定防火牆 (iptable)

PXE Server 虛擬機器 - Jessie (Debian Testing)
  • IP:192.168.10.254
  • TFTP Server (tftpd-hpa)
  • HTTP Server (apache2)
  • DHCP Server (isc-dhcp-server)

虛擬機器 - Windows XP
  • IP:192.168.10.100
  • 裝來玩的

2. 建立橋接網路 br10

2.1 手動指令

查看橋接網路 Bridge:
brctl show

做出一個橋接:
brctl addbr br0
brctl show
ifconfig br0

做連接,假設 br0 連到 eth0
brctl addif br0 eth0
brctl show

想要移除可以輸入指令:
brctl delif br0 eth0
brctl delbr br0
brctl show
ifconfig

2.2 修改設定檔

設定檔 /etc/network/interfaces,eth0 及 lo 保持不動,
自訂名稱及網段,但不建議使用 192.168.0.xx,
容易跟家裡的無線網路衝到 ip,
這邊筆者設名稱:br10/網段:192.168.10.xx

設定檔如下:
auto br10
iface br10 inet static
       address 192.168.10.1
       netmask 255.255.255.0
       gateway 實體機ip
       bridge_ports tap0
       bridge_fd 9
       bridge_hello 2
       bridge_maxage 12
       bridge_stp off

重啟網卡並查看有無 br10 出現
/etc/init.d/networking restart
ifconfig br10
ping 192.168.10.1

3. 安裝 QEMU-Linaro (不用完整 support)

參考文章:
安裝 QEMU Linaro 完整版

3.1 下載 QEMU-Linaro

請至官方下載最新版,並自行編譯後安裝起來。
點此下載 qemu-linaro-1.7.0-2014.01.tar.gz

3.2 編譯及安裝 

(橋接網路只要基礎安裝即可)

tar xfva qemu-linaro-1.7.0-2014.01.tar.gz
cd qemu-linaro-1.7.0-2014.01/
./configure
make -j 8
sudo make install

裝完後檢查一下版本:
which qemu-system-x86_64
輸出:
/usr/local/bin/qemu-system-x86_64

把它複製下來,查看版本
/usr/local/bin/qemu-system-x86_64 -version

輸出:
QEMU emulator version 1.7.0 (qemu-linaro 2014.01), Copyright (c) 2003-2008 Fabrice Bellard

以上輸出,即表示目前所使用的 QEMU 版本為 1.7.0 (qemu-linaro 2014.01)


若遇到 Bug 問題,可以至 安裝 QEMU Linaro 完整版 爬文。

4. 建立 pexserver.img

4.0 下載 Debian-Jessie

debian-testing-amd64-CD-1.iso

4.1 建立資料夾

mkdir -p ~/qemu_image/virtual_classroom

4.2 建立 10 GB 之 image 檔

qemu-img create pxeserver.img 10G

4.3 寫一個 buildpxe.sh

 若遇到無法開啟的問題,請爬看 Bridge 建立橋接網路(QEMU Debug) 文章下方 Debug 區。

CPU=host
SMP=2
MEMORY=1024
HDA=pxeserver.img
MAC="DE:AD:BE:CF:EC:B7"
BRIDGE=br10
CDROM=debian-testing-amd64-CD-1.iso

qemu-system-x86_64 -enable-kvm -cpu $CPU -smp $SMP -m $MEMORY \
-hda $HDA \
-cdrom $CDROM -boot d \
-net nic,macaddr=$MAC -net bridge,br=$BRIDGE

4.4 Debian Jessie 分割設定 ( PXE Server)

partition1 / root / 2.5G / XFS
partition2 / swap / 0.5G / swap
partition3 / home / 剩下容量 / XFS

其他 Bug 排除,參考文章:Bridge 建立橋接網路(QEMU Debug) 下方內容。

如果遇到 modules 目題,可能就要重新做核心編譯,
找到所缺少的模組。











15/03/16 行前準備

一、行前準備:

1. 安裝工具介紹: X86/ARM Toolchain

1.1 執行底下指令:

# apt-get install gcc cpp g++

1.2 安裝完後來驗證 gcc/cpp/g++ 之版本

gcc -v
cpp -v
g++ -v

其它工具,例如 nasm 或是 java 等暫跳過,等遇到的時候再來安裝。

2. 使用 x86 toolchai 來升級 Linux 核心

2.1 核心升級,請安裝以下套件:

sudo apt-get install kernel-package libncures5-dev bzip2 xz-utils

2.2下載核心並建立連結

wget ftp://120.117.72.71/advlinux/linux-3.14.34.tar.xz
su
tar xfva linux-3.14.34.tar.xz -C /usr/src
ln -s /usr/src/linux-3.14.34 linux
cd linux

2.3 編譯 Linux 核心

a. 選單 .config 設定 
先至原始核心內複製基礎 config 檔案 
cp /boot/config-3.2.0-4-amd64 /usr/src/linux/.config

b. 設定選單 (於 /usr/src/linux 目錄下)
make menuconfig

可以使用幾個指令查看目前硬體,依硬體設備及需求來設定屬於自己的核心參數:
lspci -v
lsusb
cat /proc/cpuinfo

以下是我的電腦設定,以南臺 P503 教室電腦為例:
→ General setup → Kernel compression mode (XZ)

→ Processor type and features → Processor family (Core 2/newer Xeon)

→ Power management and ACPI options → CPU Frequency scaling →  Default CPUFreq governor (ondemand)

→ Power management and ACPI options → CPU Frequency scaling → x86 CPU frequency scaling drivers → [ ] Intel P state control

→ Power management and ACPI options → CPU Frequency scaling → x86 CPU frequency scaling drivers → <  > AMD...

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → Core Netfilter Configuration →  Netfilter nf_tables...

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → Core Netfilter Configuration →  ... match support

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → Core Netfilter Configuration →  Netfilter nf_tables ...

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → Core Netfilter Configuration →  Netfilter ....

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → IP: Netfilter Configuration →  IPv4 nf_tables ...

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → IP: Netfilter Configuration →  IPv4 NAT

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → IPv6: Netfilter Configuration →  IPv6 nf_tables ...

→ Networking support → Networking options → Network packet filtering framework (Netfilter) → IPv6: Netfilter Configuration →  IPv6 NAT

→ Networking support → Networking options → Network packet filtering framework (Netfilter) →    Ethernet Bridge nf_tables support

→ Networking support → Networking options → Network packet filtering framework (Netfilter) →    Ethernet Bridge tables (ebtables) support

→ Networking support → [ ]   Wireless  ----

→ Networking support → < >   WiMAX Wireless Broadband support  ----

→ Networking support → < >   RF switch subsystem support  --->

→ Networking support → < >   Bluetooth subsystem support  ---

→ Networking support → < >   CAN bus subsystem support  ----

→ Networking support → < >   IrDA (infrared) subsystem support  ----

→ Networking support → < >   CAIF support  ----

→ Networking support → < >   NFC subsystem support  ----

→ Device Drivers → Network device support → Ethernet driver support → < > ALL

→ Device Drivers → Network device support → Ethernet driver support →  Intel ...

→ Device Drivers → Network device support → Ethernet driver support →  Intel(R) 82575/82576 PCI-Express Gigabit Ethernet support

→ Device Drivers → Network device support → < >   FDDI driver support

→ Device Drivers → Network device support → < >   PLIP (parallel port) support

→ Device Drivers → Network device support → [ ]   Wireless LAN  ----

→ Device Drivers → Network device support → < >   IEEE 802.15.4 drivers  ----

→ Device Drivers → Network device support → [ ]   ISDN support  ----

 → Device Drivers → < > Multimedia support  ----

→ Device Drivers → Sound card support →    Advanced Linux Sound Architecture  --->

→ Device Drivers → USB support →      xHCI HCD (USB 3.0) support

→ Device Drivers → USB support →      EHCI HCD (USB 2.0) support

→ Device Drivers →   VFIO Non-Privileged userspace driver framework

→ Device Drivers → [*] IOMMU Hardware Support  --->

另外附有同為 P503 電腦 Po-Ming 老師所設定的 config 檔:
點此下載

c. 開始跑編譯 (於 /usr/src/linux 目錄下)
time make-kpkg --initra --revision=1.0.1.versionName kernel_image -j 8

d. 編完後,安裝核心 (於 /usr/src 目錄下)
cd /usr/src
dpkg -i linux-image-3.14.34*.deb

e. 重新開機,並選用新核心開啟
reboot

f. 編完以後可以查看:

核心容量大小 vmlinuz, initrd :
du -sh /lib/modules/*

模組容量大小modules :
du -sh /boot/*

二、其他相關補充

於同一個核心下,想要修改選單參數,可以只編模組,編完再丟到 /lib/modules/3.14.34/ 目錄下,編譯指令如下:
time make modules -j 8











Reference:
http://pominglee.blogspot.tw/p/linux_9.html