TITLE:		Setting up a minimalistic X terminal using the svga vncviewer
LFS VERSION:	All (tested against LFS-pre4.0)
AUTHOR:		Csaba Henk <ekho@math-inst.hu>

SYNOPSIS:
	This hints shows how to equip an uClibc-based bootfloppy with
	the svga vncviewer. With this enhanced bootfloppy you can turn even
	low-end computers to X terminals. We also describe a possible
	minimalistic server + client (X terminal) configuration.

VERSION:	0.5

CONTENTS

 Introduction
 What do we need?
Completing the bootfloppy
 Installing more of uClibc
 Installing svgalib
 Installing svncviewer
 Creating the bootdisk
Setting up the network environment
 Setting up general server programs
 Installing the vnc server
 Connecting from client to server 
Closing remarks
 Version notes
 Bugs and weirdnesses
 Todo
 Credits
Appendix A -- svgalib-1.9.17-Makefiles.diff

PREFACE

* For the most recent version check out 

http://www.math-inst.hu/~ekho/lowlife/svnc-x_terminal.txt

* See changelog at 

http://www.math-inst.hu/~ekho/lowlife/Changelog

* Comments, ideas, critics, flames are welcome.

* (If you are an LFS user, you can skip this.) Although this document is
formally an LFS hint, you can see use of it without knowing what LFS is
(I tried to write it keeping this possibility in mind). Anyway, I
recommend you to check out what LFS is. If you are not familiar with
compiling source code in unix/linux environment, then there is no use of
going on without checking out what LFS is. Consult the following docs
first:

http://www.linuxfromscratch.org/lfs/intro.shtml
http://www.linuxfromscratch.org/faq/
http://www.linuxfromscratch.org/view/4.0/chapter02/chapter02.html

HINT

Introduction
------------

As the Linux Xterminal mini-HOWTO claims: "Ideally, an XT is silent,
swift, and deadly". A cheap and great way to achieve this ideal is to
use old PC's as terminals. Two issues arise here: 

1) How to make them display X?
2) How to make them silent?

Concerning 2), the possible sources of noise are fans and HD. In case
of old PC's (486s and old Pentiums) the CPU needs no fan -- if there
is any fan, you can find it at the power supply, and it's not a big
risk to plug that out. This is where oldness becomes a feature.
Concerning the HD: to make the it quiet, you should avoid using it.
An alternative is using a ramdisk for carrying your root filesystem.
The most easy way to make the kernel mount a ramdisk as root
filesystem is booting from a floppy. This is why I use a bootfloppy.

This design implies that in case of an old PC, we will be tight on
memory. One kind of reaction to this circumstance is trying to trim
down X for the floppy. I chose an other way, namely, displaying X via
vnc: we put svgalib + the svga vncviewer to the floppy.

This hint is a continuation of the uclibc-bootfloppy hint (

http://hints.linuxfromscratch.org/hints/uclibc-bootfloppy.txt
http://www.math-inst.hu/~ekho/lowlife/uclibc-bootfloppy.txt

) which describes how to put together a bootfloppy based on recent linux
kernel, the uClibc C library and busybox. The steps described here
should be performed when you get to the "Other programs" chapter of the
uclibc-bootfloppy hint.

This way we get a "bleeding edge" bootfloppy with recent versions of
the used programs, and this setup needs no more than 7M RAM -- in
this value kernel mem usage, ramdisk size, svncviewer displaying a
bunch of graphic programs are all included. This value could still be
decreased by using older stuff; why do I insist on being at the
bleeding edge? See the answer in the uclibc-bootfloppy hint.

This hint,additional info and downloadable bootdisk image with svnc
can be found at my homepage (or at its mirror):

http://www.math-inst.hu/~ekho/lowlife/
http://www.personal.ceu.hu/students/01/Csaba_Henk/lowlife/

