Sunday, 1 November 2015

Reset root password by accessing file system on Guest OS from Physical host - CentOS

Everyone know as how to reset your forgotten root password on Linux - (, but in this article since I'm using Guest OS on KVM hypervisor, I would demonstrate as how to mount the root file system and reset password.


Hostname: kvm1
Diskname: vm1
path:     /var/lib/libvirt/images/vm1.img 

- Firstly, you need to shutdown your VM, doing it which it's running can cause disk corruption.
#virsh shutdown vm1

- Check your VM is in shut off state
#virsh list --all

- Get an unused loop device
#losetup -f

- Map VM image to your loop device
#losetup /dev/loop0 /var/lib/libvirt/images/vm1.img

- Print your partition table of the image file which is been mapped to the loop device and identify the correct partition where your root file system mounted upon.
#fdisk -l /dev/loop0

Disk /dev/loop0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000907df

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1   *        2048     1050623      524288   83  Linux
/dev/loop0p2         1050624     3147775     1048576   82  Linux swap / Solaris
/dev/loop0p3         3147776    20971519     8911872   83  Linux

- In order to mount the VM's partitions, you need to create partition mappings
#kpartx -av /dev/loop0
Disk /dev/loop0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000907df

      Device Boot      Start         End      Blocks   Id  System
/dev/loop0p1   *        2048     1050623      524288   83  Linux
/dev/loop0p2         1050624     3147775     1048576   82  Linux swap / Solaris
/dev/loop0p3         3147776    20971519     8911872   83  Linux

- Here my root file system was on /dev/vda3 which is associated with 
/dev/loop0p3 which would be mounted.
#mount /dev/mapper/loop0p3 /mnt

- remove password field from root user in /etc/shadow 
#vim /mnt/etc/shadow

Note: If your system has been enabled with SELinux, it is very much required to autorelabel else you will be unable to login. Check below snap.

#touch /mnt/.autorelabel

- Once done, remove your mappings and start the VM. 
#umount /mnt
#kpartx -dv /dev/loop0
#losetup -d /dev/loop0

- Start your VM 
#virsh start vm1

- In your console, when you login with 'root' without providing any password.

Friday, 11 September 2015

zfs cheat sheet - Creation of Storagepools & Filesystems using zpool & zfs #Solaris 11

The ZFS file system is a file system that fundamentally changes the way file systems are administered, with features and benefits not found in other file systems available today. ZFS is robust, scalable, and easy to administer.ZFS uses the concept of storage pools to manage physical storage, ZFS eliminates volume management altogether. Instead of forcing you to create virtualized volumes, ZFS aggregates devices into a storage pool.File systems are no longer constrained to individual devices, allowing them to share disk space with all file systems in the pool. You no longer need to predetermine the size of a file system, as file systems grow automatically within the disk space allocated to the storage pool. When new storage is added, all file systems within the pool can immediately use the additional disk space without additional work

                     zpool commands                        Description
zpool create testpool c0t0d0Create simple pool named testpool with single disk
creating default mount point as poolname(/testpool)
-n do a dry run on pool creation
-f force creation of the pool
zpool create testpool mirror c0t0d0 c0t0d1Create testpool mirroring c0t0d0 with c0t0d1
creating default mount point as poolname(/testpool)
zpool create -m /mypool testpool c0t0d0Create pool with different mount point than default
zpool create testpool raidz c2t1d0 c2t2d0 c2t3d0Create RAID-Z testpool
zpool add testpool raidz c2t4d0 c2t5d0 c2t6d0Add RAID-Z disks to testpool
zpool create testpool raidz1 c2t1d0 c2t2d0 c2t3d0 c2t4d0 c2t5d0 c2t6d0Create RAIDZ-1 testpool
zpool create testpool raidz2 c2t1d0 c2t2d0 c2t3d0 c2t4d0 c2t5d0 c2t6d0Create RAIDZ-2 testpool
zpool add testpool spare c2t6d0Add spare device to the testpool
zpool create testpool mirror c2t1d0 c2t2d0 mirror c2t3d0 c2t4d0Disk c2t1d0 mirrored with c2t2d0 &
c2t3d0 mirrored with c2t4d0
zpool remove testpool c2t6d0Removes hot spares and cache disks
zpool detach testpool c2t4d0Detach the mirror from the pool
zpool clear testpool c2t4d0Clears specific disk fault
zpool replace testpool c3t4d0Replace disk like disk
zpool replace testpool c3t4d0 c3t5d0Replace one disk with another disk
zpool export testpoolExport the pool from the system
zpool import testpoolImports specific pool
zpool import -f -D -d /testpool testpoolImport destroyed testpool
zpool import testpool newtestpoolImport a pool originally named testpool under
new name newtestpool
zpool import 88746667466648Import pool using ID
zpool offline testpool c2t4d0Offline the disk in the pool
Note: zpool offline testpool -t c2t4d0 will offline temporary
zpool upgrade -aupgrade all pools
zpool upgrade testpoolUpgrade specific pool
zpool status -xHealth status of all pools
zpool status testpoolStatus of pool in verbose mode
zpool get all testpoolLists all the properties of the storage pool
zpool set autoexpand=on testpoolSet the parameter value on the storage pool
Note: zpool get all testpool gives you all the properties
on which it could be used to set value
zpool listLists all pools
zpool list -o name,size,altrootshow properties of the pool
zpool historyDisplays history of the pool
Note: Once the pool is removed, history is removed.
zpool iostat 2 2Display ZFS I/O stastics
zpool destroy testpoolRemoves the storage pool

                       zfs commands                      Description
zfs listLists the ZFS file system's
zfs list -t filesystem
zfs list -t snapshot
zfs list -t volume
zfs create testpool/filesystem1Creates ZFS filesystem on testpool storage
zfs create -o mountpoint=/filesystem1 testpool/filesystem1Different mountpoint created after ZFS creation
zfs rename testpool/filesystem1 testpool/filesystem2Renames the ZFS filesystem
zfs unmount testpoolunmount the storagepool
zfs mount testpoolmounts the storagepool
NFS exports in ZFSzfs share testpool - shares the file system for export
zfs set share.nfs=on testpool - make the share persistent
across reboot
svcs -a nfs/server - NFS server should be online
cat /etc/dfs/dfstab - Exported entry in the file
showmount -e - storage pool has been exported
zfs unshare testpoolRemove NFS exports
zfs destroy -r testpoolDestroy storage pool and all datasets under it
zfs set quota=1G testpool/filesystem1set quota of 1GB on the filesystem1
zfs set reservations=1G testpool/filesystem1set reservation of 1GB on the filesystem1
zfs set mountpoint=legacy testpool/filesystem1Disable ZFS auto mounting and enable
through /etc/vsftab
zfs unmount testpool/filesystem1unmounts ZFS filesystem1 in testpool
zfs mount testpool/filesystem1mounts ZFS filestystem1 in testpool
zfs mount -amounts all the ZFS filesystems
zfs snapshot testpool/filesystem1@fridayCreates a snapshot of the filesystem1
zfs hold keep testpool/filesystem1@fridayHolds existing snapshot & attempts to destroy using zfs destroy will fail
zfs rename testpool/filesystem1@friday FRIDAYRename the snapshots
Note: snapshots must exists in the same pools
zfs diff testpool/filesystem1@friday testpool/filesystem1@friday1Identify the difference between two snapshots
zfs holds testpool/filesystem1@fridayDisplays the list of snapshots help
zfs rollback -r testpool/fileystem1@fridayRoll back yesterday snapshot recursively
zfs destroy testpool/fileystem1@thursdayDestroy snapshot created yesterday
zfs clone testpool/filesystem1@friday testpool/clones/fridayClone was created on the same snapshot
Note: Cannot create clone of a filesystem in a pool that is different from where original snapshot resides.
zfs destroy testpool/clones/FridayDestroy the clone


Sunday, 30 August 2015

Create Local Repository - Solaris 11.2

Image packaging system(IPS) and important concept from Solaris 11 onwards. I would like to create local repository on the local system by downloading the files from the Oracle website using oracle's automated script.

Download below files and copy to your local Solaris server.
Below are the files copied on my local Solaris server. 

root@solnode1:/var/share/pkg# pwd
root@solnode1:/var/share/pkg# ls -l
total 14373953
-rwx------   1 root     root        5594 Aug 29 20:52 install-repo.ksh
drwxr-xr-x   3 pkg5srv  pkg5srv        7 Aug 30 08:42 repositories
-rw-r--r--   1 root     root     1771800121 Aug 29 15:32
-rw-r--r--   1 root     root     1889867782 Aug 29 15:35
-rw-r--r--   1 root     root     1902167161 Aug 29 16:46
-rw-r--r--   1 root     root     1790358735 Aug 29 16:44
-rw-r--r--   1 root     root         227 Aug 29 16:14 sol-11_2-repo-md5sums.txt

root@solnode1:/var/share/pkg#./install-repo.ksh -d /var/share/pkg/repositories/ -v -c

The script would compare the checksums of the downloaded files, uncompress and would initiate the repository creation. 

your current publisher would be pointing to "" and it needs to be changed to your local repository. 

root@solnode1:~# pkg publisher
solaris                     origin   online F

root@solnode1:~# pkg set-publisher -G '*' -M '*' -g file:///var/share/pkg/repositories solaris

root@solnode1:~# pkg publisher
solaris                     origin   online F file:///var/share/pkg/repositories/

To enable clients to access the local repository via HTTP, enable the application/pkg/server Service Management Facility (SMF) service.

root@solnode1:~# svccfg -s application/pkg/server setprop pkg/inst_root=/var/share/pkg/repositories

check does repos work
root@solnode1:~# svcprop -p pkg/inst_root application/pkg/server

Reload the pkg.depotd repository service.

root@solnode1:~# svcadm refresh application/pkg/server

we had successfully created solaris 11.2 local repository.

Saturday, 8 August 2015

Contiguous space re-partition - Linux

To recap, root FS has run out of space and data contained on it couldn't be removed or compressed to free up the space. Since there was swap space, so I planned to reclaim from the swap partition without losing the data and would extend the space for root file system. 

Host : susenode2
OS   : SuSE 11 / CentOS / Redhat 
Disk : sda

It could have been easy we could have extended volume using LVM, since our disks are not under LVM, we are here trying to re-create the entire partition without losing data.

Current scenario :

Disk (sda) is been into 3 partitons, of which sda1, sda2 are my data partitions & sda3 swap partition. I would recreate the swap partition and would extend the root file system. It could also be seen that the sector are contiguous between sda2 and sda3 hence I could destroy those two partitions and re-create.

df & fdisk & swap output :

susenode2:~ # df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext3    13G   12G  570M  96% /
udev           tmpfs  369M  128K  369M   1% /dev
tmpfs          tmpfs  369M     0  369M   0% /dev/shm
/dev/sda1      ext3   1.1G 1015M   18M  99% /application/logs
susenode2:~ #

susenode2:~ # fdisk -l /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000cc8af

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     2265087     1131520   83  Linux
/dev/sda2   *     2265088    31625215    14680064   83  Linux                  <<===  root fs
/dev/sda3        31625216    41943039     5158912   82  Linux swap / Solaris   <<===  swap 
susenode2:~ #

susenode2:~ # swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       5158908 0       -1
susenode2:~ #

Since we are resizing the root file system, we need to get into rescue environment to do so. 
I am using an SuSE DVD or you could also use knoppix and would get into rescue environment and make necessary changes to the partition table. 

I would delete the partition sda2 and sda3 and would recreate the sda2 from the sector which was at the start i..e 2265088 and would end for my required size say 18G which I would add to this file system.

I would later create a new partition for the swap for remaining space. 

snaps are as below :

Once partition are created we need to change the type of the partition to swap for sda3 and since sda2 was bootable, make sure you would toggle boot flag on the partition.

Make sure you now has enough space in the disk where you could resize your root file system and make the swap partition. 

Rescue:~ # mkswap /dev/sda3
Rescue:~ # reboot

After reboot, check your root file system space. It would have been increased and swap space decreased. 

Snap after increasing file system :

susenode2:~ # df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext3    18G   12G  5.3G  69% /
udev           tmpfs  369M  128K  369M   1% /dev
tmpfs          tmpfs  369M     0  369M   0% /dev/shm
/dev/sda1      ext3   1.1G 1015M   18M  99% /application/logs
susenode2:~ #

susenode2:~ # fdisk -l /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000cc8af

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     2265087     1131520   83  Linux
/dev/sda2   *     2265088    40013823    18874368   83  Linux
/dev/sda3        40013824    41943039      964608   82  Linux swap / Solaris
susenode2:~ #

susenode2:~ # swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       964604  0       -1
susenode2:~ #


Sunday, 26 July 2015

speedtest mini : check your internet speed locally - CentOS 7 is one of the most popular internet speed tests. It is very helpful if you want to determine your Internet download and upload speed similarly speedtest-mini can be performed on local server. 

Install your apache, PHP,  start httpd service and make sure 'httpd' service is allowed by your firewall. 

#yum install -y httpd php php-mysql php-gd php-mcrypt
#systemctl start httpd
#firewall-cmd --add-service=http 

Download "speedtest mini" from from their official site : 
#cd /var/www/html

Register to and download the latest version of mini. 

Make sure your apache doccument root is in /var/www/html
# grep -i "^documentroot" /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html"
#cd /var/www/html/mini
#mv index-php.html index.html

point your browser to http://<ipaddress>/mini and start testing your speed on the local servers. 


Wednesday, 8 July 2015

Configure Network Teaming - RHEL 7

Network teaming is method for linking NIC's together logically to allow for failover or higher throughput. 

RHEL 7 implements network teaming with a small kernel driver and a user space daemon, teamd. The kernel handles network packets efficiently and teamd handles logic and interface processing. Software, called runners, implement load balancing and active-backup logic, such as roundrobin. The following runners are available to teamd:

- broadcast : a simple runner transmits each packet from all ports

- roundrobin : simple runner which transmits packets in a rounf-robin fashion from each port

- activebackup : failover runner which watches for link changes and selects an active port for data transfers

- loadbalance : this runner monitors traffic and uses a hash function to try to reach a perfect balance when selecting ports for packet transmission.

- lacp : implements the 802.3ad Link aggregation control protocol. can use the same transmit port selection possibilities as the loadbalance runner.


- Create team interface 
- Assigning the IPv4 or IPv6 attributes of the team interface
- Assign the port interfaces
- Bring the team and port interfaces up/down

Current existing network interfaces are beow, "eno33554984" and "eno50332208" will be the interfaces that will be the ports for the teamed interface.

# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:14:ef:0e brd ff:ff:ff:ff:ff:ff
3: eno33554984: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:14:ef:18 brd ff:ff:ff:ff:ff:ff
4: eno50332208: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:14:ef:22 brd ff:ff:ff:ff:ff:ff

- Create team interface:
# nmcli connection add type team con-name team0 ifname team0 config '{"runner": {"name": "activebackup"}}'
Connection 'team0' (b781d554-6d28-4baa-9af8-5f3331acd427) successfully added.

'{"runner": {"name": "activebackup"}}' - In this setup i'm using method 'activebackup' 

- Assign IPv4 attributes to team interface.
# nmcli connection modify team0 ipv4.addresses ''
# nmcli connection modify team0 ipv4.method manual

- assign the port interfaces
# nmcli connection add type team-slave con-name team0-port1 ifname eno33554984 master team0
Connection 'team0-port1' (d7ae4a56-4872-4264-8f2f-215742deae92) successfully added.
# nmcli connection add type team-slave con-name team0-port2 ifname eno50332208 master team0
Connection 'team0-port2' (c6755f4b-8071-4294-b3d5-691da53cf264) successfully added.

- check the current state of teamed interfaces. 
# ip a show team0
7: team0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether 00:0c:29:14:ef:18 brd ff:ff:ff:ff:ff:ff
    inet brd scope global team0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe14:ef18/64 scope link
       valid_lft forever preferred_lft forever

# teamdctl team0 state
  runner: activebackup
    link watches:
      link summary: up
        name: ethtool
        link: up
    link watches:
      link summary: up
        name: ethtool
        link: up
  active port: eno33554984

Ping from your network gateway through the 'team0' interface, and when the 'eno33554984' || 'eno50332208' have been disconnected, you still have no interruptions in the PING. 

how to disconnect one of the interface:
#nmcli device disconnect eno33554984
#teamdctl team0 state
#nmcli device connect eno33554984

- Display team ports of the team0 interface
# teamnl team0 ports

- Display the active port
# teamnl team0 getoption activeport


Thursday, 2 July 2015

Install and configure Git - CentOS 7

In this article i'll try to explain how to install, configure and use Git.

Environment: CentOS 7
Git version:

Git is a distribute version control system used by developers, but since it can also store .files(files with '.') system admin's can use to store their customized configurations files like .bashrc, .vimrc or other important scripts ..etc

Git takes a snapshot of how the files look at that instant in time and will store a reference to it. It wouldn't matter to Git what data you insert as it will check-sum it using SHA1 algorithm and create 40-character hex key. Git is based on key-value data system.

- Install Git by using 'yum' 

#yum install git -y

# git --version
git version

I already have a Git account registered if you don't have, signup to and complete your registration. create your first repository by '+' 
NOTE: repository name you choose or the directory name which you create should be the alike. Git usually sync the directory or the files from your laptop or desktop to the one having the same name as in the GitHub.


setup your name and e-mail (similar email ID that you used to create GitHub account) on your local laptop or desktop which must be run very first time inorder to setup Git.

sunilka@centos7]$ git config --global "sunilka"
sunilka@centos7]$ git config --global ""
sunilka@centos7]$ git config --list

