How to deal with sparse files

Jephe Wu - http://linuxtechres.blogspot.com

Objective: understanding sparse files under Linux
Environment: CentOS 5.5 64bit


1.  What is the sparse file?
a sparse file is a type of computer file that attempts to use file system space more efficiently when blocks allocated to the file are mostly empty

http://en.wikipedia.org/wiki/Sparse_file

dd if=/dev/zero of=sparse-file bs=1k count=0 seek=5120


Will create a file of five megabytes in size, but with no data stored on disk (only metadata).

2. How to detect it

ls -lhs sparsefile  (it's sparse file once you see the different sizes)
or
du -sh sparsefile  (check the real size)
ls -lh sparsefile   (check the "visible" size)

3. how to copy/transfer with sparse file
a. transfer through network
tar cvzSpf - sparsefiles |ssh jephe@server '(cd /path/to; tar xzSpf -)'

b. copy locally
cp [ --sparse=always ] sparsefile newsparsefile
note: after copying, use 'ls -lhs newsparsefile' to check if it's sparse file

c. tar locally
tar Scvpzf sparsefile.tar sparsefiles

d. rsync to remote server
rsync -S --progress sparsefile jephe@server:newsparsefile

4. how to create sparse files under CentOS 5
(https://access.redhat.com/kb/docs/DOC-2282)
# dd if=/dev/zero of=jephe.img bs=1M count=1 seek=4K
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.005998 seconds, 175 MB/s
# ls -lh jephe.img
-rw-rw-r-- 1 user user 4.1G Oct 17 10:55 jephe.img
# du -h jephe.img
2.1M    jephe.img
# mkfs.ext3 jephe.img
...
# du -h jephe.img
196M    jephe.img
# mount -o loop gbfs.img /mnt/gbfs
# df -h /mnt/jephe/
Filesystem            Size  Used Avail Use% Mounted on
/shared/jephe.img      4.0G  137M  3.7G   4% /mnt/jephe

5. examples of sparse files
a. /var/log/faillog
b. KVM guest disk image file
c. database snapshots

Some common Oracle related FAQs

Jephe Wu - http://linuxtechres.blogspot.com


1. When compiling package body, it gives error like this: (Oracle 11g 64bit on CentOS 5.5 64bit)
ORA-04045: errors during recompilation/revalidation of schema_name.package_name
ORA-00600: internal error code, arguments: [kglgtbo2], [0x077F53600], [0x77F53600], [], [], [], [], []

Solution:  clean shutdown database then startup again
sqlplus / as sysdba
sql> alter system checkpoint;
sql> shutdown immediate;
sql> startup;

2. After downloading SQL Developer on Windows 7 pro, double click sqldevleoper, it gives error like this:
The program can’t start because MSVCR71.dll is missing from your computer. Try reinstalling the program to fix this program.

Solution:  download msvcr71.dll from dll-files.com, then put it under c:\windows\system32 (according to
http://i.justrealized.com/2009/how-to-fix-missing-msvcr71dll-problem-in-windows/ )

3. Schema statistics
exec dbms_stats.gather_schema_stats('JEPHE', cascade=>true);

4. SP2-0027: Input is too long (> 2499 characters) - line ignored
The script includes a SQL command that is over 2500 characters in length on one line

Solutions (from Oracle support)
1. Format the script or command so that any of your lines does not exceed the line length of 2500 characters.  Modify script (or command) by inserting a carriage return at a point where the first line does not exceed 2500 characters. Remember that each line of SQL command cannot exceed 2500 characters, so insert as many carriage returns as needed.
or
You can make the input blocks smaller, and concatenate them.

For example:
insert into table values ('<a number of characters up to 2499>'
|| '<more characters up to 2499>');

2. Then execute script (or command).

Note:  Another option is to use a different tool, like SQL Developer, which does not have this limitation. 

Check which lines are greater than 2499 characters:
i=1;lines=`wc -l filename.sql | awk '{print $1}'`; while [ $i -le $lines ]; do echo  -n $i; echo -n " ";sed -n $i'p' filename.sql | wc -c ;i=$[$i+1];done | sort -t" " -k 2 -n

Setting up VNC reverse connection and using VNC for Linux installation

Jephe Wu - http://linuxtechres.blogspot.com

Objective: Allow VNC server machine to connect to VNC viewer which is at listening mode when necessary. Also using VNC method to install Linux.
Environment: CentOS 5.5 32bit, Windows Vista 32bit with TightVNC.


Cases:
1.  Connect Windows to Windows
Install TightVNC first, one PC starts up TightVNC Viewer, then click on 'Listening Mode' button.
On another Windows desktop,  Startup TightVNC server in application mode or service mode, right click the icon at the right bottom of the screen, click 'Attach Listening Viewer', then type in Viewer IP address.
After that, VNC server Windows desktop will appear in the Listening viewer Windows machine.

2. Connect Linux VNC server to Windows VNC viewer at listening mode
Firstly, startup vncserver first on CentOS 5.5 remote console:

$ vncserver

It will automatically choose a display, starting from :1, if someone else has already started up vncserver from some other remote console, it will use :2.  Let's say it starts a vnc server at jephe.domain.com:1


$ vncconfig display=:1 -connect 192.168.100.20
or
$ export DISPLAY=:1; vncconfig -connect 192.168.100.20

note: 
a. before running above command, starting up TightVNC viewer at listening mode first on Windows vista machine, its IP address is 192.168.100.20.
b. Above command will push vnc display :1 to the client side at 192.168.100.20 port 5500 which is listening mode port


3. How to connect to remote Linux server which is pre-deployed somewhere and Linux server can access Internet directly but not other way round

Case description:  You need to physically deploy a pre-configured Linux server in the school, you are not able to directly ssh into that server anymore after deployment, the server itself is able to access Internet through school network (NAT). How do you check that server if someone in school reports to you there might be some problem with that server.

Solution: 
a. On your company data center server, start vncserver service at 1.2.3.4:1, you can remotely vnc connect to that server 1.2.3.4:1 by using TightVNC client. Now you get a X windows terminal on your Windows desktop
b. After that, run 'xhost +schoolpublicipaddress' from the xterm window to allow school IP to be able to push a xterm terminal to your data center vncserver console
c. Before server deployment, prepare a cronjob script to periodically (every 10 minutes) check your data center website URL at http://jephe.domain.com/vnccount
The vnccount file content is just one line which is your today's date such as:
20101010

If the file content is greater than before, of course, save the previous content first, then run xterm command like this:
xterm -display jephe.domain.com:1

Whenever you need to connect to school Linux server, increase the date count, wait for 10 minutes, a xterm window will appear, then you can control that school server.


4. Install Linux using VNC method:
Boot from Linux Installation DVD or CD, type in 'linux vnc vncpassword=jephe1' (note: password string must be at least 6 characters), after setting up a static IP address or letting it get it through DHCP, it will startup a VNC server for you to connect.

Moreover, you can use VNC reverse connection method to directly connect to your Windows VNC viewer which is running a listening mode. type in 'linux vnc vncconnect=10.0.0.4'. IP address 10.0.0.4 is your Windows TightVNC vncviewer PC.

5. References and FAQs
a. Linux vnc installation:

on the kernel line you need the following additional info :
vnc vncconnect=192.168.0.23 headless ip=dhcp ksdevice=eth0 method=http://mirror.centos.org/centos/4/os/i386/ lang=en_GB keymap=uk

b. How to connect to Linux/Unix server xwindows, gnome, kde etc with great performance
You can install freenx or NoMachine Linux server rpm and use NoMachine Windows client to connect to it.
For FreeNX, run 'yum install freenx' after installing DAG RHEL repository rpm.


c. vino-server for Gnome VNC
go to gnome desktop, System menu, perferences, Remote Desktop Preferences, tick 'Allow other users to view your desktop' and uncheck 'Ask you for confirmation' to enable VNC server for gnome itself. You might need to restart gnome to take effect, or use google search 'Enable and control the gnome VNC vino-server from the command line'.

d. vncviewer gnome full screen mode
You can exit from CentOS 5.5 gnome vncviewer full screen mode by pressing F8 key, then uncheck 'full screen'.

Setting up virtual machines with CentOS, KVM, Socat and RealVNC

Jephe Wu - http://linuxtechres.blogspot.com

Objective: use open source softwares to set up a virutal Windows XP machine inside Linux with excellent remote access performance
Environment: Dell OptiPlex 780 as host server running CentOS 5.5 64bit with 8G RAM, both KVM and Xen virtualzation installed, socat, realvnc, Windows XP virtual machine with bridged network br0 and Qumranet paravirtualized ethernet adapter, as well as RedHat VirtIO SCSI Disk Device driver.


Concept:
1. use CentOS 5.5 with KVM instead of Xen as KVM has the future. 
You don't have to change anything after inital KVM setup to enable bridged network, For NATed Windows machine, you can use 'socat' for tcp port forwarder on the VNC ports to make inside Windows XP virtual machine accessable to outside world, but vnc response speed is quite slow.

To gain better network, hard disk and graphic drawing speed, you can:
2. use realvnc server on virtual Windows XP SP3 machine instead of KVM builtin vnc server for better response speed, so called VNC mirror driver
3. use the latest virtio-win ISO package to install qumranet paravirtualized ethernet adapter driver instead of the default realtek network adapter for better network performance
4. use bridged network interface instead of the default NAT for Windows virtual machine for even better network performance and easier configuration, this requires some configuration after initial setup.
5. use RedHat VirtIO SCSI Disk Device driver instead of the default IDE driver for better block device performance.

Steps
1. Installation of CentOS 5.5
Download CentOS 5.5 DVD and burn it for local installation, or use NFS network installation if you are using DVD iso file.

Choose KVM and Gnome during installation.

If you have only CentOS 5 lower version, after install, run 'yum update' to upgrade to the latest.

2. KVM configuration
If you have installed both KVM and Xen, server will boot from Xen kernel, you will need to vi /etc/grub.conf to change it to boot from the plain Linux kernel which has KVM support.

By default, when you configure KVM virtual machine, it use NATed network only because it doesn't have bridged network for you to use. After that, the localhost will be listening at port 5900 for vnc connection, but it cannot be connected remotely since it's only listening at localhost.

You can use 'socat' (http://www.dest-unreach.org/socat/) as proxy to forward the vnc port by following the steps below:

# Install DAG rpmforge package
# rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS//rpmforge-release0.3.6-1.el5.rf.x86_64.rpm

# yum install socat

# add local2 to /etc/syslog.conf
[root@jephe ~]# grep socat /etc/syslog.conf
local2.*                        /var/log/socat.log

# socat -d -d -lmlocal2 tcp4-listen:5900,bind=10.0.0.1,su=nobody,reuseaddr,fork tcp4:127.0.0.1:5900 &
note:
This will enable inside KVM virtual machine to accept connection from network.
a. log will be recorded in local2 which is /var/log/socat.log according to /etc/syslog.conf
b. listening on 10.0.0.1:5900 (host server tcp socket instead of the KVM builtin one which is localhost:5900)
c. su as nobody
d. receiving vnc connection from network then forward to inside Windows XP SP3 virtual machine at 192.168.100.230:5900

You can put above socat command into /etc/rc.d/rc.local.

Issues:
With above configuration, the VNC response speed is very slow. You can improve it by using realvnc or tightvnc on virtual machine itself and change network adapter to bridged mode as well as using Redhat VirtIO ethernet adapter and RedHat VirtIO SCSI Disk Device driver.

3. Installing Windows KVM guest
a. When installing Windows 7, use 'Windows vista' option. When you choose 'local CDROM install' option, the installation path part might become grey, you can check if you enabled 'messagebus' daemon and 'haldeamon' daemon:

service autofs start
service messagebus start
service haldaemon start

So that the auto mount will work under gnome desktop, check 'ls -l /dev/cdrom' which should be symbolic linked to /dev/scd0.

4. Improving performance  - RealVNC with mirror driver
installing realvnc or tightvnc latest version (both are supporting so-called mirror driver)
realvnc server free edition can enable bi-directional cut and paste but there's no password protection for configuration change once login
tightvnc has password protection for console admin but has no support for pasting out text from virtual machine, only one way pasting.

After installing realvnc or tightvnc, you can use socat like this:

# socat -d -d -lmlocal2 tcp4-listen:5900,bind=10.0.0.1,su=nobody,reuseaddr,fork tcp4:192.168.100.230:5900 &

5. Improving performance - Bridged network


Refer to http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/sect-Virtualization-Network_Configuration-Bridged_networking_with_libvirt.html

Here are my steps according to above RHEL doc website:

vi /etc/xen/xend-config.sxp and changing the line:

 (network-script network-bridge)

To:

 (network-script /bin/true)

# chkconfig NetworkManager off
# service NetworkManager stop

# cd /etc/sysconfig/network-scripts
Add the following line to ifcfg-eth0 which will be used for bridge.
BRIDGE=br0
MTU=9000

add the following lines to ifcfg-br0
[root@jephe network-scripts]# more ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
DELAY=0
BROADCAST=10.0.0.255
IPADDR=10.0.0.1
NETMASK=255.255.255.0
NETWORK=10.0.0.0

After configuring, restart networking or reboot.

# service network restart

Configure iptables to allow all traffic to be forwarded across the bridge.

# iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
# service iptables save
# service iptables restart

 Restart the libvirt daemon.

# service libvirtd reload

Exit virt-manger, then start it again. Delete storage device hda , then add again, choose 'virtio' type. You should now have a "shared physical device", which guests can be attached and have full LAN access. Verify your new bridge:

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.000000000000       yes
br0             8000.000e0cb30550       no              eth0

Note, the bridge is completely independent of the virbr0 bridge. Do not attempt to attach a physical device to virbr0. The virbr0 bridge is only for Network Address Translation (NAT) connectivity.

6. Improving performance - Virtio ethernet adapter driver
download the latest Windows guest network drivers from http://www.linux-kvm.com/  (left side of web page, under 'Windows Guest Drivers' - download the driver CD.

mounted as local cdrom image in virt-manager before rebooting Windows virtual machine.
After booting up, it found the new ethernet adapter, then I used cdrom to install driver.

After that, shutdown virtual machine, delete the existing network card in virt-manager, then add one more network card with virtio type, now you should be able to see bridged network br0 option

7. Improving performance - Windows block driver (RedHat VirtIO SCSI Disk Device driver)
In my case, I use Windows XP SP3 32bit as virtual machine, so I have done the following:
Visiting website http://www.linux-kvm.com/ , at the left side to download the latest Windows guest drivers CD or floppy (during installation)

and check
http://www.linux-kvm.com/content/block-driver-updates-install-drivers-during-windows-installation

Important: You cannot just delete hda then create vda, then startup guest, you need to add a small vda storage first without deleting the main hda, after that, boot into guest to install Redhat virtio scsi disk driver, after that, shutdown, delete vda and hda, then create vda to use the existing image.
otherwise, it will not be able to boot Windows. Here are the steps:


Shutdown virtual machine, use virt-manager to add one more storage device ( use dd if=/dev/zero of=/root/testfile bs=1k count=1000 to create a small file first as vd0 just for installing virtio disk driver)


After booting up virtual machine again, Windows XP detected a new scsi hard disk, then use above driver inside the cdrom to install it.


Shutdown again, use virt-manager to delete vd0 and hd0, after that ,create another storage device, use the existing hard disk image file, boot up again, this time, hard disk will become RedHat VirtIO SCSI Disk Device driver

Now, use vnc client to connect to Windows virtual machine and enjoy the fast speed.

Let's say you want to use floppy disk version after initial installation of virtual machine, you can use dd if=viostor-floppy.img of=/dev/fd0 to write to floppy disk. You also can mount the floppy img file as loop device as follows:

mkdir /mnt/viostor
cd /mnt/viostor
mount viostor-31-03-2010-floppy.img viostor/


8. Using virtio driver to improve network and disk performance when installing RHEL 5.4 guest

How can I improve network and disk performance of Red Hat Enterprise Linux KVM guests?- https://access.redhat.com/kb/docs/DOC-18756 

and

How do I set up virtio on a kvm guest to get better network and I/O performance on RHEL4 and RHEL5? - https://access.redhat.com/kb/docs/DOC-25441

If using the virt-manager graphical tool to install a new guest, select following options on the OS type selection page to enable virtio network and block device drivers for this new guest:

OS type: Linux
Version: Generic 2.6.25 or later kernel with virtio


9. High Resolution Graphic on KVM Windows guest 
For Windows 7 pro guest in KVM, you can use advanced mode option to choose monitor model up to resolution 1280x1024.



http://www.linux-kvm.com/content/using-high-resolution-graphics
http://www.linux-kvm.com/content/using-vmware-vga-kvm-windows-guests




10. How to clone the existing KVM guest
cd /var/lib/libvirt/images
virt-clone --original guest1 --name newguest1 --file newguest1.img

11. See Also
a. http://www.linux-kvm.org/page/Main_Page
b. http://virtualization.info/en/
c. http://www.linux-kvm.com/

d. KVM virtio network drivers

http://www.linux-kvm.com/content/tip-how-setup-windows-guest-paravirtual-network-drivers
http://www.linux-kvm.com/content/latest-release-windows-virtio-network-drivers
http://sourceforge.net/projects/kvm/files/kvm-driver-disc/

e. KVM Windows block drivers

http://www.linux-kvm.com/content/redhat-54-windows-virtio-drivers-part-2-block-drivers
http://www.linux-kvm.com/content/block-driver-updates-install-drivers-during-windows-installation

Use socat as tcp forwarder on Windows and restrict a few hosts to connect to it

Jephe Wu - http://linuxtechres.blogspot.com

Objective
: on the company LAN, only one Windows server is able to connect to smtp server, some other servers need to connect to smtp server as well
Environment: Windows 2000 server A with one NIC, another Windows 2000 server B will be connecting to A at port 25 to reach company smtp server smtp.domain.com.


Steps:


1. download and setup socat for Windows on server A

download 2 files below from http://www.gentilkiwi.com/telechargements-s43-t-socat.htm#englishversion

socat-1.7.1.3.zip and cyg-dep.zip

Create c:\socat directory and extract both zip files under it

How to test it?
c:
cd socat
socat
2010/10/06 10:59:02 socat[15822] E exactly 2 addresses required (there are 0); use option "-h" for help


Note: once you see above output, that means socat is installed successfully.

2. run socat command with options to do tcp port forwarder
on Windows server A:
cd socat
socat -d -d -lf socat.txt tcp4-listen:25,reuseaddr,fork,tcpwrap=socat,allow-table=socat-allow,deny-table=socat-deny tcp4:smtp.jephe.com:25

the following are the content inside socat-allow and socat-deny files:
For socat-allow:
socat:1.2.3.4,5.6.7.8

For socat-deny:
socat:all


on Windows server B:

You can use server A as smtp server which will be forwarded by socat to smtp.domain.com

note: you can use range option to restrict the source ip such as 10.0.0.0/8 if that's better for your situation.

Appendix:
a. You can use plugdaemon (http://www.taronga.com/plugdaemon/) on Linux or iptables(snat/dnat, even for single NIC with kernel parameter net.ipv4.ip_forward enabled) to achive port forwarding function also.
b. socat website is at http://www.dest-unreach.org/socat/

Oracle database deployment without system and schema passwords

Jephe Wu -  http://linuxtechres.blogspot.com

Objective:  deploying Oracle database scripts without OS user 'oracle' account login and Oracle system users and individual schema passwords for accountability and audit purpose
Environment: RHEL 5, Oracle 11g 64bit


Steps:
1. configure sudosh for Oracle database server so that you can login as your own id then run 'sudo -u oracle sudosh' to switch to oracle user.

Refer to article http://linuxtechres.blogspot.com/2008/12/use-sudosh-to-enable-server-auditing.html


2. login as sysdba then alter session to change current schema to individual client schema
$ sudo -u oracle sudosh


then use the following shell script to deploy script.sql to individual client schemas without password:

$ cat deploy.sh
#!/bin/sh
FILENAME=deploy
for i in jephe1 jephe2 jephe3
do
echo "deploying $i"
cat >${FILENAME}_$i.sql <<END
spool ${FILENAME}_$i.log
set define off sqlblanklines on
@script.sql
spool off;
exit;
END


sqlplus / as sysdba <<END
alter session set current_schema=$i;
@${FILENAME}_$i.sql
END

sleep 1
done

3. After deployment, recompile all client schemas
$ cat compileallschemas.sh
#!/bin/sh
cd /home/oracle/
cat > comileallschemas.sql << END
exec dbms_utility.compile_schema('JEPHE1');
exec dbms_utility.compile_schema('JEPHE2');
exec dbms_utility.compile_schema('JEPHE3');
END


sqlplus / as sysdba @/home/oracle/compileallschemas.sql

4. check all invalid objects 
$ cat numberofinvalidobjects.sql
select count(*)  jephe1, object_type from dba_objects where owner='JEPHE1' and status <> 'VALID' and object_name not like 'EXCLUDED_OBJ%' group by object_type;
select count(*)  jephe2, object_type from dba_objects where owner='JEPHE2' and status <> 'VALID' and object_name not like 'EXCLUDED_OBJ%' group by object_type;
select count(*)  jephe3, object_type from dba_objects where owner='JEPHE3' and status <> 'VALID' and object_name not like 'EXCLUDED_OBJ%' group by object_type;
exit;


$ sqlplus / as sysdba @numberofinvalidobjects.sql
==========================================
$ cat invalidobjects.sql
column object_type format A10;
column object_name format A30;
select object_type "Invalid jephe1",object_name from dba_objects where owner='JEPHE1' and status <> 'VALID' and object_name not like 'EXCLUDED_OBJ%' order by object_type,object_name
select object_type "Invalid jephe1",object_name from dba_objects where owner='JEPHE1' and status <> 'VALID' and object_name not like 'EXCLUDED_OBJ%' order by object_type,object_name
select object_type "Invalid jephe1",object_name from dba_objects where owner='JEPHE1' and status <> 'VALID' and object_name not like 'EXCLUDED_OBJ%' order by object_type,object_name
exit;


$ sqlplus / a sysdba @invalidobjects.sql