debian,ubuntu,linux,howtos,manuals,notes,manpages

Convert Red Hat to Debian Squeeze

November 9, 2011
By

=====Convert Red Hat to Debian Squeeze Remotely on a Live system=====

Last week i was confronted with a compromised custom Asterisk (Elastix) based PBX that suffered security issues, both in the configuration, such as easy passwords very lenient permissions, as well as the normal CentOS provided security updates since some software/drivers outside of the native repo’s was installed “yum update” was promissing the old RPM dependency hell.

In short: this PBX needed some love desperately.

I’m sure that someone more familiar with CentOS (red hat) could easily solve these issues and reinstate the existing install into a more stable and secure state, but since i limit myself mainly to Debian based and would like to get rid of the Elastix/FreePBX interface to Asterisk i decided that a fresh Debian install would be needed.

Pretty simple you would say, only there was one minor issue, the machine was about 7,814 kilometers down the road and remote hands were hard to find. Auch!!!!

After wasting about a week to find a solution to this I started looking for some more unconventional solution and bumped into some old HowTo’s how to do it remotely via SSH. It took me a number of runs in a Virtual testing machine but i finally got the required routine down.

The Basic idea is to do the following:
* Disable the SWAP partition.
* Debootstrap Debian into the old SWAP partition.
* Reboot into Debian on the old SWAP partition.
* Copy over the Debian installation to the normal / partition over the existing CentOS install..
* Reboot into Debian on / root.
* Re-enable the SWAP partition.

====Investigate the current situation====

First have a look at the current partition layout issuing the command.
fdisk -l

This gave me the following result
[root@elastix ~]# fdisk -l

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 19457 156183930 8e Linux LVM

fdisk shows a /boot partition on /dev/sda1 and a LVM setup on /dev/sda2.

Lets have look at the LVM setup by issuing the command:
lvdisplay

This shows the following LVM partitions.
[root@elastix ~]# lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/LogVol00
VG Name VolGroup00
LV UUID tt92rH-Aoo9-TQUY-PlpS-4fot-MmzT-qZpkSa
LV Write Access read/write
LV Status available
# open 1
LV Size 147.00 GB
Current LE 4704
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

— Logical volume —
LV Name /dev/VolGroup00/LogVol01
VG Name VolGroup00
LV UUID qTAuIF-oIFN-95xV-C0jd-xmrd-Nf0v-dxe7eI
LV Write Access read/write
LV Status available
# open 1
LV Size 1.94 GB
Current LE 62
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1

Above you see the 147.00 GB / root partition on /dev/VolGroup00/LogVol00
and the 1.94 GB SWAP partition on /dev/VolGroup00/LogVol01.

The 1.94 GB SWAP partition is more then enough to hold the temporary debian minimum install so we will use that space.

First check your running services and the consumed RAM too see if we can safely do without the SWAP, disable unneeded services if needed.

====Remove the SWAP partition====
Disable the SWAP by issuing the following command:
swapoff -a
Too check the current status of the PV holding the LV’s:
lvm pvs
Below you see that there is 148.94G on the Physical Volume and no free space.
[root@elastix ~]# lvm pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup00 lvm2 a- 148.94G 0

Lets remove the LVM partition that holds the SWAP space.
lvm lvremove /dev/VolGroup00/LogVol01
Answer “y” as shown below to remove the active logical volume.
[root@elastix ~]# lvm lvremove /dev/VolGroup00/LogVol01
Do you really want to remove active logical volume LogVol01? [y/n]: y
Logical volume "LogVol01" successfully removed

After removing /dev/VolGroup00/LogVol01 investigate the Physical Volume again.
lvm pvs

Below you see that there is now 1.94G of free space within the Physical Volume.
[root@elastix ~]# lvm pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup00 lvm2 a- 148.94G 1.94G

====Resize the Physical Volume====
Now we can resize the Physical Volume to the size of the VolGroup00 partition and thus make the 1.94G available for use.
lvm pvresize /dev/sda2 --setphysicalvolumesize 148.94G
This will give the confirmation that the Physical Volume is resized
[root@elastix ~]# lvm pvresize /dev/sda2 --setphysicalvolumesize 148.94G
Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

Lets have another look at the Physical Volume using the cylinder size of the partition.
lvm pvs --units s

This will show as below:
[root@elastix ~]# lvm pvs --units s
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup00 lvm2 a- 312344576S 4063232S

Lets have a look with parted to see the cylinder layout on /dev/sda.
parted /dev/sda unit s print

[root@elastix ~]# parted /dev/sda unit s print

Model: ATA Hitachi HTE72321 (scsi)
Disk /dev/sda: 312581807s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 63s 208844s 208782s primary ext3 boot
2 208845s 312576704s 312367860s primary lvm

