Basic ArchLinux Setting(i386)

###ArchLinux Installation First we download the iso from the archlinux.org, then using iso to boot. Partition it into many disks as you like.

Now begin to install:

	$ mount /dev/sda2 /mnt
	$ swapon /dev/sda1
	$ pacstrap /mnt base
	$ genfstab -p /mnt >> /mnt/etc/fstab

Chroot into the newly installed system:

	$ arch-chroot /mnt
	$ ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
	$ vi /etc/locale.gen
	# enable en_US related
	$ locale-gen
	$ vi /etc/mkinitcpio.conf
	# we can remain the default, notice if you use usb, enable usb related. 
	$ mkinitcpio -p linux
	$ passwd root

Grub install and configure:

	$ pacman -S grub
	$ grub-install --target=i386-pc --recheck --debug /dev/sda
	$ grub-install --target=i386-pc --recheck --debug /dev/sda

Install vim:

	$ pacman -S vim

Install dhcpcd:

	$ pacman -S dhcpcd
	$ systemctl enable dhcpcd@enp0s3
	$ pacman -S net-tools # for using ifconfig

Install openssh:

	$ pacman -S openssh
	$ systemctl start sshd
	$ systemcrl enable sshd.service

###Build rsim3 on ArchLinux First download the package from rsim3. Install the base-devel:

	pacman -S base-devel

Install boost, boost-libs, libpcap, cppunit:

	pacman -S boost boost-libs libpcap cppunit

Then you can enjoy the compiling and get the result.

Use Redis For Inter-Communication

###Play With Redis On ArchLinux, we install redis via:

	$ sudo pacman -S redis

Enalbe and start the redis.service:

	$ sudo systemctl enable redis.service
	$ sudo systemctl start redis.service
	$ ps -ef | grep redis
	redis     7609     1  0 16:03 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379

Play with redis:

	[Trusty@DashArch queue]$ redis-cli 
	127.0.0.1:6379> set name leezk
	OK
	127.0.0.1:6379> get name
	"leezk"
	127.0.0.1:6379> del name
	(integer) 1
	127.0.0.1:6379> exists name
	(integer) 0
	127.0.0.1:6379> exit

###RQ: Simple Job Queue For Python Install redis and rq:

	$ sudo pip2 install redis
	$ sudo pip2 install rq

Install requests for debugging(Not related with RQ and Redis):

	$ sudo pip2 install requests

Use following file for RedisQueue:

import redis

class RedisQueue(object):
    """Simple Queue with Redis Backend"""
    def __init__(self, name, namespace='queue', **redis_kwargs):
        """The default connection parameters are: host='localhost', port=6379, db=0"""
        self.__db= redis.Redis(**redis_kwargs)
        self.key = '%s:%s' %(namespace, name)

    def qsize(self):
        """Return the approximate size of the queue."""
        return self.__db.llen(self.key)

    def empty(self):
        """Return True if the queue is empty, False otherwise."""
        return self.qsize() == 0

    def put(self, item):
        """Put item into the queue."""
        self.__db.rpush(self.key, item)

    def get(self, block=True, timeout=None):
        """Remove and return an item from the queue. 

        If optional args block is true and timeout is None (the default), block
        if necessary until an item is available."""
        if block:
            item = self.__db.blpop(self.key, timeout=timeout)
        else:
            item = self.__db.lpop(self.key)

        if item:
            item = item[1]
        return item

    def get_nowait(self):
        """Equivalent to get(False)."""
        return self.get(False)


Testing the RedisQueue with following: Put something into the RedisQueue:

	>>> from RedisQueue import RedisQueue
	>>> q = RedisQueue('test')
	>>> q.put('hello World!')

Fetch something from the RedisQueue:

	>>> from RedisQueue import RedisQueue
	>>> q = RedisQueue('test')
	>>> q.get()
	'hello World!'

###Rewrite Dictionary Program

Re-compile Kernel For Supporting 512M Memory For RaspberryPI In Qemu

###Cross Compiler Prepration Get the cross-compiler from github:

	[Trusty@XXXyyy tools]$ pwd
	/media/y/raspberryPI/tools
	[Trusty@XXXyyy tools]$ git clone git://github.com/raspberrypi/tools.git

