LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2010, 08:47 AM   #46
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30

Grail, so am I right when I say
/dev/sda=>hd0
/dev/sdb=>hd1
/dev/sdc=>hd2
/dev/sdd=>hd3
?

Now I have only 2 disks. It is not probable I will have more then 2 disk because I have only 2 SATA channels and 2 IDE channels. But I asked just to be clear.

Yes menu.lst is long because it has more systems bargaining for drive can exchange or one disk can be unplugged. I have already script mechanism to exclude drive mapping.

This words o'you I don't understand:
"Meaning, root (/) will be assigned a UUID and the same for any other systems on the single machine, but, the UUID for root
would always be the same (as it is unique and only assigned to one value) and therefore you could now parse the entire file
for that UUID and change it and so on for the other UUIDs."

Maybe you speak about mechanism to exchange old UUID for new UUID. But that's that what I prepare. This script we write now should do the think later. 1st step is to get UUID form to HD(x,y) form. Then I can shift the numbers ++x,++y,--x,--y (correct assignment if necessary). And I could to convert back from HD(x,y) to UUID form.

Now the main think I wait is solution of the last code o'mine. I don't know how to test x value and to conversion to number a=>0, b=>1, etc.
 
Old 04-29-2010, 09:38 AM   #47
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Grail, so am I right when I say
/dev/sda=>hd0
/dev/sdb=>hd1
/dev/sdc=>hd2
/dev/sdd=>hd3
?
Yes

Quote:
Maybe you speak about mechanism to exchange old UUID for new UUID
Sorry but no this is not what I was speaking of.

I will try with an example:
Code:
title Sata Mandriva
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=linux root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788
initrd (hd0,2)/boot/initrd.img

title failsafe
kernel (hd0,2)/boot/vmlinuz BOOT_IMAGE=failsafe root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c failsafe
initrd (hd0,2)/boot/initrd.img
From the above two examples and for all other such groupings in your menu.lst the following is always true:

root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c

So it does not matter if this line is in the file once or a hundred times but it will ALWAYS be the same

What I am saying is that you need to only find it once (not 20+ times) and make the change for all

Now something else I noticed is that some of the entries already have the entry - (hdx,y) in them, like:

(hd0,2)/boot/vmlinuz

Maybe this can also be used for the conversion??

Lastly, have you actually tested that your system will still boot if you change the root (or other) UUIDs into the hdx,y format??
 
Old 04-29-2010, 10:37 AM   #48
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
root=UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c

So it does not matter if this line is in the file once or a hundred times but it will ALWAYS be the same. What I am saying is that you need to only find it once (not 20+ times) and make the change for all
Never mind. I want both methods. It is fact (U R right) that if I would need to do all the steps* for about 10 items that is too slow work.

* Which steps I mean - step like from hd0,2 make hd1,2 or 1,3, then convert to uuid (maybe just because I want to check if the uuid is correct... if is in blkid list... )... and then maybe to convert it back to hd(?,?)... Yeah it is too long. But...

But later I can give it in function and to make this call for whole file...

If I something change I want to have a control over every item what is in it. That means, I still need to process every item through the main loop mentioned above. The main loop gives me ability to work with every item (title rename, set hdx.y, set uuid ). I could do one thing and it is to apply the function on whole file.

Therefor I still need the code to solve. It is not important if I apply the code on 1 block or on whole file. I need the basic form of code, and later I can to adjust it or expend it.

Quote:
Originally Posted by grail View Post

Now something else I noticed is that some of the entries already have the entry - (hdx,y) in them, like:

(hd0,2)/boot/vmlinuz

Maybe this can also be used for the conversion??
The script needs to unify the form. (hd0,2) never minds. Because I convert the /dev/sda3 uuid to (hd0,2). So the form will be unified.

Realize. If i reinstall system then I have new uuid. So can't be found in blkid. This is next thing. If the uuid is not found I will call blkid again and output will go to select. I will select disc which uuid I would use.

But still... for me is important the code to convert /dev/something to hd(x,y)

Last edited by webhope; 04-29-2010 at 10:38 AM.
 
Old 04-29-2010, 11:35 PM   #49
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think I follow where you are going, but I have come up with an alternative anyway, which you can change as you need.
The following code will assume that the block_new loop has also been removed:

Code:
#!/bin/bash

uuids=($(awk 'BEGIN{RS="UUID="}/-/{gsub(/ .*/,"");if(!_[$0]++)print}' menu.lst))

for uuid in ${uuids[@]}
do
    dev=$(blkid -U $uuid)

    drive=$(echo -n ${dev:7:1} | od -A n -t dC)
    ((drive -= 97))

    partition=${dev:8}
    ((partition--))

    echo "(hd${drive},${partition})"
done
Let me know what you think and if you don't understand any of it?
 
Old 04-30-2010, 03:00 AM   #50
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
OK, thanks. Maybe you could to explain:

Code:
+ for uuid in '${uuids[@]}'                      
++ blkid -U eab515e9-bc3e-4024-9f01-55fddaa0fb1c 
+ dev=/dev/sda3                                  
++ echo -n a                                     
++ od -A n -t dC                                 
+ drive='   97'                                  
+ (( drive -= 97 ))                              
+ partition=3                                    
+ (( partition-- ))                              
+ echo '(hd0,2)'                                 
(hd0,2)
What means the od arguments?

