Sunday, August 26, 2012

Sync 2 repo

ref:http://git.wikia.com/wiki/Sync_2_repo
http://git-scm.com/book/en/Git-Branching-Remote-Branches

From here We might have the interesting issue, that there is a central "internal" git server, but we want to automatically synchronize it to some other "external" repository as well.
  • Creating a local bare repository on the central "internal" git server

Look also at the end of the link above for a simpler solution:
git clone --mirror

You might want to add groups and permission stuff before
ssh @146.124.44.190
cd /pub/git
...
Another experiment:
Assume the "internal" git server is here:
/home/rber/projects/ICOM/qt/try-it-icom/internal-git-server
Assume the "external" git server is here:
git://reliable.indefero.net/reliable/
On the "internal" git server we'll checkout the "external" repository:
cd /home/rber/projects/ICOM/qt/try-it-icom/internal-git-server
git clone --bare git://reliable.indefero.net/reliable/qt-everywhere-scripts.git
We must now manually configure the remote branch tracking and then synchronize the repositories:
cd /home/rber/projects/ICOM/qt/try-it-icom/internal-git-server/qt-everywhere-scripts.git
git remote add origin git://reliable.indefero.net/reliable/qt-everywhere-scripts.git
git fetch -v

Automatically synchronize:
git fetch origin
git merge origin

Friday, August 24, 2012

CentOS6安装gitosis服务器

ref: http://mcuos.com/thread-8372-1-1.html


1, 新加用户git,该用户将作为所有代码仓库和用户权限的管理者:
---------------------------------------
[guowenxue@centos6 ~]$ sudo useradd -m git
[guowenxue@centos6 ~]$ sudo passwd git
Changing password for user git.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.


2,切换到git用户,并初始化git帐户信息:
---------------------------------------
[guowenxue@centos6 ~]$ su git
Password: 
[git@centos6 guowenxue]$ cd
[git@centos6 ~]$ 

[git@centos6 ~]$ git config --global user.name "git"
[git@centos6 ~]$ git config --global user.email "git@email.com"

3,修改/etc/sudoers文件,把git用户添加到sudo里去,方便使用sudo管理:
---------------------------------------
[guowenxue@centos6 ~]$ sudo vim /etc/sudoers
****
git   ALL= NOPASSWD: ALL
****


4,安装一下python的setup tool, 这个是为了gitosis做准备:
---------------------------------------
Resolving mirrors.ustc.edu.cn... 202.141.160.110, 2001:da8:d800:95::110
Connecting to mirrors.ustc.edu.cn|202.141.160.110|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 343724 (336K) [application/x-redhat-package-manager]
Saving to: “python-setuptools-0.6.10-3.el6.noarch.rpm”

100%[==========================================================================================>] 343,724     1.73M/s   in 0.2s    

2012-07-10 23:49:22 (1.73 MB/s) - “python-setuptools-0.6.10-3.el6.noarch.rpm” saved [343724/343724]

[git@centos6 ~]$ ls
python-setuptools-0.6.10-3.el6.noarch.rpm
[git@centos6 ~]$ sudo rpm -ivh python-setuptools-0.6.10-3.el6.noarch.rpm 
Preparing...                ########################################### [100%]
        package python-setuptools-0.6.10-3.el6.noarch is already installed

5,下载并安装gitosis,官方的代码有问题,我们从github上下载:
---------------------------------------
[git@centos6 ~]$ git clone git://eagain.net/gitosis.git
Initialized empty Git repository in /home/git/gitosis/.git/
eagain.net[0: 208.78.102.120]: errno=Connection refused
fatal: unable to connect a socket (Connection refused)

早期的下载地址git://eagain.net/gitosis已经失效,作者已将其移到github上管理,下面是新的地址:

[git@centos6 ~]$ git clone git://github.com/tv42/gitosis.git
Initialized empty Git repository in /home/git/gitosis/.git/
remote: Counting objects: 630, done.
remote: Compressing objects: 100% (180/180), done.
remote: Total 630 (delta 442), reused 629 (delta 441)
Receiving objects: 100% (630/630), 97.75 KiB | 38 KiB/s, done.
Resolving deltas: 100% (442/442), done.
[git@centos6 ~]$ 

