Tuesday, September 7, 2010

Synergy setup between a MacBook and Linux machine

I just got a new MacBook Pro a couple days ago. Wow, it's a nice machine. For my first Apple product, I'm pretty impressed. I have a couple things I don't like about it that I'll probably rant about some other time. But for today's post, I wanted to chronicle my adventure in getting it to be a Synergy server for an old Dell Dimension running Ubuntu. All said, it was the most problematic software configuration I've had in a really long time, but in the end I got it working pretty well. I kind of reminded me of trying to sort out IRQ and DMA addresses way back in the dark ages.

I first heard about Synergy a couple years ago when a friend of mine connected his iMac and Windows boxes together. For those who don't know, Synergy is an open source project that allows one machine's keyboard and mouse to be used on another machine in a seamless manner. The machine that has the keyboard and mouse physically connected is the server and the machine that only has a screen on it and uses the input devices from the other is the client. The two machines are connected through the network. So, this solution doesn't require any additional hardware than what the computers probably already have. Once configured, the mouse pointer can be moved off the side of one machine's screen onto another and the keyboard focus follows. It can also work with more than two machines. Any number of boxes can be setup as clients to use the common server.

Official installation instructions can be found on the Synergy wiki. I followed them mostly. Below are some issues I ran into and how I worked around them.

Step one was to download the software from the repository. For the Mac it came as a dmg file that installed without issue. For the Linux machine, it was available in both deb and rpm formats. Since I'm running Ubuntu, I loaded the deb file. It installed just fine too. In both cases, it put two applications in /usr/bin: the client, syngergyc, and the server, synergys.

Step two was to configure the server. This is done through a text file. The instructions seem to imply that it is optional, but it isn't. This is the only way to configure the server. Another point not clear in the instructions is the configuration file is only needed on the server and can be anywhere and called anything. You specify the name, including the path, on the command line for the server.

Somewhere I heard about GUI configuration tools; I thought it was on the setup page, but when I went to find it, I couldn't. Where ever I heard about them, I tried SynergyKM on the Mac. It was a dismal failure. It seemed to get confused and couldn't maintain its state very well so I eventually abandoned it, but not after wasting some time trying to get it to work. In the end, I just wrote the configuration file myself. It wasn't as hard as it first seemed. I called it /etc/synergy.

I also found a GUI tool for the Linux client and tried using it. However it used a different version of Synergy and when the Ubuntu Software Center tried to install it, it somehow messed up the apt state files. Another weird problem. I haven't had apt files get messed up in quite a while. I had to go into aptitude and do some clean-up to repair things so the GUI tools would work properly again. In the clean-up I also removed the GUI tool and just handled setup manually in a terminal window.

After installation and configuration, the next step is to run both the server and client in a terminal to test connectivity. My suggestion is to not skip this step. I thought I could and ran into some opaque problems that became obvious when I ran the software in a terminal. They all related to machine names used and my getting confused about which names to use where. By default, the instructions say the hostname is used, but that didn't really work too well for me on the Mac, so I ended up specifying my own names for the synergy connections. It took a bit for me to get this to work on both sides, mainly due to my own wrong assumptions about how things worked. When I ran things in a terminal, the error messages indicating my mis-configuration made short work of getting things sorted.

So, I once things ran in two terminal windows, the next step was to start the software automatically at startup. On the old, deprecated site, there's an Autostart Guide and this is linked to from the code.google.com wiki setup page with some additional information. I didn't see either on the new, official site and had to do some experimentation using the information from these two pages to get everything working properly. I ran into several time consuming problems doing this.

The first problem was a how to connect to the MacBook by name. The Synergy client needs either the IP address or the DNS name of the server in its command line. This makes sense from a client programming perspective. However, from a system administration perspective not so much. I'm using DHCP to assign IP addresses and as I have a small home network, I don't have a DNS server setup to handle local requests like this. The standard name resolution mechanism didn't work for me. I wanted to use the netbios name since it was available but it took a bit to figure out how to get it. In the end, I found the nmblookup command which, given the name will give some information, including the IP address. But, it's not in an incredibly useful format for scripts. I had pipe the output to grep to get just the line I wanted and then pipe that to cut to get just the IP address. Specifically the command looks like this:
nmblookup macbook_name | grep "<" | cut -f 1 -d" ".

With this in hand, I wrote a short bash script to look for a server and then connect to it once it found one. This is where I ran into my second problem. It's been a while since I've done any shell programming and I had to do a bunch of web searching to remind myself of the details. Everything indicated string equality could be done with either "==" or "=". As a C programmer, I defaulted to using "==". My script kept giving me unexpected operator errors on odd lines without operators. After commenting some lines out, I found the if statement at fault. On a whim, I changed the operator to be "=" and the problem went away.

The final version of the script became this:
#!/bin/sh
svr_ip=""
until [ "$svr_ip" != "" ]
do
svr_ip=`nmblookup macbook_name | grep "<" | cut -f 1 -d" "`
if [ "$svr_ip" = "" ]; then
sleep 2
fi
done
synergyc $svr_ip

(Note: right now this will fail if the server reboots and gets a different IP address since once syngeryc starts, it continues running even when the server goes offline.)

Once the script was done, I then followed the instructions to modify the gdm files, substituting my shell script for the synergyc call.

When this was all working, my next step was to get the autostart working on the Mac. By and large, this went faster than the Linux side but not without its own problems.

The first one was that the install location of the synergy software was different from the examples. So, the first time I tried it, it simply didn't do anything. There were no errors anywhere that I could find pointing to the problem. It was a matter of going through the script line by line until I found it.

With the software's path corrected, I ran into the second problem. When I logged on, the screen went blank and I never got anything else. The menu bar never appeared. The dock never appeared. The desktop icons never appeared. I couldn't do anything. However, the mouse moved the cursor and it would move onto the Linux desktop. Ok, I knew it was running, but how to get my desktop back? As a new Mac user I had no clue.

After a bit of searching, I found I could boot the installation CD and fire up Terminal to get to my system. I inserted the CD, held down the power button to turn the machine off and then held down the "C" key as I turned it back on. As advertised, it booted the CD and I found the terminal in the Utilities menu. I edited the script and added an ampersand to the end of the call to start the server so it would fork the process and run in a different thread. After rebooting, everything finally worked as expected.

As I said up front, once I got it working, it works really well in spite of the problematic configuration. The only issues I've found are relatively minor: 1) copy/paste doesn't work between machines, 2) the screen saver on the server doesn't cause the screen saver on the client to start, 3) I haven't gotten it to work prior to logging onto the Mac and 4) the above noted problem if the server reboots and gets a different IP address. While it'd be nice if these worked, in the greater scheme of things, they are pretty minor failures and I'll live with them for now. I think with more time the last two can be overcome with simply configuration changes that I haven't figured out yet.

No comments: