Showing posts with label LVM. Show all posts
Showing posts with label LVM. Show all posts

Tuesday, May 22, 2012

LVM INACTIVE

After a reboot I noticed that my iSCSI LVM was missing.   I did an lvscan and it was listed, but as INACTIVE.

I ran lvchange -a y /dev/vg_nobakcup/lg_nobackup and the logical volume became active again.
I ran a lvscan to verify it was active.  
I then mounted the volume.
Upon reboot I had the same problem, so I added lvchange -a y /dev/vg_nobackup/lg_nobackup and rebooted again.   This time the volume was active and mounted.  PROBLEM SOLVED.

I believe the issue is this is an iSCSI Volume connecting to Microsoft's iSCSI Storage server.  I believe the connection just times out before connecting.  

Wednesday, May 16, 2012

Adding LVM from command line

This is how you setup a Physical Disk, Volume Group and a Logical Volume using the command line in Linux.

This example will be from a Redhat Enterprise 6.2 Installation.

So we will create a Physical Disk, Physical Volume, then a Volume Group, then a Logical Volume and then mount the volume.  Type only what is in "red".
 


Step #1 - Find out what device your new hard drive is assigned.

fdisk -l 

You should see all your disk including the one that you just added.  Here is the one that I just added.  

Disk /dev/sdb: 343.6 GB, 343597383680 bytes
255 heads, 63 sectors/track, 41773 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       41774   335544319+  8e  Linux LVM

Our new device is called "/dev/sdb1"

Step #2 - Partition the disk with fdisk. (Create Physical Disk)

fdisk /dev/sdb1
m - to see all the command options
n - to add the new partition
p - to make it a primary partition
1 - to make it the 1st partition
First cylinder (1-41773, default 1):  PRESS ENTER
Last cylinder, +cylinders or +size{K,M,G} (1-41773, default 41773): PRESS ENTER

***NOW WE NEED change the partition's system id
t - to change the partition's system type
L - to list all the system type
FOR LVM type 8e
8e - for LVM PRESS ENTER
w - to write table to disk and exit.

STEP #3 - Create a Physical Volume with pvcreate.

pvscan - will show you the physical volumes.
PV /dev/sda2   VG vg_redhat62   lvm2 [69.51 GiB / 0    free]
  Total: 1 [69.51 GiB] / in use: 1 [69.51 GiB] / in no VG: 0 [0   ]

As you can see our new physical disk does not show up.  It will show up after we create a physical volume.
pvcreate /dev/sdb1 - creates the physical volume.
 Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created
pvscan - will now show our new physical volume
 PV /dev/sda2   VG vg_redhat62     lvm2 [69.51 GiB / 0    free]
  PV /dev/sdb1                      lvm2 [320.00 GiB]
  Total: 2 [389.51 GiB] / in use: 1 [69.51 GiB] / in no VG: 1 [320.00 GiB]

STEP #4 - Create the Volume Group
 vgcreate vg_data /dev/sdb1 - This creates the Volume Group.  give it a name and point it to your physical disk.
  Volume group "vg_data" successfully created
You can run either vgscan or vgdisplay to see your new Volume Group.

STEP #5 - Create a Logical Volume
lvcreate -l 100%FREE -n lg_data vg_data - This creates the Logical Volume.
  Logical volume "lg_data" created
The "-l 100%FREE" mean to use 100% of available space and "-n" is for naming.  So I called the new Logical Volume "lg_data" and pointed it to my new Volume Group "vg_data".

To check on the new Logical Volume run either lvscan or lvdisplay.
lvscan
  ACTIVE            '/dev/vg_data/lg_data' [320.00 GiB] inherit
  ACTIVE            '/dev/vg_redhat62/lv_root' [33.14 GiB] inherit
  ACTIVE            '/dev/vg_redhat62/lv_home' [28.67 GiB] inherit
  ACTIVE            '/dev/vg_redhat62/lv_swap' [7.70 GiB] inherit

