Archive

Archive for the ‘linux’ Category

Finding free disk space in Ruby

December 16th, 2008 4 comments

UPDATED I can’t find a standard Ruby idiom for finding out the free/occupied disk space on a partition, so here’s a hack for doing it under Linux:

1
2
3
4
5
6
7
8
9
def disk_used_space( path )
  `df -Pk #{path} |grep ^/ | awk '{print $3;}'`.
    to_i * 1.kilobyte
end

def disk_free_space( path )
  `df -Pk #{path} |grep ^/ | awk '{print $4;}'`.
    to_i * 1.kilobyte
end

The -P (POSIX compliant output) argument to df is important, it forces the output to be more regular, thus easier to parse. Without -P, you’ll run into trouble if the names of the disk devices are particularly long, e.g. if the machine uses LVM:

mymachine:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/md0               9614052   2819896   6305788  31% /
/dev/mapper/storage-s1
                     331624912    395808 314383488   1% /mnt/s1
/dev/mapper/storage-s2
                     109681544    192248 103917712   1% /mnt/s2

The 1.kilobyte idiom is available only if you use ActiveSupport, e.g. in a Rails application. If you can’t use ActiveSupport, just write 1024 instead.

Update: the -P option is not enough, you should also use -k to get the output in 1 KB blocks. Thanks to commenter Martin Rehfeld for the tip.

Hope you find this useful.

Categories: linux, programming

Romanian Police evangelising Linux to students

June 9th, 2006 1 comment

A newsletter from the Iaşi police reports that community officers have been visiting secondary schools in Iaşi to warn students of the perils of using unlicened software and illegally copied media. The police officers introduced the 7th-graders to the Linux operating system and the thousands of free applications that accompany it.

Hats off! This is really the first time I hear about officials actually mentioning the Open-Source alternative, in the context of unlicensed commercial software.

For my unaware readers… This happens in a country whose Government itself signs “strategic partnerships” with Microsoft and spends many millions of taxpayer €’s on MS licences in the educational system, in spite of the tight budgets, underpaid staff, lack of teachers in rural areas, and the otherwise deplorable state of Romanian schooling.

The text of the news item, in Romanian, is mirrored here.

Categories: IT politics, linux

Resizing partitions: The forgotten resize2fs

May 8th, 2006 No comments

So you want to resize one of your partitions. If you’re using ext2/ext3, and the FS is larger than usual (mine was 170 GB), parted (otherwise a pretty neat tool) may fail on you with this message:

1
No Implementation: This ext2 file system has a rather strange layout!  Parted can't resize this (yet)

This happens with parted 1.6.25.1 (the current stable version). The parted FAQ says that new code has been added in 1.7 to address this; however 1.7.0rc5 fails with a creepy error message, and an apology for me having run into a bug :)

Not more than a Google search away, I found the venerable resize2fs, which did the job perfectly.

When using resize2fs however, there is some additional work that you have to do. More specifically, resize2fs will resize the filesystem itself, not the underlying partitions. You must use fdisk (or equivalents) to enlarge the partition before growing the fs, or to cut the partition after shrinking the fs.

You should also NOT run resize2fs (or any other fs manipulation tool, for that matter) except on a CLEAN and consistent filesystem. Run e2fsck before and after resizing.

And finally, you may want to make sure that what you’re resizing is an ext2 filesystem, and not the newer ext3. As a reminder, to convert from ext3 to ext2, use

1
 # tune2fs -O^has_journal /dev/sdaX

And, after you’ve grown/shrunk your partition, you can go back to ext3 with:

1
 # tune2fs -j /dev/sdaX

Don’t forget to update your fstab.

Good luck!

Categories: linux