目录
vmware 具有快照功能,安装配置过程中尽可能拍摄快照,出错无法解决时可以还原到某个快照。
首先在 vmware 下安装 ubuntu10.04 i386。
一、创建 root 账户
sudo passwd root
123456
123456
sudo gedit /etc/gdm/gdm.schemas
修改root位置
<schema>
<key>greeter/Include</key>
<signature>s</signature>
<default></default>
</schema><schema>
<key>greeter/Exclude</key>
<signature>s</signature><default>bin,root,daemon,adm,lp,sync,shutdown,halt,mail,news,uucp,operator,nobody,nobody4,noaccess,postgres,pvm,rpm,nfsnobody,pcap</default>
</schema>
<schema>
<key>greeter/Include</key>
<signature>s</signature>
<default>root</default>
</schema><schema>
<key>greeter/Exclude</key>
<signature>s</signature><default>bin,daemon,adm,lp,sync,shutdown,halt,mail,news,uucp,operator,nobody,nobody4,noaccess,postgres,pvm,rpm,nfsnobody,pcap</default>
</schema>
sudo reboot
以 root 账户登录,以下操作均是在 root 账户下操作。
二、更改源
gedit /etc/apt/sources.list
deb http://old-releases.ubuntu.com/ubuntu/ lucid main restricted
deb http://old-releases.ubuntu.com/ubuntu/ lucid multiverse universe
deb http://old-releases.ubuntu.com/ubuntu/ lucid-updates main restricted
deb http://old-releases.ubuntu.com/ubuntu/ lucid-updates multiverse universe
deb http://old-releases.ubuntu.com/ubuntu/ lucid-backports main restricted
deb http://old-releases.ubuntu.com/ubuntu/ lucid-backports multiverse universe
deb http://old-releases.ubuntu.com/ubuntu/ lucid-security main restricted
deb http://old-releases.ubuntu.com/ubuntu/ lucid-security multiverse universe
sudo apt-get update
三、网络配置
如果上不了网,vmware-编辑-虚拟网络编辑器-还原默认设置。虚拟机-设置-网络驱动器,选择net模式。
四、安装 tools
自带 tools 不起作用,下载下面版本。
VMwareTools-10.1.6-5214329.tar.gz
安装时,可先上传到邮箱,再在 firefox 中用登录邮箱,下载到 ubuntu 桌面,然后打开终端,mv 到合适的安装位置,如 /home,再解压,进入文件夹,执行安装。安装后,关机,CD设置为自动检测,开机。即可使用 tools。
以上四步完成了系统的基本配置,建议此时保存快照。
五、安装 ns2.34
1、更新软件源列表
sudo apt-get update #更新源列表
2、安装g++
gcc -v 查看 gcc 版本为 gcc-4.4.3(默认),但无g++,需要安装g++-4.4。
sudo apt-get install g++-4.4
sudo apt-get install g++-4.4-multilib
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 40 #设置优先级为40
sudo update-alternatives --config g++ #选择默认的g++版本
g++ -v #查看g++版本
3、安装其他依赖
sudo apt-get install tcl8.4 tcl8.4-dev tk8.4 tk8.4-dev #for tcl and tk
sudo apt-get install libxmu-dev libxmu-headers #for nam
4、然后可以进行安装:
下载 ns-allinone-2.34.tar.gz,放在 /home 目录下。
cd /home
tar -zxvf ns-allinone-2.34.tar.gz #解压
cd ns-allinone-2.34
./install #安装
安装过程中报错:
ld -shared-o libotcl.so otcl.o
otcl.o: Infunction `OTclDispatch':
/usr/ns-allinone-2.34/otcl-1.13/otcl.c:495:undefined reference to `__stack_chk_fail_local'
otcl.o: Infunction `Otcl_Init':
/usr/ns-allinone-2.34/otcl-1.13/otcl.c:2284:undefined reference to `__stack_chk_fail_local'
ld:libotcl.so: hidden symbol `__stack_chk_fail_local' isn'tdefined
ld: finallink failed: Nonrepresentable section on output
make: ***[libotcl.so] 错误 1
otcl-1.13make failed! Exiting ...
Seehttp://www.isi.edu/nsnam/ns/ns-problems.html forproblems
错误原因及解决办法:gcc 4.0版本以前是用ld -share来生成共享库的,但是到了4.0以上的版本(我的为gcc version4.4.1),这个命令改为了gcc -share即可。
step1:
cd /home/ns-allinone-2.34/otcl-1.13
gedit configure.in
把77行处的
SHLIB_LD="ld-shared"
改为
SHLIB_LD="gcc -shared"
保存退出
step2:
gedit configure
把6304行
SHLIB_LD="ld-shared"
改为
SHLIB_LD="gcc -shared"
保存退出
step3:
重新安装,即可安装成功。
cd ../ #cd /home/ns-allinone-2.34
./install #安装
5、设置环境变量
cd #回到家目录
gedit .bashrc #
在.bashrc文件末尾添加:
PATH="$PATH:/home/ns-allinone-2.34/bin:/home/ns-allinone-2.34/tcl8.4.18/unix:/home/ns-allinone-2.34/tk8.4.18/unix"
LD_LIBRARY_PATH="/home/ns-allinone-2.34/otcl-1.13:/home/ns-allinone-2.34/lib"
TCL_LIBRARY="$TCL_LIBRARY:/home/ns-allinone-2.34/tcl8.4.18/library"
保存退出。
6、验证
打开一个新终端,输入ns,如果出现%,则说明安装成功。
ns /tcl/ex/simple.tcl 一个简单的例子。
以上完成了ns-2.34安装,建议此时保存快照。
六、安装 leach
1、将补丁包 leach+pegasis_ns234.patch 放在 /home/ns-allinone-2.34 下。
leach+pegasis_ns234.patch 已上传至:
https://download.csdn.net/download/weixin_41565755/12107080
2、打补丁,下面命令其中一个,在 /ns-2.34 下自动添加了一些文件。
patch -p0 < leach+pegasis_ns234.patch
3、查看在 /ns-2.34 下添加的文件,重要的文件或目录为如下三个
mit/ # leach 协议所在
test # leach 启动文件
leach_test # leach 参数配置文件
4、运行和报错解决
在 leach_test 中配置协议和参数。
#!/bin/bash
# This file runs a generic LEACH protocol simulation.# This is the algorithm that we are going to run. Type leach, leach-c, stat-clus or pegasis
alg=leach# Number of clusters we want. It is recommended to use 5% of the total for LEACH and LEACH-C.
# WARNING! - It is important to use 1 cluster for Pegasis number of nodes in the scenario.
num_clusters=5# bs_x, bs_y is the location of the base station in the field.
bs_x=50
bs_y=50# The directory and filename that we want our output to be written.
dirname="mit/leach_sims"
filename=$alg# Topology
# This file is the scenario that we are going to use.
# This file can be editted manually if you are very careful to create
# a predefined topology. To generate a random topology go to the
# ./mit/uAMPS/sims directory and run 'ns genscen'.
topology_file="mit/uAMPS/sims/100nodes.txt"# Energy values. How much energy does each node have initially.
eq_energy=1
init_energy=2# stop is the time to stop the simulation if it is still running.
stop=1000# x,y is the size of the field.
x=100
y=100# Number of nodes. WARNING! This should be 1 higher then the number
# of nodes generated in the scenario.
nn=101RCA_LIBRARY=mit/rca
export RCA_LIBRARY
uAMPS_LIBRARY=mit/uAMPS
export uAMPS_LIBRARYns tcl/ex/wireless.tcl \
-sc mit/uAMPS/sims/nodescen.tcl \
-rp $alg \
-x $x \
-y $y \
-nn $nn \
-stop $stop \
-eq_energy $eq_energy \
-init_energy $init_energy \
-filename $filename \
-dirname $dirname \
-topo $topology_file \
-num_clusters $num_clusters \
-bs_x $bs_x \
-bs_y $bs_y \
2>$dirname/$filename.err 1>$dirname/$filename.out &
在 /ns-2.34 目录下直接执行 ./test 可能会提示权限不够,需要提高 test、leach_test 两个文件的权限,先在 /ns-2.34 下执行如下两行命令。
# 添加最高权限
chmod 777 test
chmod 777 leach_test
或者
# 只添加可执行权限
chmod u+x test
chmod u+x leach_test
在 /ns-2.34 目录下执行 ./test,查看在 mit/leach_sims/ 目录下生成的数据文件。
./test
根据 leach.err、leach.out 下的提示修改错误。
无错误情况下,在 leach.err 下只有两行输出,如下:
# leach.err #
mac/chanel.cc: INITIALIZE THE LIST xListHead
SORTING LISTS ...DONE!
无错误情况下,在 leach.out 文件末尾是结束时间和一些统计数据,大概如下:
At 520.02960000000007: - mit/uAMPS/stats.tcl
Total Energy = 197.37753015005549
Total Data = 10216
Total Alive = 5
Simulation complete. - mit/uAMPS/stats.tcl
运行必然会出错,错误处理如下:
(1)错误一
leach.err 中提示 source /home/xxx/ranode.tcl 等类似的文件找不到。主要是因为 uamps.tcl 文件中的 source 命令中的目录和我们的软件执行目录不匹配。test 文件是执行文件,在 /ns-2.34 目录下,因此应以 /ns-2.34 为执行过程中的执行过程中的当前目录。
修改 mit/uAMPS/sims/uamps.tcl 文件中的 7 个 source 后面的目录如下:
source mit/rca/ns-ranode.tcl
source mit/uAMPS/ns-bsapp.tcl
source mit/uAMPS/extras.tcl
source mit/uAMPS/stats.tcl
source mit/rca/resources/ns-resource-manager.tcl
source mit/rca/resources/ns-energy-resource.tcl
source mit/rca/resources/ns-neighbor-resource.tcl
(2)错误二
提示找不到 mit/uAMPS/sims/nodescen 文件,修改 nodescen 文件文件名
将 mit/uAMPS/sims/ 目录下的 nodescen 文件的文件名改为 nodescen.tcl,将 leach_test 文件中的执行语句也修改成 nodescen.tcl。两者一致即可。
-sc mit/uAMPS/sims/nodescen.tcl \
将 mit/uAMPS/sims/ 目录下的 genscen 文件的文件名改为 genscen.tcl,该文件是 tcl 脚本写的,用来产生 leach 协议中节点的随机部署。不修改也没有关系,修改之后更清晰。
(3)warnning
no class variable Phy/WirelessPhy:alive_
出现警告信息不用担心,协议可以正常运行,也可消除警告。在 tcl/lib/ns_default.tcl 文件第692行加入7个初始值,具体的初始值应该对照 mac 文件下的 wireless-phy.cc 值输入,如下所示:
Phy/WirelessPhy set alive_ 1 ;# 0 = dead, 1 =alive
Phy/WirelessPhy set Efriss_amp_ 100*1e-12 ;# Friss amp energy(J/bit/m^2)Phy/Wireless
Phy set Etwo_ray_amp_ 0.013*1e-12 ;#Two-ray amp energy (J/bit/m^4)
Phy/WirelessPhy set EXcvr_ 50*1e-9 ;# Xcvr energy(J/bit)
Phy/WirelessPhy set sleep_ 0 ;# 0 = awake, 1 =asleep
Phy/WirelessPhy set ss_ 1 ;# amountof spreading
Phy/WirelessPhy set dist_ 0 ;# approx. distance totransmitter
注意:这样修改之后,要重新编译,编译前需要先修改 Makefile 文件和 Makefile.in 文件
(4)修改 Makefile 文件
修改Makefile.in 文件
CC = gcc-4.4
CPP = g++-4.4
修改 Makefile 文件
CC = gcc-4.4
CPP = g++-4.4
在 DEFINE 行的最后添加
-DMIT_uAMPS
在INCLUDE行的中间添加
-I./mit/rca -I./mit/uAMPS \
在OBJ_CC 下面代码中gaf/gaf.o \之前添加,如下三行,注意,前面是一个 tab,后面不能有空格。
mit/rca/energy.o mit/rca/rcagent.o \
mit/rca/rca-ll.o mit/rca/resource.o \
mac/mac-sensor-timers.o mac/mac-sensor.o mit/uAMPS/bsagent.o \
make clean
make
./test
(5)修改 ns-bsapp.tcl 文件,其中有两处错误,这两处错误导致 PEGASIS 协议无法运行,对 LEACH 无影响。
搜索 “LEACH/PEGASIS” 找到如下两段代码,将其修改为 “Application/PEGASIS”。
修改前:
第一处代码:
if {$opt(rcapp) == "LEACH/LEACH-C" || \
$opt(rcapp) == "LEACH/PEGASIS" || \
$opt(rcapp) == "LEACH-C/StatClustering"} {
# 向代理层传参,由代理层负责簇首的优化选择。{普通节点数、簇数、退伙迭代次数、退火参数}
[$self agent] transfer_info [expr $opt(nn) - 1] \
$opt(num_clusters) \
$opt(bs_setup_iters) \
$opt(bs_setup_max_eps)
# 第二步:基站配置 leach-c
$ns_ at [expr $now_ + $opt(finish_adv)] "$self BSsetup"
}
第二处代码:
if {$opt(rcapp) == "LEACH/LEACH-C" || \
$opt(rcapp) == "LEACH/PEGASIS"} {
$ns_ at [expr [$ns_ now] + $opt(ch_change)] "$self BSsetup"
}
修改后
第一处代码:
if {$opt(rcapp) == "LEACH/LEACH-C" || \
$opt(rcapp) == "Application/PEGASIS" || \
$opt(rcapp) == "LEACH-C/StatClustering"} {
# 向代理层传参,由代理层负责簇首的优化选择。{普通节点数、簇数、退伙迭代次数、退火参数}
[$self agent] transfer_info [expr $opt(nn) - 1] \
$opt(num_clusters) \
$opt(bs_setup_iters) \
$opt(bs_setup_max_eps)
# 第二步:基站配置 leach-c
$ns_ at [expr $now_ + $opt(finish_adv)] "$self BSsetup"
}
第二处代码:
if {$opt(rcapp) == "LEACH/LEACH-C" || \
$opt(rcapp) == "Application/PEGASIS"} {
$ns_ at [expr [$ns_ now] + $opt(ch_change)] "$self BSsetup"
}
祝贺!
至此,ubuntu10.04、ns-2.34、leach 全部安装完毕,不过记得 —— 快照!