Add the cross-compiler to system path:

	export PATH="/media/y/raspberryPI/tools/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH"

Now input “arm-linux-g” + tab you will see the cross-compiler is ready. ###Prepare the kernel Get the kernel source from github:

	git clone git://github.com/raspberrypi/linux.git

Now we need to get the patches for supporting the raspberryPI, Torlus has done the patches which could also be downloaded from github:

Now we need to configure the kernel:

	cd linux
	make ARCH=arm versatile_defconfig

NO, I MADE A MISTAKE, IT SHOULDN’T COMPILE KERNEL, SHOULD COMPILE QEMU!!!

###Compile qemu Get the modified qemu branch from github:

	git clone git://github.com/Torlus/qemu.git

Switch to ‘rpi’ branch:

	git fetch origin
	git branch -v -a # List the available branches.
	git checkout -b raspberry origin/rpi	# Checkout the origin/rpi to local raspberry
	git fetch
	git checkout raspberry

Begin to configure qemu, notice we have to use python2 for configuring the qemu:

	workon venv2
	./configure --help
	./configure --target-list="arm-softmmu arm-linux-user" --enable-sdl 

Now begin to make the qemu, use -j8 for speeding up, adjust the number according to your own machine:

	make -j8

Now under the following directory you will get the qemu-system-arm:

	$ pwd
	/media/y/raspberryPI/qemu/arm-softmmu

Get the kernel.img from the SD image.

###Use the compiled qemu for running We have to change the scripts for running the qemu:

	/media/y/raspberryPI/qemu/arm-softmmu/qemu-system-arm -net nic,macaddr=$macaddr -net user -kernel kernel.img -cpu arm1176 -m 512 -M raspi -no-reboot -serial stdio -append "rw earlyprintk loglevel=8 panic=120 keep_bootcon rootwait dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1024 bcm2708_fb.fbheight=768 bcm2708.boardrev=0xf bcm2708.serial=0xcad0eedf smsc95xx.macaddr=B8:27:EB:D0:EE:DF sdhci-bcm2708.emmc_clock_freq=100000000 vc_mem.mem_base=0x1c000000 vc_mem.mem_size=0x20000000  dwc_otg.lpm_enable=0 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait" -sd 2014-01-07-wheezy-raspbian.img  -device usb-kbd -device usb-mouse -usbdevice net 

Now we can run the raspberryPI in QEMU with 512MB memory, but, the network is still unavailable.

Building Qemu Based RaspberryPI Development Environment

First download the latest image from http://www.raspberrypi.org/downloads, mine is Wheezy. And we also have to download the qemu-compatible kernel from following address:

	$ wget http://xecdesign.com/downloads/linux-qemu/kernel-qemu

Change the img file according to http://localhost/blog/2013/09/04/qemu-for-raspberrypi/, follow this tutorial, you have to change the img file size and its content, but we have to do some modifications. The run-qemu file is changed to following:

	#!/bin/bash
	USERID=$(whoami)
	
	# Get name of newly created TAP device; see https://bbs.archlinux.org/viewtopic.php?pid=1285079#p1285079
	precreationg=$(/usr/bin/ip tuntap list | /usr/bin/cut -d: -f1 | /usr/bin/sort)
	sudo /usr/bin/ip tuntap add user $USERID mode tap
	postcreation=$(/usr/bin/ip tuntap list | /usr/bin/cut -d: -f1 | /usr/bin/sort)
	IFACE=$(comm -13 <(echo "$precreationg") <(echo "$postcreation"))
	
	# This line creates a random mac address. The downside is the dhcp server will assign a different ip each time
	printf -v macaddr "52:54:%02x:%02x:%02x:%02x" $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff )) $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff ))
	# Instead, uncomment and edit this line to set an static mac address. The benefit is that the dhcp server will assign the same ip.
	# macaddr='52:54:be:36:42:a9'
        macaddr = '52:54:79:3c:80:c0'
	 
	qemu-system-arm -net nic,macaddr=$macaddr -net tap,ifname="$IFACE" -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" $*
	  
	sudo ip link set dev $IFACE down &> /dev/null
	sudo ip tuntap del $IFACE mode tap &> /dev/null

