úterý 5. dubna 2016

CentOS: change the system drive

Let's have a standard installation of a recent CentOS (6.5, Jan 2014).

The disk layout (created by the installation script) is

[root@server]# fdisk /dev/sdb
Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00009e69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2              64       14594   116707328   8e  Linux LVM

[root@server]#  mount |grep ^/dev
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
/dev/sdb1 on /boot type ext4 (rw)
/dev/mapper/VolGroup-lv_home on /home type ext4 (rw)

However, firstly I have to replace the system disk, and secondly, I do not like LVM .

1. Initialize the new disc (/dev/sdc) : clone the boot sector (MBR) and /boot partition (it contains some EFI stuff and grub etc...)
dd if/dev/sdb of=/dev/sdc bs=512000 count=520

2. fdisk /dev/sdc
delete partiton 2 and create properly aligned swap, root and home partitions, format as ext4 (or other) fs

3. mount /dev/sdc3 /mnt/3 && mount /dev/sdc4 /mnt/4 && mount /dev/sdc1 /mnt/3/boot
rsync -avPx / /mnt/3 && rsync -avPx /home /mnt/4

4. get uuid of new partitions
blkid /dev/sdc?
and replace corresponding partitions in fstab on the new disk
vim /mnt/3/etc/fstab
UUID="414....."    /      ext4 defaults 1 1
UUID="123-..."    /boot   ext4 defaults 1 2
UUID="456-..."    swap   swap  defaults 0 0
UUID="789-..."    /home   ext4 defaults 1 2
..
4. change root partition (use UUID of the new root partition) in grub.conf and clean up the kernel command line:
vim /mnt/3/boot/grub/grub.conf

original line:
        kernel /vmlinuz-2.6.32-431.5.1.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root nomodeset rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

new line:
        kernel /vmlinuz-2.6.32-431.5.1.el6.x86_64 ro root=UUID="759c2224-3881-4cca-a14b-f0baf771084f" nomodeset  LANG=en_US.UTF-8 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us
(no "redhat graphical boot" (rhgb), no DM raid, no MD raid, no LUKS encryption)

5. VERY IMPORTANT

touch /mnt/3/.autorelabel
(flag .autorelabel in root of the new fs forces SELINUX to relabel the filesystem.)
Otherwise login attempts on the new system  would fail with "selinux is preventing /bin/login ..."