Information: Don’t forget to update /etc/fstab, if necessary.

====Resize the /dev/sda2 partition====

Too resize the /dev/sda2 partition too fit exactly the Physical Volume we already made smaller and be able to use the free space, we first remove the entire LVM partition and recreate it later with a smaller size.

To remove the LVM partition issue the command:
parted /dev/sda rm 2

[root@elastix ~]# parted /dev/sda rm 2
Information: Don't forget to update /etc/fstab, if necessary.

And the recreate it, Note that the Start cylinder needs to be the same as the one removed earlier and the End cylinder is the same as the old one MINUS the free cylinders that were show earlier by “lvm pvs –units s”
parted /dev/sda mkpart primary 208845s 308513472s
Below shows no errors so it all seems to be fine.
/dev/sda2 VolGroup00 lvm2 a- 312344576S 4063232S
[root@elastix ~]# parted /dev/sda mkpart primary 208845s 308513472s
Information: Don't forget to update /etc/fstab, if necessary.

Set the LVM flag on the recreated LVM partition.
parted /dev/sda set 2 lvm on

To check the new state of the partition layout on /dev/sda with parted issue the command:
parted /dev/sda print
This will show the following result:
[root@elastix ~]# parted /dev/sda print

Model: ATA Hitachi HTE72321 (scsi)
Disk /dev/sda: 160GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 107MB 107MB primary ext3 boot
2 107MB 158GB 158GB primary lvm

And to have a look with fdisk
fdisk -l /dev/sda
[root@elastix ~]# fdisk -l /dev/sda

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 19205 154152314 8e Linux LVM

We are not ready yet, to make the resizing of the Physical Volume final issue the command:
pvresize /dev/sda2
====Create a new partition in the free space====

We will create a new partition to hold the temporary debian install.
Start up fdisk on /dev/sda:
fdisk /dev/sda
Its never a bad thing to Print the current layout by issuing “p”
Command (m for help): p
Instigate a New partition with “n”
Command (m for help): n
Select “p” to make it a Primary partition
Command action
e extended
p primary partition (1-4)

We alreday have sda1 and sda2, so lets select “3″
Partition number (1-4): 3
Press “enter” to have it start on the first available cylinder
First cylinder (19205-19457, default 19205):
Using default value 19205

And press “enter” to have it end on the last available cylinder
Last cylinder or +size or +sizeM or +sizeK (19205-19457, default 19457):
Using default value 19457

Now we want to select what Type our new partition should become with “t”
Command (m for help): t
Select the partition number we want change the type for with “3″
Partition number (1-4): 3
And use “83″ to make it a linux type partition
Hex code (type L to list codes): 83
To Write the new partition layout to disk use “w”
Command (m for help): w
Since /dev/VolGroup00/LogVol01 is deleted from the disk and no longer available in the partition layout we remove it from fstab.
nano /etc/fstab
And make fstab to look like below:
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0

To finalize the new partition layout the disk has to be remounted, and since we can not unmount the / partition the system is running on we need to reboot.
reboot
And wait a while for it to reboot and log back in via SSH.
Yes, i know. Some sweat started to break out waiting for SSH to welcome you back in.

====Create a debootstrap RPM on another debian machine===

On a seperate Debian machine we first install alien
apt-get install alien

Get a debootstrap .deb for squeeze
wget http://ftp.nl.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.26+squeeze1_all.deb
Use alien to convert the .deb to a .rpm
alien -rkv debootstrap*.deb
Copy over the .rpm to the system we are converting
scp debootstrap-1.0.26+squeeze1-1.noarch.rpm root@192.168.0.99:/usr/local/src/

====debootstrap Debian to the new partition===
Now that the new partition is ready to be used, we first create a new file system on it.

mke2fs -j /dev/sda3

Then we need to mount it but first we create an easy alias for the new mount.
export ASD=/mnt/asd
Create the location for the new mount
mkdir -p $ASD

And mount /dev/sda3
mount /dev/sda3 $ASD
Lets install the debootstrap rpm we cerated earlier and move to the location we copied it to, in our case:
cd /usr/local/src/
And install it
rpm -Uvh debootstrap*

Before we use debootstrap we need to install binutils, otherwise debootstrap can not unpack the .debs
yum install binutils
And finally debootstrap debian into our mounted /dev/sda3, always check which mirror to use because this could take a while if you have a slow far away mirror.
/usr/sbin/debootstrap --arch i386 squeeze $ASD http://mirrors.geekbone.org/debian

After debootstrap is finished we re-use some old configs, first copy over the resolv.conf file
cp /etc/resolv.conf $ASD/etc/
And then copy the hosts file
cp /etc/hosts $ASD/etc/

