Re: CentOS upgrade - access to chroot in %pre?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Cobbler includes a pre-script for backing up ssh host keys durian an install.  The script does the work of searching for your root partition in a less complicated way than the python example.  I'll attach the script in case it's a helpful example.

Bryan Schneiders
bschneiders@xxxxxxxx
301-562-1900 ext 305


Shabazian, Chip wrote:
If you will have access to the network before and during the upgrade, you could probably gather the partition/LVM information before the upgrade, post it somewhere, wget it in the %pre and use it to mount your drives. I haven’t tried any of this, but common sense tells me it should be doable.

*From:* kickstart-list-bounces@xxxxxxxxxx [mailto:kickstart-list-bounces@xxxxxxxxxx] *On Behalf Of *Matt Rose
*Sent:* Tuesday, August 25, 2009 11:27 AM
*To:* Discussion list about Kickstart
*Subject:* Re: CentOS upgrade - access to chroot in %pre?

quick, non-tested sample code to give you an idea, the following *WILL NOT* work on your machine, unless you're really lucky and have your root partition as the second partition on the first hard drive.

Even if it does work, it won't really do anything.

%pre --interpreter /usr/bin/python --erroronfail --log=/tmp/kspre.log
import sys,os,time,shutil,parted,re,subprocess,zipfile
sys.path.append("/usr/lib/anaconda")
import isys,iutil

### This finds the first hard drive
drive = isys.hardDriveDict().keys()[0]

### The following will mount the second partition on the first harddrive as /tmp/sysimage
rootloc = "/tmp/sysimage"
os.makedirs(rootloc)
rootdrive = "/dev/%s2" % drive
isys.mount(rootdrive,rootloc,fstype="ext3")

###The following does some work to set up a somewhat sane chroot environment
os.system("chmod 644 /dev/null")
isys.mount("/dev","/tmp/sysimage/dev",fstype="tmpfs")
isys.mount("/proc","/tmp/sysimage/proc",fstype="proc")
isys.mount("/selinux","/tmp/sysimage/selinux",fstype="selinuxfs")

###The following function lets you open up subprocesses in the chroot environment
def chroot():
    os.chroot(rootloc)

###The following runs an rpm command to check the release in the chroot
subprocess.Popen("rpm -qa *release",preexec_fn=chroot,shell=True,stdout=subprocess.PIPE).stdout.read()

###Cleanup
isys.umount("/tmp/sysimage/dev")
isys.umount("/tmp/sysimage/proc")
isys.umount("/tmp/sysimage/selinux")
isys.umount("/tmp/sysimage")





Matt Rose wrote:

Yes, on upgrade, but it's not easy. You have to detect and mount the partitions by hand. If you know some python, you can include the anaconda libs and helper functions, but if you're just using sh, you'd have one hell of a %pre script.

Matt

Moray Henderson (ICT) wrote:

Hi everyone,

Is it possible to make the /mnt/sysimage/... chroot available to the %pre script of a CentOS 5.3 upgrade?

Moray.

"To err is human.  To purr, feline"

_______________________________________________

Kickstart-list mailing list

Kickstart-list@xxxxxxxxxx <mailto:Kickstart-list@xxxxxxxxxx>

https://www.redhat.com/mailman/listinfo/kickstart-list


------------------------------------------------------------------------

_______________________________________________
Kickstart-list mailing list
Kickstart-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/kickstart-list
#raw
# Nifty trick to restore ssh keys without using a nochroot %post

echo "Saving ssh host keys..." > /dev/ttyS0

keys_found=no

insmod /lib/jbd.o
insmod /lib/ext3.o

mkdir -p /tmp/ssh

drives=$(list-harddrives | awk '{print $1}')
for disk in $drives; do
    DISKS="$DISKS $(fdisk -l /dev/$disk | awk '/^\/dev/{print $1}')"
done

# Try to find the keys on ordinary partitions
for disk in $DISKS; do
    name=$(basename $disk)
    mkdir -p /tmp/$name
    mount $disk /tmp/$name
    [ $? -eq 0 ] || continue # Skip to the next partition if the mount fails

    # Copy current ssh host keys out to be reused
    if [ -d /tmp/${name}/etc/ssh ]; then
        cp -a /tmp/${name}/etc/ssh/ssh_host* /tmp/ssh
        keys_found="yes"
        umount /tmp/$name
        break
    fi
    umount /tmp/$name
    rm -r /tmp/$name
done

# Try LVM if that didn't work
if [ "$keys_found" = "no" ]; then
    lvm lvmdiskscan
    vgs=$(lvm vgs | tail -n +2 | awk '{ print $1 }')
    for vg in $vgs; do
        # Activate any VG we found
        lvm vgchange -ay $vg
    done
    
    lvs=$(lvm lvs | tail -n +2 | awk '{ print "/dev/" $2 "/" $1 }')
    for lv in $lvs; do
        tmpdir=$(mktemp -d findkeys.XXXXXX)
        mkdir -p /tmp/${tmpdir}
        mount $lv /tmp/${tmpdir} || continue # Skip to next volume if this fails

        # Let's see if the keys are in there
        if [ -d /tmp/${tmpdir}/etc/ssh ]; then
            cp -a /tmp/${tmpdir}/etc/ssh/ssh_host* /tmp/ssh/
            keys_found="yes"
            umount /tmp/${tmpdir}
            break # We're done!
        fi
        umount /tmp/${tmpdir}
        rm -r /tmp/${tmpdir}
    done
    
    # And clean up..
    for vg in $vgs; do
        lvm vgchange -an $vg
    done
fi

# Loop until the ssh rpm is installed
if [ "$keys_found" = "yes" ]; then
    while : ; do
        sleep 10
        if [ -d /mnt/sysimage/etc/ssh ] ; then
            cp -f /tmp/ssh/ssh_host* /mnt/sysimage/etc/ssh/
            logger "SSH-HOST-KEY copied to newly installed system"
            break
        fi
    done &
fi
#end
_______________________________________________
Kickstart-list mailing list
Kickstart-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/kickstart-list

[Index of Archives]     [Red Hat General]     [CentOS Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux