Newest Linux project, what do you think?
I DO NOT RECOMMEND TRYING THIS IF YOUR KNOWLEDGE OF THE THE LINUX SYSTEM IS ANYTHING LESS THAN INTERMEDIATE!
UPDATE: I updated the bash script and fixed it. I am now checking what the current runlevel rule is and using that.
I wrote a bash script on my Arch Linux box to put a computer in a bootloop. I have tested it on my bsd init style Archbox and it worked like a charm! Its small and trivial but it was fun and I wanted to share. Please do not run it without knowing how to chroot and fix your run levels.
To ru W/O client/server
UPDATED BASH SCRIPT:
## This function parses out the two run levels and favors the uncommented RL.
## Your current runlevel.Then pushes the result to a tmp file.
bl=$(cat /etc/inittab |
awk '/id/ {print $1}' |
awk '!/#/ {print}');
echo -e "$bl" > tmp.txt;
echo -e "\nCurrent runlevel rule"; echo "=>" & cat tmp.txt
## Init's restart run level. What we are using for a bootloop.
runlvlid=id:6:initdefault:
## For testing purposes. To autoreset the run level.
runlvliddef=id:5:initdefault:
## Used to determine which if statement we will use.
## Parses out the runlevel number only.
runlvl=$(cat tmp.txt | awk -F":" '{print $2}')
echo -e "TOP_runlvl_no is\n=> $runlvl"
if [ "$runlvl" == '1' ]; then
echo -e "Current runlvl is\n=> $runlvlid"
rm -r /etc/inittab.test2
touch /etc/inittab.test2
cp /etc/inittab.test /etc/inittab.test2
arg=$runlvlid
awk -v var=$arg '{ gsub(/id:1:initdefault:/,var,$0); print }' /etc/inittab.test2 > /etc/inittab.test
sudo shutdown -r now
fi
if [ "$runlvl" == '2' ]; then
echo -e "Current runlvl is\n=> $runlvlid"
rm -r /etc/inittab.test2
touch /etc/inittab.test2
cp /etc/inittab.test /etc/inittab.test2
arg=$runlvlid
awk -v var=$arg '{ gsub(/id:2:initdefault:/,var,$0); print }' /etc/inittab.test2 > /etc/inittab.test
sudo shutdown -r now
fi
if [ "$runlvl" == '3' ]; then
echo -e "Current runlvl is\n=> $runlvlid"
rm -r /etc/inittab.tmp
touch /etc/inittab.tmp
cp /etc/inittab /etc/inittab.tmp
arg=$runlvlid
awk -v var=$arg '{ gsub(/id:3:initdefault:/,var,$0); print }' /etc/inittab.tmp > /etc/inittab
sudo shutdown -r now
fi
if [ "$runlvl" == '4' ]; then
echo -e "Current runlvl is\n=> $runlvlid"
rm -r /etc/inittab.test2
touch /etc/inittab.test2
cp /etc/inittab.test /etc/inittab.test2
arg=$runlvlid
awk -v var=$arg '{ gsub(/id:4:initdefault:/,var,$0); print }' /etc/inittab.test2 > /etc/inittab.test
sudo shutdown -r now
fi
if [ "$runlvl" == '5' ]; then
echo -e "Runlvl rule reset to\n=> $runlvlid"
rm -r /etc/inittab.tmp
touch /etc/inittab.tmp
cp /etc/inittab /etc/inittab.tmp
arg=$runlvlid
awk -v var=$arg '{ gsub(/id:5:initdefault:/,var,$0); print }' /etc/inittab.tmp > /etc/inittab
echo -e "\nSetting bootloop."
sleep 2
sudo shutdown -r now
fi
#For testing purposes only.
if [ "$runlvl" == '6' ]; then
echo -e "Current runlvl is\n=> $runlvlid"
rm -r /etc/inittab.tmp
touch /etc/inittab.tmp
cp /etc/inittab /etc/inittab.tmp
arg=$runlvliddef
awk -v var=$arg '{ gsub(/id:6:initdefault:/,var,$0); print }' /etc/inittab.tmp > /etc/inittab
echo "*** Defaults set. ***"
sudo shutdown -r now
fi
There are obviously a "TON" of loop holes that I have to fix!
EDIT: You must comment out the 5th runlevel before running it the script. At least until I can fix up the script. If it doesn't work it is because run level 5 is uncommented. RE-coment it back out.
HOW TO CHROOT into your Linux box:
create a new directory to mount your hard drive on.
use
to find your root device. I use an external HDD so external HDD's use sdb and sba. USB's use sdc. Root partitions are always sd*3 on an ext4 filesystem. Boot partition are always sd*1 and use ext2 FS. Your home directoy also uses ext4, unless you specify otherise.
So your layout should be /dev/sd*1 /boot ext2 //your boot partition
/dev/sd*2 swap swap //swap space
/dev/sd*3 / ext4 //root
/dev/sd*4 /home ext4
*NOTE: Swap is windows virtual memory counterpart.
Now mount your root device with
Now cd into your newly mounted root device with
Next you'll need to mount proc, sysfs, and dev so you can mount your boot partition and chroot
use
mount -t sysfs sys sys/
mount -o bind /dev dev/
Mount your boot partition with
CHROOT
poll the dhcp server for a wired connection with
Now you need to restore your run levels. The script set your inittab to run level 6. Which is reboot. You want to eplace that 6 with a 5. Run level 5 is for the desktop environment.
Your innittab should be located in /etc/(For bsd init style framework). I Not sure about sysvinit and there inittab location. but i am almost certain it is in the same spot. All should be well if you did this right and you can now reboot.
I WILL SAY AGAIN -->
I DO NOT RECOMMEND TRYING THIS IF YOUR KNOWLEDGE OF THE THE LINUX SYSTEM IS ANYTHING LESS THAN INTERMEDIATE!
EDIT2: I added a server so you can do it remotely lol
SERVER CODE:
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, listener, binder;
char buffer[4096];
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
socklen_t clilen;
int s_opt, yes = 1;
if(argc < 3) {
printf("USAGE: %s + IP Address + Port No.\n", argv[0]);
exit(EXIT_FAILURE);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
printf("SOCKET(-1) error --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
close(sockfd);
}
if(sockfd) {
do {
{
printf("Waiting for connection...\n");
}
} while(!accept);
}
s_opt = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if(s_opt < 0) {
printf("SETSOCKOPT(-1) error --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
close(sockfd);
}
server = gethostbyname(argv[1]);
if(server == NULL) {
printf("GETHOSTBYNAME(NULL) error --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
close(sockfd);
}
memset(&serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[2]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
binder = bind(sockfd, (const struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(binder < 0) {
printf("BIND(-1) error --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
close(sockfd);
}
listener = listen(sockfd, 20);
if(listener < 0) {
printf("LISTEN(-1) error --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
close(sockfd);
}
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if(newsockfd < 0) {
printf("ACCEPT(-1) error --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
close(sockfd);
}
else if(newsockfd) {
printf("=> Made a connection.\n");
}
for(;;) {
//shutdown(sock, SHUT_WR);
ssize_t bytes_read = read(newsockfd, buffer, sizeof(buffer));
if(bytes_read < 0) {
printf("READ(-1) --> %s.\n", strerror(errno));
exit(EXIT_FAILURE);
}
else if (bytes_read)
while(bytes_read > 0) {
int eight = strlen(buffer);
while(eight >= 8) {
if((memcmp(buffer, "bootloop", 8)) == 0) {
execl("/bin/bash", "bash", "/home/amboxer21/Documents/bootloop-real.sh", (char *)NULL);
}
else {
printf("Unknkown command.\n");
break;
}
}
}
}
close(sockfd);
close(newsockfd);
return 0;
}
compile with
What do you think?
In any case, you're making a stack of assumptions about init and system architecture.
For example; while parts of this may be true for your system they're certainly not a standard:
_________________
Giraffe: a ruminant with a view.
Well for one I love using AWK! It's such an awesome language! Parsing strings is so much fun!! I think that's why I love network coding so much! I have to manipulate strings. And the task is very tedious! If the tinies thing is wrong or one rule is not properly met, then I wont work. That's what makes it such a challenge!
There's something about AWK! It's so powerful but so freaking easy to use. If you can think it up, you can do it! It's a sys admins heaven lol
I do have productive projects I am working on though. Some GUI's, servers, etc. this was just because I was learning about unit and was itching to use AWK!
For an ext4 type FS, on an external device it is true.
Ah well if it's an excuse to play with awk.
Rather too potentially destructive for my tastes, though - I'd be more included to play about with awk using my own user privileges: much safer.
On your device it's true...
_________________
Giraffe: a ruminant with a view.
Rather too potentially destructive for my tastes, though - I'd be more included to play about with awk using my own user privileges: much safer.
On your device it's true...
I am not going to argue with you. You are a well established member and if your telling me I am wrong then I am probably wrong. I say this because I see you as the type of person that they wont state something unless they know it to be true. So enlighten me please
Yeah, its for awk purposes! I love awk! Some people say perl is better but I think its way to ugly! I love the beauty of awk!
I used awk to parse out the open ports, IP addresses and process ID's of the netstat -antp command. That was way to easy but also fun! I loved the project so much I made a GUI notification system for it. With kill buttons and stuff. Check it out here --> http://www.gtkforums.com/viewtopic.php?f=16&t=177952 the awk is very bulky. It was before I knew how to group numerous awk commands.
The great thing about that project was that I got to use all of my favorite programming tools all in one; GUI programming, awk, network programming, and C.
Device name sequences signify nothing more than their order of appearance and that order is usually determined by the hardware. So sda, sdb, sdc, sdd are just four block devices with (at this level of naming) unknown partitions, filesystems or locations.
There is nothing about "sdc" which means it is a USB device, external or internal. It is simply the third block device available to the system. As it happens sdc on the system I'm using at the moment is an internal SATA device with two Linux partitions, one of which is bootable. (the other one would be too, if I would only get around to installing a system onto it...)
Because I have four internal disk drives sd[a-d] available, when I plug in a USB device it appears as sde - with another plugged in afterwards appearing as sdf and so on.
But that's just how my system works out.
On your system, if you already have devices sda and sdb in use, then plugging in a USB drive would normally mean it appears as sdc.
And that's just how it works out on your system.
Remember also that a USB stick (as a simple example of an external device) is normally supplied preformatted with a FAT filesystem but it can be partitioned to use whatever filesystems are required, so even on your system sdc1 could be fat32, sdc2 could be ReiserFS, sdc3 could be ext4 and so on - so actually, it's much more useful than saying an external drive is ext4.
Root partitions can be mounted from any device.
I'm currently booted from sdb9 but it could also be any of: sdb6, sdb7, sdb9, sdc1, sdc5 (if I installed something!) or sdd2, depending on what system I require. sdd2 and sdb7 are ReiserFS, sdc1 and sdb9 are ext4, sdb6 is ext3.
But that's half the fun of Linux: the toolbox is absolutely stuffed with all sorts of useful things and there is inevitably overlap so the answer to "What's the best tool?" is often "Whichever one you're most familiar with that gets the job done".
_________________
Giraffe: a ruminant with a view.
If I use a Linux distro that has a bsd init style framework(Arch Linux), I am pretty sure I understand the trivial stuff that you are not giving me credit for. I don't want to come off a jerk but I hate when I "feel" like I am being treaded like a dummy. Not saying that is/ was your intentions but that is the way you have made me feel!
I don't think you understood anything that came out of my moth lol
I never said that. You have to specify the FS type unless you are using a user friendly distro like ubuntu which specifies the FS type for you! There is an option of course to do this manually instead of the default automation.
I built my box from ground up. Even the desktop. I began with a tty console and slowly built up a stand alone openbox WM. I CHOSE TO USE AN EXT4 FS. With an ext2 type boot partition of course. Swap space included.
This i did not know. Thank you!
There is nothing about "sdc" which means it is a USB device, external or internal. It is simply the third block device available to the system. As it happens sdc on the system I'm using at the moment is an internal SATA device with two Linux partitions, one of which is bootable. (the other one would be too, if I would only get around to installing a system onto it...)
If I use a Linux distro that has a bsd init style framework(Arch Linux), I am pretty sure I understand the trivial stuff that you are not giving me credit for. I don't want to come off a jerk but I hate when I "feel" like I am being treaded like a dummy. Not saying that is/ was your intentions but that is the way you have made me feel!
I don't think you understood anything that came out of my moth lol
There is nothing personal in anything I've said and it puzzles me how you appear to think that there is.
I never said that. You have to specify the FS type unless you are using a user friendly distro like ubuntu which specifies the FS type for you! There is an option of course to do this manually instead of the default automation.
I built my box from ground up. Even the desktop. I began with a tty console and slowly built up a stand alone openbox WM. I CHOSE TO USE AN EXT4 FS. With an ext2 type boot partition of course. Swap space included.
I mentioned some things only because you made this statement as part of some instructions on how a user can find their root device:
I mention these again now for absolutely no other reason than they are incorrect and that your instructions are rendered meaningless by their inclusion:
so external HDD's use sdb and sba. --> on your machine.
USB's use sdc. --> on your machine.
Root partitions are always sd*3 on an ext4 filesystem --> no, they are not.
Boot partition are always sd*1 --> no, they are not.
and use ext2 FS --> not necessarily: grub has been able to load modules to support other FS's for some time now.
(this last item - that boot partitions are always sd*1 and use ext2 - is part of what I was referring to when I said "There is nothing about a device's name or location which determines what type of filesystem it will have.")
_________________
Giraffe: a ruminant with a view.
Sorry, but that is completely untrue - not least because ext4 is the filesystem that lives in a partition: ext4 doesn't define anything about how many partitions or what their purpose is supposed to be.
You're saying that a filesystem has partitions - it's the other way around...
What you describe is a typical default setup for any Linux system - the filesystems used are irrelevant to that (well, except swap), and the partition ordering is also irrelevant.
How do you explain I'm currently booted from sdb9, an ext4 partition?
_________________
Giraffe: a ruminant with a view.
lol I just realized that
How do you explain I'm currently booted from sdb9, an ext4 partition?
Yeah, thats how Linux is set up. If you are booted from sdb9 on an ext4, you manually mounted that point or you are not using Linux.
NOTE: Everything i have said, was in regards to Linux.
Linux uses the EXT4 file systems by default.
How do you explain I'm currently booted from sdb9, an ext4 partition?
The early versions of RedHat didn't even suggest typical settings - you were expected to research them in advance and "roll your own", and the devices to mount could be and still can be practically anywhere: a different partition, a different drive - or a partition on a drive in a different country.
As requirements (especially for domestic users) have settled down into a typical requirement and distros have become much fancier over the years they do tend to use much the same partitioning arrangement - but that's not the same as saying it's how Linux is set up.
And "manually mounting" - you mean adding a line to fstab? I suppose that is manual, but it takes seconds and is done once during installation...
A device has a partition or partitions, and they each have a filesystem, and that device's naming and sequence has nothing to do with the filesystem being used on any of the partitions.
This is a bit old (and more than a little crazy!) but it shows just how much partitions and installations can be thrown around on Linux:
http://www.justlinux.com/forum/showthread.php?t=147959
Linux the OS has no default filesystem, but it does have a few filesystems that get used regularly. What does happen is that distros tend to make their default FS the current "flavour-o-the-month" which is currently ext4. Not so long ago it was ReiserFS and before that, ext2|3.
_________________
Giraffe: a ruminant with a view.
It feels unnecessarily complicated to me, because it's already possible to see what's running and who owns it so therefore I can easily see what should be killed (ideally this would be nothing!) - either by name or PID. Things that run as root are usually essential system things and probably best left running.
I find that piping stuff between the core utilities such as cut, tr, head, tail and so on generally achieves what I want very quickly and since killing off root processes is something I very very rarely need to do, those tools are perfectly adequate for the job and strung together as required, "live" at a console.
Also there tend to be more structured ways of doing this (see /etc/init.d/) and with this system it's easy enough to say things like:
/etc/init.d/apache2 stop|start|restart|status for example. I think most disros use something very similar to this these days.
I simply don't require a GUI for any of this stuff so it's never occurred to me to write one.
But back onto the topic - remember that?
Having had my ISP fiddle about with their mail systems and changing servers around I've now lost as a result a very useful mail finger service that would give me a concise list of EMails waiting: from, to, date, size etc. It scratched a small itch and answered very quickly the question "I wonder if there's any mail waiting?" with the minimum of fuss, and with no GUI or even a functional mail client required.
Support for this was dropped because, I was told - it's "old school". Apparently I'm expected to use some ridiculous web interface instead.
So I'm writing a small replacement which will return the same results. Not using finger, since their fancy new systems won't do that, but using POP3 talking directly to the mail server through Telnet. There's a bit of hoop-dancing required to extract the mail header block so I can extract the from/to/subject headers, but so far it's going Ok.
That should keep me amused for a while...
_________________
Giraffe: a ruminant with a view.
I just used
To show me what is running with root privs. I Know i could use ps
Maybe something like
Will show me what proc has root privs. But what fun is that? Its too simple! I would take the latter just because i enjoy programming.
/etc/init.d/apache2 stop|start|restart|status for example. I think most disros use something very similar to this these days.
I simply don't require a GUI for any of this stuff so it's never occurred to me to write one.
You could also make program/scripts inside of init.d at boot with a handy little tool sysv supplies called update-rc.d. It is apart of the sysv-rc package. Makes updating run levels a much more trivial task!
Support for this was dropped because, I was told - it's "old school". Apparently I'm expected to use some ridiculous web interface instead.
I would have to agree! I feel the conventional mail system Linux provides is a security risk! I actually close down the incoming and outgoing mail port, on my box via iptables. Google is so much more convenient ans much more secure!
That should keep me amused for a while...
I was always under the assumption you were a windows user.