Help with UUID replacement in fstab and menu.lst


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with UUID replacement in fstab and menu.lst
# 1  
Help with UUID replacement in fstab and menu.lst

I am trying to write a small backup application for Linux systems file by file if partitions are created by the user using fdisk. I am facing a problem when I am done with restoring the files. The problem is with UUID mismatch in restored fstab and menu.lst.

Using C I could create a partlist.txt file where I have 3 entries:
device_name, "UUID" mountpoint

Need to restore the following:

1. UUID in menu.lst by matching UUID of "/" from partlist.txt
2. Replace correct UUIDs from partlist.txt for all mountpoints in fstab

Also cannot depend on the ordering of lines anywhere.

Attached the sample generated partlist.txt and fstab + menu.lst from Red Hat 6.
I am not an expert in using sed and awk. So any help is really appreciated.
# 2  
Something like this..
Code:
miro@miro-ntb:Downloads$ cat partlist.txt 
/dev/sda1 "f75b104c-678e-4f80-a618-70d4bf4a3ede" /
/dev/sda3 "hsjsd675-678e-4f80-a618-70d4bf4a3ede" /home
/dev/sda2 "psb8767c-678e-4f80-a618-70d4bf4a3ede" /boot

Code:
miro@miro-ntb:Downloads$ cat fstab.txt 
#
# /etc/fstab
# Created by anaconda on Mon Feb 14 16:03:26 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=10cb97f9-e52e-472b-a20b-c1e90c6d561b /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
HHH     /home  smthn defs 0 0

Code:
miro@miro-ntb:Downloads$ awk '
NR==FNR{a[$3]=$2; next} #store uuids in assoc. array
{ #on each line from fstab
for(i in a) { #loop through array
  if($2==i)  #check whether mountpoint is the same
  {$1="UUID=" a[i]} #replace 1st field with UUID=uuid
}; 
print $0;  #print the whole line
}
' <(sed -e 's/\r//' -e 's/"//g' partlist.txt) fstab.txt  

#
# /etc/fstab
# Created by anaconda on Mon Feb 14 16:03:26 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=f75b104c-678e-4f80-a618-70d4bf4a3ede / ext4 defaults 1 1
UUID=psb8767c-678e-4f80-a618-70d4bf4a3ede /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
UUID=hsjsd675-678e-4f80-a618-70d4bf4a3ede /home smthn defs 0 0

The sed command gets rid of carriage return and quotes.
# 3  
Hi mirni,

Thank you so much. I have tried this just now and it works like a charm for the /etc/fstab file.

I need to replace the same in menu.lst file also (attached as menu.txt). Would you please provide the script for that also?
# 4  
This is the script to modify menu.txt. It modifies the file in-place. It'd be safer to omit sed's '-i' switch in the second line, and let it print out the new version of menu.txt (redirect it into a new file); so that the original is kept untouched.
Code:
miro@miro-ntb:Downloads$ cat test.sh
#!/bin/bash

root_uuid=$(sed -n ' s/[^ ]* "// ;/\/[\r]*$/  s/".*//p' partlist.txt) #extract root UUID
sed -i 's/root=[^ ]*/root=UUID='$root_uuid'/' menu.txt #this file will get modified

Code:
miro@miro-ntb:Downloads$ cat menu.txt 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-71.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-71.el6.i686 ro root=/dev/mapper/VolGroup-lv_root rd_LVM_LV=VolGroup/lv_root rd_LVM_LV=VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet
    initrd /initramfs-2.6.32-71.el6.i686.img
miro@miro-ntb:Downloads$ ./test.sh
miro@miro-ntb:Downloads$ cat menu.txt 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=UUID=f75b104c-678e-4f80-a618-70d4bf4a3ede
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux (2.6.32-71.el6.i686)
    root (hd0,0)
    kernel /vmlinuz-2.6.32-71.el6.i686 ro root=UUID=f75b104c-678e-4f80-a618-70d4bf4a3ede rd_LVM_LV=VolGroup/lv_root rd_LVM_LV=VolGroup/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet
    initrd /initramfs-2.6.32-71.el6.i686.img


Last edited by mirni; 04-08-2011 at 09:31 AM..
# 5  
Thanks a lot mirni!

---------- Post updated at 06:15 PM ---------- Previous update was at 06:08 PM ----------

One question mirni...
Is there any way to edit fstab in-place too? Otherwise I need to redirect the lines in a temp file and replace fstab again.
# 6  
AFAIK awk doesn't support in-place editing. A workaround is needed. If anyone knows how to deal with this with the original "awk < (sed file1) file2" design, please post.

Code:
miro@miro-ntb:Downloads$ cat fstab.txt 

