Sunday, August 19, 2012

Configuring Linux or: How I Learned to Stop Worrying and Love the Command Line

Foreword
The 7 year old HDD in my laptop died.  This gave me an excuse to switch from Windows XP to Linux.  Since there was a lot of configuration to set things up I figured I'd document my trials and tribulations here for future reference.  Note, this was written over about two weeks as I set things up.  It sometimes reflects this, but I tried to edit it to make sense overall.


Setup
Years ago, I had previously used Slackware with Fluxbox.  So, I installed Slackware 13.37; the install was exactly the same as it had been 10 years ago.  The network didn't work, and as I didn't really have another computer for troubleshooting I really didn't feel like dealing with Slackware.  I have been wanting to give Ubuntu a try,  and so I had also downloaded the newest Ubuntu, and I installed that.

Ubuntu used Gnome as its default desktop environment, although it seems to have switched to Unity, which I think is still Gnome based.  I've never liked Gnome, and now both it and KDE have gone out of their way to be unusable.  Luckily, there is a version of Ubuntu with lightweight xfce called Xubuntu.  This didn't matter much as I planned on using the even more lightweight Fluxbox again.

It helps if you are familiar with the general structure of files in Linux.  Here is a good overview:
http://www.thegeekstuff.com/2010/09/linux-file-system-structure/ 
http://lowfatlinux.com/


Installing Programs
When I used Slackware I used to install everything by compiling source, however, Ubuntu is well known for its friendly package manager and I made extensive use of it.  It's nice because if you enter the name you think a program will have on the terminal it will tell you if it's installed.  If it's not it will suggest similar names you may have meant.  If there is a program called that, but it's just not installed it will tell you the command to run to install it: sudo apt-get install program-name, which can then be selected and middle clicked to install right away.


Extra Programs
I had to install a lot of random utilities to do everything here.  I won't bother mentioning them all; if you try something and it complains that something isn't installed, then just install it with apt-get.  Rather, I will list the programs that I actually use directly that I had to find.

Geany is a text editor that I found as a replacement for Notepad++.  It's ironic because when I first moved from Linux back to Windows one of my complaints was that I couldn't live without syntax highlighting provided by whatever text editor I had been using.  I found Notepad++ as a suitable replacement on Windows and grew to love it.  Now, moving back to Linux I don't know how I'll live without all the features of Notepad++.  I'm liking Geany more as I use it.  I'd say it may even be better than Notepad++.

For bit torrent, I went with qbittorrent. By default I had Transmission installed, which seemed good for people that didn't like bit torrent (I promise this opinion isn't biased by the fact that I associate Transmission with Macs).  Anyway, qbittorrent is an open source clone of uTorrent, which is good because every since uTorrent was sold it was becoming increasingly annoying.

I found gmusicbrowser as a replacement for Foobar2000.  I don't have many requirements in a music player, just a simple interface that can handle a lot of files.  This wasn't hard to find, however, I couldn't find anything that simply had a random song feature.  I should point out they all had shuffle, which randomizes the order of songs and plays them back.  What I wanted was a button that when pressed would play a random song, but then playback would proceed in the normal order.  I like to have that command mapped to one of my mouse buttons.

The closest I could find was a random album button, and to get this I had to manually edit the skin file to add the button to the system tray popup.  As far as I can tell there is no command for it, so I have to use the button in the system tray.

Note this is no longer true.  I'm using mpd, and wrote a section about it below.


Shell Scripts
Linux shell scripts are just a way to run commands that would normally be entered on the terminal via a text file.  This is similar to batch files in Windows.  The difference is that since everything in Linux is built on the command line from the ground up, they're much more powerful, and sensible to figure out.  Just put commands in a text file and then run it with the command sh file.  For more complex stuff I'll just use Perl.  The output of a script can be redirected to a text file with sh file > text.txt.  I used several shell scripts where I needed to combine several commands into one.


Fluxbox Menu
Fluxbox is pretty simple.  There are a handful of config files that control just about everything.  These files are in a folder called ~/.fluxbox.  One is called menu and it contains the Fluxbox menu that comes up when you right click the desktop (similar to the Windows start menu).  This menu is pretty easy to edit, here are some good guides.  There is one important note though.  It doesn't update unless you update it yourself.  There's a program called MenuMaker that will scan the system for programs and build a menu.  You can run this with mmaker fluxbox -f, but if you want to have a custom menu you'll need to combine its output with your custom menu.  I made a simple shell script to do this:
echo "[begin] (Dale)"
mmaker fluxbox -c -i
cat custommenu.txt
echo "[end]"