In this hint we the assume that you use an x86 PC (both for making and
booting the floppy) with gcc. You may try to port it to another
architecture / compiler. Doing it on another architecture should not
be hard. Doing it with another compiler depends on how much does
uClibc support that compiler.

If you copy command from this hint to your shell, be careful that
line-terminating backslashes (\) will keep their position (no whitespace
characters should follow them). A possible solution is open this hint in
the Vim GUI, and copy'n'paste from there.


What do we need?
----------------

Here I list the programs which we need for the bootfloppy and the ones
which are used in the particular server + client setup which will be 
described in the sequel.

Generally you are encouraged to use the most update versions of the
programs (and your favorite distributions, if there are more of them).
Still there are some items where I have some additional comment on the
version/distribution; these are marked with (!). You can find the
version notes at the end of this hint. 

* The server side:

tightvnc
 http://prdownloads.sourceforge.net/vnc-tight/tightvnc-1.2.*_unixsrc.tar.bz2
xinetd
some kind of telnet daemon
some kind of tftp daemon

* The client (bootfloppy) side

svgalib-1.9.17 (!)
 http://www.arava.co.il/matan/svgalib/svgalib-1.9.17.tar.gz
svnc-0.1 (!)
 ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/svnc-0.1.tgz
 and the mirrors listed in
 http://www.realvnc.com/pipermail/vnc-list/2002-June/031178.html
tightvnc (only needed for compiling svnc)


Completing the bootfloppy
=========================

First you need a bootflopy development environment. Setting up one is 
described in the uclibc-bootfloppy hint. Do what the uclibc-bootfloppy 
hint tells you, until you get to the "Other programs" chapter. 

Then do the following; you are supposed to work as the bootdisk user in
the present chapter (ie.,
type

su - bootdisk

if you didn't do it yet), except for a short while (you will be told
when to become root).

Installing more of uClibc
-------------------------

For svgalib we need the math libraries of uClibc. If you installed gpm         
to the floppy filesystem correctly, you already have them. If you don't        
have them on the floppy filesystem, type:

cd /usr/i386-linux-uclibc/lib/ &&
cp -a libm-* libm.so.0 ~/rfloppy/lib

Installing svgalib
------------------

First some necessary corrections for the 1.9.17 version of svgalib;
hopefully these will be unnecessary for subsequent releases. You find a
patch in Appendix A for svgalib-1.9.17, or at

http://www.math-inst.hu/~ekho/lowlife/svgalib-1.9.17-Makefiles.patch

This patch corrects some typos in the Makefiles. Put the patch to the
svgalib source dir as svgalib-1.9.17-Makefiles.patch . Then type

ln -s ../egadrv.c src/drivers &&
patch -Np1 -i svgalib-1.9.17-Makefiles.patch 

(the first command is the correction of a file misplacement).

These were the version-specific corrections. Now let's go for the real
stuff.
 
Adjust the list of supported drivers in Makefile.cfg according to
your needs. By carefully choosing the few drivers you need, you can
decrease the size of the binary with about 100 kb or maybe more! However, 
our filesystem is big enough for the svga library with all default drivers
compiled in. If the kernel release you use for the bootfloppy is not the
same as the one used on your development platform, also set the
KERNELRELEASE, INCLUDEDIR variables according to this.

Now we compile svgalib and install it to the directory of the floppy's
filesystem. In this development version of svgalib the svga-aware
programs are needed no more to run as setuid root, but a kernel module
is to be loaded for using svgalib. The commands below also compile and
install the kernel module. 

make shared &&
cd sharedlib &&
for q in "" gl; do 
strip libvga$q.so.1.9.*
ln -s libvga$q.so.1.9.* libvga$q.so.1 
ln -s libvga$q.so.1 libvga$q.so
done &&
mkdir -p $HOME/rfloppy/usr/lib && 
cp -a libvga* $HOME/rfloppy/usr/lib &&
# Now comes the compilation of the module
cd ../kernel/svgalib_helper &&
make all &&
mkdir -p $HOME/rfloppy/lib/modules &&
cp svgalib_helper.o $HOME/rfloppy/lib/modules &&
cd ../..

