Last active
April 19, 2016 02:06
-
-
Save Immortal-/43750e9dabbef8811948d87beef012ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <stdio.h> | |
#include <ctime> | |
using namespace std; | |
void InstallSoftware(); | |
void FormatDrive(); | |
string exec(const char*); | |
bool wait(const unsigned long &Time) | |
{ | |
clock_t Tick = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f); | |
if(Tick < 0) // if clock() fails, it returns -1 | |
return 0; | |
clock_t Now = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f); | |
if(Now < 0) | |
return 0; | |
while( (Now - Tick) < Time ) | |
{ | |
Now = clock_t(float(clock()) / float(CLOCKS_PER_SEC) * 1000.f); | |
if(Now < 0) | |
return 0; | |
} | |
return 1; | |
} | |
int main() | |
{ | |
cout << "Welcome to Chris McCaslin's" <<endl <<"Using USB Storage as Rootfs Installer" << endl ; | |
if(exec("opkg list block-mount").empty()) | |
{ | |
InstallSoftware(); | |
FormatDrive(); | |
}else{ | |
FormatDrive(); | |
} | |
exec("mkdir /mnt/sda1"); | |
exec("mount /dev/sda1 /mnt/sda1"); | |
exec("mount /dev/sda1 /mnt ; tar -C /overlay -cvf - . | tar -C /mnt -xf - ; umount /mnt"); | |
wait(2.5*1000); | |
exec("block detect > /etc/config/fstab"); | |
cout << "DONE: now run vi /etc/config/fstab and change" << endl << "option target '/mnt/sda1'" << endl << "TO: option target '/overlay'" << endl << "And option enabled 0 to option enabled 1"<<endl; | |
return 0; | |
} | |
void InstallSoftware() | |
{ | |
exec("opkg update"); | |
wait(3.5*1000); //wait 3m 30sec | |
exec("opkg install block-mount e2fsprogs kmod-fs-ext4 kmod-usb-storage-extras"); | |
} | |
void FormatDrive() | |
{ | |
exec("mkfs.ext4 -F /dev/sda1"); | |
} | |
string exec(const char* cmd) { | |
FILE *fp; | |
char path[1035]; | |
/* Open the command for reading. */ | |
fp = popen(cmd, "r"); | |
if (fp == NULL) { | |
printf("Failed to run command\n" ); | |
} | |
/* Read the output a line at a time - output it. */ | |
while (fgets(path, sizeof(path)-1, fp) != NULL) { | |
printf("%s", path); | |
} | |
/* close */ | |
pclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment