clean_ubuntu_uninstaller (v5)
#! /bin/bash
## YannUbuntu 17/10/2010 - https://launchpad.net/~yannubuntu - licensed under GNU-GPL v3 (http://www.gnu.org/licenses/gpl.html)
## Special thanks to Josepe36 and all contributors of this thread : http://forum.ubuntu-fr.org/viewtopic.php?pid=3786646
## CLEAN REMOVAL OF UBUNTU
## Allows to perform clean uninstall of any Linux distribution previously installed via the "improved_ubuntu_installer" script.
## To test this script, you need to execute it (sudo ./clean_ubuntu_uninstaller) from a live-session
## For better integration, a launcher can be created e.g. in System->Administration (http://pix.toile-libre.org/?img=1287036329.png), and a "Uninstall Ubuntu" button can also be added to the live-CD welcome screen (http://pix.toile-libre.org/?img=1287036852.png).
## To be improved : display the name/versions of the Linux distros in the menu (http://pix.toile-libre.org/?img=1287037811.png), take into account disks other than sda...
## DESINSTALLATION D'UBUNTU
## Permet de désinstaller proprement n'importe-quelle distribution Linux ayant été installée via le script "improved_ubuntu_installer".
## Pour tester ce script, il suffit de l'exécuter (sudo ./clean_ubuntu_uninstaller) à partir d'une session-live
## Pour une meilleure intégration, un lanceur peut etre créé par ex. dans le menu Systeme->Administration (http://pix.toile-libre.org/?img=1286518777.png), on peut aussi ajouter un bouton "Désinstaller Ubuntu" sur l'écran d'accueil du live-CD (http://pix.toile-libre.org/?img=1286348674.png).
## A améliorer : afficher le nom des distros Linux dans le menu (http://pix.toile-libre.org/?img=1286347844.png), restauration sur disque autre que sda...
detect_backup(){
tab=$(sudo fdisk -l|grep -i "$TEMP_FILESYSTEM"|cut -c1-10) ;
if [ "$tab" ]; then
echo "The existing $TEMP_FILESYSTEM partitions are : $tab" ;
##MOUNTS THE $TEMP_FILESYSTEM PARTITIONS, DETECTS WHICH CONTAIN AN OS, AND WHICH CONTAIN A 63 SECTORS BACKUP
for i in $tab
do
sudo umount $i ;
sudo mount $i /mnt;
folder=$(sudo ls /mnt |grep -i "$TEMP_SYSTEMFOLDER");
if [ "$folder" ]; then
control=$(sudo ls /mnt/$TEMP_SYSTEMFOLDER | grep "save-63-sectors.img") ;
if [ "$control" ]
then echo "$i is a $TEMP_FILESYSTEM partition containing a backup";
QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP=$(($QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP+1)) ;
LIST_OF_PARTITIONS_WITH_BACKUP[$QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP]=$i
LIST_OF_FOLDERS_WITH_BACKUP[$QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP]=$TEMP_SYSTEMFOLDER
if [ "$QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP" = "1" ]; then sudo dd if=/mnt/$TEMP_SYSTEMFOLDER/save-63-sectors.img of=/tmp/save-63-sectors.img bs=512 count=63
fi
else echo "$i is a $TEMP_FILESYSTEM partition without backup";
QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP=$(($QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP+1))
fi
##Creates a tab with partitions containing an installation of Linux
if [ "$TEMP_FILESYSTEM" = "linux" ]; then
QUANTITY_OF_DETECTED_LINUX=$(($QUANTITY_OF_DETECTED_LINUX+1))
LIST_OF_EXISTING_LINUX[$QUANTITY_OF_DETECTED_LINUX]="$i"
fi
##Creates a tab with partitions containing an installation of Windows
if [ "$TEMP_FILESYSTEM" = "ntfs" ] || [ "$TEMP_FILESYSTEM" = "fat" ]; then
QUANTITY_OF_DETECTED_WINDOWS=$(($QUANTITY_OF_DETECTED_WINDOWS+1))
fi
##Creates a tab with partitions containing an installation of MacOS
if [ "$TEMP_FILESYSTEM" = "hfs" ] ; then
QUANTITY_OF_DETECTED_MACOS=$(($QUANTITY_OF_DETECTED_MACOS+1))
fi
fi
sudo umount $i ;
done
else echo "There is no $TEMP_FILESYSTEM partition" ;
fi
}
#__________________________________________________________________________________________________________________________________________________________________
### MAIN MENU
uninstall_linux()
{
## CONTROLS IF A BACKUP OF THE 63 FIRST SECTORS ALREADY EXISTS ON THE DISK, IF YES IT IS MEMORIZED IN /tmp. ALSO COUNTS THE QUANTITY OF OS.
QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP=0
QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP=0
QUANTITY_OF_DETECTED_LINUX=0
QUANTITY_OF_DETECTED_WINDOWS=0
QUANTITY_OF_DETECTED_MACOS=0
LINUX_SYSTEM_FOLDER="boot"
HFS_SYSTEM_FOLDER="boot"
NTFS_SYSTEM_FOLDER="Windows"
FAT_SYSTEM_FOLDER="WINDOWS"
echo "10"
TEMP_FILESYSTEM="linux"; TEMP_SYSTEMFOLDER="$LINUX_SYSTEM_FOLDER" #I put Linux first because I guess the backups on Linux partitions have better reliability (not affected by virus)
detect_backup $TEMP_FILESYSTEM $TEMP_SYSTEMFOLDER $QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP $QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP $QUANTITY_OF_DETECTED_LINUX $LIST_OF_EXISTING_LINUX $LIST_OF_PARTITIONS_WITH_BACKUP $LIST_OF_FOLDERS_WITH_BACKUP
##IF NO LINUX HAVE BEEN DETECTED
if [ "$QUANTITY_OF_DETECTED_LINUX" = "0" ]; then
echo "100"
if [ "$language" = "English" ]; then
zenity --error --text="No Linux installation has been found on this computer."
else
zenity --error --text="Aucune installation Linux n'a été détectée sur cet ordinateur."
fi
##IF MINIMUM ONE LINUX HAS BEEN DETECTED
else
echo "30"
TEMP_FILESYSTEM="hfs"; TEMP_SYSTEMFOLDER="$HFS_SYSTEM_FOLDER"
detect_backup $TEMP_FILESYSTEM $TEMP_SYSTEMFOLDER $QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP $QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP $QUANTITY_OF_DETECTED_MACOS $LIST_OF_PARTITIONS_WITH_BACKUP $LIST_OF_FOLDERS_WITH_BACKUP
echo "60"
TEMP_FILESYSTEM="ntfs"; TEMP_SYSTEMFOLDER="$NTFS_SYSTEM_FOLDER"
detect_backup $TEMP_FILESYSTEM $TEMP_SYSTEMFOLDER $QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP $QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP $QUANTITY_OF_DETECTED_WINDOWS $LIST_OF_PARTITIONS_WITH_BACKUP $LIST_OF_FOLDERS_WITH_BACKUP
echo "80"
TEMP_FILESYSTEM="fat"; TEMP_SYSTEMFOLDER="$FAT_SYSTEM_FOLDER"
detect_backup $TEMP_FILESYSTEM $TEMP_SYSTEMFOLDER $QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP $QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP $QUANTITY_OF_DETECTED_WINDOWS $LIST_OF_PARTITIONS_WITH_BACKUP $LIST_OF_FOLDERS_WITH_BACKUP
echo "100" #Closes the progress indication dialog (for safety purpose we need to prevent the user from cancelling in the middle of removal operations)
echo "We detected $QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP backup(s), et $QUANTITY_OF_DETECTED_SYSTEMS_WITHOUT_BACKUP OS without backup."
echo "We detected $QUANTITY_OF_DETECTED_LINUX Linux distribution(s), $QUANTITY_OF_DETECTED_MACOS MacOS, and $QUANTITY_OF_DETECTED_WINDOWS Windows."
##IF ONLY ONE LINUX HAS BEEN DETECTED, THEN WE RESTORE THE 63 ORIGINAL SECTORS
if [ "$QUANTITY_OF_DETECTED_LINUX" = "1" ]
then
##IF NO BACKUP HAVE BEEN DETECTED
if [ "$QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP" = "0" ]; then
##Dual-boot Linux-Windows
if [ "$QUANTITY_OF_DETECTED_WINDOWS" != "0" ]; then
if [ "$language" = "English" ]; then
zenity --info --title="Automatic removal is impossible" --text="There is no boot backup on this computer. To uninstall Ubuntu, it is recommended to delete the Ubuntu partition then reinstall a Windows bootloader (with a Windows repair CD for example). It is a non-trivial and risky manipulation, so please backup your data before doing it."
else
zenity --info --title="Impossible de désinstaller automatiquement" --text="Il n'existe pas de sauvegarde d'amorcage sur cet ordinateur. Pour désinstaller Ubuntu, il est recommandé d'effacer la partition Ubuntu puis réinstaller un amorceur Windows (avec un CD de réparation Windows par exemple). Il s'agit d'une manipulation non-triviale et risquée, donc pensez à sauver vos données auparavant."
fi
##Dual-boot Linux-MacOS
elif [ "$QUANTITY_OF_DETECTED_MACOS" != "0" ]; then
if [ "$language" = "English" ]; then
zenity --info --title="Automatic removal is impossible" --text="There is no boot backup on this computer. To uninstall Ubuntu, it is recommended to delete the Ubuntu partition then reinstall a MacOS bootloader (with a MacOS repair CD for example). It is a non-trivial and risky manipulation, so please backup your data before doing it."
else
zenity --info --title="Impossible de désinstaller automatiquement" --text="Il n'existe pas de sauvegarde d'amorcage sur cet ordinateur. Pour désinstaller Ubuntu, il est recommandé d'effacer la partition Ubuntu puis réinstaller un amorceur MacOS (avec un CD de réparation MacOS par exemple). Il s'agit d'une manipulation non-triviale et risquée, donc pensez à sauver vos données auparavant."
fi
else
##The disk contains only one OS (Linux)
if [ "$language" = "English" ]; then
zenity --info --title="Automatic removal is impossible" --text="There is no other operating system nor boot backup on this computer. You can uninstall Ubuntu by deleting its partition, but then your computer won't have any operating system any more."
else
zenity --info --title="Impossible de désinstaller automatiquement" --text="Il n'existe pas d'autre système d'exploitation, ni de sauvegarde d'amorcage sur cet ordinateur. Vous pouvez désinstaller Ubuntu en effacant sa partition, mais votre ordinateur n'aura alors plus de système d'exploitation pour le faire fonctionner."
fi
fi
##IF MINIMUM ONE BACKUP HAS BEEN DETECTED
else
##Dual-boot Linux-Windows
if [ "$QUANTITY_OF_DETECTED_WINDOWS" != "0" ]; then
if [ "$language" = "English" ]; then
zenity --question --title="Are you sure you want to uninstall Ubuntu ?" --text="This will delete Ubuntu and restore the initial boot sector of your computer, thus giving back access to Windows only. All your data in Ubuntu will be lost. Do you want to continue ?" || exit 101;
else
zenity --question --title="Etes-vous sûr de vouloir désinstaller Ubuntu ?" --text="Ceci effacera Ubuntu et restaurera l'amorcage initial de votre ordinateur, vous redonnant accès à Windows uniquement. Attention, toutes vos données contenues dans Ubuntu seront effacées. Souhaitez-vous continuer ?" || exit 101;
fi
##Dual-boot Linux-MacOS
elif [ "$QUANTITY_OF_DETECTED_MACOS" != "0" ]; then
if [ "$language" = "English" ]; then
zenity --question --title="Are you sure you want to uninstall Ubuntu ?" --text="This will delete Ubuntu and restore the initial boot sector of your computer, thus giving back access to MacOS only. All your data in Ubuntu will be lost. Do you want to continue ?" || exit 101;
else
zenity --question --title="Etes-vous sûr de vouloir désinstaller Ubuntu ?" --text="Ceci effacera Ubuntu et restaurera l'amorcage initial de votre ordinateur, vous redonnant accès à MacOS uniquement. Attention, toutes vos données contenues dans Ubuntu seront effacées. Souhaitez-vous continuer ?" || exit 101;
fi
else
##The disk contains only one OS (Linux)
if [ "$language" = "English" ]; then
zenity --question --title="Are you sure you want to uninstall Ubuntu ?" --text="There is no other operating system on this computer. If you continue, Ubuntu will be deleted and the initial boot sector will be restored. This can sometimes be usefull to facilitate the reinstallation of the original operating system. All your data in Ubuntu will be lost, and your computer won't have any operating system any more. Do you want to continue ?" || exit 101;
else
zenity --question --title="Etes-vous sûr de vouloir désinstaller Ubuntu ?" --text="Il n'existe pas d'autre système d'exploitation sur cet ordinateur. Si vous continuez, Ubuntu sera effacé et l'amorcage initial de votre ordinateur sera restauré. Ceci peut parfois faciliter la réinstallation de l'OS original. Attention, toutes vos données contenues dans Ubuntu seront effacées et votre ordinateur n'aura alors plus de système d'exploitation pour le faire fonctionner. Souhaitez-vous continuer ?" || exit 101;
fi
fi
## RESTORES THE FIRST BACKUP DETECTED ON THE DISK
### saves temporarily the current first sector (in order to restore the current partition table just after)
sudo dd if=/dev/sda of=/tmp/backup_of_current_first_sector.img bs=512 count=1 ;
### restores the boot backup in the 63 first sectors
sudo dd if=/tmp/save-63-sectors.img of=/dev/sda bs=512 count=63 ;
### restores the current partition table
sudo dd if=/tmp/backup_of_current_first_sector.img of=/dev/sda bs=1 skip=446 count=64
## Renames all the backups (in order to avoid interferences with next Ubuntu installation, while keeping it in case of trouble or for test purpose)
i=1; while [ "$i" != "$(($QUANTITY_OF_DETECTED_SYSTEMS_WITH_BACKUP+1))" ]; do
sudo umount ${LIST_OF_PARTITIONS_WITH_BACKUP[$i]} ;
sudo mount ${LIST_OF_PARTITIONS_WITH_BACKUP[$i]} /mnt;
sudo mv /mnt/${LIST_OF_FOLDERS_WITH_BACKUP[$i]}/save-63-sectors.img /mnt/${LIST_OF_FOLDERS_WITH_BACKUP[$i]}/sav63sectors_old.img
sudo umount ${LIST_OF_PARTITIONS_WITH_BACKUP[$i]} ;
i=$(($i+1));
done;
##Erases the partition chosen by the user
sudo umount ${LIST_OF_EXISTING_LINUX[1]} ;
sudo mount ${LIST_OF_EXISTING_LINUX[1]} /mnt;
sudo rm -r /mnt/*
sudo umount ${LIST_OF_EXISTING_LINUX[1]} ;
if [ "$language" = "English" ]; then
zenity --info --title="Ubuntu has been successfully uninstalled" --text="Thank you for trying Ubuntu, see you soon !"
else
zenity --info --title="Ubuntu a été correctement desinstallé" --text="Merci d'avoir essayé Ubuntu, et à bientôt !"
fi
fi
##IF SEVERAL LINUX HAVE BEEN DETECTED, THEN WE ASK THE USER TO CHOSE ONE, WE DELETE IT AND WE REINSTALL GRUB ON ANOTHER REMAINING DISTRIBUTION
else
i=1; while [ "$i" != "$(($QUANTITY_OF_DETECTED_LINUX+1))" ]; do TAB="$TAB ${LIST_OF_EXISTING_LINUX[$i]}";i=$(($i+1)); done;
if [ "$language" = "English" ]; then
choice=$(zenity --list --title="Removal of Ubuntu" --text="Which Linux distribution do you want to uninstall ?" --column="" $TAB) || exit 102;
else
choice=$(zenity --list --title="Désinstallation d'Ubuntu" --text="Quelle distribution Linux souhaitez-vous desinstaller ?" --column="" $TAB) || exit 102;
fi
## If we uninstall the first distribution of the list, we reinstall GRUB into the 2nd one
if [ "$choice" = "${LIST_OF_EXISTING_LINUX[1]}" ];then
##Erases the partition chosen by the user
sudo umount ${LIST_OF_EXISTING_LINUX[1]} ;
sudo mount ${LIST_OF_EXISTING_LINUX[1]} /mnt;
sudo rm -r /mnt/*
sudo umount ${LIST_OF_EXISTING_LINUX[1]} ;
##Reinstalls GRUB into the 2nd Linux partition
sudo mount ${LIST_OF_EXISTING_LINUX[2]} /mnt;
sudo grub-install --root-directory=/mnt /dev/sda
sudo umount ${LIST_OF_EXISTING_LINUX[2]}
if [ "$language" = "English" ]; then
zenity --info --title="Ubuntu has been successfully uninstalled" --text="Ubuntu has been successfully removed, and Grub has been reinstalled into ${LIST_OF_EXISTING_LINUX[2]}. Thank you for trying Ubuntu, see you soon !"
else
zenity --info --title="Ubuntu a été correctement desinstallé" --text="Ubuntu a été correctement desinstallé, et Grub a été réinstallé dans ${LIST_OF_EXISTING_LINUX[2]}. Merci d'avoir essayé Ubuntu, et à bientot !"
fi
else
##If we uninstall the 2nd (or 3rd or 4th..) distribution of the list, we reinstall GRUB into the 1st one
##Erases the partition chosen by the user
sudo umount $choice ;
sudo mount $choice /mnt;
sudo rm -r /mnt/*
sudo umount $choice ;
##Reinstalls GRUB into the 1st Linux partition
sudo mount ${LIST_OF_EXISTING_LINUX[1]} /mnt;
sudo grub-install --root-directory=/mnt /dev/sda
sudo umount ${LIST_OF_EXISTING_LINUX[1]}
if [ "$language" = "English" ]; then
zenity --info --title="Ubuntu has been successfully uninstalled" --text="Ubuntu has been successfully removed, and Grub has been reinstalled into ${LIST_OF_EXISTING_LINUX[1]}. Thank you for trying Ubuntu, see you soon !"
else
zenity --info --title="Ubuntu a été correctement desinstallé" --text="Ubuntu a été correctement desinstallé, et Grub a été réinstallé dans ${LIST_OF_EXISTING_LINUX[1]}. Merci d'avoir essayé Ubuntu, et à bientot !"
fi
fi
fi
fi
}
#___________________________________________________________________________________________________________________________________________________________________
## BEGINNING OF SCRIPT
### LANGUAGE MENU
language=$(zenity --list --title="English / French ?" --text="This is a temporary menu. For testing purpose only." --column="" "English" "Français") || exit 102;
if [ "$language" = "English" ]; then
( uninstall_linux $language ) | zenity --progress --auto-close --title="Welcome" --text="Scanning systems" --percentage=0
else
( uninstall_linux $language ) | zenity --progress --auto-close --title="Bienvenue" --text="Détection des systèmes" --percentage=0
fi
echo "End of script."
exit 0