From 24b8b753ccf72bdd0123f449ba67a17a58399ad7 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 3 Jul 2013 19:33:08 +0800 Subject: [PATCH] Usermode/automounter - Added automounter --- Makefile | 2 +- .../Applications/automounter_src/Makefile | 12 +++ Usermode/Applications/automounter_src/main.c | 92 +++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 Usermode/Applications/automounter_src/Makefile create mode 100644 Usermode/Applications/automounter_src/main.c diff --git a/Makefile b/Makefile index f0f85df1..4ff95bd0 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ USRLIBS := crt0.o acess.ld ld-acess.so libc.so libposix.so USRLIBS += libreadline.so libnet.so liburi.so libpsocket.so USRLIBS += libimage_sif.so libunicode.so -USRAPPS := init login CLIShell cat ls mount +USRAPPS := init login CLIShell cat ls mount automounter USRAPPS += bomb lspci USRAPPS += ip dhcpclient ping telnet irc wget telnetd USRAPPS += axwin3 gui_ate gui_shell diff --git a/Usermode/Applications/automounter_src/Makefile b/Usermode/Applications/automounter_src/Makefile new file mode 100644 index 00000000..96f0b154 --- /dev/null +++ b/Usermode/Applications/automounter_src/Makefile @@ -0,0 +1,12 @@ +# Project: Automounter + +-include ../Makefile.cfg + +CPPFLAGS += +LDFLAGS += + +DIR = SBin +BIN = automount +OBJ = main.o + +-include ../Makefile.tpl diff --git a/Usermode/Applications/automounter_src/main.c b/Usermode/Applications/automounter_src/main.c new file mode 100644 index 00000000..c30924ab --- /dev/null +++ b/Usermode/Applications/automounter_src/main.c @@ -0,0 +1,92 @@ +/* + * Acess2 Automounter + * - By John Hodge (thePowersGang) + * + * main.c + * - Program core + */ +#include +#include +#include +#include +#include +#include + +#define LVMBASE "/Devices/LVM" +#define AMOUNTROOT "/Mount/auto" + +void TryMount(const char *Volume, const char *Part); + +// === CODE === +int main(int argc, char *argv[]) +{ + mkdir(AMOUNTROOT, 0755); + // TODO: Handle variants like 'by-label' and 'by-id' + + // Iterate '/Devices/LVM/*/*' + DIR *dp = opendir(LVMBASE); + assert(dp); + struct dirent *ent; + while( (ent = readdir(dp)) ) + { + if( ent->d_name[0] == '.' ) + continue ; + if( strncmp(ent->d_name, "by-", 3) == 0 ) + continue ; + + char path[sizeof(LVMBASE)+1+strlen(ent->d_name)+1]; + sprintf(path, LVMBASE"/%s", ent->d_name); + DIR *ddp = opendir(path); + assert(ddp); + + // Attempt to mount each sub-volume + struct dirent *dent; + int nParts = 0; + while( (dent = readdir(ddp)) ) + { + if(dent->d_name[0] == '.') + continue ; + if(strcmp(dent->d_name, "ROOT") == 0) + continue ; + + TryMount(ent->d_name, dent->d_name); + + nParts ++; + } + closedir(ddp); + + // If only ROOT exists, attempt to mount that as '/Mount/auto/devname' + if( nParts == 0 ) + { + TryMount(ent->d_name, NULL); + } + } + + closedir(dp); + + return 0; +} + +void TryMount(const char *Volume, const char *Part) +{ + // Create device path + size_t devpath_len = sizeof(LVMBASE)+1+strlen(Volume)+1 + (Part ? strlen(Part)+1 : 5); + char devpath[devpath_len]; + sprintf(devpath, LVMBASE"/%s/%s", Volume, (Part ? Part : "ROOT")); + + // Create mount path + size_t mntpath_len = sizeof(AMOUNTROOT)+1+strlen(Volume)+1 + (Part ? strlen(Part)+1 : 0); + char mntpath[mntpath_len]; + sprintf(mntpath, AMOUNTROOT"/%s", Volume); + if( Part ) { + strcat(mntpath, "-"); + strcat(mntpath, Part); + } + mkdir(mntpath, 0755); + + // Attempt mount + if( _SysMount(devpath, mntpath, NULL, "") ) { + fprintf(stderr, "Unable to mount '%s'\n", devpath); + } +} + -- 2.20.1