Also create the file $HOME/rfloppy/etc/vga/libvga.config, and put into it
your mouse type at least. Eg., my libvga.config looks like:

mouse IMPS2
mdev /dev/misc/psaux

If you feel like, you can also compile the svga test suite. It is useful
if you want to custom-tailor your svga configuration (which means
adjusting the libvga.config file). To do so, type

cd demos &&
make "LIBS=../sharedlib/libvga.so ../sharedlib/libvgagl.so" &&
cd ../threeDKit &&
make "LIBS=../sharedlib/libvga.so ../sharedlib/libvgagl.so" \
 "INCLUDES = -I../include -I. -I../src -I../gl" &&
cd ..

in the svgalib source directory. You will find the executables in the
demos and threeDKit directories.  

Do not delete the svgalib source dir -- we will need it in the following
section!

Installing svncviewer
---------------------

Do not yet extract the svncviewer tarball! The following commands will
assume that the svgalib source dir, the tightvnc and svncviewer tarballs
and you (ie., the bootdisk user) are at $HOME.

To get svncviewer compiled, we need the libvncauth.a library form the
tightvnc distribution. (Thus svncviewer is compiled inside the vnc
source tree.) The usual way to compile both of libvncauth.a and
svncviewer is to create makefiles with the xmkmf utility, and then
running make. 

