Tuesday, February 2, 2016

Install ShareLaTex on Centos/Ubuntu/Debian server with CSF Firewall enabled

Today I spent a couple of hours to install ShareLaTex on a CentOS server so that I can collaborate with some other people on publishing papers...

The best way to install ShareLaTex on any platform is to use their Docker image (Instructions here).

docker run -d \
  -v ~/sharelatex_data:/var/lib/sharelatex \
  --name=sharelatex \
  -p 3000:80 \
  sharelatex/sharelatex

The above command installs SharedLaTex docker image, shares Host's ~/sharelatex_data folder to save data inside and forwards port 3000 of the host machine to the docker so that ShareLaTex can be accessible from host external IP address. (IP:3000)

If you have CSF firewall installed, docker0 interface won't work correctly and the host machine will not be accessible from the container. This can be fixed by adding the following entries to /etc/csf/csfpost.sh

iptables -t nat -N DOCKER
iptables -t nat -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE

iptables -t filter -N DOCKER
iptables -t filter -A FORWARD -o docker0 -j DOCKER
iptables -t filter -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -t filter -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
iptables -t filter -A FORWARD -i docker0 -o docker0 -j ACCEPT

iptables -A -A FORWARD -i docker0 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o docker0 -j ACCEPT
iptables -A INPUT -i docker0 -j ACCEPT

csfpost.sh ought to be executable (+x).
Also we should whitelist local IP address of the container by adding the following line to /etc/csf/csf.allow:
172.17.0.0/16

Now restart CSF and enjoy your ShareLaTex installation:
csf -r

That's it!

Wednesday, January 6, 2016

[Tutorial] Install zerofree on CentOS 6, 7, Redhat, Fedora, ...

You might need zerofree when you want to compact your VM's hard disk. There's no RPM package of this application for Centos 6 or 7 (rhel 6 & rhel 7) so the only way is to compile it from the source. It goes like this:

yum install e2fsprogs-devel -y
wget http://frippery.org/uml/zerofree-1.0.3.tgz
tar -zxf zerofree-1.0.3.tgz
cd zerofree-1.0.3

make


after successfully building the application, copy it to /usr/bin

cp zerofree /usr/bin

Now you can remount the destination partition as read-only and zerofree it!

You can also use dd instead of zerofree:

dd if=/dev/zero of=test.file
 
...wait for the virtual disk to fill, then
 
rm test.file

and shut the VM down. Then on your VirtualBox host do:
 
VBoxManage modifyhd --compact yourImage.vdi

Saturday, September 19, 2015

Configure PETSc and Eclipse Parallel on OSX / Linux [C/C++/Fortran]

Nowadays I'm working on my master project which requires use of PETSc framework in c++ language.
Those who develop Finite Element codes know how essential it is to use Sparse matrices and fast solvers to solve FEM's stiff matrix. PETSc does this, it makes it extremely easy to define and use sparse matrices and provides many direct/iterative modern solvers.

The steps below are checked on both Mac OSX Yosemitte and Linux Ubuntu 14.04 LTS. Installation on other linux distributions is the same, just the package names differ (openmpi-devel on redhat vs openmpi-dev on debian).
1- Install openmpi, blas and lapack using your OS package manager. You need to install their devel pacakges if you're using linux.

2- Download latest version of PETSc from their website. I recommend the lite version.

3- Extract the package and enter it.

4- Set these two environment variables in your bash_rc:
PETSC_DIR= /path/to/your/petsc/directory
PETSC_ARCH=debug


5- run ./configure

PETSc will automatically detect your mpi compilers and gives you the next command to compile the whole package.


6- Follow the commands that PETSc gives you until the compilation / testing finish.

7- you compiled the debug version of PETSc. now again, set the PETSC_ARCH environment variable value to:
PETSC_ARCH=release
and run ./configure with no debugging:

./configure --with-debugging=0 COPTFLAGS='-O3' CXXOPTFLAGS='-O3' FOPTFLAGS='-O3'
and again follow PETSc commands to finish compilation.

8- It's now the time to choose an IDE. I myself recommend Eclipse. It's free and lightweight and supports C, C++ and Fortran.
Download the "Eclipse for Parallel Application Developers" from their website.
Eclipse requires Java which can easily get installed from OS package manager.
If you want to program Fortran in Eclipse, you need to download and install the Photran plugin on Eclipse.

9- Openmpi compilers are installed in /usr/local/bin on Mac OSx. You might get the following errors in case the /usr/local/bin path isn't loaded by default in Eclipse:
"/bin/sh: mpicc: command not found"
"/bin/sh: mpic++: command not found"
"/bin/sh: mpif90: command not found"

The fix is simple:
cd /usr/bin
sudo ln -s /usr/local/bin/mpicc
sudo ln -s /usr/local/bin/mpicxx
sudo ln -s /usr/local/bin/mpic++
sudo ln -s /usr/local/bin/mpif90

10- Open Eclipse and create a new MPI C/C++/Fortran project. Check if you can run hello world without any problem.
Now it's time to set eclipse to include PETSc library. (Read PETSc Manual -> OTHER PETSC FEATURES -> Eclipse Users)

Right click on your project -> Properties.
10-1- C/C++ Build -> Environment
Add PETSC_DIR with path to your directory. Then add PETSC_ARCH with value of "debug" for the "Debug" configuration and "release" for the "Release" configuration of Eclipse.
10-2- C/C++ Build -> Settings -> C++ Linker -> Libraries ("Fortran Build -> Settings -> Fortran Linker -> Libraries" in case of Fortran compiler)
Add
petsc
to list of Libraries and
${PETSC_DIR}/${PETSC_ARCH}/lib
to "Library Search Path":

