LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 01-29-2010, 11:24 PM   #1
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 277

Rep: Reputation: 32
Challenge: Defragmeting a windows ntfs partition in linux


This is not a question, I have done it. I just want to post a little howto in case anyone runs into a similar problem.

I will not go into particular details, just the rough description. If need details on something, I'll be happy to explain the part in question.


Scenario:

I am working on linux for almost a decade now. However, there are some things that need to be done on windows, like gaming (ATI drivers for linux are still a bit tricky, especially for newer cards) or rs232 (serial port) based apps (old smartphone management, PIC or ATMega Programming, old graphic calculator like TI92 etc.).

So in short, I had once set up a windows system. I wanted to use my files both under linux and windows. The solution for this problem is obvious: I used a NTFS partition I could mount under both OS.

Since I am a security concerned citizen, I use to encrypt my disks. However I refused using Vista and Win7 hadn't come out yet, so I was running XP64. The opensource LUKS tools didn't work on that system (driver issues), so I used Truecrypt.

Some time ago, I abandoned the windows partition after a fault in the MBR, which rendered the Truecrypt bootloader unusable (yes, even with the rescue disc). Since I rarely have to time play games anymore, that's not a major concern. The rest of the tasks can be handled using a virtual machine (in my case Sun's VirtualBox).

So, after about 10 months, I had ended up with a severely fragmented, Truecrypt encrypted NTFS partition which badly needed defragmenting. Linux NTFS-3G drivers have become fairly reliable, but their reliabality decreases as the filesystem fragmentation increases. And the only OS which can defragment a filesystem of this time is windows NT.


Procedure:
The basic underlying problem is that you need to access your raw device from a windows system that by itself lives in an isolated virtual environment. The SMB/CIFS shares are ok to access data, but NAS Filesystems rely on the servers filesystem and cannot be defragmented.

What can be defragmented is a SAN share (iSCSI target), because the protocol by itself forwards the device to the host over a network, not the filesystem.

(1)
So first of, make sure you have a virtual machine running windows somewhere. it actually doesn't need to be virtual, it can be a remote computer on the same subnet, but in my case, it is a virtual machine. As I said, I used VirtualBox.

(2)
Next make sure the Windows box has access to the same network as your linux host. In VirtualBox I used a network interface in NAT mode. I suppose you could also juggle a second loopback device around on the linux host to fulfill the same purpose and keep the windows box of the public network if that is a concern.

(2.5)
If you have encrypted your NTFS partition, you need to decrypt it (not mount it!). Using LUKS, use the cryptsetup command to create a decrypted alias of the partition. If you have used Truecrypt, use it's frontend to mount the partition as you would if you wanted to access data, then unmount the alias from its final mountpoint, thus leaving the unencrypted frontend to the partition intact.

So now you have:
  1. A linux host with a fragmented NTFS partition (which might be encrypted) that is not mounted.
  2. A windows machine (either virtual or remote) with access to the same subnet as your linux host.

(3)
Next you need to setup your unencrypted partition alias as an iSCSI target. I used open-iscsi (http://iscsitarget.sourceforge.net/). In the config file (/etc/ietd.conf), I used the following line:

Code:
Target iqn.2010-01.zw41c:disk1.part5.ntfs
Lun 0 Path=/dev/mapper/truecrypt1,Type=fileio
I will not go into iscsi details here as the topic is a bit elaborate. I hope the code fragment above is sufficient to show the concept. If you use a GUI (Suse provides one for Yast2), this task is simplified.

Start the iSCSI target daemon (in my case ietd). You can confirm that it works by detecting iscsi hosts with your initiator.

Note to those who use a second windows computer (not a virtual machine!) for this task: If you are not the only one on your network, seriously consider using a password an/or ssl tunnel! otherwise your raw hard disk partition is accessible to anyone - There are no access limits! They could simply format the whole thin! Also, reconfigure your firewall to allow access to the iscsi daemon (default tcp port should be 3650).

(4)
Next we need to enable iscsi initiator capabilities in windows. Though Microsoft never made a big hype about it, it is fairly common on servers and available to desktop windows versions. the software can be found here: http://www.microsoft.com/downloads/d...displaylang=en

Download and install the appropriate version on your windows box. After that, you should be able to access your iscsi target using the GUI - which supprisingly is quite straight forward and self explanatory. Just Click on "Discovery"->"Add", enter the data of your linux host, confirm, let it scan, goto "Targets", open "Details" on your linux hosts target, select the drive and confirm.

(5)
If everything went ok, your explorer should now show your ntfs partition as a local drive.

And voila, defragment it! Just rightclick -> defragment or use the software of your choice.

A word of warning (again): make sure your linux host has no access to the ntfs partition when you defragment it! I do not even want to imagine how severely that might confuse the ntfs module! UNMOUNT THE PARTITION!



This procedure worked flawlessly on my host. I would be thankful if someone who tried this could give me some feedback if I was just lucky or if it really this easy.

Hope this helps

Cheers

Last edited by ichrispa; 01-29-2010 at 11:31 PM. Reason: Support
 
Old 01-30-2010, 04:36 AM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
I can see you put a lot of work into this, and maybe it's because I just woke up, but I really don't get it. If you are running Windows in the VM, why couldn't it defrag itself? I imagine performance would be pretty poor, but then it sounds like there is a lot of overhead at work here too.
 
Old 01-30-2010, 05:22 AM   #3
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 277

Original Poster
Rep: Reputation: 32
Well, the virtual machine can defrag itself and all drives that are virtual attached. usually virtual drives reside on disks in the form of common files. unfortunately, the VM can only defragment "virtual drives" bound into the machine. There are many ways to get drives into the machine, however getting them back out of there in one piece is a whole different topic. Virtual machines assume you want to virtualize a ressource, not process it and then return it to its proper location.

The defragmented partionen never becomes a virtual system, it stays physical (as in "resides on my harddisk and should stay there").

There is another approach to this: if you have enough storage capacity, create a new filesystem and relocate all the data from the fragmented disk on that (like an external firewire or usb drive). Then reformat the NTFS and move the data back. It's not defragmenting, but it should linearize the access tables somewhat. Unfortunately, I don't have 1TB of spare disk space.

Another question would be: "Why not abandon the use of NTFS all together if you are using windows?". My personal answer is that at some point, I will be using windows again and I want the data to stay accessible.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can i delete windows files from linux in ntfs partition sting1220 Linux - Newbie 1 12-23-2007 12:08 AM
Can access NTFS partition in Linux but not Windows oudent Linux - General 2 12-10-2005 04:22 PM
ntfs partition no more visible from windows after linux install fif Linux - Newbie 4 05-18-2005 01:12 AM
Can I mount my windows NTFS partition from Linux? jdruin Fedora 14 04-21-2004 11:49 AM
accessing windows ntfs partition from linux rsingh Linux - Newbie 2 06-10-2003 10:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 04:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration