Page 1 of 1

Linux n00b needs help!

Posted: Fri Aug 01, 2008 7:39 am
by Igor_Kavinski
I'm trying to run a script in Ubuntu Desktop 64-bit to inject the grub bootloader into an IMG file. Here's the script:

#!/bin/sh
IMAGE="EGATE.IMG"
offset=$(($(sfdisk -d $IMAGE|awk '/start=/{print $4}'|head -n1|sed 's/,//')*512))
uuid=$(dd if=$IMAGE skip=$((0x27+$offset)) bs=1 count=4 2>/dev/null|hexdump -e '"%X"'|sed 's/\(....\)/\1-/')
LOOP="/dev/loop0"
OPTIONS="umask=000,shortname=mixed,quiet,utf8"
DIR=/tmp/target
mkdir -p $DIR
losetup -d $LOOP
losetup $LOOP $IMAGE
umount $DIR
mount -o loop,offset=$offset,$OPIONS $IMAGE $DIR
mkdir -p $DIR/boot/grub
cat > $DIR/boot/grub/menu.lst <<EOT
timeout 0
title Test
kernel /ce_bz
EOT
cp -v /usr/lib/grub/x86_64-pc/stage1 $DIR/boot/grub
cp -v /usr/lib/grub/x86_64-pc/stage2 $DIR/boot/grub
grub --device-map=/dev/null --batch <<EOT
device (hd0) $LOOP
root (hd0,0)
setup --stage2=$DIR/boot/grub/stage2 (hd0)
EOT
cat > $DIR/splash.idx <<EOT
root=UUID=$uuid
EOT
umount $DIR
losetup -d $LOOP



But I get the following error when I try to run the script:

~/Desktop$ ./inject
bash: ./inject: /bin/sh^M: bad interpreter: No such file or directory


What am I doing wrong? Also please take a look at the script and point out any errors in it. Thanks!

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 8:24 am
by bthylafh
That ^M indicates to me that you've got MS-DOS line endings in your text file, that is, CR LF. You need to convert it to Unix line endings, with just LF. There are a few Linux programs out there to do that.

I have the recode program installed, and an alias:

alias dos2ux='recode ibmpc..latin1'

and then IIRC you type

# dos2ux file

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 8:31 am
by AvKn
A faster way would be to type the command "tr -d "\015" < inject > inject-converted". That's assuming that the current directory is where the script file is, and that the script's filename is inject. That command calls the tr program to delete (hence -d) all "\015" characters (that ^M line-ending) in the file inject, then saves the trimmed file to as inject-converted (you can change inject-converted to any filename you want.)

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 8:48 am
by blitzy
not to take things off topic, but this is a prime example of why linux fails.

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 9:27 am
by bthylafh
You're an example of why trolls fail, actually.

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 10:30 am
by AvKn
This is an example of why Linux fails for you. It's different for others.

I have this ironic belief that Linux works best for either novice computer users or very advanced users. It's because that Linux actually is so simple and painless to use, as long as you don't go off the beaten path and do something like installing applications from source. (Have you even tried Linux recently? Installing applications there is even easier than in Windows, as long it's a standard app.) On the other hand, an ordinary user from time to time tries things that beginners wouldn't do, like trying to improve performance by installing new drivers manually. This, admittedly, is much harder in Linux than in Windows, and something that often only advanced users can do without the hassle of googling.

In Igor's case, do you think anyone who has only recently learned how to use a computer care about bootloaders?

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 12:15 pm
by bitvector
blitzy wrote:
not to take things off topic, but this is a prime example of why linux fails.

I can think of a lot more important shortcomings than failing to execute an MS-DOS formatted text file as a shell script.

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 12:35 pm
by Igor_Kavinski
Thanks AvKn! The "tr" command worked beautifully :D

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 9:15 pm
by blitzy
I guess I should have clarified my point, it's not the fact that the bootloader doesnt work. That is something only an advanced user would want to dig with. My point is regarding the line endings, and lack of an easy to understand plain english error message. Or would it even be that hard for their application to gracefully handle a file with the incorrect ms-dos line endings given how common an issue it is likely to be. From my experience a lot of command line apps in linux are not as easy to use as they could be, using weird syntax and generally lacking easy to understand instructions.

and yes i have used linux, recently ubuntu hardy which is pretty good.

Re: Linux n00b needs help!

Posted: Fri Aug 01, 2008 9:32 pm
by notfred
It's a perfectly clear error message, it says exactly what the problem is - the interpreter that is specified in the script "/bin/sh^M" doesn't exist. It isn't one of those error messages that don't say what the problem is but guess at what the solution might be, because if it isn't that particular problem then you are SoL.

This is also not likely to be a common issue, most POSIX scripts are written on POSIX compliant systems, not on systems that violate standards and use extra characters at the end of lines like the Microsoft family of OSs do. You also have to remember that this way of scripting comes from the early days of UNIX, way before Bill Gates wrote his first OS.

There are plenty of simple ways of dealing with this: "file blah" will tell you precisely what blah is (whether it is MS-DOS line endings or plain ASCII text) and dos2unix and unix2dos have been around since DOS first got network support.

I don't want to come across as one of those stereotypical "RTFM" Linux fans, but please stop and think before posting something like that. There may be other failings in Linux that mean it has not spectacular uptake on the desktop yet, but this really isn't one of them.

Re: Linux n00b needs help!

Posted: Sat Aug 02, 2008 1:01 am
by AvKn
Igor: no problem. :D

In the first place, today's Linux distributions actually tries to make people avoid the command line. It's why they're now making graphical interfaces for every conceivable function of the OS, but the terminal will always be there when something fails. The terminal isn't for everyone; it's an artifact of Linux's text user interface-only roots, and it will never be changed to be extremely user-friendly for today's point-and-click world.

Re: Linux n00b needs help!

Posted: Sat Aug 02, 2008 4:11 am
by Igor_Kavinski
OK the script ran fine and the grub bootloader was injected successfully, thus letting me get a taste of Asus Express Gate :D However, I got the following errors during the script:

ioctl: LOOP_CLR_FD: No such device or address
umount: /tmp/target: not mounted

What do these mean? How come the script succeeded even though some of the commands in it failed? I suppose the commands WERE needed otherwise why would the script's author include them in the first place?

Re: Linux n00b needs help!

Posted: Sat Aug 02, 2008 9:22 am
by bthylafh
Early in the script, $DIR is defined as /tmp/target. You see a few lines down that it's unmounted with a umount command -- but it hasn't been mounted yet. I'm not sure why the script writer did that; if it was to be "extra safe" then he should have put a comment in or echoed something.

Towards the end of the script, it umounts $DIR again, because it's a temporary mount and isn't needed any more.

Re: Linux n00b needs help!

Posted: Sat Aug 02, 2008 10:32 am
by notfred
In a similar way to the unmount before mounting, the script also deletes the loopback device "losetup -d $LOOP" before it actually sets it up, hence the ioctl error message. It's not really a professional way to do that stuff, things should be checked before using if there is a danger of them already being in use and then aborting with the reason why and potentially recommendations to fix. As an example, in the case of the loopback device you can find the next free one with "losetup -f" rather than hard coding to use /dev/loop0 and if that was done (along with a test for that failing) then the script would be more robust.