Home > Centos, Linux, Sysadmin > Reinstall CentOS using grub

Reinstall CentOS using grub

December 31st, 2009 Leave a comment Go to comments

This post is here mainly because I always forget how to do it. This is one of the simplest ways to reinstall a Centos (will probably work for RHEL and maybe even Fedora) system without needing PXE or physical access to the machine. Make sure that that you have tested you kickstart before you use it and don’t blame me if anything goes wrong.

Save the following script and make it executable then run it. It will ask some questions about networking and hostname and then write a new grub stanza to you grub.conf. It will also download the correct kernel and initrd from the information you have given it and put them in the correct position for grub to find them when it boots.

When you reboot you should be able to select Kickstart Centos and it will boot off the new kernels and pull down the kickstart then reinstall.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash -x
 
echo -n "Enter kickstart url: "
read -e ksurl
 
echo -n "Enter Hostname: "
read -e hostname
echo -n "Enter IP Address: "
read -e ipaddr
echo -n "Enter Gateway: "
read -e gateway
echo -n "Enter Netmask: "
read -e netmask
echo -n "Enter Nameservers: "
read -e nameservers
 
repourl=$(curl $ksurl 2>/dev/null | sed -n 's/.*\(http\)/\1/ p')
#echo $repourl
vmlinuz_url="${repourl}/isolinux/vmlinuz"
initrd_url="${repourl}/isolinux/initrd.img"
 
date_now=$(date +%Y%m%d%H%M%S)
 
grub_stanza="
title Kickstart Centos 5 ${date_now}
        root (hd0,0)
        kernel /reinstall/vmlinuz ksdevice=eth0 load_ramdisk=1 prompt_ramdisk=0 ramdisk_size=16384 serial hostname=${hostname} ip=${ipaddr} gateway=${gateway} netmask=${netmask} dns=${nameservers} noipv6 ks=${ksurl}
        initrd /reinstall/initrd.img
"
 
echo "$grub_stanza"
 
echo -n "Please check the grub stanza above and enter 'y' if it is correct: "
read -e confirmed
 
if [ $confirmed == 'y' ]; then
        echo "Downloading kernel and initrd..."
        mkdir -p /boot/reinstall
        (cd /boot/reinstall/;/usr/bin/urlgrabber $vmlinuz_url )
        (cd /boot/reinstall/;/usr/bin/urlgrabber $initrd_url )
        cp /boot/grub/grub.conf /boot/grub/grub.conf.bak_`date +%Y%m%d%H%M%S`
        echo "$grub_stanza" >> /etc/grub.conf
fi
Categories: Centos, Linux, Sysadmin Tags: , , , ,
  1. November 11th, 2010 at 12:47 | #1

    Hi,

    Great script! One suggested modification would be to change:
    repourl=$(curl $ksurl 2>/dev/null | sed -n ‘s/.*\(http\)/\1/ p’)
    to:
    repourl=$(curl $ksurl 2>/dev/null | sed -n ‘s/^url.*\(http\)/\1/ p’)

    That way config files that contain other URLs (in ours we have %ksappend http://) won’t interfere with the script getting the proper repo URL.

  2. andrew
    December 29th, 2010 at 17:43 | #2

    Instead of starting a kickstart install, is it possible to start a VNC install ? I am always considering a easiest way of installing Centos remotely.

  3. December 29th, 2010 at 18:01 | #3

    @andrew
    I don’t see why not although I haven’t tried it before. Take a look at the bottom of the anaconda documentation at http://fedoraproject.org/wiki/Anaconda/Options and the Kickstart docs http://fedoraproject.org/wiki/Anaconda/Kickstart . Both of them have methode for enabling VNC and should work depending on your use case. I think you will get a VNC connection earlier in the boot process with the Anaconda one which may enable you to see issues that may occur before the kickstart file is retrieved. Please come back and let us know how you get on.

  1. No trackbacks yet.

Spam Protection by WP-SpamFree