This will just dump the menu to the screen.  Redirect it to the menu file with:  sh createmenu > .fluxbox/menu.  Just put the custom part of your menu in the text file, and you're set.  This is the custom menu I used, which includes easy access to several of the commonly edited Fluxbox config files:

[separator]
[submenu] (Mine)
[exec] (Thunar File Manager) {Thunar}
[exec] (FireFox) {firefox}
[exec] (gmpc) {gmpc}
[exec] (Pidgin Internet Messenger) {pidgin}
[exec] (qBittorrent) {qbittorrent}
[exec] (Calculator) {gcalctool}
[exec] (Geany) {geany}
[exec] (Leafpad) {leafpad}
[end]
[submenu] (FluxBox)
[workspaces] (Workspaces)
[submenu] (Styles)
[stylesdir] (/usr/share/fluxbox/styles)
[stylesdir] (~/.fluxbox/styles)
[end]
[submenu] (Config Files)
[exec] (startup) {geany .fluxbox/startup}
[exec] (menu) {geany .fluxbox/menu}
[exec] (keys) {geany .fluxbox/keys}
[exec] (layout) {geany .fluxbox/init}
[end]
[config] (Configure)
[reconfig] (Reconfig)
[restart] (Restart)
[separator]
[exit] (Exit)
[end]



Music Player Daemon
I eventually got tired of not having a random song button.  I asked on stackexchange about a music player that had a random song command.  The answers seemed to indicate that I was not explaining the concept of what I wanted to do very well. Reguardless, the common suggestion was to use mpd, which is a background command line music player, designed to be controlled by some other frontend.  Note, mpd still doesn't have an actual play random song command, but I made a shell script to emulate that functionality.

To begin, I had trouble getting mpd to work at all.  First it wouldn't connect to my gvfs music shares, and then it didn't produce audio.  I ended up following this guide after which, despite being the same stuff I was doing, it worked.  I'm sorry I can't give more details on what I actually did to fix my problems for anyone following this (ie future me).

After that, I had to figure out what client to use.  mpc is a command line client that just issues commands.  I also had Gnome Music Player Client installed, and it was suggested elsewhere, so I went with that.  I must say, I must listen to music in a very different way from every other person on Earth, as I always find default music player's 'libraries' to be very unnecessarily complicated.  I just want all my music in one giant list which will play back in order.  Anyway, gmpc worked ok once I figured out how to set it up.

Another problem I had was that mpd purposely only supports one directory.  If you have music in more than one directory and wish to add it all you are expected to set up symlinks in the one directory to the others.  This is all well and good, but as I mentioned before all my music is on Windows Samba shares mounted via gvfs.  Despite having write access in Linux to them, I couldn't set up symlinks in one to the other.  After some experimenting to find the actual problem I found I could create symlinks to them, just not on them.  So the solution was to just set up symlinks in my home directory to the various shares.  This worked well, as it simplified the 20 levels deep nature of the shares at the same time.

All this and mpd still doesn't have an actual random command.  It did have a random mode though.  I got the functionality I wanted with this shell script:
mpc random
mpc next
mpc random


This turns on random mode, skips to the next (now random) song, and then turns off random mode.  Since mpd is very lightweight this is fast enough for me.


The Mouse and Media Buttons
I have a Logitech Performance Mouse MX, which I like very much.  Unfortunately, Logitech doesn't support Linux at all.  This meant I had to configure all the extra buttons myself.  I found these two commands gave a good speed (which I like to be very fast):
xinput set-prop 9 "Device Accel Velocity Scaling" 30
xinput set-prop 9 "Device Accel Profile" 2


As for the mouse buttons, I had to first find what the buttons were called.  There's a simple program called 'xev' which displays the names of buttons that are pressed.  After I found the names I added these lines to the Fluxbox config file keys:
Mouse9 :Exec sh random
Mouse7 :Exec sh tabright
Mouse6 :Exec sh tableft
Mouse13 :Exec xte 'key F5'
Mouse8 :Exec xte 'keydown Control_L' 'key W' 'keyup Control_L'


Note the shell scripts.  I did this because I use the mouse wheel left and right to switch tabs (Ctrl+page down/up).  Since the sideways motion is not very good it would send a lot of key presses all at once and was unusable.  I fixed this by adding some delays via this shell script:
xte 'keydown Control_L'
sleepenh 0.1
xte 'key Page_Up'
sleepenh 0.1
xte 'keyup Control_L'


I had to install sleepenh to get sleep times < 1 second.

Next, to get the media buttons (play, next, mute, etc) on my laptop to work I added these lines to the keys file:
None XF86AudioLowerVolume :Exec amixer -q set Master 5%- unmute
None XF86AudioRaiseVolume :Exec amixer -q set Master 5%+ unmute
None XF86AudioMute :Exec sh togglemute

