Saturday, April 28, 2007

Restore Emacs Meta Key in FreeBSD

When working under FreeBSD console at a PC, I cannot use ALT as META key in the Emacs. It means the Alt key is uneffective. Thus, I had to press ESC then X to input Emacs commands. It is such a trouble than I often press wrongly. Now there is a method to use ALT as META function, Just use the tool kbdcontrol to load the emacs keyboard map, us.emacs.kbd.
kbdcontrol -l us.emacs

Friday, April 27, 2007

Hibernate Your Laptop

Hibernate has the ability to take a snapshot of the state of a computer's memory and save it to disk, and restore that image later. This allows you to fully power down a computer at an arbitrary point in time and not need to reload programs and reopen documents on power-up. In modern laptops, suspension is not done by the BIOS. They use ACPI and ask the operating system to do most of the job. If you can't satisfy yourself with standby mode (suspend to RAM) because you leave your computer in suspend mode for too long (or because you're not using a 2.5 or later kernel and therefore lack suspend-to-RAM support), then you have to suspend to disk. Now, we can hibernate into not only a swap partition, but also a file. And to complete this task, you should patch your linux kernel firstly. I practised the suspend2 in my notebook with Gentoo Linux. Also you can follow the HOWTO docuements at suspend2 official website, which is a nice tutorial. If you also use Gentoo, you need not patch the linux kernel manually. You just only run the command below:
# emerge suspend2-sources
And portage system will patch your linux kernel automaticly, then:
# cd /usr/src/linux
# make menuconfig
Select suspend2 options, here is the path:
  Power management options (ACPI, APM)  --->
  [*] Suspend2  ---> 
      --- Suspend2 
      ---   Image Storage (you need at least one allocator)
      [*]   File Allocator   
      [*]   Swap Allocator 
      ---   General Options 
      (swap:/dev/hda8) Default resume device name
      [*]   Allow Keep Image Mode
      [*]   Replace swsusp by default
And make sure your .config contains such settings, and "swap:/dev/hda8" is the default swap partition to be suspended:
Then compile and install your new kernel, then reboot. *ATTATION* DO NOT forget to backup your original working kernel. If you find such content when "dmesg| less", (eg, "Software Suspend Suspending enabled."), it means you can hibernate your computer now. Now, It's time to manage the hibernate actions. Firstly, fetch the hibernate-script by emerge or from suspend2 website:
# emerge hibernate-script
If you use Thinkpad, you can use the hotkey "Fn+F12" to hibernate with acpid. The event files lie in the directory /etc/acpi/events, and Here is the content of /etc/acpi/events/hibernate:
event=(button/power|ibm/hotkey HKEY 00000080 0000100c)
action=/usr/sbin/hibernate -F /etc/hibernate/suspend2.conf
The HKEY number 00000080 0000100c is the action to press the hotkey "Fn+F12". Here is the detail. If you mount vfat filesystems, you'd better to umount them, when hibernating. Put such settings into the configuration file /etc/hibernate/suspend2.conf
UnmountFSTypes vfat fat msdos
And remount them when re-booting your system with this setting in /etc/hibernate/ususpend-disk.conf:
Mount /path/to/vfat/

Tuesday, April 17, 2007

Install Gentoo Stage3 in Thinkpad T40p

This article is an old post I wrote 2 years ago so that I translate it into English Version and MODIFIED some points. The original can be found Here.


Press Power button of you computer, insert a linux livecd into the cdrom, then choose to boot from cdrom. When you login into the operating system of the livecd, switch into a terminal and let's start your gentoo installation from here...

Download Gentoo LiveCD or stage3 packs

You can use links or lynx download livecd and stage3 packages from, or if you stay at cernet.


Now, we partition the disks and be careful. Here is an example of my computer, and linux can be installed in any primary or extend partitions. You can use fdisk to complete your task. I suggest 4 partitions for your linux system:
  • /boot, ext2 format, 50M bytes, where to put vmImage and grub
  • /swap, swap format, about size of your physical memory large if you need supsend2(hibernate into disk). You can also totally disable this swap partition if your memory is large enough, such as 1G or 2G bytes :)
  • /, root partition can be formated as reiserfs4 due to the performances, 8~10G bytes
  • /home, formated as ext3 because ext3 expressed steady and tough.
