Tuesday, October 27, 2009

Virtualization VGA Driver Passthrough

Mr. Teo En Ming has posted a few blog entries on how to pass the host video driver through Xen to a virtual guest. This allows for 3D gaming in a guest environment, as well as other productive uses.

Wednesday, October 21, 2009

Shortcut: Pipe Your Output and Change Directory

Objective: Piping your output into the "cd" command can have many uses. I want a Google "I'm Feeling Lucky" option when locating files on my filesystem, thus saving me the time and energy of typing out a long path once I've found it's location.

Script:
# cd $( dirname $( locate type.in.search.value.here | head -n 1 ))

How it works: 
Let's work from the inside out. 
The "head" command by default prints the first 10 lines of a file. # head messages
With the -n switch we can decide exactly how many lines we want to print to the standard output or in this case, a variable.
The "locate" command is my current favorite search tool in Linux, just don't forget to use # updatedb to update your directory database so that this command works properly. 
Typically, if I run # locate daniel  I'd see /home/daniel plus every file in that directory on my terminal. When we pipe the output of #locate daniel to # head -n 1 then the output is only the first result. 
The markings $(   ) take what ever is inside the parenthesis and create somewhat of a temporary variable. This allows for a different kind of pipe. At the root try this # echo home | cd . Now, try this, 
# cd $( echo home ) . Similar way of piping commands, but sometimes it's better to use one than the other. 
The "dirname" command outputs whatever is behind the leading / of a path. # dirname /home/daniel would out put /home and # dirname /home/daniel/temp.txt would output /home/daniel. Because you can't change directory to a file name, this allows us to go to the containing folder for the file we are searching for. So we pipe the results of the dirname to cd, which takes us to our destination. 
That's a lot of typing, and in most cases it would be quicker to just type the path while hitting tab a couple times to complete long folder names. We can this as a function to your BASH profile, that is if BASH is your shell. BASH is all I know, but I'd love to post instructions for other shells if you got them. 
Open up /home/$user/.bashrc
# vi /home/daniel/.bashrc
Adding a function is easy by typing
function name.of.function
{
} 
Insert your code between the brackets, with one difference
function gimmie
{ # cd $( dirname $( locate $1 | head -n 1 )) } 
In BASH, the variable $1 is the value of first command line parameter, with $2 being the second and so on. This variable accepts whatever command parameter we pass to it.
# gimmie temp.txt
temp.txt will get passed to $1, locate will find its location, head will take the first result being /home/daniel/temp.txt, dirname will strip away the filename temp.txt and cd will change our present working directory to /home/daniel
Credits: Thanks for helping me come up with this script. 
LinuxQuestions.org members:
GrapefruiTgirl, PTrenholme, :::, catkin, Tinkster, pixellany      


Apache Tomcat 6.0 Setup (w/daemon)


Objective: Install Tomcat and run as a daemon. As this is my second Tomcat post, I have issues with their documentation. Here's my guide to getting Tomcat working. 

OS: CentOS 5.3
Tomcat Version: 6.0.20


Document Conventions:
$CATALINA_HOME is the root directory of your Tomcat installation.  


Preparation/Prerequisites: GCC, Autoconf, Java SDK
# yum install gcc 
# yum install autoconf 
# yum install java 
# yum install java-devel 

NOTE: Creating a tomcat user is completely optional but may help with error checking and security
# groupadd tomcat
# useradd -g tomcat -s /sbin/nologin -d /home/tomcat tomcat
# passwd tomcat
>tomcat 

Installation
# cd /usr/local/
# wget http://latest.version.of.tomcat
NOTE: Repositories may have an out of date version
# tar zxvf apache-tomcat-6.0.20.tar.gz
# chown -R tomcat.tomcat apache-tomcat-6.0.20
# rm apache-tomcat-6.0.20.tar.gz 

Daemon Configuration
NOTES FROM APACHE: Tomcat can be run as a daemon using the jsvc tool from the commons-daemon project. Source tarballs for jsvc are included with the Tomcat binaries, and need to be compiled. Building jsvc requires a C ANSI compiler (such as GCC), GNU Autoconf, and a JDK. Before running the script, the JAVA_HOME environment variable should be set to the base path of the JDK.
Alternately, *when calling the ./configure script, the path of the JDK may be specified using the --with-java parameter, such as ./configure --with-java=/.*
NOTE: With JDK 1.6 default is /usr/lib/jvm/java-1.6.0-openjdk.x86_64
     Using the following commands should result in a compiled jsvc binary, located in the $CATALINA_HOME/bin folder. This assumes that GNU TAR is used, and that CATALINA_HOME is an environment variable pointing to the base path of the Tomcat installation.
     Please note that you should use the GNU make (gmake) instead of the native BSD make on FreeBSD systems.

# cd $CATALINA_HOME/bin
# tar xvfz jsvc.tar.gz
# cd jsvc-src
# autoconf
# ./configure
# make
# cp jsvc ..
# cd .. 

Starting Tomcat Daemon
NOTE: It is possible to start the daemon using this command
# ./bin/jsvc -cp ./bin/bootstrap.jar \ -outfile ./logs/catalina.out -errfile ./logs/catalina.err \ org.apache.catalina.startup.Bootstrap
However, using the script template has given much more success.
# cd $CATALINA_HOME/bin/jsvc-src/native
# vi Tomcat.sh
# Adapt the following lines to your configuration
JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk.x86_64
CATALINA_HOME=/usr/local/apache-tomcat-6.0.20
DAEMON_HOME=/usr/local/apache-tomcat-6.0.20/bin
TOMCAT_USER=tomcat
TMP_DIR=/var/tmp
CATALINA_OPTS=
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$DAEMON_HOME/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar 

case "$1" in
  start)
    #
    # Start Tomcat
    #
    $DAEMON_HOME/jsvc \
    -user $TOMCAT_USER \
    -home $JAVA_HOME \
    -Dcatalina.home=$CATALINA_HOME \
    -Djava.io.tmpdir=$TMP_DIR \
    -outfile $CATALINA_HOME/logs/catalina.out \
    -errfile '&1' \
    $CATALINA_OPTS \
    -cp $CLASSPATH \
    org.apache.catalina.startup.Bootstrap
    #
    # To get a verbose JVM
    #-verbose \
    # To get a debug of jsvc.
    #-debug \

