Change hostname on RHEL 6/CentOS 6

20151224013419As part of my current job I have to deploy VMs for new and existing customers all the time. Most of the work is handled by automation, using a combination of PowerCLI and SaltStack. Sometimes, when VMs get stuck or fail to connect online for whatever reason I have to manually configure them. Changing the hostname is one of those manual steps. As I don’t do it manually often enough I sometimes forget some of the steps. So, as reminder to myself, here are the steps to change the hostname on RHEL 6 or CentOS 6:

The Steps

1. The first step is to run the hostname command followed by the new server name and hit Enter:

[root@web01 ~]# hostname web02

2. As you can see from the above the current hostname was web01, I changed it to web02. Running the hostname command on it’s own will show you the current and new hostname of web02:

[root@web01 ~]# hostname
web02

Changing the hostname using the method above does not permanently change the hostname, in other words if you reboot the server the changes will not survive, the hostname will revert back to its previous name. To make the hostname change permanent continue to step 3 below.

3. The next step is to update the /etc/sysconfig/network file by opening it with vi or any other editor of your choice:

[root@web01 ~]# vi /etc/sysconfig/network

4. Within the file you will see something like the below:

NETWORKING=yes
HOSTNAME=web01

Simply change the hostname to the new name, in my case it is web02:

NETWORKING=yes
HOSTNAME=web02

Exit and save the changes to the file.

5. We have one final file to update, which is /etc/hosts. Edit it vi:

[root@web01 ~]# vi /etc/hosts

You should see a line with the server’s static IP followed by the hostname, per the below:

127.0.0.1   localhost
172.16.0.50     web01

Once again, change it to reflect the new desired hostname, in my case web02:

127.0.0.1   localhost
172.16.0.50     web02

Exit and save the changes to the file.

6. To commit the changes made in the network and hosts files above in steps 4 & 5 respectively you can either reboot the server or restart the network services. If it’s a production system and you want to avoid any downtime restart the network services by running:

[root@web01 ~]# service network restart

After restarting the network services the operating system will use the new hostname.

Shrink an ext4 filesystem in RHEL 6/Centos 6

In a previous post I demonstrated how to increase the size of an ext4 filesystem in RHEL 6/CentOS 6. In this post I will demonstrate how to shrink or reduce the size of an ext4 filesystem in RHEL 6/CentOS 6. As part of this shrink process I will also remove one of the disks in the Volume Group that the filesystem we are shrinking is part of.

My Environment

Let me briefly confirm the environment I am using for this demo.  My server is a VMware VM and the OS is Centos 6.6, it presently contains 5 virtual disks:

20150614181018

All the virtual disks are in use and the system comprises multiple filesystems. We are going to use the filesystem /var/video to demonstrate how to shrink a filesystem.

The Shrink Process

Please note that you should stop any applications first and then unmount the filesystem before proceeding with the steps below. I would also ensure that you back up the system prior to any shrinking activity.

The process required to reduce a filesystem starts at the top layer, with the filesystem itself. It must be reduced, then the Logical Volume is reduced. If required the Volume Group can also be reduced if the ultimate goal is to remove Physical Volumes from the system.  The order of this process is shown below:

Filesystems --> Logical Volumes --> Volume Groups --> Physical Volumes

So without further ado, let’s crack on with the shrinking process.

1. Check the current size of the filesystem by running df -h:

