I am using a C program open(), close(), read(), write()
technique to copy a large file to RAW disk. The disk size is 20 GB and the file size is 17 GB but, every time after around writing 945 MB write()
throws No space left on device
error.
I have run fdisk -l /dev/sdb
and returns 20.5 GB and du /dev/sdb
says 945126569
Then I tried, cat mylargefile > /dev/sdb
it too throws same No space left on device
error and then I do cat /dev/sdb > /tmp/sdb.img
it completes normally. Then I do ls -ld /tmp/sdb.img
it responds 945126569
I can use the same disk to create ext4
file system and format it without any issues, so disk error is improbable. (I guess ...)
I am using Ubuntu 16.04 LTS amd64 OS with latest GCC to build my program.
Can anyone suggest where am I going wrong or what needs to be done to avoid this?
du /dev/sdb
should say 0 if /dev/sdb
is a block device. Try also blockdev --report /dev/sdb
.
What happened is that in the begining you didn't have a device file named /dev/sdb
at all, and you created a regular file named /dev/sdb
,
copied 945 MiB into it. This filled the partition on which /dev/
is located, and thus you get the error. fdisk
just reads the partition table that is contained in the first 945 MiB and thinks it sees a hard disk of 20 GiB.
When you do cat mylargefile > /dev/sdb
, the file /dev/sdb
is first truncated to size 0, so there is now 945 MiB free space again that cat
will proceed to fill.
To avoid this: make sure that you open a device by its correct name. In C open the device without O_CREAT
.