Code:
-A, --address-radix=RADIX
              decide how file offsets are printed

-t, --format=TYPE
              select output format or formats
-d     same as -t u2, select unsigned decimal 2-byte units
What means -A n ? What means adress radix?
What means the last letter C ?
I am pleased to know new command.

Edit:
I think about how to use this code. I think I will work with the block_new to proceed every item separately. However there is one thing about awk I'd like to know. If I have somethenig in variable x throught the gensub fnc. Ho can I do the comparation.

I have a awk command with this:
BEGIN{...} {
x=gensub(...)
# now I want e.g. to do something like:
if x<=n x=0 # how should be correct syntax?
print x
}

Edit:
One more thing I don't understand. U use this form of increase ((n++)) an I use this: i=$((i+1));
What is better? What is more reliable?

Last edited by webhope; 04-30-2010 at 04:08 AM.
 
Old 04-30-2010, 06:47 AM   #51
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
What means address radix?
decide how file offsets are printed
Quote:
What means -A n ?
RADIX is d for decimal, o for octal, x for hexadecimal or n for none.
Quote:
What means the last letter C ?
d[SIZE] signed decimal, SIZE bytes per integer
SIZE is a number. For TYPE in doux, SIZE may also be C for sizeof(char)

The nice thing about the code I presented is that in the for loop you can use a very simple sed statement to replace the UUID throughout the entire
menu.lst. For example in the menu.lst you provided me, this would generate 5 sed scripts to update ALL stanzas in the file.

Quote:
# now I want e.g. to do something like:
if x<=n x=0 # how should be correct syntax?
You need to look at http://www.gnu.org/manual/gawk/html_node/index.html - it is an excellent resource that I use
In awk an "if" looks like - if(<expression>)<do things here>

Quote:
U use this form of increase ((n++)) an I use this: i=$((i+1));
What is better? What is more reliable?
Neither or both. I prefer the "++" method as have used it in other languages for years and I recognize it immediately, but other bash programmers may say they prefer the one you have used. Each to their own
 
Old 04-30-2010, 07:34 AM   #52
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
The nice thing about the code I presented is that in the for loop you can use a very simple sed statement to replace the UUID throughout the entire
menu.lst. For example in the menu.lst you provided me, this would generate 5 sed scripts to update ALL stanzas in the file.
I don't like this "all at once" solution. Because man needs to proceed every system's device reference separately. If you would get file into memory, then you would proceed Mandriva Linux and change data in the file, then you want to continue to viewing other systems in the file but you read it from the memory.... Now file is changed but data in memory are not changed. I work out my own solution with use of your code.

Can you to explain simply what the command od does in your code? As much as it is possible to be simple. Edit: I know that it converts character to ord value but how you got it

Last edited by webhope; 04-30-2010 at 08:10 AM.
 
Old 04-30-2010, 08:19 AM   #53
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So we are looking at the device in /dev and all hard drives come in the form hdXY or sdXY where X is a letter of the alphabet representing which drive
we are looking at (a=1, b=2, and so on) and Y is which partition of that drive.
We also know that the characters a-z are numerically matched in the ASCII table to 97-122 inclusive.

Therefore when we use:
Quote:
echo <letter> | od -A n -t dC
We get the numerical value for the letter we have removed from the second last position of the device.

We then know that grub likes to be a bit different and start its numbering for both the device and the partition from 0.
Therefore, we subtract 97 (the ASCII value for 'a') from the drive number and take 1 from the partition number so we get a mapping like:

/dev/[sh]da1 = (hd0,0)
 
Old 04-30-2010, 08:40 AM   #54
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Wink

OK I found a help for "od":

http://linux.about.com/library/cmd/blcmdl_od.htm

and explanation:

Od writes one copy of each output line using each of the data types that you specified, in the order that you specified.

Last edited by webhope; 04-30-2010 at 09:54 AM.
 
Old 04-30-2010, 08:50 AM   #55
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
If I want to convert $drive
Code:
drive = 97;
drive=$(echo $drive | od -A n -t Cd )
to Character how should I change the od arguments?

This code returns:

d: character „C“ in string of „„Cd““ is wrong

Last edited by webhope; 04-30-2010 at 09:56 AM.
 
Old 04-30-2010, 09:57 AM   #56
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
No you are going the wrong way.
Code:
drive=a
drive=$(echo $drive | od -A n -t dC)

echo $drive # this will now equal 97
 
Old 04-30-2010, 10:09 AM   #57
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
No you are going the wrong way.
But now I need to convert in reverse direction from ord to char.
ord=97
drive=$(echo $ord | od -A n -t ?? )

Last edited by webhope; 04-30-2010 at 10:17 AM.
 
Old 04-30-2010, 11:10 AM   #58
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am not sure why, but this would work:
Code:
x=97
printf \\$(printf '%03o' $x)
 
Old 04-30-2010, 11:14 AM   #59
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Thanks :-) Everytime U use different way to do things ...
 
Old 04-30-2010, 12:09 PM   #60
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not intentionally, but in this case the reverse using od is not within my skill set
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Migrate Regexp from SED to AWK cgcamal Programming 9 04-23-2010 10:32 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
awk regexp for one character match nemobluesix Linux - General 7 02-16-2009 10:50 PM
Volume has problems including no uuid in /dev/disk/by-uuid abejarano Linux - Hardware 3 12-31-2008 08:41 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration