Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Tuesday, August 9, 2011

Automating Eclipse launch

I've recently taken up the Eclipse team at Google (we integrate Google's internal build tools and distributed development environment with the Eclipse Platform) and we are constantly faced with the challenge of maintaining several distributions in a multi-user environment.


I thought I'd use some of the goodness we've come up with in my home environment too, so have put together a much-scaled-down version of our launch script, for my personal use - and thought I'd share this more broadly, as I'm sure other folks will find this useful too.


I generally have multiple installation and user configuration profiles, and even from the desktop launcher, I much prefer to use launch scripts than just pointing the shortcut to the binary executable.


Here is my generic configuration script:
#!/bin/bash
#
# Runs the stable version of Eclipse.
# By default, it will launch Indigo, installed in /opt/eclipse; different
# versions can be launched by invoking this script and setting the
# ECLIPSE_INSTALL and CONFIG variables to point to the correct places.
#
# The default configuration directory is set to '/home/$USER/.eclipse/eclipse37'
# but this can be changed exporting a different value for CONFIG.
# See /home/marco/bin/eclipse-helios for an example of how to do this.

##############################
# Globals (default value):
#
#   LD_LIBRARY_PATH           (/usr/local/lib:/usr/lib)
#     library search path for C++ dynamic libraries
#
#   ECLIPSE_INSTALL           ()
#     installation directory
#
#   CONFIG                    ()
#     user's configuration directory
#
#   MEM_ALLOC_POOL, PERM_SIZE (2,048MB, 256MB)
#     Memory allocation and PermGen size
#
#   JVM                       (/usr/local/java/bin/java)
#     Java VM executable (java)
#
# By setting any of the variables above after source`ing this script
# and before invoking launch_eclipse() the default value(s) can be changed
##############################
declare ECLIPSE_INSTALL
declare CONFIG
declare MEM_ALLOC_POOL
declare PERM_SIZE
declare JVM="/usr/local/java/bin/java"

##############################
# Concatenates input paramters to LD_LIBRARY_PATH, if not already present
##############################
munge_library_path() {
  for dir in $@; do
    if [ -n "${dir}" ]; then
      local is_present=`echo $LD_LIBRARY_PATH | grep ${dir} | wc -l`
      if [ ${is_present} -eq 0 ]; then
        export LD_LIBRARY_PATH=${dir}:$LD_LIBRARY_PATH
      fi
    fi
  done
}

##############################
# Sets up global variables with default values if not already set
##############################
check_dirs() {
  # Installation directory
  if [ -z "${ECLIPSE_INSTALL}" ]; then
    echo "You must set ECLIPSE_INSTALL to the Eclipse installation directory"
    exit -1
  fi
  if [ ! -d ${ECLIPSE_INSTALL} ]; then
    echo "Cannot find Eclipse installation directory (${ECLIPSE_INSTALL})"
    exit -1
  fi

  # Configuration directory, user-specific
  if [ ! -d "${CONFIG}" ]; then
    mkdir -p ${CONFIG}
  fi
}


##############################
# The location of the JVM, currently the default (OpenJDK 6)
##############################
locate_jvm() {
  if [ ! -e "${JVM}" ]; then
    echo "Could not locate a valid JVM at ${JVM}"
    echo "Trying to locate a valid java installation"
    JVM=`which java`
    if [ -z "${JVM}" ]; then
      echo "Could not find a valid JRE, giving up"
      return -1
    fi
    echo "Found a JRE at ${JVM}"
  fi
}

##############################
# Runs a few checks, sets up the LD library path and then launches Eclipse
#
# Param:
#   clean   will set the '-clean' Eclipse option that will clear the plugins cache
##############################
launch_eclipse() {
  check_dirs
  locate_jvm
  munge_library_path /usr/local/lib /usr/lib

  if [ "$1" == "clean" ]; then
    CLEAN="-clean"
  fi

  $ECLIPSE_INSTALL/eclipse $CLEAN -vm $JVM -configuration $CONFIG \
      -bundlepool $CONFIG/plugins \
      -vmargs -Xmx${MEM_ALLOC_POOL:-"2048M"} -XX:MaxPermSize=${PERM_SIZE:-"256M"}
}
This is used in the actual shell script (called, unimaginatively, eclipse):
#!/bin/bash
#
# Runs the stable version of Eclipse.
# By default, it will launch Indigo, installed in /opt/eclipse; different
# versions can be launched by invoking this script and setting the
# ECLIPSE_INSTALL and CONFIG variables to point to the correct places.
#
# See eclipse_config.sh for more details

