#!/bin/bash echo "======================================================" echo "UUID manipulater by Kiyoshi SUZUKI written at 2011/5/8 for Debian Squeeze" echo "======================================================" if [ $USER != "root" ];then echo "execute me via Root. Abort." exit 0 fi LANG=C if [ -z "$(apt-cache policy hexedit|grep Installed|grep -v none)" ];then echo 'package "hexedit" is not installed. Do you allow installing it ?(yes:y, no:others)' read a if [ $a = "y" ];then apt-get install hexedit||(echo "Installation of hexedit failed. Abort.";exit 0) fi fi starttime=$(date +%Y-%m-%d_%k:%M|tr -d " ") echo "Starting at $starttime. This will be added to the copied former configuration files. ====================================================================" blkid echo "===============================================" echo "Type the partition device file name the UUID of which you want to modify (for example, sda1)" read a if [ ! -e /dev/$a ];then echo "/dev/$a does not exist. Abort!" exit 0 fi filesystem=$(blkid /dev/$a|sed -e "s/.*TYPE=\"//" -e "s/\".*//") echo "filesystem of /dev/$a is $filesystem" littleendian () { if [ $(expr $(echo $check1|wc -c) % 2) = 0 ];then echo "number of word \"$check1\" is odd. Abort." exit 0 else repeatnum=$(expr $(expr $(echo $check1|wc -c) - 1) / 2 - 1) len=$(expr $(echo $check1|wc -c) - 1) while [ 0 -lt $repeatnum ];do check1="$(echo $check1|cut -c3-$(expr $(expr $repeatnum \* 2) + 2))$(echo $check1|cut -c1-2)$(if [ $(expr $(expr $repeatnum + 1) \* 2) -lt $len ];then echo $check1|cut -c$(expr $(expr $repeatnum \* 2) + 3)-;fi)" repeatnum=$(expr $repeatnum - 1) done fi } modify () { dd if=/dev/$a bs=1 count=$num skip=$skip > test 2>/dev/null check1=$(hd test |head -n 1| sed -e "s/|.*//"|cut -c 9-|tr -d " ") if [ $littleendian = "on" ];then littleendian;fi check2=$(blkid /dev/$a|tr -d "-"|sed -e "s/.*UUID=\"//" -e "s/\".*//") if [ $check1 = $(echo $check2|tr "[A-Z]" "[a-z]") ];then olduuid=$(blkid /dev/$a|sed -e "s/.*UUID=\"//" -e "s/\".*//") echo "UUID is $olduuid" echo "Please type what UUID you want to change to" read newuuid newuuid=$(echo "${newuuid}00000000000000000000000000000000"|cut -b1-$(expr $num + $num)) if [ $(echo $newuuid|tr -d "0123456789abcdef") ];then echo "===============================================" echo "UUID must consist of \"0123456789abcdef\" only. Abort." echo "===============================================" exit 0 fi echo "===============================================" echo "Do you want to change UUID of /dev/$a to ${newuuid} ? (Yes:y or No:other)" read b if [ $b = "y" ];then check1=$newuuid if [ $littleendian = "on" ];then littleendian fi echo "${check1}y"|hexedit test 2>/dev/null check1=$(hd test|head -n 1| sed -e "s/|.*//"|cut -c 9-|tr -d " ") if [ $littleendian = "on" ];then littleendian;fi echo "===============================================" echo "Please confirm new UUID of /dev/$a is:" echo $check1 echo "Is it OK? (Yes:y, No:other)" read ans if [ $ans = "y" ];then dd if=test bs=1 count=$num of=/dev/$a seek=$skip 2>/dev/null && echo "UUID is modified. \"blkid\" command gives: $(blkid /dev/$a) You should manually update (modify) system settings such as /etc/fstab, /boot/grub/grub.cfg, /etc/initramfs-tools/conf.d/resume and so on. I will check /etc/fstab, /boot/grub/grub.cfg, /etc/initramfs-tools/conf.d/resume for $olduuid." sync newuuid=$(echo $check1|cut -c1-$(echo $cutspace|cut -d " " -f1)) loc1=$(echo $cutspace|cut -d " " -f1) cutspace=$(echo "$cutspace"|cut -d " " -f2-) for loc2 in $cutspace;do newuuid=$newuuid$(echo -$(echo $check1|cut -c$(expr $loc1 + 1)-$loc2)) loc1=$loc2 done changefstabetal else echo "If you want to change, type \"y\". Abort." exit 0 fi else echo "If you want to change, type \"y\". Abort." exit 0 fi else echo "Something is wrong. Abort!" exit 0 fi } modifyfile () { if [ -e /mnt$file ];then if [ $filesystem = "ntfs" -o $filesystem = "vfat" ];then olduuid=$(echo $olduuid|tr "[a-z]" "[A-Z]") newuuid=$(echo $newuuid|tr "[a-z]" "[A-Z]") fi if [ "$(grep $olduuid /mnt/$file|grep -v ^#)" ];then for line in $(grep -n $olduuid /mnt$file|grep -v ^[0-9]*:#|tr "\t " "_");do answer="1" while :;do echo "There is the following line in $file in $aa:" echo $(echo $line|sed -e "s/^[0-9]*/& lines:/") echo "Do you change \"$olduuid\" to \"$newuuid\" ?(yes=y,no=n)" read answer if [ "$answer" = "y" ];then perm=$(ls -l /mnt$file|cut -c2-10) perm2=$(expr 0 $(echo $perm|cut -c1-3|sed -e "s/r/ + 4/" -e "s/w/ + 2/" -e "s/x/ + 1/" -e "s/-//g"))$(expr 0 $(echo $perm|cut -c4-6|sed -e "s/r/ + 4/" -e "s/w/ + 2/" -e "s/x/ + 1/" -e "s/-//g"))$(expr 0 $(echo $perm|cut -c7-9|sed -e "s/r/ + 4/" -e "s/w/ + 2/" -e "s/x/ + 1/" -e "s/-//g")) cp -f /mnt$file /tmp/tempfile${starttime} mv -n /mnt$file /mnt${file}${starttime} touch /mnt$file chmod a+w /mnt$file cat /tmp/tempfile${starttime}|sed "$(echo $line|cut -d: -f1)s/$olduuid/$newuuid/" > /mnt$file chmod $perm2 /mnt$file break elif [ "$answer" = "n" ];then break fi done done fi fi } changefstabetal () { deviceslist=$(blkid|cut -d: -f1) for aa in $deviceslist;do umount -l /mnt 2>/dev/null filesystem1=$(blkid $aa|sed -e "s/.*TYPE=\"//"|sed -e "s/\".*//") mountpoint1="" if [ $filesystem1 = "ntfs" ];then if [ "$(mount|grep $aa' ')" ];then echo "$aa is mounted now. $aa is tried to be unmounted" mountpoint1=$(mount|grep "$aa "|head -n1|tr -s " "|cut -d" " -f3) umount -l $aa fi fi mount -t $filesystem1 $aa /mnt -o rw 2>/dev/null || continue echo "checking $aa" file="/etc/fstab" modifyfile file="/boot/grub/grub.cfg" modifyfile file="/boot/grub/menu.lst" modifyfile file="/etc/initramfs-tools/conf.d/resume" modifyfile sync umount /mnt 2>/dev/null sync if [ "$mountpoint1" ];then mount -t ntfs $aa $mountpoint1;fi done } case $filesystem in ext2|ext3|ext4) olduuid=$(blkid /dev/$a|sed -e "s/.*UUID=\"//" -e "s/\".*//") echo "UUID is $olduuid" echo "Please type what UUID you want to change to" read newuuid newuuid=$(echo $newuuid|tr "[A-Z]" "[a-z]"|tr -d -- -) newuuid=$(echo "${newuuid}00000000000000000000000000000000"|cut -b1-32) if [ $(echo $newuuid|tr -d "0123456789abcdef") ];then echo "===============================================" echo "UUID must consist of \"0123456789abcdef\" only. Abort." echo "===============================================" exit 0 fi newuuid="$(echo $newuuid|cut -c1-8)-$(echo $newuuid|cut -c9-12)-$(echo $newuuid|cut -c13-16)-$(echo $newuuid|cut -c17-20)-$(echo $newuuid|cut -c21-32)" echo "===============================================" echo "Do you want to change UUID of /dev/$a to ${newuuid} ? (Yes:y or No:other)" read b if [ $b = "y" ];then tune2fs -U $newuuid /dev/$a && echo "UUID is modified. \"blkid\" command gives: $(blkid /dev/$a) You should manually update (modify) system settings such as /etc/fstab, /boot/grub/grub.cfg, /etc/initramfs-tools/conf.d/resume and so on. I will check /etc/fstab, /boot/grub/grub.cfg, /etc/initramfs-tools/conf.d/resume." sync changefstabetal exit 0 else echo "If you want to change, type \"y\". Abort." exit 0 fi ;; swap) num=16 skip=1036 littleendian="off" cutspace="8 12 16 20 32" modify ;; ntfs) num=8 skip=72 littleendian="on" cutspace="16 " modify ;; reiserfs) num=16 skip=65620 littleendian="off" cutspace="8 12 16 20 32" modify ;; btrfs) num=16 skip=65568 littleendian="off" cutspace="8 12 16 20 32" modify ;; vfat) littleendian="on" cutspace="4 8" if [ -n "$(blkid /dev/$a|grep msdos)" ];then num=4 skip=39 else num=4 skip=67 fi modify ;; *) echo "Filesystem type $filesystem is not supported. Abort." exit 0 ;; esac exit 0