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?
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.
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;
}
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.
I would pad the filesystem above what is produed. On my system I had a 16mb binary so I gave it 20mb.
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.)