171 :Exec mpc next
172 :Exec mpc toggle
173 :Exec mpc prev
174 :Exec mpc stop


A note about mute.  There appears to be a bug in amixer where the mute toggle mutes three channels, but only unmutes one.  This means you have to go in and manually unmute in the gui.  I wasted a good hour trying to figure out what was going on with that.  I had to write a shell script to mute and unmute all the channels.  Here it is:
if amixer sget Master | grep "\[on\]"
then sh muteall
else sh unmuteall
fi


and muteall is just:
amixer set Master mute
amixer set "Master Mono" mute
amixer set PCM mute



Startup File
One of those Fluxbox config files is the startup file.  As the name implies it runs any commands in it when Fluxbox starts.  It's important to note the command exec fluxbox is what runs Fluxbox itself.  Any commands after that won't run until Fluxbox exits.  Also, any command that will continue to run needs to have an ampersand placed after it.  Here are the lines I added to my startup file:
xinput set-prop 9 "Device Accel Velocity Scaling" 30
xinput set-prop 9 "Device Accel Profile" 2
xrandr -s 1440x900 -r 60
fbsetbg -c -r ~/.fluxbox/backgrounds/
sh createmenu > .fluxbox/menu
xfce4-power-manager &
gvfs-mount smb://Server-PC/Media &
python ~/gmail-notify/notifier.py &


I already explained some.  xrandr sets the resolution.  fbsetbg sets the wallpaper to a random one.  The xfce4-power-manger gives popup alerts when the laptop switches to battery power.  gvfs mounts windows samba shares.  And, gmail notify is a Linux equivalent to the Windows gmail notifier by Google.

I made a bunch of 1440x900 wallpapers from various Hubble shots, and posted them here:
http://daleswanson.blogspot.com/2012/08/astronomy-wallpapers.html


Fluxbox Theme
Fluxbox themes are simple text files.  I downloaded a few, but really didn't like most.  I finally settled on the included 'squared blue' but modified it somewhat.  I'll include my custom theme in the zip of all my custom files at the end of this post.


Cronjobs
Cronjobs are the Linux way of running a script or program automatically at set times, however, they are much more powerful than the Windows equivalent.  The 'crontab' stores the various cronjobs set to run.  It isn't stored as a simple file on the computer (while the system is running).  To edit it enter the command crontab -e (-l will list the current one).  This will allow you to edit it in a terminal based text editor, a concept I don't think I will ever embrace.

The format is somewhat interesting.  It is in the form of: Mi H D Mo DOW command.  Or: minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday).  For example: 38 6 4 8 0 command would run at 6:38 am on Sunday Aug 4th.  Much greater flexibility comes from wildcards (*), multiple entries (,) and multiples (/):
12 4 * * * command - Every day at 4:12 am
12 4,8 * * * command - Every day at 4:12 am and 8:12 am
*/10 * * * * command - Every minute that is evenly divisible by 10 (ie every 10 minutes).

Many more examples and a more thorough explanation are in this guide.

I wrote a simple backup script that currently consists of just a copy command, but I will likely expand it.  I set it up to run every hour with 38 * * * * nice -n19 ionice -c3 sh ~/backup.  Note the ionice.  In Linux, nice is program which sets priority.  The idea being that higher priority processes will get CPU and memory before lower priority ones will.  In theory, this means that if a process is set very low it won't have any impact on the overall speed no matter how much CPU it eats up.

For things being run as cronjobs it is likely that you just want them to run with whatever idle resources there are, and to not get in your way.  You can do this by making the command in your crontab that calls the script have a lower nice level (note -19 is highest priority, 19 is lowest, 0 is default, so kind of backwards [also it seems to be perhaps 20, at least sometimes]).

This is all well and good, but a backup script probably won't eat up too much CPU.  What it will eat up is HDD I/O.  That is where ionice come in to play.  As you've probably already guessed ionice is a similar concept but just for I/O priority.  In this case, class 3 is the lowest priority, and what I set the above backup script to.


Thunar Custom Actions
Thunar is the default file manager for xfce.  It has something called custom actions which give you the ability to run commands on files from a right click menu.  This has proven to be a very useful feature, which I've used to replace (and expand upon) the features of file menu tools in Windows.

They are stored in an xml file at ~/.config/Thunar/uca.xml.  I've included that file in the zip of my custom text files at the end of this post.



Custom Text Files
As promised I've compiled a bunch of the text files I edited and uploaded them here:
http://daleswanson.org/blog/custom.7z

No comments:

Post a Comment