[root@lnx-svr-01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  908M   16G   6% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot
/dev/mapper/data-music
                      9.8G  4.4G  4.9G  48% /var/music
/dev/mapper/data-video
                      9.8G  4.4G  4.9G  47% /var/video
/dev/mapper/data-pictures
                      4.8G   10M  4.6G   1% /var/pictures
[root@lnx-svr-01 ~]#

To see the usage in Megabytes run df -m:

[root@lnx-svr-01 ~]# df -m
Filesystem           1M-blocks  Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                         17519   908     15715   6% /
tmpfs                      939     0       939   0% /dev/shm
/dev/sda1                  477    25       427   6% /boot
/dev/mapper/data-music
                          9952  4441      4999  48% /var/music
/dev/mapper/data-pictures
                          4888    10      4624   1% /var/pictures
/dev/mapper/data-video
                          9944  4430      5003  47% /var/video
[root@lnx-svr-01 ~]#

We can see that /var/video is 9.8GB in size and is using 47% of its total capacity. We want to reduce /var/video down to 4.5GB in size.

2. Run e2fsk first to perform a filesystem check:

[root@lnx-svr-01 ~]# e2fsck -f /dev/data/video
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/data/video: 12/655360 files (0.0% non-contiguous), 1207792/2619392 blocks

3. Run resize2fs to shrink the filesystem to your desired size:

[root@lnx-svr-01 ~]# resize2fs /dev/data/video 4800M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/data/video to 1228800 (4k) blocks.
The filesystem on /dev/data/video is now 1228800 blocks long.

I have resized the device /dev/data/video to 4800MB. To change the unit of size to Gigabytes change M to G.

4. Run lvreduce to shrink the Logical Volume to a size slightly larger than the filesystem. In step 3 we shrunk the filesystem to 4800MB so we will shrink the Logical Volume video to a slightly larger size. We will therefore reduce it to 4900MB:

[root@lnx-svr-01 ~]# lvreduce -L 4900M /dev/data/video
  WARNING: Reducing active logical volume to 4.79 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce video? [y/n]: y
  Size of logical volume data/video changed from 9.99 GiB (2558 extents) to 4.79 GiB (1225 extents).
  Logical volume video successfully resized
[root@lnx-svr-01 ~]#

5. So the shrink process is complete. Let’s mount the filesystem:

[root@lnx-svr-01 ~]# mount /var/video/

6. Now run df -h to see the new size of the /var/video filesystem:

[root@lnx-svr-01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  908M   16G   6% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot
/dev/mapper/data-music
                      9.8G  4.4G  4.9G  48% /var/music
/dev/mapper/data-pictures
                      4.8G   10M  4.6G   1% /var/pictures
/dev/mapper/data-video
                      4.5G  4.4G     0 100% /var/video
[root@lnx-svr-01 ~]#

7. We know that we have reduced the Logical Volume video from 9.8GB to 4.5GB, so we now have 5.3GB of free space in the Volume Group data. Run vgdisplay to confirm the amount of free space in the Volume Group data:

[root@lnx-svr-01 ~]# vgdisplay /dev/data
  --- Volume group ---
  VG Name               data
  System ID
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               24.97 GiB
  PE Size               4.00 MiB
  Total PE              6392
  Alloc PE / Size       5059 / 19.76 GiB
  Free  PE / Size       1333 / 5.21 GiB
  VG UUID               IglQep-hydn-lm2D-Dc1N-LsL4-edpi-BzgMuE

[root@lnx-svr-01 ~]#

We can see that there are 1333 extents or 5.21GB of free space in the Volume Group data.

8. Run pvdisplay to show all Physical Volumes on the system:

[root@lnx-svr-01 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               2
  Allocated PE          1276
  PV UUID               OpA0C8-wv7K-0cZT-3zHW-GXeb-YLM8-hWbRjL

  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               data
  PV Size               10.00 GiB / not usable 4.70 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2558
  Free PE               0
  Allocated PE          2558
  PV UUID               VC7As8-pB3U-GGqa-l0xg-NIVj-jv7l-dTGGHl

  --- Physical volume ---
  PV Name               /dev/sdd1
  VG Name               data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               53
  Allocated PE          1225
  PV UUID               xKOs8i-UQy1-mpvu-8Hhl-zjF4-FYjb-w4DwSu

  --- Physical volume ---
  PV Name               /dev/sde1
  VG Name               data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               1278
  Allocated PE          0
  PV UUID               s1XEBf-PCbp-fygl-yVc0-BySI-32FM-mu3laE

  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               tx5fGZ-LURm-Bqgq-Jxqz-AuJD-IWgl-YMxPle

[root@lnx-svr-01 ~]#

We can see that Volume Group data comprises of /dev/sdb1/dev/sde1. The only Physical Volume that has no data on it is /dev/sde1. We know this because it has no Allocated PEs (Physical Extents) and also that the Free PE equals = Total PE, meaning that all the extents that comprise /dev/sde1 are free:

Total PE              1278
Free PE               1278
Allocated PE          0

9. So if no data actually resides on Physical Volume /dev/sde1 we we need to remove it from Volume Group data. To do this run vgreduce per the below:

[root@lnx-svr-01 ~]# vgreduce data /dev/sde1
  Removed "/dev/sde1" from volume group "data"
[root@lnx-svr-01 ~]#

10. To remove Physical Volume /dev/sde1 run pvremove per the below:

[root@lnx-svr-01 ~]# pvremove /dev/sde1
  Labels on physical volume "/dev/sde1" successfully wiped
[root@lnx-svr-01 ~]#

So we are at a stage where the virtual disk that maps to /dev/sde can be removed. Although I know that Hard Disk 5 maps to /dev/sde (because I added it in a previous post) that doesn’t mean that the order of disks in Linux matches up with the order of disks in VMware – often they don’t match up.

11. To verify the virtual disk to linux disk mapping run the ls command per the below to list all the disks in the system:

[root@lnx-svr-01 video]# ls -l /sys/block/sd*
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sda -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:0/end_device-2:0/target2:0:0/2:0:0:0/block/sda
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sdb -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:1/end_device-2:1/target2:0:1/2:0:1:0/block/sdb
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sdc -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:2/end_device-2:2/target2:0:2/2:0:2:0/block/sdc
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sdd -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:3/end_device-2:3/target2:0:3/2:0:3:0/block/sdd
lrwxrwxrwx. 1 root root 0 Jun 14 18:05 /sys/block/sde -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:4/end_device-2:4/target2:0:4/2:0:4:0/block/sde
[root@lnx-svr-01 video]#

From the above we see the bottom line relates to disk /sde. The important bit is at the end of the line; 2:0:4:0/block/sde. The numbers highlighted in red represent the SCSI-controller-no:Target-no which we require to identify the virtual disk in VMware. This corresponds to Hard Disk 5 in the VM:

20150622134432

So we definitely know that Hard Disk 5 maps to /dev/sde. As it is has been removed from LVM we can remove it from the VM now:

20150611165654

The Happy Ending

Process over, you can breathe a sigh of relief! I will re-iterate that you should ensure that you have up-to-date backups of the system prior to any shrinking activity – you can never be too safe.

Reference(s):
How to Find corresponding VMDK in Linux/UNIX