Create a hostname file
nano $ASD/etc/hostname
And use for example the hostname
voip

Now we change our root to the new debian system
chroot $ASD /usr/bin/env -i HOME=/root TERM=$TERM PS1='u:w$ ' PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/bash --login

Here we create the fstab file for the partitions we want mounted.
nano /etc/fstab

# filesystem mount fs-type options dump fsck-order

/dev/sda3 / auto defaults 0 1
proc /proc proc defaults 0 0

After that we mount the proc file system
mount -t proc proc /proc

hostname voip.example.com

Create our network details to be used when the new system boots
nano /etc/network/interfaces

# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or
# /usr/share/doc/ifupdown/examples for more information.

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.0.99
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 192.168.0.1

dpkg-reconfigure passwd

Set a password for root, without it we could not login remotely anymore
passwd

Install the locales package
apt-get install locales
And reconfigure the locales to be used
dpkg-reconfigure locales
Select en_US.UTF-8 UTF-8 to be installed and used as default.

After that we set up our time zone
dpkg-reconfigure tzdata

Set up the sources to be used with apt-get
nano /etc/apt/sources.list

deb http://mirrors.geekbone.org/debian/ squeeze main contrib non-free
deb-src http://mirrors.geekbone.org/debian/ squeeze main contrib non-free

deb http://security.debian.org/ squeeze/updates main contrib non-free
deb-src http://security.debian.org/ squeeze/updates main contrib non-free

update apt’s index with new repositories
apt-get update

And install ssh
apt-get install openssh-server

Check your system and find a matching kernel to be used
apt-cache search linux-image

In my case on this 32 bit machine, the often used 686 version
apt-get install linux-image-2.6-686
After the new kernel is installed we logout of the chroot
logout

And we copy over the newly installed kernel images to the /boot partition
cp $ASD/boot/vmlinuz-* /boot
cp $ASD/boot/initrd.img-* /boot

Now we need to update grub to use our debian kernel and partition on the next time it boots
nano /boot/grub/menu.lst

Mind that the boot stanza second from the top seems to be the default booted
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
# initrd /initrd-version.img
#boot=/dev/sda
default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
#hiddenmenu
title Elastix (2.6.18-164.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-164.el5
module /vmlinuz-2.6.18-164.el5xen ro root=/dev/VolGroup00/LogVol00
module /initrd-2.6.18-164.el5xen.img
title Debian!
root (hd0,2)
kernel /boot/vmlinuz-2.6.32-5-686 root=/dev/sda3 ro
initrd /boot/initrd.img-2.6.32-5-686
title Elastix-base (2.6.18-164.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-164.el5 ro root=/dev/VolGroup00/LogVol00
initrd /initrd-2.6.18-164.el5.img

After grub is updated, we unmount the mounted /dev/sda3
umount $ASD/proc
umount $ASD

Its time to hold your breath again and hope you did everything OK
reboot

====Prepare the final system for use====
If all went fine we have just booted in our new Debian environment on /dev/sda3.

Create a new file system on /dev/sda1, i like to use ext4 for this.

mkfs.ext4 /dev/sda1

Create a temporary /boot location
mkdir /boot2

And mount /dev/sda1 on there
mount /dev/sda1 /boot2
Copy over all from the old temporary boot location to the new temporary boot location
cp /boot/* /boot2/

Unmount /dev/sda1 again and remove the boot2 placeholder and the old temporary /boot
umount /boot2
rmdir /boot2
rm -rf /boot/*

nano /etc/fstab
Set up the new /boot location to be used on the next boot
# filesystem mount fs-type options dump fsck-order

/dev/sda1 /boot ext4 defaults 1 2
/dev/sda3 / auto defaults 0 1
proc /proc proc defaults 0 0

Mount the /boot partition
mount -a

Lets install grub2
apt-get install grub2

and make sure you choose to install it on the MBR of /dev/sda

Now lets prepare our new and final / root partition.
First we create a new filesystem on /dev/sda2
mkfs.ext4 /dev/sda2

And the use dd to copy over the running temporary debian system to its final location
dd if=/dev/sda3 of=/dev/sda2 bs=1024
Issue a filecheck on /dev/sda2 to be sure
e2fsck -f -y /dev/sda2
And resize it
resize2fs -p /dev/sda2
And then another file system check
e2fsck -f -y /dev/sda2

Besides using a new /boot location on the next boot we also want our / root to be on /dev/sda2
nano /etc/fstab

# filesystem mount fs-type options dump fsck-order

/dev/sda1 /boot ext4 defaults 1 2
/dev/sda2 / auto defaults 0 1
proc /proc proc defaults 0 0

Its time to update grub2 to reflect the new situation
update-grub

Leave a Reply

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

*