Tag: linux

perl one liners = magic

perla word about the power of unix shell commands: perl one liners, i.e. one line of perl that can save you hours of error-prone hand changes. when i want to do some massive changes in my code this is what i use.

say you need replcae a record name from abc_record to xyz_record, first if you use perl recursivlly it will touch all files and revision control will makr them for commit. you don’t want that. so use grep to filter the relevant files:

grep "#abc_record\b" . -rl

next, do the replace:

perl -i -pe s/"#abc_record\b"/"xyz_record"/g `grep "#abc_record\b" src -rl`

note the use of \b to mark a word boundary.

a more complex replace is if you need to change the code from using a record to use a wrapper function. so for exmaple we have the record:

-record(abc_record, {fld}).

and we want to start use a wrapper module:

-module(rtools).
-export([fld/1]).
 
fld(R) -> R#abc_record.fld.

so we can do it instantly like that:

perl -i -pe s/"\b(\w+)#abc_record\.fld"/"rtools:fld(\1)"/g `grep "#abc_record\.fld" src -rl`

here we have switched from using A#abc_record.fld to record_tools:fld(A)

another trick is to remove complete lines (!), so for exmaple you can get rid of all the -compile(export_all) you have fullishly inserted into your code:

perl -i -nle 'print if !/-compile\(export_all\)/' `grep export_all src -rl`

one safty tip, omit the -i first to try before you actuall change files.

this was just a learn-by-example, more info can be found at:
http://sial.org/howto/perl/one-liner
http://www.unixguide.net/unix/perl_oneliners.shtml

yet another colinux instructions

colinuxi hope this is my last post on colinux and erlang, but although i had written several time about it, i will do it once more, to sum up a little guide to setup a new colinux with the correct way to set erlang on it. i also added few words on adding the nitrogen web framework.

> is used for windows command prompt
$ is used for linux shell prompt

ubuntu 8.04 server

qemu

  • direct download – http://www.h6.dion.ne.jp/~kazuw/qemu-win/qemu-0.9.0-windows.zip
  • extract qemu into a new directory c:\linux\qemu
  • start windows command prompt
  • make a 3gb image:
     > fsutil file createnew c:\linux\distro\qemu_ubuntu_3gb 3221257728
  • make a image for a 512mb swap file:
     > fsutil file createnew c:\linux\distro\qemu_swap_512mb 536903168
  • on c:\linux\qemu create the next batch file: install.bat, whith the following content:
    @ECHO OFF
    set qemu_dir=C:\linux\distro
    set hd=%qemu_dir%\qemu_ubuntu_3gb
    set swap=%qemu_dir%\qemu_swap_512mb
    set cdrom=%qemu_dir%\ubuntu-8.04.2-server-i386.iso
    set mem=384
    qemu -hda %hd% -hdb %swap% -cdrom %cdrom% -m %mem% -boot d -L .
    pause
  • run the batch file you have just created: install.bat
  • go through the linux installation, select manually partition when asked, and do the following:
    • create new partitionchoose /dev/sda
    • choose create as: primary partition
    • choose file system: ext3
    • create new partitionchoose /dev/sdb
    • choose create as: primary partition
    • choose file system: linux-swap
  • install, and when you get to the point it asks for restart – you have finished. it takes some time, so be patient.

colinux

  • direct download – http://downloads.sourceforge.net/colinux/coLinux-0.7.3.exe
  • run and install on c:\colinux
  • don’t use or download any of the suggested distributions on the install process
  • download and install unxutils – http://gnuwin.epfl.ch/apps/unxutils/en/install/
  • convert qemu ubuntu image to colinux image
     > cd c:\linux\distro
     > dd if=qemu_ubuntu_3gb of=ubuntu_3gb.img bs=512 skip=63
  • you only need the file ubuntu_3gb.img the other 2 files (qemu_swap_512mb and qemu_ubuntu_3gb) can be removed
  • create new swap file:
     > fsutil file createnew c:\linux\distro\swap_512mb 536870912
  • create a file on c:\linux\distro named ubuntu8.04.conf, and paste the following into it:
    kernel=vmlinux
    sda1="c:\linux\distro\ubuntu-8.04.2-server-i386.ext3.3gb.img"
    sdb1="c:\linux\distro\swap_512mb"
    root=/dev/sda1 fastboot 3
    ro
    mem=384
    eth0=slirp
    eth1=tuntap
  • create a shortcut for colinux-daemon.exe, right click on it and edit its properties, to add the following parameters (on the shortcut target field):
    c:\colinux\colinux-daemon.exe -t nt @c:\linux\distro\ubuntu8.04.conf
  • start this new shortcut, running the colinux the first time
  • edit /etc/network/interfaces and the windows tap connection, see my previous post named colinux
  • install ssh and update:
    $ sudo apt-get install ssh
    $ sudo apt-get update
    $ sudo apt-get upgrade
    $ chsh -s /bin/bash user_name
  • connect using putty to 192.168.37.20:22

compress and backup the colinux image

  • to determine the free space in megabytes
    $ df -m
  • fill image with zeros
    $ dd if=/dev/zero of=foobar bs=1M count='above result less 5'
    $ rm foobar
  • logout, and compress from windows using a compression app, should get around 100MB file

install erlang from source