#
# /etc/fstab
# Created by anaconda on Mon Feb 14 16:03:26 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=10cb97f9-e52e-472b-a20b-c1e90c6d561b /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
miro@miro-ntb:Downloads$ { rm fstab.txt && awk '
BEGIN{while ( (getline < "partlist.txt")>0) { 
gsub(/\r|"/,""); a[$3]=$2;}
}
{
for(i in a) { if($2==i){$1="UUID=" a[i]}}; 
print $0;
}' > fstab.txt; } < fstab.txt
miro@miro-ntb:Downloads$ cat fstab.txt 

#
# /etc/fstab
# Created by anaconda on Mon Feb 14 16:03:26 2011
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=f75b104c-678e-4f80-a618-70d4bf4a3ede / ext4 defaults 1 1
UUID=psb8767c-678e-4f80-a618-70d4bf4a3ede /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

This workaround is adapted from here: https://www.unix.com/shell-programmin...line-edit.html. You can find an explanation how this '{ rm file && awk > file ; } < file' construct works.
Cheers
mirni
# 7  
Thanks really!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #616
Difficulty: Medium
There is no separate char data type in Python.
True or False?

9 More Discussions You Might Find Interesting

1. BSD

Find Partition/Slice UUID

I thought I had figured this out at one point, but I can't remember. Is there a way/command to get the UUIDs of a disk's partitions/slices in FreeBSD? Linux has the blkid command, which doesn't seem to be available. (2 Replies)
Discussion started by: AntumDeluge
2 Replies

2. Shell Programming and Scripting

Script to unmount and mount by UUID

Hi, Need a bit of help on this one as I am a scripting noob. I have a linux based NAS that mounts USB hard drives in an inconsistent location and to make matters worse, seems to lose the mount for an unknown reason and doesn't remount automatically unless the drive is removed and re-inserted.... (4 Replies)
Discussion started by: gtr33m
4 Replies

3. UNIX for Dummies Questions & Answers

Missing menu.lst file in Ubuntu

I am not able to find menu.lst in /boot. During the Linux Kernel Compilation I installed the kernel using make install. Next I created an initrd image. I had to modify the Grub configuration file - /boot/grub/menu.lst which I am not able to find. Any resolution for the issue? (3 Replies)
Discussion started by: rupeshkp728
3 Replies

4. Boot Loaders

Help Me edit the menu.lst !

I am trying to install three OS (Windows VISTA, OpenSUSE 11.3 & Solaris 11 Express) on a single drive of a laptop. However when I go to edit the /rpool/boot/grub/menu.lst to put the entry so it can boot the OpenSUSE 11.3 I get errors some of which result into starting all over again. I have also... (3 Replies)
Discussion started by: Tenyhwa
3 Replies

5. Solaris

Need to create a menu.lst for Solaris 11 Express, OpenSUSE 11.3 & Windows Vista

I have partitioned and installed Windows Vista, OpenSUSE and Solaris 11 Express on a LapTop hardDrive. However I am not able to boot OpenSUSE 11.3 although I have it in menu.lst which I put in a Solaris partition directory /rpool/boot/grub. Could someone tell me how to go about it. See what I did... (2 Replies)
Discussion started by: Tenyhwa
2 Replies

6. SuSE

Need to edit the menu.lst so it can work

I am trying to install three OS (Windows VISTA, OpenSUSE 11.3 & Solaris 11 Express) on a single drive of a laptop. However when I go to edit the /rpool/boot/grub/menu.lst to put the entry so it can boot the OpenSUSE 11.3 I get errors some of which result into starting all over again. I have also... (0 Replies)
Discussion started by: Tenyhwa
0 Replies

7. UNIX for Advanced & Expert Users

Generate UUID for a host

Hello Experts, Is there a way to generate Universally Unique identifiers on all Unix flavours such as Solaris, RHELinux,Suse Linux, MacOS,HP UX etc? If i can get a system command or a system call or an algorithm/script/program to generate a unique identifier, it will be helpful. Thanks in... (1 Reply)
Discussion started by: GajendraSharma
1 Replies

8. Shell Programming and Scripting

shell script to alter grub menu.lst

Hi folks, I have a dual-boot Ubuntu/Windows machine and I wanted to create a script to change the menu.lst file so it will change the default boot partition (this is so I can reload the machine remotely and allow it to boot to the Windows partition). Today I have to sudo cp a template file I... (1 Reply)
Discussion started by: ppucci
1 Replies

9. Shell Programming and Scripting

menu.lst, boot options?

Hi all, I would like to have some details on menu.lst!! the reason is ,if i am trying to add my own boot option where do i need to add it? is it in menu.lst only or elsewere(am referring to unix os) because i tried adding a unique boot option and it was not reflected when the system booted?... (8 Replies)
Discussion started by: wrapster
8 Replies

Featured Tech Videos