10-3- C/C++ Build -> Settings -> C++ Compiler -> Includes ("Fortran Build -> Settings -> Fortran Compiler -> Directories" in case of Fortran compiler)
Add the following values to Include Paths :
${PETSC_DIR}/include
${PETSC_DIR}/${PETSC_ARCH}/include

10-4 C/C++ Build -> Settings -> C++ Linker ->Miscellaneous ("Fortran Build -> Settings -> Fortran Linker -> Miscellaneous" in case of Fortran compiler)
Add
-Wl,-rpath,"${PETSC_DIR}/${PETSC_ARCH}/lib"
to Linker flags:

10-5- That's all, check if you can run PETSc hello world:

#include <iostream>
#include "petsc.h"
using namespace std;

int main(int argc, char *argv[]) {
    PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
    PetscPrintf(PETSC_COMM_WORLD,"Hello World\n");
    PetscFinalize();

    return 0;
}

On Mac OSX:


On Ubuntu 14.04 LTS:


10-6- Fortran compiler needs the following extra setting:

10-6-1 Fortran Build -> Settings -> Fortran Compiler -> Miscellaneous
Add "-cpp -dM" flag.



That's all!

Wednesday, October 22, 2014

Amazon Glacier Backup tools on your server | cPanel, Virtualmin, Directadmin, ...

Amazon Glacier is a cost-effective way to store your backup data.
Recently I set up a cPanel server for weekly database backups on glacier. Here are the steps:

First, install glacier-cmd:

yum install git python
cd /usr/src
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
# For cpanel:
ln -s /usr/local/cpanel/3rdparty/bin/git /usr/bin/git
#
git clone https://github.com/uskudnik/amazon-glacier-cmd-interface
cd amazon-glacier-cmd-interface
python setup.py install

 
Open ~/.glacier-cmd and enter your credential details:

[aws]
access_key=YOUR_ACCESS_KEY
secret_key=YOUR_SECRET_KEY

[glacier]
region=us-west-1
bookkeeping=false
bookkeeping-domain-name=backups
logfile=~/.glacier-cmd.log
loglevel=INFO
output=print





Now you can use the following simple script and cronjob to set a weekly backup:
/script/backup:

#!/bin/sh
NOW=$(date +"%d-%m-%Y")
rm -fr /home/backup-tmp
mkdir /home/backup-tmp
mysqldump -A > /home/backup-tmp/backup-all-db-$NOW.sql
gzip /home/backup-tmp/backup-all-db-$NOW.sql
glacier-cmd upload --description "$NOW Backup" VAULT_NAME /home/backup-tmp/backup-all-db-$NOW.sql.gz



Cronjob:

5 8 * * 6       /script/backup







Monday, May 26, 2014

Fortran Tips

Here I've listed some common fortran tips:

-  The form of the information contained in a variable can also be declared implicitly, e.g. IMPLICIT DOUBLE PRECISION (A-H, O-Z) . [Fortran fundamentals] (Read more)
 -  A program may contain only one blank COMMON block but any number of named COMMON blocks. (Read more)

Thursday, April 17, 2014

CFD: Non-uniform grid mesh for 2D Rectangular - Fortran code

Result:

Here's part of the code I used to generate a non-uniform grid using fortran. To understand meaning of exp_x & exp_y powers, you need a piece of paper to sketch and calculate it by yourself.



exp_x=1.05 !x-axis expansion factor
exp_y=1.10 !y-axis expansion factor

a=0.
b=1.
c=0.
d=1.

!***********************************
! Constructing non-uniform grid !
!***********************************
! del_x & del_y : Uniform step size in X & Y direction
! eta_x & eta_y : First non-uniform step size in X & Y direction
! i_th step size is : eta_x*expansion_factor^step
del_x=(b-a)/n_x
del_y=(d-c)/n_y
eta_x=(b-a)*(1-exp_x)/(2*(1-exp_x**(n_x/2)))
eta_y=(d-c)*(1-exp_y)/(2*(1-exp_y**(n_y/2)))

PRINT *, "Uniform X-Step:", del_x
PRINT *, "Uniform Y-Step:", del_y
PRINT *, "First Non-uniform X-Step:", eta_x
PRINT *, "First Non-uniform Y-Step:", eta_y

x(1)=a
y(1)=c
! X_START to Middle_X
DO i=2,n_x/2+1
       x(i)=x(i-1)+eta_x*exp_x**(i-2)
END DO
! Middle_X to End
DO i=n_x/2+2,n_x+1
       x(i)=x(i-1)+eta_x*exp_x**(n_x+1-i)
END DO
! Y_START to Middle_Y
DO j=2,n_y/2+1
       y(j)=y(j-1)+eta_y*exp_y**(j-2)
END DO
! Middle_Y to Y_END
DO j=n_y/2+2,n_y+1
       y(j)=y(j-1)+eta_y*exp_y**(n_y+1-j)
END DO
!***********************************
! non-uniform grid done !
!***********************************
 

Sunday, January 5, 2014

Deactive "sort by subject" of roundcube

In roundcube webmail client when you click on "subject" column, it will sort all your mails by subject! I don't know if it's useful or not but many times occured that I uncounsioucly clicked on this and it took long time to sort all messages by subject! Here's how to deactivate column sorting by subject :
- Open file : program/steps/mail/func.inc
- Search for "./#sort", for me it's around line 443. Then replace :
'onclick' => 'return '.JS_OBJECT_NAME.".command('sort','".$col."',this)
with :
'onclick' => 'return true'

That's it!

How to Stream RTSP / Webcam / IP Camera Over the Web

I had a Hikvision IPC-B120 that provides a simple RTSP stream, which I could view in VLC. I also wanted to see my Logitech BRIO’s feed in a ...