source /home/marco/bin/eclipse_config.sh

ECLIPSE_INSTALL="/opt/eclipse"
CONFIG="/home/${USER}/.eclipse/eclipse37"

launch_eclipse


What is left is to just point a desktop (or menu) launcher to this script and replace the generic icon with the one in the eclipse/ folder (icon.xpm)




CREDITS -- I would very much like to gratefully acknowledge Machtelt Garrels and his much-consulted "Bash Guide for Beginners;" I never seem able to remember the bash conditional operators!

Tuesday, May 24, 2011

Ubuntu: Network Manager is evil

Warning - this post if filled with hatred, if you believe in forgiveness, you may want to look away now.

In my book, the only sin worse than uselessness, is actively causing damage and waste of time.
What punishment ought to be meted then, to something that, having proven to be useless, causes untold waste of time.

Yes, NetworkManager, and nm-applet: I'm looking at you two.

Quite apart from never having quite understood what use one could have for either, I have found a number of situations in which NM has caused waste of time, only the last of which has been to prevent a KVM virtual machine I just created to connect to the network, despite all my efforts, searching on the Web, trying out countless variations on configurations and, yes, several reboots.

I have been following along the instructions given here, only to be unable to connect in UserMode, barely managing (via the Bridged interface) to connect to my host machine, and totally failing to obtain any connection to the outside world.

Enter

sudo apt-get purge network-manager network-manager-gnome

and, hey, presto, all is good and well, and I can happily connect from my guest OS to the Internet.
(see also here)

Simply changed my /etc/network/interfaces file to reflect my settings (YMMV, adjust to your LAN settings) and all works just fine:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto br0
iface br0 inet static
        address 192.168.1.10
        network 192.168.1.0
        netmask 255.255.255.0
        broadcast 192.168.1.255
        gateway 192.168.1.1
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

Just remember to restart the network after you have made the changes:

sudo /etc/init.d/networking restart


Die in pain, Network Manager, and may the world be a better place without you.

Sunday, May 8, 2011

Skype on Ubuntu 10.04 - mic doesn't work

