Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"

Monday, May 14, 2012

Create filesystem in a partition

One of my application creates un-necessary logs or dumps that fills my filesystem .
Say example I have only root partition "/" and other filesystems(/var/, /usr etc..) are in "/" .
Sometime one of my application creates lots of files and unwaned logs at path /var/log/mycore and that fills my root partition and my oher application stops working .

I want to give a permanent solution, but I have some limitations as below
- I don't have extra free disk block to create new partition
- I can't add any any new disk/LUN etc .
- I can't stop that application logging etc...

Here's solution that have applied on one of my server and it's working as am expected .

## Example
# df -h  /
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              72G   57G   12G  79% /

## I have 12 GB free space now check size of dir: /var/log/mycore
# du -sch /var/log/mycore
2G     /var/log/mycore
2G     total

## Okay now we have 2 way
1> Define quota .. but not preferred .
2> Create a filesystem with allocated space

## Am preferring to go with way-2 as below
As have 12G free space on "/", so I will allocate 3GB to /var/log/mycore as below

Step-1: Create a file usign dd with size 3GB
# dd  if=/dev/zero of=/var/log/FS_mycore bs=1024M count=3

Step-2: Create file system as per your requirement here am going with ext4
# mkfs.ext4  /var/log/FS_mycore

Step-3: Stop your application

Step-4:  Move /var/log/mycore  and create a new directory (Note: Reserve the permissions as original dir has)
# mv /var/log/mycore  /var/log/mycore_old
# mkdir  /var/log/mycore

Step-5: Mount and copy back all files and delete /var/log/mycore_old
# mount  -o loop  /var/log/FS_mycore  /var/log/mycore
# mount  -l


# df -h  /var/log/mycore/
Filesystem            Size  Used Avail Use% Mounted on
/var/log/FS_mycore    3.0G   68M  2.8G   2% /var/log/mycore/

# cp -rp  /var/log/mycore_old/* /var/log/mycore/

# cd  /var/log/mycore/ ; ls -lrth
# rm -fr  /var/log/mycore_old/

## Make entry in fstab
/var/log/mycore_old     /var/log/mycore         ext4    defaults,loop        0 0


##  Testing
# umount  /var/log/FS_mycore # mount -a
# mount -l 

## Step-6: Don't forget to start your application and check logs and all to verify all are okay ...

 


> Shirish Shukla

Diff file1 file2


Many time we come across situation that we want exact diff between two files ..
There are various Linux command which we can use like

# diff
# cmp

EG:
# cat  1
1
2
3
4
5
6
# cat  2
2
3
6
7
8

### Using diff
# diff -iw 1 2
1d0
< 1
4,5d2
< 4
< 5
6a4,5
> 7
> 8


Expln:
1d0< 1
- Add "1" at line 1 of file 2
4,5d2
< 4
< 5

- Add  "4" at line 4 of file 2
- Add  "5" at line 5 of file 2
6a4,5
> 7
> 8

- Add  "7" at line 7 of file 2
- Add  "8" at line 8 of file 2

-- Actual count as line 0 - N


Here it's tough to found whats mean of above if you are using it first time, What we expect is I want only those line which are missing in file 2 .

So have written a small PerlScript to achieve so, it's possible with BashScript too, but to parse a large file BashScript will take more time .


Script : diff.pl

#!/usr/bin/perl
# Written By - Shirish
#### perl  diff.pl file1 file2  OUTPUT
#### Will give you exact diff between file1 and file2 in OUTPUT file
##### Only lines that are in file1 but missing in file2
#################
#open a, "<filea";
#open b, "<fileb";
$x = $ARGV[0];
$y = $ARGV[1];
$z = $ARGV[2];
open a, "< $x";
open b, "< $y";
open (OP, ">$z") || die ("Unable to create Report");
local $/;
my @a = split /\n/, <a>;
my @b = split /\n/, <b>;
my %b = map { $_ => 1 } @b; # Make hash of B
my @res = grep { !defined $b{$_} } @a; # Everything in A not in B
print OP join "\n", @res;
#print join "\n", @res;
print "\n";

## How to use
## What's in file 1 which are absent in file 2
# perl diff.pl 1 2 1-2 
# cat 1-2
1
4
5

## What's in file 2 which are absent in file 1
# perl diff.pl 2 1 2-1
# cat 2-1
7
8




> Shirish Shukla




difference between two file linux, unix, find diff of two files, command diff, perl script to find diff of two files, compare two files, linux, linuxva, shirish, shukla

Followers

Pls LIKE my Story !!!