[git@centos6 ~]$ cd gitosis/
[git@centos6 gitosis]$ sudo python setup.py install 
running install
running bdist_egg
running egg_info
... ...
creating dist
creating 'dist/gitosis-0.2-py2.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing gitosis-0.2-py2.6.egg
creating /usr/lib/python2.6/site-packages/gitosis-0.2-py2.6.egg
Extracting gitosis-0.2-py2.6.egg to /usr/lib/python2.6/site-packages
Adding gitosis 0.2 to easy-install.pth file
Installing gitosis-init script to /usr/bin
Installing gitosis-run-hook script to /usr/bin
Installing gitosis-serve script to /usr/bin

Installed /usr/lib/python2.6/site-packages/gitosis-0.2-py2.6.egg
Processing dependencies for gitosis==0.2
Searching for distribute==0.6.10
Best match: distribute 0.6.10
Adding distribute 0.6.10 to easy-install.pth file
Installing easy_install script to /usr/bin
Installing easy_install-2.6 script to /usr/bin

Using /usr/lib/python2.6/site-packages
Finished processing dependencies for gitosis==0.2
[git@centos6 gitosis]$ 


6,配置gitosis的SSH管理帐号:
---------------------------------------
大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供
一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。
首先先确认一下是否已经有一个公钥了。SSH 公钥默认储存在账户的主目录下的 ~/.ssh 
目录。关键是看有没有用 something 和 something.pub 来命名的一对文件,这个 something 
通常就是 id_dsa 或 id_rsa。有 .pub 后缀的文件就是公钥,另一个文件则是密钥。假如没有这些文件,或者干脆连 .ssh 目录都没有,可以用 ssh-keygen 来创建。该程序在
Linux/Mac 系统上由 SSH 包提供,而在Windows 上则包含在 MSysGit 包里。
[git@centos6 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa):  它先要求你确认保存公钥的位置 
Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase):  它会让你重复一个密码两次,如果不想在使用公钥的时候输入密码,可以留空
Enter same passphrase again: 
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
d0:f9:5a:2a:ec:a0:69:1c:0e:aa:33:fe:1e:51:30:ca git@centos6.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|   o             |
|. . o  . .       |
| E   .. o        |
|    .  . .       |
|   .    S o      |
|. . ..   +       |
|.+ o. o o        |
|+ +o.o .         |
|+==o  .          |
+-----------------+
[git@centos6 ~]$ 

7,创建git存放代码的仓库:
---------------------------------------
默认gitosis-init会将git仓库放在git用户的home的repositories目录下,而我们想把仓库
放在/opt/git/repositories,这时我们可以做个符号链接:

[git@centos6 ~]$ sudo mkdir -p /opt/git/repositories
[git@centos6 ~]$ sudo chown -R git.git /opt/git/
[git@centos6 ~]$ sudo chmod -R 700 /opt/git/
[git@centos6 ~]$ ln -s /opt/git/repositories/
[git@centos6 ~]$ gitosis-init < .ssh/id_rsa.pub  (execute as root)
Initialized empty Git repository in /opt/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /opt/git/repositories/gitosis-admin.git/
[git@centos6 ~]$ ll
total 340
drwxr-xr-x 8 git git   4096 Jul 11 00:25 gitosis
-rw-rw-r-- 1 git git 343724 Jul  3  2011 python-setuptools-0.6.10-3.el6.noarch.rpm
lrwxrwxrwx 1 git git     22 Jul 11 00:25 repositories -> /opt/git/repositories/
[git@centos6 ~]$ ls repositories/
gitosis-admin.git