I got the following fix from here, but it does not tell the entire story:
(my setup: Acer Aspire One AO751h,
Ubuntu Lucid 10.04.2 LTS)




  •  
    I had the same problem on an Acer Timeline for a long time, but the balance trick works.


    Solution:
    A better way than changing the output balance is to use alsamixer or pavucontrol and mute one of the input channels. In alsamixer: F4 to change to recording, left/right to move to microphone, and z or c to mute one channel. In pavucontrol go to the "input devices" tab, unlock the channels, and mute one of the channels.


    It seems the normal volume control uses one balance slider for all inputs and outputs together. 

    The reality is that two poorly designed (and worse implemented) pieces of software interact in a way to make it virtually impossible for the average user to figure out what's going on:

    • Pulse Audio -- it still amazes me how a simple problem (enable audio on a PC) could be made so complicated and unusable;
    • Skype for Linux -- a real disgrace that a company of the might and power of Skype could let us down so badly: I just wish they had the decency of release the software open source, so we could fix it ourselves!
    Be that as it may, in addition to the 'fix' above, you need to also disable the "Allow Skype to adjust mixer levels" in the Sound Options tab of Skype - otherwise you can see skype continually adjusting your microphone's gain down until it gets to the point of being unaudible.

    Leaving it here for future reference: I don't expect Skype to fix this anytime soon!

    Tuesday, November 30, 2010

    Using Boost in Ubuntu with Eclipse

    Boost is an open source library of extremely useful and carefully designed C++ classes and methods ranging from graph algorithms, to regular expressions matching, to multi-threading.
    Part of the Boost library was also integrated into the C++ standard as the TR1 set of libraries.
    You can learn more about Boost here.
    Using it with Eclipse in Ubuntu is definitely possible, but not straightforward; so I decided to post this simple tip here, to help folk spare some of the grief.
    The first step in using Boost in your code is to download the header (.hpp) files and (optionally) source code - the latest release is 1.45 and is available from Boost's website.
    In theory, you could compile it, and build the libraries' binaries yourself: by all means, go ahead and do it, but there is an easier way, if you can live with a slightly 'older' version.

    Ubuntu 9.10 (Karmic Koala) comes with Boost 1.34 pre-configured (I think, this is, at any rate, the version that I had on my system):
    $ ls /usr/lib/libboost*
    /usr/lib/libboost_date_time-gcc41-1_34_1.so.1.34.1
    /usr/lib/libboost_date_time-gcc41-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_date_time-gcc42-1_34_1.so.1.34.1
    /usr/lib/libboost_date_time-gcc42-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_filesystem-gcc41-1_34_1.so.1.34.1
    /usr/lib/libboost_filesystem-gcc41-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_filesystem-gcc42-1_34_1.so.1.34.1
    /usr/lib/libboost_filesystem-gcc42-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_iostreams-gcc41-1_34_1.so.1.34.1
    /usr/lib/libboost_iostreams-gcc41-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_iostreams-gcc42-1_34_1.so.1.34.1
    /usr/lib/libboost_iostreams-gcc42-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_regex-gcc41-1_34_1.so.1.34.1
    /usr/lib/libboost_regex-gcc41-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_regex-gcc42-1_34_1.so.1.34.1
    /usr/lib/libboost_regex-gcc42-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_signals-gcc41-1_34_1.so.1.34.1
    /usr/lib/libboost_signals-gcc41-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_signals-gcc42-1_34_1.so.1.34.1
    /usr/lib/libboost_signals-gcc42-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_thread-gcc41-mt-1_34_1.so.1.34.1
    /usr/lib/libboost_thread-gcc42-mt-1_34_1.so.1.34.1

    However, by using Synaptic, you can actually install the more recent 1.40.0 version:


    This will install a bunch of libboost_*.so.1.40.0 files into /usr/lib: in order to match the libraries with the header files (and avoid introducing incomprehensible compilation errors at best, and subtle bugs at worst) you should download the matching boost_1_40_0.tar.bz2 from Boost download archives.

    We would be almost there, were it not for the fact that the soname does not really comply with the -l gcc linker option.

    In gcc the -L option specifies a 'search directory' (this would not be needed, as /usr/lib is searched for libraries by default) and the -l specifies additional dynamic (.so) or static (.a) libraries: you specify the file to include during the link step by omitting the `lib` prefix and the .so or .a extension.
    In other words, if your HelloBoost source uses code from Boost Regex (libboost_regex.so) you would specify it like thus:
    $ gcc -Wall hello_boost.cc -o HelloBoost -lboost_regex

    You can see where I'm heading with all this: the last step will be to add symbolic links that do away with the soname suffixes of Boost libraries, and all will be well:
    $ ln -s /usr/lib/libboost_regex.so.1.40.0 /usr/lib/libboost_regex.so

    Given that there are in total 24 files for the whole of Boost 1.40, I've done a bit of find & replace magic, and have come up with the following shell file (the other advantage being that, when a later version becomes available for Ubuntu, I will not have to change any of my Eclipse projects setting, but just change the VERSION value in the script):
    #!/bin/bash
    #
    # Simple script to enable Boost with simple -l gcc option
    # Use: gcc -Wall hello_world.cc -o HelloWorld -lboost_regex
    #
    # Created by M. Massenzio, 2010-11-30
    
    VERSION=1.40.0
    
    for libname in libboost_date_time \
                libboost_filesystem \
                libboost_graph_parallel  \
                libboost_graph      \
                libboost_iostreams  \
                libboost_math_c99f  \
                libboost_math_c99l  \
                libboost_math_c99   \
                libboost_math_tr1f  \
                libboost_math_tr1l  \
                libboost_math_tr1   \
                libboost_mpi        \
                libboost_prg_exec_monitor \
                libboost_program_options  \
                libboost_python-py25      \
                libboost_python-py26      \
                libboost_regex            \
                libboost_serialization    \
                libboost_signals          \
                libboost_system           \
                libboost_thread           \
                libboost_unit_test_framework \
                libboost_wave \
                libboost_wserialization 
    do
      ln -s /usr/lib/$libname.so.$VERSION /usr/local/lib/$libname.so
    done
    


    The last and final step is to tell Eclipse what to look for when building your binary: right-click on your project's folder (in the C++ Perspective, Project Explorer) then Properties > C/C++ Build > Settings, select GCC C++ Linker > Libraries and add the 'stripped' library names in the option list:


    Note for the curious: the additional libraries listed there are for Google C++ Unit testing framework, GUnit, available here - you will have to build it, but that's pretty straightforward, resulting in the two libgtest.a and libgtest_main.a libraries - be careful to add those only to your 'Test' configuration and exclude your unit tests from build in your Release/Debug configurations.

    Saturday, November 20, 2010

    Dump CPU temperature data to a file in Ubuntu

    Update: I've figured out that it makes a lot more sense to have a reading of the CPU load to correlate with the temperature reading, so I've added that too using /usr/bin/uptime.

    Sunday, November 8, 2009

    rsync on an SSHFS mount point

    Turns out that
    "If you plan [to] use rsync to sync files from your local machine to your ssh-server, you need "workaround=rename"
    Otherwise, you will not be able to update an older version of a file with a newer one. Without this option, you will be able to create files and folders and delete them, but you will get an error like this, if you try to update a file:
    rsync: rename "/path/to/file.Q3Btvn" -> "to/file": Operation not permitted (1)

    The original entry was from a comment to this entry in Ubuntu Forums; reporting it here, as it was driving me insane (been wasting quite some time trying to figure whether it was a permission issue).

    The complete /etc/fstab entry in my system looks something like:
    # This mounts the entire filesystem of server on /mnt/server mountpoint
    # Use with care!
    sshfs#marco@server:/ /mnt/server fuse users,auto,workaround=rename 0 0

    However, if you are mounting a Samba file server, and the path contains spaces, beware of the escape code for that being different from the URL one (%20) or the console (\ followed by space; as in: /fileserver/My\ Pictures).

    /etc/fstab requires the \040 escape code to mount a filesystem with spaces in its pathname:
    # This mounts the Samba share My Pictures on the NFS server 
    #fileserver under /media/pictures
    //fileserver/My\040Pictures/ /media/pictures cifs user=guest,password=,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0

    Sunday, September 27, 2009

    Mounting SSHFS drives in Ubuntu

    If you have a remote file server that you usually access via SSH, it is extremely simple to mount a directory on your 'local' filesystem (many applications will not allow you to 'browse' to network drives when looking for files to open).

    You can simply add the following to /etc/fstab (remember to mkdir -p /path/to/dir and chown it to yourself):
    # sshfs (see http://ubuntuforums.org/showthread.php?t=270806)
    # The lines below will NOT mount the directories automatically - use: mount /path/to/dir (no need to sudo)
    # To mount an arbitrary directory use: sshfs myserver:/remote/dir /path/to/dir
    sshfs#marco@myserver:/media/data/shared/media /media/multimedia fuse users,auto 0 0
    executing mount /media/multimedia will make all files on myserver located in /media/data/shared/media visible (assuming user `marco` on myserver has the right permission).

    To avoid having to type a password each time you mount that directory, I recommend to create a public/private key-pair (there are a number of good tutorials about how to do this, you can see http://pkeck.myweb.uga.edu/ssh/) which you will need anyway for the next step.

    Mounting automatically when starting Gnome

    Unlike all the other mountpoints defined in /etc/fstab, and despite the `auto` option, the sshfs ones will not mount at boot (I suspect that that is because /etc/fstab are mounted as root, who is most definitely not allowed to ssh into my server - nor into yours, right??? - and in any event, even if it were, would result in a pretty useless situation as I would have either no access to the directories, or I would have to make them all world-readable, which is a BAD idea at the best of times....)

    Using .bashrc is really not an option, as it requires me to launch a terminal session, and then leave it open for the duration of my Gnome session (closing it will automatically umount my drives).

    I use Gnome (at home, and KDE at work - still can't quite decide which one is better, and I laugh at all those embroiled in this religious war.... they're both excellent windows managers and leave Windows in a trail of shame and uselessness) so I'd like to have the drives mounted when I start the session, without having to launch a terminall session.

    The easy option is to create a simple shell file in my ~/bin directory:
    #!/bin/sh
    # Mounts external drives on sshfs (not mounted automatically from /etc/fstab)

    mount /media/multimedia
    mount /media/www

    and add it to the session startup scripts for the Gnome session: System > Preferences > Startup Applications

    If you now log out and log back in (no need to reboot) you will find your sshfs mountpoints proudly showing in the local directory.

    You may also want to consider changing the 'auto' option back to 'noauto' should speed up the boot process.