STEP #6 - Format the new Logical Volume
 mkfs.ext4 -m 0 /dev/vg_data/lg_data - File type is ext4 the "-m 0" don't reserve disk space for superuser. It save 5%
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
20971520 inodes, 83885056 blocks
0 blocks (0.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2560 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

STEP #7 - Mount the Logical Volume
Create a directory that you want to mount the volume to.
mkdir data
mount /dev/vg_data/lg_data /data

These steps created a physical disk, a physical volume, a Volume group, a Logical volume, formatted the volume and mounted it.
 df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_redhat62-lv_root
                       33G  2.5G   29G   9% /
tmpfs                 2.8G  272K  2.8G   1% /dev/shm
/dev/sda1             485M   53M  407M  12% /boot
/dev/mapper/vg_redhat62-lv_home
                       29G  174M   27G   1% /home
/dev/mapper/vg_data-lg_data
                      315G  195M  299G   1% /data

As you can see are new LVM is listed last and is mounted.   

Friday, August 12, 2011

LVM Snapshot script

LVM Snapshot script
Used for copying a MySQL schema to a test environment


There are so many ways to do this, but our need was to not have any production down time* and have the test database available ASAP.  

I wrote up a little how to and I thought I might share it so others could use it.  Like I said there are many ways to do this so just take a look and use the logic that works for you. 

A Method to copy Data to a test environment


This method will copy all data in a volume and MySQL a test environment with no downtime.  The method used could also be used for backing up data or replication.
Here are the steps needed to accomplish the copy to test.
1.       Add an additional hard drive for Storage. 
2.       Configure the disk using fdisk and then create a Physical Volume with the pvcreate command.*
3.       Add the Physical Volume to the VolGroup with the vgextend command.*
4.       Setup MySQL to run a second instance.*
5.       Run the this script.

#! /bin/sh

user=username
pwd=password
olddb=originalname
newdb=newname
socket=/tmp/mysql.sock2

#This will remove LVM and stop the second instance of MySQL .
mysqladmin -u$user -p$pwd --socket=$socket shutdown 
sleep 4
umount /dev/VolGroup01/databackup
lvremove -f /dev/VolGroup01/databackup

#This will flush mysql and Lock Tables and LVM snapshot (see below)**
mysql -u$user -p$pwd < lvm_snapshot

#At this point the volume and MySQL files have been copied

#This will will mount the LVM snapshot to /mnt/databackup
 mount -t ext3 /dev/VolGroup01/databackup  /mnt/databackup

#This will start the second instance of MySQL on port 3305
mysqld_multi start 2

#This is needed to allow enough time for MySQL to start and create the mysql.sock2 file
sleep 4

#This will login to the second instance of MySQL and rename the schema to dl4test. 
mysqlconn="mysql -u$user -p$pwd -S $socket -h localhost"

$mysqlconn -e "CREATE DATABASE $newdb"
params=$($mysqlconn -N -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='$olddb'")

for name in $params; do
      $mysqlconn -e "RENAME TABLE $olddb.$name to $newdb.$name"; done;

$mysqlconn -e "DROP DATABASE $olddb"

#This will grant user saitest access to the newdatabase schema
$mysqlconn -e "GRANT ALL PRIVILEGES ON newdatabase.* TO 'username'@'%' IDENTIFIED BY 'saitest'";

This script takes about 10 seconds to run and requires no down time in production.  
Note: the schema name change does not change any stored procedures so a script or logic would need to be added in order to create those after renaming the schema.

*More Information on Setup:
1.  Use fdisk to setup the disk. 
Type fdisk /dev/sdb1 (or whatever device your new hard drive is) and press Enter.  Then press, m,n,e(choose the defaults), p, and w to save the changes.  
2.  Create a Physical Volume.
Type “pvcreate /dev/sdb1”
3. Extend the Volume Group.
Type “vgextend VolGroup01 /dev/sdb1”

4. Add a second instance of MySQL.
Modify the /etc/my.cnf with the following changes.
a.       Add a [mysqld_multi] section that looks like this:
[mysqld_multi]
mysqld          = /usr/bin/mysqld_safe
mysqladmin      = /usr/bin/mysqladmin

b.      Rename [mysqld] to [mysqld1]
c.       Add a [mysqld2] section with the following parameters:
[mysqld2]
port            = 3305
datadir         = /mnt/databackup/mysql
socket          = /tmp/mysql.sock2
pid-file        = /mnt/databackup/mysql/mars-snapshot-test.pid2
log_error       = /mnt/databackup/mysql/error2.log
To start the second instance of mysql use the following command:
“mysqld_multi start 2”
To stop the second instance of mysql use the following command:
Mysqladmin –uusername –ppassword –socket=/tmp/mysql.sock2 shutdown.
To login to the second instance of mysql you will need to login using the socket:
Mysql –uusername –ppassword –S /tmp/mysql.sock2 –hlocation

DownTime - we run this at night when there are a minimal of users. 
Long queries at the time of the LVM snapshot will call some downtime.


** Here is the content of the lvm_snapshot file
FLUSH TABLES WITH READ LOCK;
system lvcreate -L 220G -s -n databackup /dev/VolGroup01/LogVol01;
UNLOCK TABLES;
QUIT