Create a directory and initialize Git by running git init, after that you have .git created with few files and directory under it. According to Git it's now been the working tree. now, everything under it can be uploaded to GitHub.

sunilka@centos7]$ mkdir configs
sunilka@centos7]$ cd configs; git init
sunilka@centos7]$ ls -ld .git

few files were been created and added into the directory, the moment we added the files into Git it creates a hash checksum and refers it by checksum.

sunilka@centos7 configs]$ git ls-files --stage
100755 af7ec7a5b7b361c10dcbf3db7286f97ef7df57d6 0       ks.cfg
100755 3e84972fcf7f688f98999d1bd5c38eaf250efcc9 0       ks_centos7.cfg
sunilka@centos7 configs]$

Now, lest pust to Gitgub which is our remote repository. first check you have remote repository already existing there
sunilka@centos7 configs]$ git remote -v
sunilka@centos7 configs]$ 

add the remote repository, URL which you had while creating the repository.
sunilka@centos7 configs]$ git remote add origin

sunilka@centos7 configs]$  git remote -v
sunilka@centos7 configs]$ 

      git remote add - add remote directory to Git
              origin - default name of the remote location

Push the file to GitHub using, use the same username and password which you use to create and access your GitHub account.
sunilka@centos7 configs]$ git push origin master

