9

How do I read/write a block device? I heard I read/write like a normal file so I setup a loop device by doing

sudo losetup /dev/loop4 ~/file

Then I ran the app on the file then the loop device

sudo ./a.out file
sudo ./a.out /dev/loop4

The file executed perfectly. The loop device reads 0 bytes. In both cases I got FP==3 and off==0. The file correctly gets the string length and prints the string while the loop gets me 0 and prints nothing

How do I read/write to a block device?

#include <fcntl.h>
#include <cstdio>
#include <unistd.h>

int main(int argc, char *argv[]) {
    char str[1000];

    if(argc<2){
        printf("Error args\n");
        return 0;
    }

    int fp = open(argv[1], O_RDONLY);
    printf("FP=%d\n", fp);
    if(fp<=0) {
        perror("Error opening file");
        return(-1);
    }
    off_t off = lseek(fp, 0, SEEK_SET);
    ssize_t len = read(fp, str, sizeof str);
    str[len]=0;
    printf("%d, %d=%s\n", len, static_cast<int>(off), str);

    close(fp);
}
  • how big is your `~/file` ? doing `losetup` for small file may be useless or invisible for system tools. – ymonad Oct 24 '14 at 03:29
  • @ymonad Its a sentence, about 40bytes. I tried a new file by using ` dd if=/dev/zero of=~/file2 count=10K` then editing it with vi. It appears to work. Does it only ignore it because I have a small file or does it ignore blocks when its <4K? If I had a 8k+20bytes is the last 20bytes ignored? Thanks for the comment –  Oct 24 '14 at 04:04
  • 1
    In my environment `losetup` give me a warning for large file: _"Warning: file does not fit into a 512-byte sector; the end of the file will be ignored"_ , and for small file: _"Warning: file is smaller than 512 bytes; the loop device may be useless or invisible for system tools."_ . So the latter may be correct. – ymonad Oct 24 '14 at 04:13
  • 1
    This warning seems to be added from `util-linux` ver 2.22. https://github.com/karelzak/util-linux/commit/3554545636f639fbd65d92cc7643e89f3c0ff7a5 – ymonad Oct 24 '14 at 04:41

2 Answers2

6

The losetup seems to map file in 512-byte sectors. If file size is not multiples of 512, then the rest will be truncated.

When mapping a file to /dev/loopX with losetup, for fiile which is smaller than 512 bytes it gives us following warning:

Warning: file is smaller than 512 bytes;
 the loop device may be useless or invisible for system tools.

For file which the size cannot be divided by 512:

Warning: file does not fit into a 512-byte sector;
 the end of the file will be ignored

This warning was added since util-linux ver 2.22 in this commit

ymonad
  • 11,710
  • 1
  • 38
  • 49
  • 4
    This is not an answer by any means! How is it getting up-voted? What is the ACTUAL FIX? – Kevin Nov 04 '18 at 05:21
  • 2
    @Kevin The actual fix is using file which has size multiple of 512. Maybe the title of the question does not fit your need. – ymonad Nov 04 '18 at 12:12
  • 1
    What would the syntax be to resolve this? That would be an actual answer) – Kevin Nov 05 '18 at 16:17
0

You can not put zeros or random values on the file to get 512 byte alignment. Use the first few byte to store the file size, followed by the file content. Now you know where the file content is ending. You put random data to achieve the 512 alignment.

e.g. File structure:

[File Size] [Data][<padding to get 512 alignment>]

vasha
  • 96
  • 2