Problem Statement:
Write a program in C++ to make USB Device Bootable by installing required system files.
Write a program in C++ to make USB Device Bootable by installing required system files.
PROGRAM
/* Enter in root before executing the program in terminal*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class bootableusb
{
public:
void formatusb();
void isocopy();
};
void bootableusb::formatusb()
{
char ch;
cout<<"\nPl take backup!\n";
cout<<"\nDo you want still to continue(y/n)=";
cin>>ch;
if(ch=='y')
{
cout<<"\n";
string path,umount,ntfs,eject;
system("blkid");
cout<<"\nEnter path of USB=";
cin>>path;
cout<<"\n";
umount="umount "+path;
eject="eject "+path;
ntfs="mkfs.vfat "+path;
system(umount.c_str());
system(ntfs.c_str());
system(eject.c_str());
cout<<"\nPl re-insert the USB in the port as it has been ejected by program!\n";
}
else
return;
}
void bootableusb::isocopy()
{
char ch;
cout<<"\nPl take backup!\n";
cout<<"\nDo you still want to continue(y/n)=";
cin>>ch;
if(ch=='y')
{
string copyiso,path,eject;
cout<<"\n";
system("blkid");
system("isohybrid ubuntu.iso");
cout<<"\nEnter path of USB=";
cin>>path;
cout<<"\n";
copyiso="dd if=/home/ubuntu.iso of="+path;
eject="eject "+path;
system(copyiso.c_str());
system(eject.c_str());
cout<<"\nYour USB is now bootable and ejected py program!\n";
}
else
return;
}
int main()
{
int ch;
bootableusb busb;
do{
cout<<"\n\t\tCREATE BOOTABLE USB";
cout<<"\n\t1.Format USB in FAT format."<<"\n\t2.Copy system files(making usb bootable)."<<"\n\t3.Exit.";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: busb.formatusb();
break;
case 2: busb.isocopy();
break;
case 3: cout<<"\nExiting...\n";
break;
default: cout<<"\nInvalid Choice!";
}
}while(ch!=3);
return 0;
}
Please Expain::
ReplyDeletestring path,umount,ntfs,eject;
system("blkid");
cout<<"\nEnter path of USB=";
cin>>path;
cout<<"\n";
umount="umount "+path;
eject="eject "+path;
ntfs="mkfs.vfat "+path;
system(umount.c_str());
system(ntfs.c_str());
system(eject.c_str());
Thank you
Hello Amaan,
DeleteSo basically in this part of the code you are trying to format the usb.
The steps being followed here are:
1. You are creating string datatype variables path(to store the path of the usb), umount(to store the system terminal command "umount usbpath" to unmount the filesystem from the file hierarchy), ntfs(to store the system command "mkfs.vfat usbpath" to build a fat filesystem on the usb) and eject(to store the system command "eject usbpath" to eject the usb).
2. "blkid" command is used to print the block device attributes which lists the path of the usb.
3. The path variable is concatenated with the various strings umount,eject and ntfs.
4. The strings containing the command are executed when the function formatusb() is called.
The following are links to a few man pages for the commands used:
http://man7.org/linux/man-pages/man8/umount.8.html
http://man7.org/linux/man-pages/man8/mkfs.8.html
http://man7.org/linux/man-pages/man1/eject.1.html
http://man7.org/linux/man-pages/man8/blkid.8.html
I hope this was helpful!
soo nce great struggle.................usefullll specially for mee
ReplyDelete