Failed to write and read file on Nanos

Hello, I hope you’re well. I’ve been working with Nanos for a few days, but I’ve come up against a problem.
I have a program that should write logs to disk, but it turns out that it’s not possible to write to disk with Nanos. I’ve followed these instructions, but it doesn’t keep failing to write, it creates the file, but it’s always empty. (https://docs.ops.city/ops/volumes)
How can I solve it? How do I write to a file?

Another small question, is it possible to interact with Nanos with a command line?

Thank you very much

We would probably need to see some code but more than likely you don’t have a large enough volume allocated. The base volume size is, by default, set to slightly more than what is initially put onto the disk so you’ll want to bump it up by however much is necessary.

As for your other question - what do you mean interact with nanos? OPS has several tools to inspect a running instance or image.

Good afternoon. I’ve tried more than once with discs much larger than what was supposed to be, because what would be written would only take up KB, but it still didn’t work. Thank you.

Can you post some sample code? What language are you using?

Yes, of course. I try to create the file, if it already exists, create it with the same name, but with a higher index. Then I close it, open it again and check the contents. It always appears empty.

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file;
    char filename[50];
    char ch;
    int file_index = 0;

    // Try to open the file
    sprintf(filename, "ARQUIVO.txt");
    
    // If the file already exists, increment the index and try again
    while ((file = fopen(filename, "r")) != NULL) {
        fclose(file);
        file_index++;
        sprintf(filename, "ARQUIVO_%d.txt", file_index);
    }

    file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error.\n");
        return 1;
    }

    // Write some text to the file
    fprintf(file, "Hello\n");
    fclose(file);

    printf("The text has been written to %s.\n", filename);

    // Open and read the file
    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error.\n");
        return 1;
    }

    printf("Contents of %s:\n", filename);
    fclose(file);

    return 0;
}

Ok - 2 things:

  1. I would put an explicit flush call after your write but before your close:
    fprintf(file, "Hello\n");

    fflush(file);

    fclose(file);

This ensures that this happens before the program exits - if this was a long running service you proably wouldn’t need to care about that but in this example the program exits really fast so there is a chance that it might not flush immediately. The difference between linux and nanos here is that when you run this program on linux the system is not shutting down - here it is.

  1. I would pad the filesystem above what is produed. On my system I had a 16mb binary so I gave it 20mb.
eyberg@box:~/ff$ cat config.json
{
  "BaseVolumeSz": "20m"
}

Without that I could tell it wasn’t actually writing cause if you turn on ‘–trace’ you’ll see something like this:

    2 file_write_complete: f 0xffffc000000978f8, sg, 0xffffc0000062efc0, completion fdesc_io_complete, status (fsstatus:1 result:unable to create extent)

Anytime you see “unable to create extent” that means you aren’t writing to the filesystem.

After you do both of those things you should be able to verify that the file was written to the image:

eyberg@box:~/ff$ ops image cat main ARQUIVO.txt
Hello

If you use fprintf() to write to the file, you should check the return value from fclose() to verify whether the write was successful: if there is not enough space in the disk, fclose() will return -1 and set errno to ENOSPC. (Likewise, if you call fflush(), you should check the return value from that function.)

Perfect! Thank you so much! It works now ahah

1 Like