any changes made to the files, must be committed before Git push. 

sunilka@centos7 configs]$ git commit -m "CentOS 7 kickstart file" ks_centos7.cfg
[master 08632bb] CentOS 7 kickstart file
 1 file changed, 1 deletion(-)
sunilka@centos7 configs]$ git commit -m "CentOS 6 kickstart file" ks.cfg
[master 498a0ca] CentOS 6 kickstart file
 1 file changed, 2 deletions(-)
sunilka@centos7 configs]$ 

sunilka@centos7 configs]$ git push origin master
Counting objects: 9, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 591 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
   57a7eb3..498a0ca  master -> master
sunilka@centos7 configs]$ 

Take a look at your GitHub page, it should have been uploaded. 

you can make a localcopy from your GitHub account using the clone feature

sunilka@centos7 configs]$ mkdir gitclones
sunilka@centos7 configs]$ cd gitclones/
sunilka@centos7 gitclones]$ git clone
Cloning into 'configs'...
remote: Counting objects: 10, done.
remote: Total 10 (delta 0), reused 0 (delta 0), pack-reused 10
Unpacking objects: 100% (10/10), done.
sunilka@centos7 gitclones]$ ls
sunilka@centos7 gitclones]$ ls configs/
ks_centos7.cfg  ks.cfg
sunilka@centos7 gitclones]$

you have same files are in your GitHub repository.