# chmod 755 Tomcat.sh
# cp Tomcat.sh /etc/rc.d/init.d/tomcat
# ./etc/rc.d/init.d/tomcat start 

To start/stop on boot/shutdown, create links in the different run level directories using this template:
# ln -s /etc/rc.d/init.d/tomcat /etc/rc.d/rc3.d/S86tomcat
# ln -s /etc/rc.d/init.d/tomcat /etc/rc.d/rc3.d/K14tomcat


Integrating Tomcat into Apache over Port 80

Objective: By default, Tomcat runs over port 8080. What if your organization, such as the US Government, likes to block port 8080 on the firewall? You can integrate Tomcat into Apache and run them both on port 80. This task is performed by the mod_jk.so module. When a request for a java page comes in over port 80, Apache will use this module to automatically forward the request to Tomcat. The biggest caveat to this task is the broken functionality of the auto-configurator in Tomcat 6.0  

OS: CentOS 5.3
Apache Version: 2.2
Tomcat Version: 6.0.20 

Prerequisites: Both Apache and Tomcat have been installed and are running properly. You can access the test page for both services over 80 and 8080 respectfully.
Mod_Jk.so: This is the module that will ultimately be the connection point between Apache and Tomcat. This can be obtained from: http://tomcat.apache.org/download-connectors.cgi It is suggested to use the precompiled binary, however the source is worth looking at as it contains a sample workers.properties file which will be created later in this document.
Document Conventions:
${tomcat_home} is the root directory of tomcat. I suggest using at least one symbolic link for your configuration files; this is from experience doing things the hard way.
#ln –s /usr/local/apache-tomcat.6.0.20     tomcat
${apache_home} is the root directory of apache. On CentOS 5.3 the default directory is /etc/httpd which contains symbolic links to (logs) /var/log/httpd, (modules) /usr/lib64/httpd/modules, and (run) /var/run. If your links or directories are different, please keep this in mind as this document cannot will reference these from time to time when necessary.

