Logical volume and grub in Linux

Source: Internet
Author: User

LVM-logical volume manager is a well-known logical volume. It can associate the underlying hard disk with PV to form a large logical volume group VG, which is equivalent to our extended partition, you can separate any size on the partition, which is not limited by the size of the low-level hard disk (the limitation here refers to the partition which does not exceed their sum, is what we hear, LV, logical volume. This makes it easy for enterprises or individuals to expand their storage space, and it also supports reducing the partition size.

Then we will summarize the role of LVM:

1. It can expand or contract the partition.

2. not limited by the underlying hard disk.

3. Supports snapshot backup.

 

In Linux, LVM consists of three commands, which correspond to a group of commands, as shown below:

Pvcreate pvmove pvremove pvscan pvdisplay

Vgcreate vgmove vgremove vgscan vgdisplay vgreduce vgextend

Lvcreate lvmove lvremove lvscan lvdisplay lvresize lvchk

You can understand their functions literally, create move Remove, and so on.

1. How to create an LV?

First, you need to prepare at least one partition. For LVM purposes, we recommend that you prepare at least two partitions in the virtual machine.

1. Create two partitions/dev/sdb1/dev/sdb2 and change their ID to 8E (key! 8e is the file system ID supported by the logical volume group.

2. Create a PV

   pvcreate /dev/sdb{1,2}

3. view the created PV.

    pvdisplay /dev/sdb2

If you want to delete it, use

    pvremove /dev/sdb2

4. Create VG

   vgcreate vg0 /dev/sdb1

If you want to expand it, ensure that the remaining PV exists first.

    vgextend vg0 /dev/sdb2

5. Create an LV

    lvcreate -L 8G -n lvm1 vg0

#-L option specifies the size-N specifies the name of the created LV. vg0 indicates the name of the created VG.

-L is also a common option, which also specifies the size, in the unit of the PV of the Logical Block.

6. format the LV

     mke2fs -f /dev/vg0/lvm1

7. Mount

Mkdir/backup // LVM is usually mounted here. If this folder is not available, create a mount/dev/vg0/lvm1/backup

2. How to expand the logical volume size?
1. Expand physical boundaries
Lvextend // expands the physical boundary
-L
-L + 5g // extend 5G without the plus sign to 5

     lvextend -L +2G /dev/vg0/lvm1   

DF-h to view the physical size at this time
2. Extend logical boundaries

Resize-P/dev/vg0/lvm1 extends logical boundaries

-P display process

3. How to Reduce the logical volume size: there is a certain risk of reducing the logical volume. do not reduce the LV error step as much as possible during work, which may cause the entire logical volume to be damaged, And/home may be easily damaged, as a result, the rhce test fails.
1. Uninstall the Backup Directory

     umount /backup

2. self-check the file system
E2fsck
-F force check

     e2fsck -f /dev/vg0/lvm1

3. Reduce logical boundaries
Resize2fs device [size] specifies the size to which the device is reduced.

    resize2fs /dev/vg0/lvm1 3G

4. Reduce physical boundaries

    lvreduce -L -1G /dev/vg0/lvm1 

Or

    lvreduce -L 3G /dev/vg0/lvm1       

5. Remount and check whether the data is damaged.

    mount /dev/vg0/lvm1 /backup

4. If you have understood the LVM creation steps and general principles, you must master one of its important backup functions, snapshot.

Snapshot is a hot standby mechanism that stores data in an instant and is called a snapshot. It works in a special way. Instead of saving all the data on the disk to be backed up, it saves a link to all the files at the time. When the files in the disk are modified, it backs up the modified file.

Therefore, based on its principle, the snapshot size must be created based on the File Usage Frequency of the disk to be backed up within a certain period of time.

How to use snapshot volumes:
1. Create a snapshot volume and specify the logical volume to be backed up

Lvcreate-l 512 M-S-N lvm1-snap-p r/dev/vg0/lvm1 //-snap specifies this is a snapshot volume

-P specifies the permission

Lvdisplay/dev/vg0/lvm1 // view the created snapshot volume cow: copy on write // you can see this information cow, which refers to its working mechanism, only copy when writing

2. Mount the snapshot volume before work
3. During work, when the file is incorrectly modified or deleted, find and back up the snapshot volume to another disk.

         tar jcf /tmp/backup.tar.bz2 ./*

4. unmount the snapshot volume after work

# Note: 1. The data backed up by the snapshot volume is frozen at the moment it is attached! Therefore, if you create a file in the backup disk, it cannot be backed up!

2. Even if the snapshot volume is detached, When you mount it again, it will still be backed up by the file that was mounted for the first time! Therefore, after the work is completed, if there is no error in the work process, delete it.

Create one as needed!

Zookeeper ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Grub topic

What is grub? Do you still remember the role of the first 446 bytes of the disk? In those 446 bytes, the most important program on the entire hard disk, bootloader, is installed to guide the OS on the disk, so that you can smoothly enter the operating system for work, grub is the leader in the boot program. It can not only guide Linux, but also guide the operating systems under different file systems. It is very powerful!

Grub is installed in two sections (three are accurate ):

Stage1 is installed in bootloader to guide the following stage
Stage1.5 load on a specific File System
Stage2 is installed in the disk partition. The/boot/GRUB configuration file grub. conf is linked to/etc/grub. conf.

Grub has powerful functions. When you lose the root password in Linux, you can use it to reset the root password. The steps are as follows:

1. Restart Linux and press any key to enter the grub Interface

2. Select the line starting with kernel, press E, press E, add a letter S at the end of the row where the cursor is located, and press enter to save and exit.

3. Save and press the B key to enter the single-user mode. At this time, you find that you already have the root permission.

4. Run the passwd command to modify your root password.

What if grub is used to obtain root privileges while you are not there? It doesn't matter. Grub provides the password function.

Edit the/boot/GRUB/grub. conf file

Add a row after the row timeout = N

Default = 0 timeout = 5 passwd = 12345 // is it too simple? And others can access this file to view it!

To prevent others from viewing the password, we can encrypt the password using MD5.

[Root @ server45 ~] # Grub-md5-crypt password: retype password: $1 $ vapvd0 $ wntenpebucemp2ftgvuw60 // a bunch of garbled characters are generated here

Copy the generated garbled characters to the grub. conf file!

What if grub breaks down? Let's simulate two situations

I. bootloader is damaged,
First, we intentionally destroy bootloader.

Dd If =/dev/Zero of =/dev/sda bs = 446 COUNT = 1 # Do not quit after you finish this command! Otherwise, your system cannot be started. Do not exceed 446; otherwise, your partition information will be lost!

In this case, you have two options to reinstall grub.
1) enter the grub command in the command line to enter the grup editing mode.

          grub>root (hd0,0)          grub>setup (hd0)          grub>quit 

2) Enter

         grub-install --root-directory=/ /dev/sda

Specify the device in the root directory
You can use this command to install grub on other hard disks. When grub is damaged and has been restarted, you can mount the hard disk to another machine and use this command to restore grub.

Ii. Grub. conf configuration file loss

1. If you have already shut down, open the grub command line several times by following the steps below:

Grub> Find (hd0, 0)/vmlinuz // search for kernel version information. My kernel is grub> root (hd0, 0) Starting with vmlnuz) // specify the partition where the root directory is located: grub> kernel/vmlinuz // enter the kernel version information grub> initrd/initrd. IMG // enter the initrd information to be consistent with the kernel version grub> boot // start

2. If you do not shut down, either of the two types of graphics is to directly enter grub in the command line to enter the grub command line and perform the above operations.

Alternatively, use Vim/boot/GRUB/grub. conf to manually write configuration information.

Default = 0 timeout = 5 splashimage = (hd0, 0)/GRUB/splash.xpm.gz // custom grub startup background image, if you are interested, study hiddenmenutitle Red Hat Enterprise Linux Server (2.6.18-164. EL5) Root (hd0, 0) kernel/vmlinuz-2.6.18-164.el5 Ro root =/dev/vol0/root rhgb quiet initrd/initrd-2.6.18-164.el5.img

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.