Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Created February 23, 2016 18:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gcmurphy/c4c5222075d8e501d7d1 to your computer and use it in GitHub Desktop.
Save gcmurphy/c4c5222075d8e501d7d1 to your computer and use it in GitHub Desktop.
Create tmpfs mountpoint in c.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mount.h>
char *
ramdisk(const char *ns, const char *sz)
{
int rc;
char *mountpoint = NULL, *options = NULL;
char path[PATH_MAX];
memset(path, 0, sizeof(path));
const char *tmpdir = getenv("TMPDIR");
if (! tmpdir){
tmpdir = "/tmp/";
}
snprintf(path, sizeof(path)-1, "%s%s_XXXXXX", tmpdir, ns);
mountpoint = mkdtemp(path);
if (!mountpoint){
return NULL;
}
asprintf(&options, "size=%s,uid=0,gid=0,mode=700", sz);
rc = mount("tmpfs", mountpoint, "tmpfs", 0, options);
free(options);
if (rc != 0){
perror("tmpfs creation failed");
rmdir(mountpoint);
return NULL;
}
return strdup(mountpoint);
}
#ifdef TESTING
int
main()
{
char *tmpfs = ramdisk("ramdisk", "1M");
if (tmpfs != NULL){
printf("created tmpfs here: %s\n", tmpfs);
free(tmpfs);
}
return 0;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment