Monthly Archive for August, 2008

Resize swap partitions on Red Hat Linux

First of all a little disclaimer - although I've done this a few times and never had any problems, one should never consider resizing partitions a completely foolproof exercise. Things can go wrong. With regards to swap though, don't worry if your swap partition is destroyed as this will not harm your system - most healthy systems will not ever need swap and unlike Windows, Linux only starts using swap when it needs it and can quite easily survive without it. You can trash it and format it as many times as you like as long as it's not in use. And of course take backups of any important data first!

Resize swap partition in Red Hat Linux

  • Download gparted (Gnome Partition Manager), burn the iso and then boot into it.
  • Gparted will identify the filesystems on each partition so your target will be clearly labelled as swap.
  • Reize your partitions as required. Try to minimise the overall number of resize and move operations as this can take several hours to complete.

When you reboot swap not be enabled - if you check using top (or htop) or free -m, you will see 0 mb of swap available. The reason for this is the UUID of the partition changed when it was resized by GParted, and this confuses the system when it tries to mount the volumes in /etc/fstab. The solution is to relabel your swap partition, by reformatting it as swap and specifying the correct label.

fdisk -l | grep swap

Note down the device name of your swap partition. e.g:

/dev/cciss/c0d0p3 1926 3837 15358140 82 Linux swap

Make a note of the label for the swap partition from fstab:

cat /etc/fstab | grep swap

e.g.

LABEL=SW-cciss/c0d0p3 swap

Now format your partition as swap, specifiying the label exactly as shown in the fstab.

mkswap /dev/cciss/c0d0p3 -L SW-cciss/c0d0p3

You can then enable the swap space straight away by using swapon devicename, or just reboot and check with free -m again, and all should be ok.

bash vs vbs (bash wins)

I've been asked by a group of users if they can have a daily report of everything the fax server has sent. Since the fax software we're using doesn't have this feature, I thought I'd write some vb script to pull all of lines containing yesterday's date from the log file (called sendlog.txt) and email them to a public folder. This took an hour or so to create and looks like this:

Const ForReading = 1

Dim fso, TempFile, yesterday, strSubject
yesterday = Date - 1

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = yesterday

Set objFSO = CreateObject("Scripting.FileSystemObject")

If (objFSO.FileExists("C:\temp\output.txt")) Then
 objFSO.DeleteFile "C:\temp\output.txt"
End If

Set objFile = objFSO.OpenTextFile("C:\Program Files\GFI\FaxMaker\sendlog.txt", ForReading)
Set TempFile = objFso.CreateTextFile("C:\Temp\output.txt", True)
Set wshShell = WScript.CreateObject ("WSCript.shell")

TempFile.Write "Fax Server sendlog for " & yesterday & vbCRLF & "----------------------------------" & vbCRLF & vbCRLF

Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString) 
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
           TempFile.Write strSearchString & vbCRLF
        Next
    End If
Loop

objFile.Close
TempFile.Close

strSubject = "FaxMaker_Sendlog_" & yesterday
wshShell.run "C:\monitoring\blat262\full\blat.exe" & " " & "C:\temp\output.txt" & " -to " & "myaddress@mydomain.com" & " -subject " & strSubject

Partial credit goes to the MS Scripting Guy who had already published part of this for me. Also you'll notice I called blat to email the file, which is considerably less complex than sending it in the vbs (or cheating, as some might call it) ... using vbs to send the message would have doubled the amount of code!

To accomplish the same task using a bash script, you could do it like this:

echo "Fax server sendlog for `date +%d/%m/%y`" > /tmp/tempfile.txt
cat /var/log/sendlog.txt | grep `date +%d/%m/%y` >> /tmp/tempfile.txt
mail -s "Fax Server log for `date +%d/%m/%y`" myaddress@mydomain.com < /tmp/tempfile.txt

Much simpler I think - only 3 lines of code!! People say Linux is harder than Windows but I think this proves that is not always the case. Manipulating files and data in Linux can take considerably less effort if you know which tools to use and how to use them.