8,把现成的git仓库embededproject-code放到新建的服务器仓库中去,并重命名为embededproject.git:
---------------------------------------
[guowenxue@centos6 ~]$ git clone --bare embededproject-code embededproject.git
Initialized empty Git repository in /home/guowenxue/embededproject.git/
[guowenxue@centos6 ~]$ sudo mv embededproject.git/ /home/git/repositories
[guowenxue@centos6 ~]$ sudo chown -R git.git /home/git/repositories/embededproject.git
[guowenxue@centos6 ~]$ sudo ls -ld /home/git/repositories/embededproject.git
drwxr-xr-x 7 git git 4096 Jul 11 00:52 /home/git/repositories/embededproject.git


9,把guowenxue这个帐户添加到embededproject项目开发中去,对整个项目可读写
---------------------------------------
9.1 使用guowenxue帐户生成SSH的公钥和私钥文件:
----------
[guowenxue@centos6 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/guowenxue/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/guowenxue/.ssh/id_rsa.
Your public key has been saved in /home/guowenxue/.ssh/id_rsa.pub.
The key fingerprint is:
46:0b:cc:41:7c:d1:fe:18:b0:03:d6:0b:03:fa:b6:99 guowenxue@centos6.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|    .+o..o       |
|   . o*.+ .      |
|  .  .+=.=       |
|   .   o+.o      |
|    o   S. +     |
|   . + .  . .    |
|    E            |
|                 |
|                 |
+-----------------+
[guowenxue@centos6 ~]$ ls .ssh/id_rsa*
.ssh/id_rsa  .ssh/id_rsa.pub

9.2 使用git管理帐户把gitosis的管理仓库克隆出来便于管理
----------
[git@centos6 ~]$ git clone git@127.0.0.1:gitosis-admin.git
Initialized empty Git repository in /home/git/gitosis-admin/.git/
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is b6:17:e1:4f:d7:89:b7:ec:f5:7c:35:65:4f:fd:1a:db.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (RSA) to the list of known hosts.
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
Receiving objects: 100% (5/5), done.
[git@centos6 ~]$ ls gitosis-admin/
gitosis.conf  keydir

9.3 将guowenxue这个开发者的公钥文件拷贝到gitosis-admin/keydir目录下并重命名
----------
[git@centos6 ~]$ cd gitosis-admin/
[git@centos6 gitosis-admin]$ sudo cp /home/guowenxue/.ssh/id_rsa.pub keydir/guowenxue.pub

9.4  修改gitosis.conf配置文件
----------
创建组embededproject_rw,设置该组的成员guowenxue,让他对embededproject项目有读写的权限:
[git@centos6 gitosis-admin]$ vim gitosis.conf 
[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = git@centos6.localdomain guowenxue

[group embededproject_rw]
writable = embededproject
members = guowenxue

9.5 更新最新的配置和公钥文件到管理库中去:
----------
[git@centos6 gitosis-admin]$ git add .
[git@centos6 gitosis-admin]$ git commit -m"update gitosis.conf file and add guowenxue account to access embededproject embededproject"
[master f8722ce] update gitosis.conf file and add guowenxue account to access embededproject embededproject
2 files changed, 2 insertions(+), 2 deletions(-)
rewrite keydir/guowenxue.pub (93%)
[git@centos6 gitosis-admin]$ git push
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 758 bytes, done.
Total 5 (delta 2), reused 0 (delta 0)
To git@127.0.0.1:gitosis-admin.git
   43a0ffe..f8722ce  master -> master


9.6 测试使用guowenxue这个账户从服务器上下载代码,并修改提交了:
----------
[guowenxue@centos6 ~]$ git clone git@192.168.1.78:embededproject.git
Initialized empty Git repository in /home/guowenxue/embededproject/.git/
remote: Counting objects: 961, done.
remote: Compressing objects: 100% (885/885), done.
remote: Total 961 (delta 354), reused 0 (delta 0)
Receiving objects: 100% (961/961), 59.91 MiB | 22.87 MiB/s, done.
Resolving deltas: 100% (354/354), done.
[guowenxue@centos6 ~]$ cd embededproject
[guowenxue@centos6 embededproject]$ ls
doc  platform  program  README  rootfs  systools
[guowenxue@centos6 embededproject]$ vim README
[guowenxue@centos6 embededproject]$ git add README 
[guowenxue@centos6 embededproject]$ git commit -m"update README file for test"
[master 8527f03] update README file for test
1 files changed, 0 insertions(+), 3 deletions(-)
[guowenxue@centos6 embededproject]$ git push
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 306 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@192.168.1.78:embededproject.git
   12ceafa..8527f03  master -> master
[guowenxue@centos6 embededproject]$ git log
commit 8527f0382f23a3c9b5c9f2b8d5c122f5bc3b810d
Author: guowenxue <guowenxue@gmail.com>
Date:   Wed Jul 11 01:39:06 2012 +0800

    update README file for test



10. 把dglwx这个帐户添加到embededproject项目中去,对整个项目只读:
---------------------------------------
10.1 生产dglwx帐户的SSH公钥和私钥文件
----------
[dglwx@centos6 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dglwx/.ssh/id_rsa): 
Created directory '/home/dglwx/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/dglwx/.ssh/id_rsa.
Your public key has been saved in /home/dglwx/.ssh/id_rsa.pub.
The key fingerprint is:
0e:9f:0d:7d:2e:e2:e0:aa:a7:64:3f:47:ca:d2:44:e5 dglwx@centos6.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|      .          |
|     o           |
|    . E  .       |
|   .  . S . .    |
|    . .+ + o     |
|  o+ o. = o .    |
| o..*..o . .     |
|  o=o+. .        |
+-----------------+
[dglwx@centos6 ~]$ 


10.2 使用git帐户添加dglwx到项目中去,并给只读权限:
----------
[git@centos6 ~]$ cd gitosis-admin/
[git@centos6 gitosis-admin]$ ls
gitosis.conf  keydir
[git@centos6 gitosis-admin]$ sudo cp /home/dglwx/.ssh/id_rsa.pub keydir/dglwx.pub
[git@centos6 gitosis-admin]$ vim gitosis.conf 

[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = git@centos6.localdomain guowenxue

[group embededproject_rw]
writable = embededproject
members = guowenxue

[group embededproject_ro]
readonly = embededproject
members = dglwx

[git@centos6 gitosis-admin]$ git add .
[git@centos6 gitosis-admin]$ git commit -m"Add dglwx account to access embededproject by readonly"
[master 967be92] Add dglwx account to access embededproject by readonly
2 files changed, 5 insertions(+), 0 deletions(-)
create mode 100644 keydir/dglwx.pub
[git@centos6 gitosis-admin]$ git push
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 840 bytes, done.
Total 5 (delta 1), reused 0 (delta 0)
To git@127.0.0.1:gitosis-admin.git
   f8722ce..967be92  master -> master
[git@centos6 gitosis-admin]$ 
[git@centos6 gitosis-admin]$


10.3 测试下载代码,修改并提交
----------
[dglwx@centos6 embededproject]$ git config --global user.name "dglwx"
[dglwx@centos6 embededproject]$ git config --global user.email "dugulengwuxue@gmail.com"
[dglwx@centos6 ~]$ git clone git@192.168.1.78:embededproject.git
Initialized empty Git repository in /home/dglwx/embededproject/.git/
The authenticity of host '192.168.1.78 (192.168.1.78)' can't be established.
RSA key fingerprint is b6:17:e1:4f:d7:89:b7:ec:f5:7c:35:65:4f:fd:1a:db.
Are you sure you want to continue connecting (yes/no)? yes
Initialized empty Git repository in /home/dglwx/embededproject/.git/
remote: Counting objects: 964, done.
remote: Compressing objects: 100% (888/888), done.
remote: Total 964 (delta 356), reused 0 (delta 0)
Receiving objects: 100% (964/964), 59.91 MiB | 22.96 MiB/s, done.
Resolving deltas: 100% (356/356), done.
[dglwx@centos6 ~]$ cd embededproject/
[dglwx@centos6 embededproject]$ ls
doc  platform  program  README  rootfs  systools
[dglwx@centos6 embededproject]$ vim README 
[dglwx@centos6 embededproject]$ git diff
diff --git a/README b/README
index 71c8795..ab90846 100644
--- a/README
+++ b/README
@@ -160,6 +160,4 @@ rm -f zImage
build.sh  linux-3.0  linux-3.0.tar.bz2  patch
[guowenxue@centos6 kernel]$ ls linux-3.0/uImage.gz 
linux-3.0/uImage.gz
-
-
-
+Modify for test
[dglwx@centos6 embededproject]$ vim README ^C
[dglwx@centos6 embededproject]$ git add README 
[dglwx@centos6 embededproject]$ git commit -m"update README for test"
[master 0cc5ef0] update README for test
1 files changed, 1 insertions(+), 3 deletions(-)
[dglwx@centos6 embededproject]$ git push
ERROR:gitosis.serve.main:Repository write access denied
fatal: The remote end hung up unexpectedly
[dglwx@centos6 embededproject]$

Wednesday, August 22, 2012

Setting up an SSL secured Webserver with CentOS

ref: http://wiki.centos.org/HowTos/Https

Setting up an SSL secured Webserver with CentOS



This guide will explain how to set up a site over https. The tutorial uses a self signed key so will work well for a personal website or testing purposes. This is provided as is so proceed at your own risk and take backups!

1. Getting the required software


For an SSL encrypted web server you will need a few things. Depending on your install you may or may not have OpenSSL and mod_ssl, Apache's interface to OpenSSL. Use yum to get them if you need them.
yum install mod_ssl openssl

Yum will either tell you they are installed or will install them for you.

2. Generate a self-signed certificate


Using OpenSSL we will generate a self-signed certificate. If you are using this on a production server you are probably likely to want a key from Trusted Certificate Authority, but if you are just using this on a personal site or for testing purposes a self-signed certificate is fine. To create the key you will need to be root so you can either su to root or use sudo in front of the commands
# Generate private key 
openssl genrsa -out ca.key 1024 

# Generate CSR 
openssl req -new -key ca.key -out ca.csr

# Generate Self Signed Key
openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt

# Copy the files to the correct locations
cp ca.crt /etc/pki/tls/certs
cp ca.key /etc/pki/tls/private/ca.key
cp ca.csr /etc/pki/tls/private/ca.csr

WARNING: Make sure that you copy the files and do not move them if you use SELinux. Apache will complain about missing certificate files otherwise, as it cannot read them because the certificate files do not have the right SELinux context.

If you have moved the files and not copied them, you can use the following command to correct the SELinux contexts on those files, as the correct context definitions for /etc/pki/* come with the bundled SELinux policy.
restorecon -RvF /etc/pki

Then we need to update the Apache SSL configuration file
vi +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf

Change the paths to match where the Key file is stored. If you've used the method above it will be
SSLCertificateFile /etc/pki/tls/certs/ca.crt

Then set the correct path for the Certificate Key File a few lines below. If you've followed the instructions above it is:
SSLCertificateKeyFile /etc/pki/tls/private/ca.key

Quit and save the file and then restart Apache
/etc/init.d/httpd restart

All being well you should now be able to connect over https to your server and see a default Centos page. As the certificate is self signed browsers will generally ask you whether you want to accept the certificate. Firefox 3 won't let you connect at all but you can override this.

3. Setting up the virtual hosts


Just as you set VirtualHosts for http on port 80 so you do for https on port 443. A typical VirtualHost for a site on port 80 looks like this

        
        AllowOverride All
        
        DocumentRoot /var/www/vhosts/yoursite.com/httpdocs
        ServerName yoursite.com


To add a sister site on port 443 you need to add the following at the top of your file
NameVirtualHost *:443

and then a VirtualHost record something like this:

        SSLEngine on
        SSLCertificateFile /etc/pki/tls/certs/ca.crt
        SSLCertificateKeyFile /etc/pki/tls/private/ca.key
        
        AllowOverride All
        
        DocumentRoot /var/www/vhosts/yoursite.com/httpsdocs
        ServerName yoursite.com


Restart Apache again using
/etc/init.d/httpd restart

4. Configuring the firewall


You should now have a site working over https using a self-signed certificate. If you can't connect you may need to open the port on your firewall. To do this amend your iptables rules:
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
/sbin/service iptables save
iptables -L -v

Tuesday, August 21, 2012

Tomcat – Java.Lang.OutOfMemoryError: PermGen Space


Tomcat production server sometime will hit the following java.lang.OutOfMemoryError: PermGen space error.
java.lang.OutOfMemoryError: PermGen space
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
It’s usually happened when the Tomcat start and stop few times. It’s just funny, however you can fine tune it with some minor changes in the Tomcat configuration setting. By default, Tomcat assigned very little memory for the running process, you should increase the memory by make change in catalina.sh or catalina.bat file.

How To Fix It?

1) Find where is Cataline.sh located. We need to make some changes in “catalina.sh” file. 
P.S Cataline.sh is located at \tomcat folder\bin\catalina.sh
2) Assign following line to JAVA_OPTS variable and add it into catalina.sh file.
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
Partial example of the catalina.sh file
#   JSSE_HOME       (Optional) May point at your Java Secure Sockets Extension
#                   (JSSE) installation, whose JAR files will be added to the
#                   system class path used to start Tomcat.
#
#   CATALINA_PID    (Optional) Path of the file which should contains the pid
#                   of catalina startup java process, when start (fork) is used
#
# $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $
# -----------------------------------------------------------------------------
 
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
-Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
 
 
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
os400=false
darwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
Darwin*) darwin=true;;
esac
 
# resolve links - $0 may be a softlink
PRG="$0"
3) Done. Restart Tomcat.
You should change the “Xms” and “PermSize” value base on your server capability.

Tuesday, July 24, 2012

CentOS minimal 安装笔记

ref: http://blog.csdn.net/lixianlin/article/details/7293691


现在CentOS最新版是6.2,提供了最小化安装的iso镜像(32位的285M,64位的322M),以前5.x版中是没有单独提供的,对于我们这些搞开发来的,是个福音,不管是下载,还是安装,都省去不少时间,存储空间都节省了不少。
下了个64位的,用WMware(7.1.4版)安装,一路下来,除了中间提示缺少gcc、kernel-devel、make等包外(最小化安装版嘛,少些包,很正常,选择"Continue"跳过),安装相当顺利,全自动的。
注意:安装的时候,如果想使用图形安装界面,内存设置必须大于652M。
The installer needs at least 392MB of memory to work. Text mode will automatically be used if the system has less than 652MB of memory.
安装完了,自动重启,结果悲剧了,启动不了,提示下面的信息:
Installing VMware Tools, please wait...
mount: special device /dev/hda does not exist
mount: block device /dev/sr0 is write-protected, mounting read-only
./run_upgrade.sh: line 21: file: command not found
仔细想了想,安装过程中未出现什么错误,验证了一下iso文件的hash,对的,安装包是完整的。
google了一下,没找到相关的资料,于是只好自己动手...
重新检查了各项设置,发现硬件设置中多了一个CD/DVD(IDE),装载了autoinst.iso,另一个CD/DVD 2(IDE)装载了安装镜像CentOS-6.2-x86_64-minimal.iso,重新建了个虚拟机,发现在选择安装镜像文件那里,有个提示“CentOS 64-bit detected.This operating system will use Easy Install.“,猜测问题可能是因为这个快速安装引起的(因为现在安装的镜像是一个最小化安装版,并不是完整版)。
猜测归猜测,实践了才知道。所以又新建了个虚拟机,在安装镜像选择那里,选择“I will install the operating system later.“,创建完后,硬件设置中设置安装镜像,一路手动安装下来,比自动安装稍微慢了一点,安装完毕,手动重启,启动成功,看来刚才猜测是对的。
安装过程最好配置好网络,因为这个最小化版不包含图形界面的网络设置工具,手动配置不太方便,以下是我上面的配置:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
NM_CONTROLLED=yes
ONBOOT=yes
HWADDR=00:0c:29:5b:1c:de
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.1.223
NETMASK=255.255.255.0
PREFIX=24
GATEWAY=192.168.1.1
DNS1=192.168.1.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
USERCTL=no
vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
vi /etc/resolv.conf
nameserver 192.168.1.1
网络配置好了,就可以用yum来安装一些常用的软件:
yum install vim
yum install wget
yum install zip
yum install unzip
yum install man
yum install gcc
yum install make
yum install setuptool
yum install system-config-network-tui
yum install system-config-firewall-tui
......
安装图形界面:
yum groupinstall "X Window System"
yum groupinstall "Chinese Support"
yum groupinstall "KDE Desktop"
安装完成后,用"init 5"即可启动图形界面。

Tuesday, May 15, 2012

1. Convert project to JavaEE project.
ref: http://www.mkyong.com/java/how-to-convert-java-project-to-web-project-in-eclipse/
Here is the steps to convert Java Project to Web Project in Eclipse
1) Create a empty dynamic web project and click on the .project file
2) Copy “web project” facet buildCommand and paste within “java project” .project buildSpec tag
 

 org.eclipse.wst.common.project.facet.core.builder
 
 

3) Copy “web project” facet nature and paste within “java project” .project natures tag
org.eclipse.wst.common.project.facet.core.nature
P.S Be careful of the .project xml tag, make sure you paste into a correct location.
4) Save it.
5) Right click on Java Project, click properties.
6) Select Project Facets and click modify project button.
7) Click Dynamic Web Module and Java check box.
Finished, Now your java project has been converted into a web project.
2. Config the WebContent folder
Update org.eclipse.wst.common.component file in .settings directory and wb-resource elements:

3. Add User lib's *.jar files into *war


The jar dependencies for Eclipse to include in a war file not placed in WEB-INF/lib is not managed by the usual "project export/imports" but by a separate panel (3.6: Deployment Assembly, 3.5: Java EE Module Dependencies) in the preferences of the dynamic web project.

      


Tuesday, May 8, 2012

Using make and writing Makefiles

Ref: http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html#creating


Using make and writing Makefiles

make is a Unix tool to simplify building program executables from many modules. make reads in rules (specified as a list of target entries) from a user created Makefile. make will only re-build things that need to be re-built (object or executables that depend on files that have been modified since the last time the objects or executables were built).
GNU make Manual. A complete reference for writing makefiles from simple to advanced features.
Also, CMake, is a cross platform build system. Among other things, cmake will generate makefiles for you. It is particularly useful for large projects, for builds that use lots of libraries, and for dealing with platform-specific compilation issues. It automatically generates (often hard to read and debug) makefiles for different platforms. For small projects, writing makefiles by hand is likely easier. See Andy Danner's Cmake documentation for more information on cmake.

Using make

  1. Create a Makefile listing the rules for building the executable the file should be named 'Makefile' or 'makefile'. This only has to be done once, except when new modules are added to the program, the Makefile must be updated to add new module dependencies to existing rules and to add new rules to build the new modules.
  2. After editing program file(s), rebuild the executable by typing make:
     % make 
    
        A specific target in the Makefile can be executed by typing:
     % make target_label
    
        For example, to execute the rm commands in the example makefile below, type:
     % make clean
    

Creating a Makefile

A Makefile typically starts with some variable definitions which are then followed by a set of target entries for building specific targets (typically .o & executable files in C and C++, and .class files in Java) or executing a set of command associated with a target label.The following is the generic target entry form:
# comment
# (note: the  in the command line is necessary for make to work) 
target:  dependency1 dependency2 ...
       command

for example:
#
# target entry to build program executable from program and mylib 
# object files 
#
program: program.o mylib.o
 gccx -o program program.o mylib.o

An example simple Makefile for a Java

#
# A simple makefile for compiling three java classes
#

# define a makefile variable for the java compiler
#
JCC = javac

# define a makefile variable for compilation flags
# the -g flag compiles with debugging information
#
JFLAGS = -g

# typing 'make' will invoke the first target entry in the makefile 
# (the default one in this case)
#
default: Average.class Convert.class Volume.class

# this target entry builds the Average class
# the Average.class file is dependent on the Average.java file
# and the rule associated with this entry gives the command to create it
#
Average.class: Average.java
        $(JCC) $(JFLAGS) Average.java

Convert.class: Convert.java
        $(JCC) $(JFLAGS) Convert.java

Volume.class: Volume.java
        $(JCC) $(JFLAGS) Volume.java

# To start over from scratch, type 'make clean'.  
# Removes all .class files, so that the next make rebuilds them
#
clean: 
        $(RM) *.class
Here is an example of a Java makefile that uses more advanced makefile syntax.

An example simple Makefile for a C (or C++)

#
# This is an example Makefile for a countwords program.  This
# program uses both the scanner module and a counter module.
# Typing 'make' or 'make count' will create the executable file.
#

# define some Makefile variables for the compiler and compiler flags
# to use Makefile variables later in the Makefile: $()
#
#  -g    adds debugging information to the executable file
#  -Wall turns on most, but not all, compiler warnings
#
CC = gcc
CFLAGS  = -g -Wall

# typing 'make' will invoke the first target entry in the file 
# (in this case the default target entry)
#
default: count

# To create the executable file count we need the object files
# countwords.o, counter.o, and scanner.o:
#
count:          countwords.o counter.o scanner.o
  $(CC) $(CFLAGS) -o count countwords.o counter.o scanner.o

# To create the object file countwords.o, we need the source
# files countwords.c, scanner.h, and counter.h:
#
countwords.o:   countwords.c scanner.h counter.h
                $(CC) $(CFLAGS) -c countwords.c

# To create the object file counter.o, we need the source files
# counter.c and counter.h:
#
counter.o:      counter.c counter.h
                $(CC) $(CFLAGS) -c counter.c

# To create the object file scanner.o, we need the source files
# scanner.c and scanner.h:
#
scanner.o:      scanner.c scanner.h
                $(CC) $(CFLAGS) -c scanner.c

# To start over from scratch, type 'make clean'.  This
# removes the executable file, as well as old .o object
# files and *~ backup files:
#
clean:
                rm -f count *.o *~

Another makefile (using makedepend and more advanced make syntax)

This is an easier to use and modify makefile, but it is slightly more difficult to read than the simple one:
#
# 'make depend' uses makedepend to automatically generate dependencies 
#               (dependencies are added to end of Makefile)
# 'make'        build executable file 'mycc'
# 'make clean'  removes all .o and executable files
#

# define the C compiler to use
CC = gcc

# define any compile-time flags
CFLAGS = -Wall -g

# define any directories containing header files other than /usr/include
#
INCLUDES = -I/home/newhall/include  -I../include

# define library paths in addition to /usr/lib
#   if I wanted to include libraries not in /usr/lib I'd specify
#   their path using -Lpath, something like:
LFLAGS = -L/home/newhall/lib  -L../lib

# define any libraries to link into executable:
#   if I want to link in libraries (libx.so or libx.a) I use the -llibname 
#   option, something like (this will link in libmylib.so and libm.so:
LIBS = -lmylib -lm

# define the C source files
SRCS = emitter.c error.c init.c lexer.c main.c symbol.c parser.c

# define the C object files 
#
# This uses Suffix Replacement within a macro:
#   $(name:string1=string2)
#         For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:.c=.o)

# define the executable file 
MAIN = mycc

#
# The following part of the makefile is generic; it can be used to 
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#

.PHONY: depend clean

all:    $(MAIN)
        @echo  Simple compiler named mycc has been compiled

$(MAIN): $(OBJS) 
        $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file) 
# (see the gnu make manual section about automatic variables)
.c.o:
        $(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@

clean:
        $(RM) *.o *~ $(MAIN)

depend: $(SRCS)
        makedepend $(INCLUDES) $^

# DO NOT DELETE THIS LINE -- make depend needs it