The Blog

Restoring Linux System from Backup Posted on

Terminal Commands

Mount the partition your Ubuntu Installation is on. If you are not sure which it is, launch GParted (included in the Live CD) and find out. It is usually a EXT4 Partition. Replace the XY with the drive letter, and partition number, for example: sudo mount /dev/sda1 /mnt.

sudo mount /dev/sdXY /mnt

Now bind the directories that grub needs access to to detect other operating systems, like so.

Now we jump into that using chroot.

Now install, check, and update grub.

This time you only need to add the drive letter (usually a) to replace X, for example: grub-install /dev/sda, grub-install –recheck /dev/sda.

grub-install /dev/sdX
grub-install --recheck /dev/sdX

Now grub is back, all that is left is to exit the chrooted system and unmount everything.

Shut down and turn your computer back on, and you will be met with the default Grub2 screen.

You may want to update grub or re-install burg however you like it.

Congratulations, you have just Repaired/Restored/Reinstalled Grub 2 with a Ubuntu Live CD!

Type at the command prompt

tar xvzf file-1.0.tar.gz – for a gzip compress tar file (.tgz or .tar.gz)
tar xvjf file-1.0.tar.bz2 – for a bzip2 compressed tar file (.tbz or .tar.bz2)
tar xvf file-1.0.tar – for uncompressed tar file (.tar)

HOWTO: Restore Grub2

Let’s reinstall Grub2 for whatever reason. Maybe you reinstalled Windows and lost Grub2 or you just plain old did something silly and lost Grub2. Remember, in Sabayon we are using Grub2 so it’s a bit different than Legacy Grub. It’s still pretty painless though and with about 5 minutes of time you will have your Grub2 reinstalled.

 

Quick Recovery

As outlined in the Gentoo Grub2 wiki page – Recovery section you can simply reinstall Grub2 to the MBR (Master Boot Record), linking to your original /boot location.

So, assuming your root partition is located on /dev/sda3 and you have a separate /boot on /dev/sda1 you would do the following:
(If you do not have a separate boot partition you do not need to do the second mount.)

# mkdir -p /mnt/new_linux/
# mount /dev/sda3 /mnt/new_linux
# mount /dev/sda1 /mnt/new_linux/boot <- IMPORTANT on Ubuntu 12.xx
# grub-install --root-directory=/mnt/new_linux /dev/sda

This tells Grub2 to install itself to the MBR on the /dev/sda device linking to the config found in /mnt/new_linux/boot.
You can then reboot and Grub2 will be back to normal – there’s no need to re-create your config (see below) as it should still be undisturbed in your /boot directory.

Completely Reinstall and Re-Configure Grub2

From the article Chroot from a livecd in the Gentoo Wiki we can learn how to chroot into our system. So boot up the livedvd or other live disk, and get to a terminal, command line or konsole.

Make sure the livedvd you use is for the same architecture as the architecture of the installation on the hard disk, i.e. to chroot to a 64-bit installation (e.g. amd64) you cannot use a 32-bit livedvd (e.g. x86), and vice versa. You will see an error message “chroot: cannot run command `/bin/bash’: Exec format error” if you try to chroot from a livedvd of a different architecture.

First a quick note on mounting filesystems. While it’s possible to mount filesystems from within the chrooted system, this is not recommended. The reason for this is that the livecd environment won’t know about these mounted systems, so if they are forgotten about and left mounted, they will not be unmounted properly when the system shuts down, which could cause damage to the filesystems on those mounts.

Mount the root partition (and, if you have /boot on a separate partition, mount that too) of the installed system. If separate partitions are used for other areas of the system (for example, a separate partition for /var/log) then these will also need to be mounted.

In the following example, /dev/hda1 is the /boot partition and /dev/hda3 is the root partition. Obviously replace those with the device names for your boot partition (if you have one) and your root partition. If your partition names are of the form /dev/sd<letter><number> rather than /dev/hd<letter><number> then obviously use that form instead. Obviously, if you do not have /boot on a separate partition to / (root) then you should omit the mount and umount commands referring to /boot.

Additionally, mount the /dev, /sysfs and /proc filesystems so that they can be used by the chrooted environment.

# mkdir -p /mnt/sabayon/boot
# mount /dev/hda3 /mnt/sabayon
# mount /dev/hda1 /mnt/sabayon/boot
# mount -t proc none /mnt/sabayon/proc
# mount -t sysfs sys /mnt/sabayon/sys
# mount -o bind /dev /mnt/sabayon/dev

So now we are set to enter into our installed system:

# chroot /mnt/sabayon /bin/bash
# env-update
# source /etc/profile
# export PS1="(chroot) $PS1"

You should then end up with the following prompt:

(chroot) #

Grub2 reads the /etc/mtab file to ascertain the filesystems that are currently mounted, so /etc/mtab must be up-to-date. If you do not have a separate boot partition then update /etc/mtab using the following command:

(chroot) # grep -v rootfs /proc/mounts > /etc/mtab

or the following command if you do have a separate boot partition:

(chroot) # cp /proc/mounts /etc/mtab

To install Grub2 with the Grub2 Stage 1 code written to the MBR of hda, use the following command:

(chroot) # grub2-install /dev/hda

or the following command if you want to install Grub2 with the Grub2 Stage 1 code written to the first sector of a separate boot partition hda1 (i.e. no Grub2 code written to the MBR):

(chroot) # grub-install /dev/hda1

It should do its thing and it should tell you it has finished installing it.
grub2-install does not create/recreate the grub.cfg file (the equivalent to Grub Legacy’s grub.conf/menu.lst file), so use the following command to create/recreate the grub.cfg file:

(chroot) # grub-mkconfig -o /boot/grub/grub.cfg

Now you can exit from the chroot environment and unmount the drives:

(chroot) # exit
# umount /mnt/sabayon/boot /mnt/sabayon/dev /mnt/sabayon/proc /mnt/sabayon

Now you can reboot to Grub2.
You can find more information in the article Grub2 in the Gentoo Wiki.

 

This entry was posted in Uncategorized. Bookmark the permalink.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *



CAPTCHA
Change the CAPTCHA codeSpeak the CAPTCHA code