However, xmkmf will not work with uClibc (unless we build X against
uClibc, and we don't want to do that). Therefore we do the following:

* run xmkmf (imake) using glibc on both of libvncauth and svncviewer;

* run make on libvncauth and svncviewer with options for using uClibc
and local svgalib and for size optimization.

In practice this means the following: first we define a variable which
stores the relative path from the svncviewer source dir to svgalib
source dir (we need this path as we don't install the uClibc-linked
svgalib on the development platform). If you follow my setup faithfully,
it should be:

export SVGADIR=../../svgalib-1.9.17

(otherwise find it out by yourself :).

Here I tell you about a patch I made for svncviewer:

http://www.math-inst.hu/~ekho/lowlife/svnc-0.1-modeforce.patch

This introduces a -mode option for svncviewer with which you can force
svncviewer to use a given svga mode (these modes are described in the
svgalib docs or check out the vgatest executable of the svgalib test
kit). If you don't apply the patch just execute the following sequence
of commands, otherwise take a break for patching when its time comes. 

bzcat tightvnc-1.2.*_unixsrc.tar.bz2 | tar xv &&
cd vnc_unixsrc &&
tar xvzf ../svnc-0.1.tgz &&
unset CC ; : &&
cd libvncauth &&
xmkmf &&
cd ../svncviewer &&
xmkmf &&
export CC=i386-uclibc-gcc &&   # Not necessary, just to restore defaults
cd ../libvncauth &&
make "CC=i386-uclibc-gcc" "CDEBUGFLAGS = $CFLAGS -fno-strength-reduce" \
 "AR = i386-uclibc-ar clq" &&
cd ../svncviewer &&
make "CC=i386-uclibc-gcc" "CDEBUGFLAGS = $CFLAGS -fno-strength-reduce" \
 "INCLUDES = -I../include -I. -I$SVGADIR/include -I$SVGADIR/gl" \
 "SVGALIB = $SVGADIR/sharedlib/libvga.so $SVGADIR/sharedlib/libvgagl.so" &&
strip svncviewer &&
mkdir -p $HOME/rfloppy/usr/bin &&
cp svncviewer $HOME/rfloppy/usr/bin

If you intend to compile svncviewer in an environment without X (ie.,
you don't have xmkmf), then try to compile libvncauth.a and
svncviewer with the help of the following scripts (they are simply
the output of the respective make commands; I give away these and not
the Makefiles because xmkmf-made Makefiles are huge and illegible):

http://www.math-inst.hu/~ekho/lowlife/make_uclibc-libvncauth 
http://www.math-inst.hu/~ekho/lowlife/make_uclibc-svncviewer 

Creating the bootdisk
---------------------

Proceed on as it is described in the similarly named chapter of the
uclibc-bootdisk hint.


Setting up the network environment
==================================

You have the bootdisk with the svga vncviewer. If you know what you want
to do with it, you have clear ideas that in what network environment you
will use it, then the rest of the hint is not too interesting for you.

In the sequel we describe a minimalist local network setup: a server
machine running a vnc server, and a client machine booted with our
bootfloppy, which can connect to the server in three basic ways: it
can transfer files via tftp, it can get a character terminal on the
server via telnet, and it can display X desktops via vnc. Using the
tftp and telnet services requires that you enabled the respective
clients when you compiled busybox. (And you will also need ifconfig
being enabled in busybox.)

We will assume that you connect the two machines directly (without a
switch or things like that) and that you have a free network interface
(network card, serial/parallel port) on both machines.

Setting up general server programs
----------------------------------

Fix an unused IP address for the server and the client side, from
private network IP-address range, from the same network class, and
store it in the $SERVER and $CLIENT variables, respectively. Eg., I
use 

$SERVER=192.168.0.2
$CLIENT=192.168.0.1

(its an example and not a command!). Then you should install xinetd
(or alternatively, inetd), some telnet daemon, and some tftp daemon.
These are covered by the BLFS book (available at

http://beyond.linuxfromscratch.org

). Choose a directory for tftp transfers, store its value in the
variable $TRANSFER. (That is, tftp clients can download files located in
$TRANSFER, and can upload files to $TRANSFER.) Choose carefully the
list of those users/groups which can write to $TRANSFER.

Configure xinetd to run telnetd and tftpd bound to $SERVER like this
(the names of the executables may be different on your system; and
consider that by the settings below tftp is ran by a specific user):

echo "
	service telnet
{
       socket_type             = stream
       wait                    = no
       user                    = root
       server                  = /usr/sbin/in.telnetd
       bind                    = $SERVER
       only_from               = $CLIENT
       log_on_failure += USERID
}

	service tftp
{
       socket_type             = dgram
       protocol                = udp
       wait                    = yes
       user                    = tftpd
       server                  = /usr/sbin/tftpd
       server_args             = $TRANSFER
       bind                    = $SERVER
       only_from               = $CLIENT
}" >> /etc/xinetd.conf

Note that binding these services to $SERVER is essential for security --
telnet transfers data unencrypted and tftp transfers data without asking
for password.

Now you can configure the network interface on the server -- in the
following example we use plip:

ifconfig plip0 $SERVER pointopoint $CLIENT

(It's your homework to find out how to get it executed at startup.)

Restart xinetd; in an "official" LFS/BLFS system it is done with

/etc/rc.d/init.d/xinetd restart

Installing the vnc server
-------------------------

In this setup we will install the vnc server in a standalone way, that
is, it won't be handled by xinetd so you will have to start it manually.
(One benefit of this approach that in this case the vnc session remains
alive after a viewer disconnects, so you can use it as a portable X
desktop, "the screen of X".) If you would like a "vnc + xinetd"-ish vnc
setup, check out the tightvnc hint (

http://hints.linuxfromscratch.org/hints/tightvnc.txt

).

Install tightvnc with the following commands:

xmkmf &&
make World &&
cd Xvnc &&
./configure &&
make &&
cd .. &&
sed '/^ *\$fontPath/s%^%#%' vncserver > vncserver.tmp &&
mv vncserver.tmp vncserver &&
./vncinstall /usr/bin /usr/share/man

[Explanation:

"sed '/^ *\$fontPath/s%^%#%' vncserver [...]" : 

this command comments the $fontPath variable out, as I see no effect of
setting it other than getting error messages (it may be different on
your system).]

Note: the above commands do not cover the installation of the java
viewer, which enables java-supporting browsers to display vnc desktops.
The java viewer is shipped with the tightvnc source code in a
precompiled form; however, if you want to act according to LFS spirit,
*do not* install it, rather download the tightvnc-1.2.*_javasrc package
from the tightvnc site, and compile it yourself. That's pretty
straightforward.  

Connecting from client to server 
--------------------------------

Now its time to boot the floppy on the client machine. We will use the
$SERVER, $CLIENT variables with the same values on the client machine as
well.

When you get the prompt on the client, set up the network -- eg.,
continuing the above example with plip:

ifconfig plip0 $CLIENT pointopoint $SERVER

* If all is nice, you can get a prompt of the server on the client simply by
typing

telnet $SERVER

* Transferring files between the server and client is as follows:

- downloading the file foo from server to client: on the server, put foo
into $TRANSFER, make it world-readable, then on the client: get sure that
foo does not exist in the current directory, then type:

tftp -g -r foo $SERVER 

- uploading the file foo from client to server: on the server type

 > $TRANSFER/foo &&
chmod a+w $TRANSFER/foo

then on the client type

tftp -p -l foo $SERVER 

-- its primitive, but saves your floppy from the bloat.

* And getting an X desktop via vnc: at the server, login as the user
whose desktop is to be reached from the client (you can use the telnet
of the client). Then type:

vncserver

(you may need to pass -geometry and -depth options to be compatible with
the video capabilities of the client).

On the client, type:

insmod /lib/modules/svgalib_helper.o &&
svncviewer $SERVER:1

and see the desktop of the server. 

Customizations:

* pass the "-interface $SERVER" option to the vnc server if you worry
for security and you want to make sure that only the client has access to
the vnc server;

* pass the "-compatiblekbd" option to the vnc server if you want to use
non-standard characters; eg., this way typing Alt+i in an xterm gives
you an acuted e. This is the way to go for people whose language uses
nonstandard characters, as vnc does not support xmodmap well.

There is a more tricky way of using vnc: with the help of ssh tunneling
you can reach any vnc server from the client which is reachable from the
server. Say you run a vnc server at pistike.org, on display 1 (port
5901), as user steve. On the server type

ssh -g -L 5902:localhost:5901 steve@pistike.org

This forwards port 5902 of the server to port 5901 of pistike.org. Thus
when you type 

svncviewer $SERVER:2

on the client, you will be connected to the vnc server of pistike.org.
Moreover, the connection between the server and pistike.org will be
secured. 

So that's the story.

Closing remarks
===============

Version notes
-------------

* svgalib-1.9.17:

As it can be read in the svgalib.org site, "Pre releases of svgalib-2
will be called 1.9.x, with no pre, but they are still very
experimental." Why do we use experimental version? Simply because it
works much more flawlessly: the stable release (svgalib-1.4.3) was also
willing to compile, but it gave me segmentation faults. Note that the
1.9.17 version has some lesser bugs which are corrected in this hint;
these corrections are likely to become unnecessary with a new release of
svgalib. 

* snvc-0.1:

You can find two different kind of releases of the svga vncviewer: there
is the original one, that's used in this hint, and there are the Debian
releases (see at

http://packages.qa.debian.org/s/svncviewer.html

). For me the deb version seems to be an unclever dirty hack: its
maintainer changed keyboard handling in way such that you can compile
the program with an arbitrary keyboard layout (it's shipped with US and
German layouts), but this way several keyboard combination got disabled:
eg., killing the viewer with Ctrl+Alt+Backspace does not work; the
Ctrl+Alt+Left/Right combinations of icewm for changing desktops neither
work. And the source ships with precompiled libvncauth.a and svncviewer
and bunch of other unnecessary things; it's quite annoying (the manpage
is fine, tho). So I sticked with the oldie-but-goldie original version
of svncviewer.

Bugs and weirdnesses
--------------------

The list below is about both of the uclibc-bootfloppy and the
svnc-x_terminal hints. 

The following weirdnesses are probably due to the respective programs
and not to doing something wrong in these hints.

* Sometimes when switching to another terminal and then back to the
svga vncviewer the keyboard gets screwed up. Using serial mouse,
sometimes the keyboard gets screwed up without any obvious reason.
However, after restarting the viewer, things behave normally again.

* Using libvgagl-dependent programs (like svncviewer) on my 486
laptop, I see a horizontal white line at half height of the screen.
I wonder whether this occurs with other LCD monitors.

Todo
----

Now it seems that the uclibc-bootfloppy and svnc-x_terminal hints got
quite close to their idea: you set up the development environment, su to
bootdisk user, type in some commands, finally put a floppy to the drive
and run the mkbootdisk script, and you have the bootfloppy.

However, I can say only "Now it seems..." but I can't say "Now it is..."
-- without testing the stuff by many people and getting feedback I
can't.

So the main TODO is to be done by you, kind reader: test and report!! 

After it happens, these hints can be claimed to be mature.

Another TODO will be in the future to upgrade to newer versions of the
installed programs, if new versions come out. 

Credits
-------

I gathered information from the following sources:

LFS mailing lists
busybox mailing list
uClibc mailing list archives
vnc mailing list archives
"Creating a custom bootdisk" chapter of the BLFS book
Bootdisk HOWTO
PLIP-install HOWTO
http://ebusiness.gbdirect.co.uk/case_studies/xterminal.html
the LODS project (http://www.khk.net/lods/index.html)	
the tomsrtbt project (http://www.toms.net/rb/)

Contributions
-------------

Archaic had some useful advice about trimming the root filesys (/etc).
Following the advice of Bill Maltby, "rdev $DISK 0,0" is used rather
than "rdev $DISK $DISK" or "rdev $DISK 2,0".


* * * Appendices * * *



Appendix A -- svgalib-1.9.17-Makefiles.patch
--------------------------------------------

%%%<svgalib-1.9.17-Makefiles.patch>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff -aur orig/svgalib-1.9.17/Makefile.cfg svgalib-1.9.17/Makefile.cfg
--- orig/svgalib-1.9.17/Makefile.cfg	2002-12-03 10:53:56.000000000 +0100
+++ svgalib-1.9.17/Makefile.cfg	2003-07-01 23:53:59.000000000 +0200
@@ -316,7 +316,7 @@
 	INCLUDE_TVGA_DRIVER=
 endif
 
-#ifneq($(INCLUDE_S3_DRIVER),y)
+ifneq ($(INCLUDE_S3_DRIVER),y)
 	INCLUDE_NORMAL_DAC =
 	INCLUDE_S3_SDAC_DAC =
 	INCLUDE_S3_GENDAC_DAC =
@@ -328,4 +328,4 @@
 	INCLUDE_ICW_DAC =
 	INCLUDE_IBMRGB52x_DAC =
 	INCLUDE_SC1148X_DAC =
-#endif
+endif
diff -aur orig/svgalib-1.9.17/demos/Makefile svgalib-1.9.17/demos/Makefile
--- orig/svgalib-1.9.17/demos/Makefile	2002-04-10 15:54:26.000000000 +0200
+++ svgalib-1.9.17/demos/Makefile	2003-07-02 00:14:23.000000000 +0200
@@ -58,7 +58,7 @@
 	$(CC) $(CFLAGS) $(LDFLAGS) -o linearspeed linearspeed.c memset.o $(LIBS)
 
 buildcsr: $(OBJS) mkcur.o
-	$(CC) -o buildcsr $(OBJS) $(FLAGS) mkcur.o -lvgagl -lvga
+	$(CC) -o buildcsr $(OBJS) $(FLAGS) mkcur.o $(LIBS)
 
 clean:	cleanbin
 	rm -f .depend *.o *~ *.bak core
%%%</svgalib-1.9.17-Makefiles.patch>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


END of "Setting up a minimalistic X terminal using the svga vncviewer" hint