In router, we add the static address for mac corresponding to 10.0.0.168, you can alter it according to your habit.

Now we want to disable the X at every startup and use vnc instead. And we can overlocking the raspberryPI to 1000MHZ, this will greatly improve the performance.

Enable the vncserver:

	$ apt-get install tightvncserver vim 

Now startup the vncserver and use vncviewer to view the desktop:

/images/Screenshot-QEMU.png

No, the correct method is listed as:

	cat /eroot@raspberrypi:~# cat /etc/init.d/startvnc 
	#!/bin/sh -e
	### BEGIN INIT INFO
	# Provides:          vncserver
	# Required-Start:    networking
	# Default-Start:     3 4 5
	# Default-Stop:      0 6
	### END INIT INFO
	
	PATH="$PATH:/usr/X11R6/bin/"
	
	# The Username:Group that will run VNC
	export USER="pi"
	#${RUNAS}
	
	# The display that VNC will use
	DISPLAY="1"
	
	# Color depth (between 8 and 32)
	DEPTH="16"
	
	# The Desktop geometry to use.
	#GEOMETRY="<WIDTH>x<HEIGHT>"
	#GEOMETRY="800x600"
	#GEOMETRY="1024x768"
	GEOMETRY="1280x1024"
	
	# The name that the VNC Desktop will have.
	NAME="my-vnc-server"
	
	OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
	
	. /lib/lsb/init-functions
	
	case "$1" in
	start)
	log_action_begin_msg "Starting vncserver for user '${USER}' on   localhost:${DISPLAY}"
	su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
	;;
	
	stop)
	log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
	su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
	;;
	
	restart)
	$0 stop
	$0 start
	;;
	esac
	
	exit 0

And now we can add this script into /etc/rc.local as:

	# Start the vncserver here:
	/etc/init.d/startvnc start

So everytime we startup the qemu based raspberryPI, we can easily attached to its geometry, and we can easily adapt the resolution.

Moving Weather App to BBB

Following is the steps for moving the weather app to BBB(BeagleBone Black) ###Apache Configuration Create the site definition file under /etc/apache2/sites-available:

	$ cp default nanjing

Edit the nanjing file with the following content:

	$ cat nanjing
	<VirtualHost *:7778>
		ServerAdmin webmaster@localhost
	        ServerName nanjing
	        ServerAlias nanjing.weather
	
		DocumentRoot /srv/www1
		<Directory />
			Options FollowSymLinks
			AllowOverride None
		</Directory>
		<Directory /srv/www1/>
			Options Indexes FollowSymLinks MultiViews
			AllowOverride None
			Order allow,deny
			Allow from all
		</Directory>
	
		ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
		<Directory "/usr/lib/cgi-bin">
			AllowOverride None
			Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
			Order allow,deny
			Allow from all
		</Directory>
	
		ErrorLog ${APACHE_LOG_DIR}/error.log
	
		# Possible values include: debug, info, notice, warn, error, crit,
		# alert, emerg.
		LogLevel warn
	
		CustomLog ${APACHE_LOG_DIR}/access.log combined
	</VirtualHost>

Then we have to edit the /etc/apache2/ports.conf file, to add the newly-added site definition file.

	$ cat ports.conf
	NameVirtualHost *:7777
	Listen 7777
	NameVirtualHost *:7778
	Listen 7778
	NameVirtualHost *:7779
	Listen 7779
	NameVirtualHost *:7780
	Listen 7780

Now you already could visit the site of http://Your_Ip_Address:7777 for the nanjing weather infos. ###Script Moving Be sure following modules has been installed:< br />

	$ pip install django pywapi pinyin
	$ pip install BeautifulSoup

Add the crontab tasks:

	0 */1 * * * python /root/code/genhtml.py
	15 */1 * * * python /root/code/genhtml_bj.py
	30 */1 * * * python /root/code/genhtml_changsha.py

Now the script will be run every hour at 0/15/30 minutes, enjoy it. Notice the timezone should be correctly configured:

	$  dpkg-reconfigure tzdata
	# Choose Asia/Shanghai