Here is the output of fdisk run at my disks. Recording your blocks of every partitions is helpful if your lost your mbr unfortunately.
Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1        1460    11727418+  af  Unknown
Partition 1 does not end on cylinder boundary.
/dev/hda2   *        1461        1947     3900960    7  HPFS/NTFS
Partition 2 does not end on cylinder boundary.
/dev/hda3            1948        9729    62508915    f  W95 Ext'd (LBA)
Partition 3 does not end on cylinder boundary.
/dev/hda5            1948        3407    11727418+   7  HPFS/NTFS
/dev/hda6            3408        6569    25394481    b  W95 FAT32
/dev/hda7            6569        6575       52416   83  Linux
/dev/hda8            6576        6710     1084356   83  Linux
/dev/hda9            6711        7684     7823623+  83  Linux
/dev/hda10           7685        9729    16426431   83  Linux
Mount your partition
Enable DMA feature of your disk, this step is optional.
# hdparm -c 1 -d 1 /dev/hda
Format your partition and mount them, be careful.
# mke2fs /dev/hda7
# mkswap /dev/hda8
# swapon /dev/hda8
# mkfs.reiser4 /dev/hda9
# mke2fs -j /dev/hda10
# mount /dev/hda9 /mnt/gentoo
# mkdir /mnt/gentoo/boot
# mount /dev/hda7 /mnt/gentoo/boot
# mkdir /mnt/gentoo/home
# mount /dev/hda10 /mnt/gentoo/home
# mkdir /mnt/gentoo/proc
# mount -t proc none /mnt/gentoo/proc
# mkdir /mnt/gentoo/dev
# mount -o bind /dev /mnt/gentoo/dev
Backup your mbr
  • Backup your mbr table:
    # dd if=/dev/(your_disk) count=1 bs=512
    # sfdisk -d /dev/(your_disk) >
    The first of those saves the mbr and the second will store all partition info (including logical partitions, which aren't part of the mbr).
  • Restore your mbr table:
    # dd of=/dev/(your_disk)
    # sfdisk /dev/(your_disk) <


Uncompress your stage3 packages downloaded before. Argument "p" of "tar" makes sure keeping the permit bits of the files in the tarball.
# cd /mnt/gentoo
# tar xvjpf /mnt/cdrom/stages/stage3-xxx.tar.bz2
Install "portage"
This step is optional, and you can use "emerge --sync" to replace when you login into the new system. Download portage, and umcompress into /mnt/gentoo.
# tar xvjf /mnt/gentoo/portage-.tar.bz2 -C /mnt/gentoo/
Configure "make.conf"
Some variables related with the system are configured at /etc/make.conf. The important is that you'd better make sure "USE" variable containing "cjk nls ntpl ntplonly", if the architecture of your computer is x86. It is recommonded to set "ACCEPT_KEYWORDS" as "x86" if you donot wanna test and debug for unstable systems :P
CFLAGS="-march=pentium-m -O3 -pipe -fomit-frame-pointer"
#CFLAGS="-O3 -march=pentium-m -mtune=pentium-m -pipe -ftracer -fomit-frame-pointer -ffast-math -momit-leaf-frame-pointers" 
CXXFLAGS="${CFLAGS} -fvisibility-inlines-hidden"
#LDFLAGS="-Wl,-O1 -Wl,--enable-new-dtags -Wl,--sort-common -s"
USE="-fortran -arts -eds -ipv6 -qt -qt3 -qt4 -kde python -vorbis acpi X \
     bash-completion cjk cups esd gtk2 imlib mime mmx mmxext nls aiglx alsa \
     nptl nptlonly opengl oss posix readline sse sse-filters sse2 \
     truetype unicode xft ati dri apm -apache -apache2 -xmms -ldap "
FEATURES="ccache parallel-fetch"

VIDEO_CARDS=" radeon vesa vga"
INPUT_DEVICES=" keyboard mouse void synaptic"
Custom Packages Independently
You can custom packages' compile setting for each by the config files under the directory "/etc/portage/*". Here is a "USE" setting sample of /etc/portage/package.use:
sys-libs/glibc userlocales
x11-terms/rxvt-unicode xft -iso14755
#x11-base/xorg-x11 -3dfx -3dnow -bitmap-fonts -font-server -hardened -insecure-drivers -ipv6 -minimal mmx nls opengl pam -sdk sse sse2 -static truetype-fonts xv -type1-fonts -xprint 
media-video/mplayer rtc avi amr encode esd mpeg quicktime real cdparanoia dvd dvdread ati win32codecs xvid -xmms cpudetection
mail-client/mutt imap pop mbox smime
net-print/cups samba
dev-util/subversion -nowebdav -apache2
x11-libs/cairo glitz pdf png
net-im/gaim -crypt gstreamer audiofile custom-flags qq msn xscreensaver startup-notification
net-www/apache -ldap
dev-lang/php -ldap -truetype -crypt apache2 gd ftp mysql xmlrpc
dev-db/mysql -berkdb -perl
media-sound/audacious chardet
media-plugins/audacious-plugins aac alsa chardet mp3 nls wma
#app-editors/vim cscope
x11-apps/xinit minimal
dev-libs/libxml2 python
net-libs/libsoup -ssl
media-sound/lame -gtk
net-wireless/wpa_supplicant madwifi
app-admin/gnome-system-tools samba
app-laptop/tpb xosd
sys-apps/hal pcmcia
media-sound/rhythmbox libnotify tagwriting keyring
www-client/mozilla-firefox -linguas_zh_CN

Prepare to Installation

Firstly, copy resolv.conf into the new system:
cp /etc/resolv.conf /mnt/gentoo/etc/
Then, chroot into the new system, begin to install...
# chroot /mnt/gentoo /bin/bash
# env-update && source /etc/profile
Update-to-date portage
Run command:
# emerge --sync
Locale Setting
According to the i18n file "/usr/share/i18n/SUPPORTED", Modifiy /etc/locale.gen. If you use chinese language, here is an example:
en_US.UTF-8 UTF-8
en_US ISO-8859-1
zh_CN GB2312
zh_CN.UTF-8 UTF-8
zh_CN.GB18030 GB18030
Time Zone Setting
If you stay at timezone +8, then:
# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Modify "/etc/conf.d/clock", and modify it as:
Adjust system time correctly.
# date MMDDhhmmYYYY
# hwclock --systohc

Install Linux Kernel

Download Kernel Source
Firstly, download the lasted stable kernel source:
# USE="-doc" emerge gentoo-sources
Compile Kernel and Install
You can use custom and optimize the kernel options according to the computer hardware.
# cd /usr/src/linux
# make menuconfig
# make && make modules_install
# cp arch/i386/boot/bzImage /boot/linux-
Install GRUB
You can choose to install bootloader into the mbr or /boot by running command "setup (hd0)" or "setup (hd0,6)". Here is the sample run at my computer:
# emerge grub
# grub
grub> root (hd0,6) 
grub> setup (hd0)
grub> quit
Then, Configure your grub profile /boot/grub/menu.lst:
default saved
color light-gray/blue black/light-gray
timeout 2

title=Gentoo GNU/Linux
root (hd0,6)
kernel /linux-2.6.17-r8 root=/dev/hda9 ro

title=Gentoo GNU/Linux (suspend2)
kernel /linux-2.6.18-suspend2-r1 root=/dev/hda9 ro resume2=swap:/dev/hda8

title=Microsoft Windows 2003 Server
rootnoverify (hd0,1)
chainloader +1

title=Mac OS X
root (hd0,0)
chainloader +1

Other Steps

setup fstab
Edit /etc/fstab to mount each partition correctly:
/dev/hda7  /boot  ext2  defaults   1 2
/dev/hda8  none            swap            sw              0 0
/dev/hda9  /  reiserfs defaults   0 1
/dev/hda10  /home  ext3  defaults   0 0
none        /proc     proc    defaults          0 0
none        /dev/shm  tmpfs   defaults          0 0
/dev/hda6 /home/wine vfat defaults,shortname=mixed,utf8,fmask=133,dmask=022,uid=shokn,gid=users 0 0
/dev/hdc /mnt/cdrom iso9660 ro,noauto,user,utf8 0 0
Add a new user
Add a new user named "shokn":
# useradd shokn-m -G users,wheel,portage,audio,games -s /bin/bash
# passwd shokn
Active the ethnet card
Add net.eth0 into the runlevel boot
# rc-update add net.eth0 boot
If you have a static IP address rather than a DHCP address, then modify /etc/conf.d/net:
config_eth0=(" netmask")
routes_eth0=("default via")
Here is another useful script to setup your eth0 address from gentoo livecd-tools:
# Copyright 1999-2004 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/www/,v 1.3 2004/07/11 23:33:45 wolf31o2 Exp $

# John Davis 

if [ `whoami` != "root" ]; then
 echo "net-setup: must be root to continue"
 exit 1

if [ -z ${1} ]; then
 echo "net-setup: please specify a network interface"
 exit 1

[ ! -d /tmp/setup.opts ] && mkdir /tmp/setup.opts
cd /tmp/setup.opts

dialog --title "Network Setup" --menu "Time to set up the ${1} interface! You can use DHCP to automatically configure a network interface or you can specify an IP and related settings manually. Choose one option:" 20 60 7 1 "Use DHCP to auto-detect my network settings" 2 "Specify an IP address manually" 2> ${1}.1
mynetsel=`cat ${1}.1`
case $mynetsel in
  /sbin/dhcpcd -t 10 ${1}
  dialog --title "IP address" --inputbox "Please enter an IP address for $1:" 20 50 "" 2> ${1}.IP
  dialog --title "Broadcast address" --inputbox "Please enter a Broadcast address for $1:" 20 50 "`cat ${1}.IP|cut -d . -f 1`.`cat ${1}.IP|cut -d . -f 2`.`cat ${1}.IP|cut -d . -f 3`.255" 2> ${1}.B
  dialog --title "Network mask" --inputbox "Please enter a Network Mask for $1:" 20 50 "" 2> ${1}.NM
  dialog --title "Gateway" --inputbox "Please enter a Gateway for $1 (hit enter for none:)" 20 50 2> ${1}.GW
  dialog --title "DNS server" --inputbox "Please enter a name server to use (hit enter for none:)" 20 50 2> ${1}.NS
  /sbin/ifconfig $1 `cat ${1}.IP` broadcast `cat ${1}.B` netmask `cat ${1}.NM`
  myroute=`cat ${1}.GW`
  if [ "$myroute" != "" ]; then
   /sbin/route add default gw $myroute dev $1 netmask metric 1 
  myns="`cat ${1}.NS`"
  if [ "$myns" = "" ]; then
   : > /etc/resolv.conf
   echo "nameserver $myns" > /etc/resolv.conf

echo "Type \"ifconfig\" to make sure the interface was configured correctly."

# vim: ts=4
Now, You have nearly completed this installation. (to be cont.)