Chinaunix首页 | 论坛 | 博客
  • 博客访问: 786791
  • 博文数量: 156
  • 博客积分: 5320
  • 博客等级: 大校
  • 技术积分: 1605
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-09 12:24
文章分类

全部博文(156)

文章存档

2010年(20)

2009年(46)

2008年(21)

2007年(21)

2006年(33)

2005年(10)

2004年(5)

分类: LINUX

2006-05-27 14:08:25

How to write to NTFS in Linux-2.6

Here are the steps to be done in order to transfer files from a Linux filesystem (JFS/XFS/Reiserfs/EXT3/whatever you prefer) to a Windows filesystem (NTFS).

Table of contents

In short

  • Create a dummy file in Windows (big enough to have room "inside" for the files you wish to import)
  • In Linux, copy files into that file (creation of new files and/or resizing existing files is not supported)

More detailed instructions here:

Create an image in Windows

The in Linux-2.6 is very limited (because Microsoft doesn't disclose the technical bits about how NTFS works). Specifically, it does not support these operations:
  • Creating/deleting files
  • Growing/shrinking files
It only allows writing over existing files.

For this reason, you have to use Windows to create an image you'll write over.

Say, you're going to copy 5 gigabytes of files to your NTFS partition. You must create a 5 gigabyte file to your NTFS partition, using your Widows. In Linux (or any unix system in general), you would do it with a

  dd if=/dev/zero bs=1m count=5000 of=file
command, but in Windows, it's not that simple.

You can create it in any way you want ― the contents of the file do not matter. If you can't think of anything, I recommend using to compress a couple of anime episodes together.

When you have created the big file, rename it as something.tar.gz. Ensure the filename ends with .tar.gz (dot tee ay ar dot jee zed).

The file may be bigger than the source files together, but it must not be smaller.

Reboot to Linux

For some reason, the has made it sure that it's approximately impossible to write a filesystem driver for Windows.
They don't want to support other systems. They want everyone to use Windows, Windows, Windows.

If your subject is an EXT2 / EXT3 filesystem, you can for a shareware Windows program that lets you read those filesystems in Windows. For the other ones mentioned, there's no such choice.

So, ironically, in order to mount a JFS/XFS/Reiserfs/whatever filesystem to copy files to Windows, you must boot into Linux.

In order to boot into Linux, you need to have Linux installed somewhere. I won't cover Linux installation & starting in this document ― I'll just assume you have it somewhere. If you're lazy and lucky, a non-permanent Linux installation such as a "livecd" is probably enough.

Testing NTFS write support

Prerequirement for understanding the rest of this document: ability to use a terminal (xterm, aterm, gnome-terminal, console, whatever you prefer).

The tricky part is that your NTFS driver in Linux must have been compiled with WRITE SUPPORT -- otherwise it won't write anything.

Try mounting your NTFS partition (remember to be root!):

  mkdir /mnt/d
  mount /dev/hda5 -t ntfs /mnt/d -o rw
Replace /dev/hda5 with your partition location. If the mount command fails and you didn't type anything wrong, you don't have NTFS support at all. Do a modprobe ntfs and try again. If it still fails, you need to compile the NTFS driver. See below.

If the mount command worked without problem, next do this:

  grep /dev/hda5 /proc/mounts
If the line you get contains "rw,", you can skip the following chapter and read about how to write.
If the line contains "ro,", mount cheated you; there is no NTFS write support in your kernel. You need to rebuild the driver.
If the line doesn't contain either of those, you probably did something wrong or your /proc filesystem is not mounted or it is something that I can't diagnose within one sentence.

Compiling the NTFS driver

You need to get the kernel source and configure it. I don't detail the instructions on how to do that (because it's impossible for me to cover all different distributions and preferences).

Notes:

  • If you use NTFS as a module, you need to handle the same version of kernel source as is the kernel you're currently running (type uname -r to see it).
  • If you use a "livecd", you might experience great obstacles in attempting to affect its configuration.

When you're in the menuconfig (make menuconfig),

  • go to File systems -> DOS/FAT/NT Filesystems
  • ensure there's a M or a * in NTFS filesystem support
  • ensure there's a * in the NTFS write support
Then exit, exit, exit and answer Yes to the "do you want to save" question.
Recompile your kernel (you should know how) and install the modules.

If you compiled NTFS as a non-module, you need to reboot now.

After done, unload the old NTFS driver:

  rmmod ntfs
If it complains it's busy, unmount all NTFS mounts (umount -a -t ntfs) and try again.
Then load the new NTFS driver:
  modprobe ntfs
Now go back to the mounting test.

Writing

At this point:
  • you have verified that your kernel has NTFS write support
  • you have mounted the target filesystem
  • the target filesystem contains a file to copy over
Mount the source filesystem (or have it already mounted), and proceed:

For the sake of clarity, we'll use now these assumptions. Adjust the instructions in your mind if these are not exactly your cases:

  • The files we're copying are: /mnt/b1/anime/FMA/*.avi
  • The 5 GB file in NTFS filesystem we're writing into is: /mnt/d/Animet/FMA.tar.gz

Use this command:

  cd /mnt/b1/anime/FMA
  tar cvfz - *.avi | dd conv=notrunc of=/mnt/d/Animet/FMA.tar.gz
This should work. Did you get error messages? Sorry, I can't help you there. You'll have to resolve it yourself.

So what happened?

  • We created a .tar.gz archive and passed it to the standard dd program which wrote it into the NTFS file.

Note that using dd, and specifically its conv=notrunc option is important here. The normal '<' and '>' ways of redirecting the stream don't work, because NTFS writes rely on the file size not changed.

Reboot to Windows

Now reboot to Windows.

Verify the filesystem

As the first thing, run a filesystem check on the NTFS filesystem. Ensure it's not broken. (It shouldn't be, but it's better to be certain than sorry.)

Open the archive

After you have verified the filesystem, go and open the .tar.gz file (the one you created in the beginning of these instructions) with your favourite archiver program (such as WinRAR).
After you have decompressed it, you may safely delete it.

Other methods

Captive NTFS

There supposedly exists a NTFS driver for Linux which uses the Windows NTFS driver directly. I tried it once but I couldn't figure out how to get it working in Linux-2.6.
It should be able to use an NTFS volume without restrictions.

Running Linux under Windows

As crazy as this sounds, it's possible using the .

I won't cover colinux installation here, but I'll cover the steps to transfer files using this method:

  1. Start colinux and mount your Linux filesystem (readonly if you wish).
    • If colinux doesn't allow mounting partitions, you can first create a partition image of the filesystem and mount the image under colinux.
  2. Start a web server under colinux and create a website that links to those files.
  3. Download the files using your Windows. It will be very fast because it all goes through a virtual network interface which has no speed limits.
This method requires no NTFS support in Linux. I have succesfully ran colinux, but I haven't tried mounting production filesystems with it.


Page created by .
E-mail address:

Thanks to Paul Jackson for the conv=notrunc information.


Last edited: 2005-11-08 15:59:08
阅读(1478) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~