I've created a swap file with

dd if=/dev/zero of=swap.img bs=1024k count=4k
mkswap swap.img
swapon swap.img

While this works, the swapon command takes ages (approx 10min) to finish. Enabling the swap file in /etc/fstab means the boot process will halt for several minutes.

I've noticed the execution time of the dd command will decrease if I increase the block size (tried with 1024 and 1 before). Does this also have an impact on swapon?

Will swapon handle sparse files? I've tried successfully before, but got a "swap file has holes" after a reboot, so I switched back to a full file.

Some more info:

  • Using Linux Mint KDE 15 RC
  • Swap file is on a mounted NTFS partition

Output of dd was

4096+0 records in
4096+0 records out 
4294967296 bytes (4,3 GB) copied, 73,9629 s, 58,1 MB/s 

Output of mkswap was

Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=108b2e2d-e00a-40d0-8e28-c0b72003e63e

swapon didn't produce any output, but dmesg afterwards showed:

Adding 4194272k swap on /host/personal/swap.img.  Priority:-1 extents:9 across:4335880k

Any idea how to create a swap file (better even twice the size) that mounts in a reasonable time?

  • Why is your swap on an NTFS partition? Why not just use a swap partition? – beatgammit Jul 13 '13 at 23:09
  • Complicated setup. Syslinux bootloader starts kernel from fat usb-key, loads root filesystem from a ntfs hdd. So there is no device I could create partitions on, hence the swapfile also from the ntfs hdd. – Chaos_99 Jul 13 '13 at 23:20
  • Are you sure about that dd command? That's a huge file, not the 4GB you seem to be expecting. – terdon Jul 14 '13 at 2:49
  • @terdon Sorry, that was a typo. I've corrected it now. As you can see by the copy&pasted output of dd, it actually WAS 4GB. – Chaos_99 Jul 14 '13 at 8:40

This is not so much an answer as a series of hints. First of all, this page mentions that running the dd command on an NTFS partition can harm the partition. No idea why and no idea if it is true but I thought it worth mentioning:

if /mnt/home is an NTFS drive, do not use dd (writing to your NTFS drive can corrupt the file system)

Whatever the details, creating a swap file on an NTFS partition does indeed seem to be problematic:

 $  dd if=/dev/zero of=swap.img bs=1024k count=4k
 4096+0 records in 
 4096+0 records out
 4294967296 bytes (4.3 GB) copied, 122.731 s, 35.0 MB/s
 $ mkswap swap.img 
 $ sudo swapon swap.img 
  swapon: /winblows/swap.img: skipping - it appears to have holes.

The exact same process run on an ext4 partition gives no error and appears to work perfectly, indicating that the problem is the NTFS drive. In addition, moving the file that worked from the ext4 partition to the NTFS one and running swapon on it gives the same "file has holes" error. So it really is not the file but the partition.

This may be a problem caused by fragmentation of my drive but a user on this forum posted that he gets the same problem on a newly formatted drive that only contains 2 files, so it looks like fragmentation is not an issue.

On the one hand, the ntfs-3G (that is the module used by the kernel to mount and access NTFS partitions) FAQ states:

Can I use swap file safely, deadlock free on NTFS?

Yes, this is possible if it’s properly setup. Below is the example how to create, initialize and turn on a 2 GB swap file safely, deadlock free:

dd if=/dev/zero of=swapfile bs=1M count=2000 
mkswap swapfile
swapon swapfile

You must turn off the swap file with ‘swapoff’ before you would be able to unmount NTFS. Please also note that other solutions, for example using swap on a loop device, are not safe and can cause deadlock!

So they seem to think it should work. On the other hand, when I run the commands above, I get the same "file has holes" error.

So, while I can't find any conclusive information, I can't make it work on NTFS. I have found various posts that suggest using an NTFS partition for a swapfile is a bad idea or that describe various problems users have run into when attempting same. In conclusion, if you have any way of avoiding NTFS it is probably a good idea.

  • Thanks a lot for the research. My only other option would be to use a swap file inside the ext4 image supplying the root file system which is mounted from the NTFS partition. But I found other reports online that also discourage the use of loopback davices for swap files. (While it might be possibly that that just meant mounting loopback devices as swap PARTITIONS.) – Chaos_99 Jul 15 '13 at 11:11
  • yep still same behavior :( – George Pligoropoulos Apr 4 '17 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.