$ sudo apt-get install build-essential
$ sudo apt-get install libncurses5-dev
$ sudo apt-get install m4
$ sudo apt-get install libssl-dev
$ sudo apt-get install openssl
 
$ cd; mkdir workspace; cd !
$ wget http://www.erlang.org/download/otp_src_R12B-5.tar.gz
$ tar xvfz otp_src_R12B-5.tar.gz
$ cd otp_src_R12B-5
$ ./configure
$ make
$ sudo make install

install some basics

$ sudo apt-get install git subversion ctags vim

setup environment

edit .bash_profile, and add the following at the bottom

export ERL_LIBS=${HOME}/erlang/lib

download and install nitrogen

$ cd; mkdir -p erlang/lib; cd !$
$ git clone git://github.com/rklophaus/nitrogen.git
$ cd nitrogen
$ make

done

colinux strikes back

because the colinux images are very old, i have decided to build one of my own, based on ubuntu 8.10. so, i followed this post, to create my colinux image. keep in mind installing ubuntu using qemu on windows takes ages (really, few hours, so schedule it to an hour before you go to sleep).

later, i just added another conf file to start this colinux, see more details on me previous post:

kernel=vmlinux
sda1="c:\linux\distro\ubuntu_8.10.ext3.3gb.img"
sda2="c:\linux\distro\ext3_disk_1gb.img"
sdb1="c:\linux\distro\swap_512mb"
root=/dev/sda1 fastboot 3
ro
mem=384
eth0=slirp
eth1=tuntap

moreover, i edited the network and samba, again, as i posted before. afterward i have installed erlang and yaws. finally, few fixes and tweaks i have encountered.

because i want it as a server:

$ sudo apt-get install linux-headers-server linux-image-server linux-server

to solve and odd error message on startup (mmap: Bad address):

$ sudo apt-get remove dmidecode --purge

to prevent yaws, and few other services i don’t need to start after boot:

$ sudo update-rc.d -f bluetooth remove
$ sudo update-rc.d -f gdm remove
$ sudo update-rc.d -f yaws remove

that’s it! if you got so far, you are now officialy an uber-geek ;)

colinux

every now and then i want the linux power on my windows. i tried all the workarounds: cygwin, unix-tools, vmware, virtual box, even the new microsoft powershell.

then i found colinux.

colinux actually runs a linux os as a windows program. i am not so strong on how it works, but i do use it and enjoy it, especially now when i develop on linux, and i don’t want to be remotely connected to another server all the time. i decided to share the setup i did for a colinux on my vista machine. here is the how-to, basically i followed the instructions at the colinux wiki:

  1. download colinux-stable from:
    http://sourceforge.net/projects/colinux/files
  2. download ubuntu distribution from the same address.
  3. install colinux c:\colinux, don’t download any distribution from the installer.
  4. unzip distribution files into c:\colinux.
  5. download 324Mb swap file from:
    http://gniarf.nerim.net/colinux/swap.
  6. unzip swap file into c:\colinux.
  7. edit c:\colinux\example.conf:
    cobd0="c:\colinux\Ubuntu-7.10.ext3.2gb.fs"
    cobd1="c:\colinux\swap_384Mb"
    root=/dev/cobd0
    ro
    initrd=initrd.gz
    mem=384
    eth0=slirp
    eth1=tuntap
  8. create and run batch file, with this content:
    colinux-daemon.exe -t nt @example.conf
  9. login using id: root, password: root.
  10. edit /etc/network/interfaces:
    auto eth0
    iface eth0
    inet staticaddress 10.0.2.15
    network 10.0.2.0
    broadcast 10.0.2.255
    netmask 255.255.255.0
    gateway 10.0.2.2
     
    auto eth1
    iface eth1
    inet staticaddress 192.168.37.20
    network 192.168.37.0
    netmask 255.255.255.0
    broadcast 192.168.37.255
     
    <li> edit tap adapter on windows: control panel\network and internet\network connections:
    <pre lang="text">properties, tcp/ipv4
    static ip: 192.168.37.10
    subnet mask: 255.255.255.0
    gateway: leave blank
  11. add new user, as root:
    useradd -d /home/user_name -g admin -m user_name 
    passwd user_name
  12. install ssh:
    sudo apt-get install ssh
  13. login using putty, connect to 192.168.37.20:22
  14. update system
    sudo apt-get update
    sudo apt-get upgrade
  15. change basic shell to bash:
    chsh -s /bin/bash user_name
  16. use samba to access linux machine from windows:
    sudo apt-get install samba
    sudo /etc/init.d/samba stop
  17. edit /etc/samba/smb.conf:
    [global]
    	workgroup = WORKGROUP
    [homes]
    	browseable=yes
    	writeable=yes
    	valid users = user_name
    	path = /home/user_name
  18. start samba again
    sudo /etc/init.d/samba start
  19.  add yourself as a samba user:
    sudo smbpasswd -L -a your_username
    sudo smbpasswd -L -e your_username
  20. on windows, right click on ‘my computer’ and choose ‘map network drive’, then enter: \\host_name\user_name, and fill your samaba password when asked to. if you haven’t changes the default host name then it is ‘ubuntu’
  21. that’s it :)

Powered by WordPress & Theme by Anders Norén