Archive for February, 2007

Partitioning disk for FreeBSD server

In this article I wish discuss some ideas about partitioning hard drive, when preparing PC for being FreeBSD server. For server install it’s necessary to make it as tight as possible.

When installing system, which main purpose will be running a few services - say, SMTP server or http proxy server, I prefer to use following partitioning:


swap - 2xRAM size, but not more than 1-2Gb
/tmp - 1-2Gb
/ - 6-8 Gb
/var -
/home- 1-2Gb
/data-

Why do I put partitions in this order? It makes overall system performance better — the closer to the center of HDD spindle — the less is sector seek time, so partitions which need faster access — swap and /tmp should go closer to the center of the disk.

The next partition is a root partition — I prefer to put there a lot of space to hold /usr directory with /usr/src and /usr/ports unpacked, also leaving space for port compilation.

/var — you should reserve there enough space for logs and mail queue and mailboxes. If you have system which should do intensive logging — put there at least 8-10Gb.

/home — if you will have some administrative users which should only monitor system, you can have /home partition pretty small - 1-2Gb, not more.

/data — spare partition where you can move working directories of services. Or you can directly mount this partition under /usr/local — especially if you run squid or apache, which by default use /usr/local/squid and /usr/local/www respectively.

This kind of partitioning allows you mount / in sync mode (you should put in fstab something like this

/dev/ad0s1a / ufs rw,sync 1 1

or even mount root filesystem in ro mode


/dev/ad0s1a / ufs ro 1 1

Mounting root filesystem in read-only mode will prevent corruption and allow system to boot under any circumstances.

Negative sides is that users won’t be able to change their passwords on their own and that you should remount it in r/w mode each time when you change system confguration.

Interesting article on disk seek times and etc — http://www.logicsmith.com/seektime.html

Optimizing Apache install

Sometimes it’s necessary to speedup page load times in Apache.
I had a problem with phpfox loading to the clients too slowly.

What I’ve done to the default Apachw2 install?

Find in config file (httpd.conf) this line
KeepAlive Off
and change to
KeepAlive On

This will turn on keep-alives (making several HTTP requests thru single tcp connectoin) and will save some time on pages which have lots of elements to load (pictures, css, javascript files and etc).

Turn on mod_defalte:
* Make sure, that mod_deflate is loaded — this line should be uncommented in the config file
LoadModule deflate_module modules/mod_deflate.so
* Turn on compression for necessary virtual host directory or location.
The following example shows how to turn on compression in Location directive

<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript text/css
</Location>

This will compress served text files only — html, javascript and css. But this is also enough to make page load much more faster. You will feel this on phpfox, phpbb or some another heavy software.

Setting up USB and CD/DVD auto mount

You can use default FreeBSD automount daemon (amd) to automatically mount and unmount external storage devices as USB or CD-ROM.

Add all necessary devices to /etc/fstab with noauto option (which will prevent automounting them at startup). This 2 entries are for single USB mass storage device and CD-ROM.


/dev/acd0 /mnt/cdrom cd9660 ro,noauto 0 0
/dev/da0s1 /mnt/flash msdosfs rw,noauto 0 0

Create these directories


mkdir /mnt/cdrom
mkdir /mnt/flash

Now we should tell amd that it should mount these devices automatically as we access their folders.

Put those options in /etc/amd.conf
[ global ]
restart_mounts = yes
unmount_on_exit= yes

And those lines to /etc/amd.map
# $FreeBSD: src/etc/amd.map,v 1.9 2002/05/15 22:24:29 obrien Exp $
* opts:=rw,grpid,resvport,vers=3,proto=tcp,nfs_retransmit_counter=10,nosuid,nodev

localhost type:=auto;fs:=${map};pref:=${key}/

localhost/cdrom type:=program;fs:=/mnt/cdrom;\
mount:=”/sbin/mount mount /mnt/cdrom”;\
unmount:=”/sbin/umount umount /mnt/cdrom”

localhost/flash type:=program;fs:=/mnt/flash;\
mount:=”/sbin/mount mount /mnt/flash”;\
unmount:=”/sbin/umount umount /mnt/flash”
One more step — enable amd startup at boot.

Add these lines to /etc/rc.conf

portmap_enable=YES
portmap_flags="-h 127.0.0.1"
amd_enable=YES
amd_flags="-a /.amd_mnt -c 10 -w 2 -l syslog /host /etc/amd.map"

Portmap will bind with localhost address - thus disallowing someone to connect from outside, which improves security.

amd_flags have interesting option -w, which specified how long keep device mounted after nobody access them. I prefer to keep this value very low (2 seconds) to avoid occasionally disconnecting device while it’s mounted.

After restart you can check that amd is running — run ps -ax | grep ‘amd\|rpc’ command. You should get a listing like this:

565 ?? Ss 0:00.01 /usr/sbin/rpcbind -h 127.0.0.1
607 ?? Ss 0:00.66 /usr/sbin/amd -p -a /.amd_mnt -c 10 -w 2 -l syslog /h

May be PIDs will be different, but this does not matter.

Ok, now if you change to /host/localhost/cdrom, it will try to mount cdrom under /mnt/cdrom, and create symbolic link in /host/localhost directory.

To make things much more comfortable, just create symlinks in root directory

ln -s /host/localhost/flash /flash
ln -s /host/localhost/cdrom /autocdrom

That’s it. You’ve got a working automounter for your pluggable devices.

Unmounting: it will unmount all devices as far as there is no program using it. So if you’ve changed to /cdrom and automounter mounted cdrom, please step out of directory (like cd .. or cd) to allow automounter unmount it.

Feel fee to ask questions in comments.

UPDATE: This method, as noted by mailing list participants, have limitation of having only one active USB mass storage device a time — but usually it’s ok.

(c) Gaspar Chilingarov




Анонс книги: Использование Shell с нуля и до профи!