Instructions:
Place your obtained mod_jk.so module and place it in the preferred httpd/modules folder.
Create a workers.properties file in {$tomcat_home}/conf
It should, at minimum contain these lines:
worker.list=ajp13
worker.ajp13.type=ajp13
worker.ajp13.host=hostname.of.webserver (alias is acceptable, fqdn is recommended)
worker.ajp13.port=8009


Edit {$tomcat_home}/conf/server.xml
Change
To hostname.of.webserver">
Below the above statement add
Change   appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
To   appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

Create a mod_jk.conf file.
NOTE: Apache Tomcat documentation will reference a directory called {$tomcat_home}/conf/jk. This directory may not exist by default and the auto configuration file that is described does not get placed in this folder.
NOTE: There are two methods to create/modify your mod_jk.conf file – from a template or from scratch. It has been documented that the auto-configuration template option will be phased out in 7.0 and is currently pretty broken.

Method 1 – Auto-Configuration:
Make sure Tomcat is not running.
#cd {$tomcat_home}/bin/shutdown
#./shutdown.sh
Start Tomcat with the jkconf switch.
#./startup.sh –jkconf
Your newly created mod_jk.conf file will appear in {$tomcat_home}/conf/auto/
NOTE: Even after shutting down and restarting Tomcat without the jkconf switch, Tomcat may overwrite the mod_jk.conf file in the {$tomcat_home}/conf/auto/ directory. It is recommended that you move this file to {$tomcat_home}/conf/jk/
NOTE: It is possible that your mod_jk.conf file will work out of the box, however if you are not getting expected results, refer to method 2 and make any changes applicable.

Method 2 – Manual Configuration:
NOTE: The code below is based on the auto-configuration method. The comment lines are there to show what was changed to make this installation work properly. The virtualhost tags are only necessary if you have more than webhost/webserver on the same machine. If you receive this error in your logs when starting httpd, “JkWorkersFile cannot occur within section” either comment out the tags or move the JkWorkersFile line, both shown below.
Create a file called mod_jk.conf in /{$tomcat_home}/conf/jk/ with these lines:
LoadModule jk_module "/etc/httpd/modules/mod_jk.so"
JkWorkersFile /usr/local/tomcat/conf/workers.properties
JkLogLevel info
#JkOptions +ForwardDirectories
#hostname.of.webserver>
#ServerName hostname.of.webserver
JkMount /manager ajp13
JkMount /manager/* ajp13
JkMount /docs ajp13
JkMount /docs/* ajp13
JkMount /examples ajp13
JkMount /examples/* ajp13
JkMount /host-manager ajp13
JkMount /host-manager/* ajp13
#

NOTE: JkMount tells Apache which directories/files to redirect to Tomcat if those pages are requested. Add accordingly.

Editing httpd.conf
Open up your httpd.conf file (located in: /etc/httpd/conf)
Add Include {$tomcat_home}/conf/auto/mod_jk.conf
Because mod_jk.conf calls mod_jk.so make sure this line is not in effect:
LoadModule jk_module "/etc/httpd/modules/mod_jk.so"

Start/Restart Tomcat
Start/Restart Apache
If you are able to navigate to http://hostname.of.webserver/manager/html (the Tomcat manager webpage) then you have successfully integrated Tomcat into Apache.


Introduction

10 PRINT "Hello World!"
20 GOTO 10
I never thought I'd start a blog. In general, there isn't much that I have to say which is that important. Through my IT career I've found the answers to questions from a variety of sources including official documentation, fourms, blogs, and sites like answers.yahoo.com. I've learned information is valuable no matter where or who it comes from. This blog will primarily be dedicated to posting my own documentation to contribute to the pool of internet knowledge. It is my hope that I can provide someone with an answer that they can't find anywhere else. 

Thanks,
Daniel