Construct a graphical installation program for Linux (3) (1)

Source: Internet
Author: User

This article describes how to implement the partition function and how to support advanced functions such as RAID and LVM from the basic partition table structure.
The partition function is a very important function for Linux graphical installation programs. Its basic function is to automatically identify the hard disk and create ext2 partitions and swap partitions on the hard disk. You can use this partition as the root partition for Linux installation.
For a fully functional installer, it should also be able to create other types of partitions, including reiserfs, fat, and so on. It should also be able to create software RAID partitions and logical volume management partitions. And the ability to filter out valid installation points and determine the most suitable mount point based on the selected partition type. Partitions can be dynamically deleted and modified, and the partition operation can be restored to the specific content before the partition operation.
The main content of this section includes the code which is based on the install program of HappyLinux. All functions are implemented in the install program of HappyLinux3.0. You can obtain the installation program source code in the/HappyLinux/happyinst/usr/bin/perl-install directory of the installation disk.
1. Introduction to the installer partition Module
In the graphical installation program, the general process of Hard Disk partitioning is:
 
Modules closely related to the partition function in the installation program include:
Diskdrake. pm
The graphic partition processing module is the main control module of the partition function. It processes interactions with users and checks the legitimacy of partition operations.
Diskdrake. rc
The resource file in the partition operation window mainly describes the expressions of different partition types.
Fs. pm
Mount and format the partition, and generate the/etc/fstab file.
Fsedit. pm
Obtain the hard disk partition structure and complete the operations of adding, deleting, and modifying partitions.
Lvm. pm
Provides a module for creating, modifying, and obtaining logical volume management partitions.
Partition_table.pm
The module that operates logical copies of Partitioned Tables, including determining the validity of partition operations, setting partition alignment, reading primary partitions, expanding partitions, and determining the type.
Partition_table_dos.pm
The module that operates on a dos-type partition table.
Partition_table_ra1_pm
Directly access the hard disk partition table to read/write partition information.
Raid. pm
The module that processes software raid devices.
2. Basic partitioning
A hard disk primary partition consists of a 240-byte hard disk primary Boot Record, a 64-byte hard disk partition table 01BEH-01FDH), and a valid identifier of the last two bytes. Layout of the primary Boot Record block:
Hard Disk 0 cylinder 0 head 1 Sector
Master Guide record code area
First Partition Table
The second partition table
Third Partition Table
Fourth Partition Table
Active guide record valid signs 55 H, AAH
Table 1-1
The meaning of each field in the hard disk partition is:
Byte displacement Length Byte) Field meaning
0 1 active partition indicator. The value is 80 H, indicating that only one of the customizable partitions can be used. The value is 00 H, indicating that the custom partitions cannot be used. This byte is also called the auto-lift flag.
1 start head number
2 1. The start fan area number is 6 lower) and the START cylinder number is 2 higher
3 1 low 8 bits of the starting cylindrical number
4. The system identifier on the partition.
The value is 01 H, indicating a 12-bit FAT format DOS partition.
The value is 04 H, indicating a 16-bit FAT format DOS partition.
The value is 07 H, indicating that the NTFS partition is used.
This value is 0BH, indicating partitions in the FAT32 format of WIN98.
The value is 0CH, indicating that partitions are in the FAT32 format of WIN98. The LBA mode is supported.
The value is 0FH, indicating that the extended partitions of WIN95 are used. The LBA mode is supported.
The value is 82 H, indicating that partitions in linux swap format are used.
The value is 83 H, indicating partitions in LINUX EXT2 format.
The value is 8EH, indicating that partitions are in LVM format.
This value is FDH, indicating that partitions in the software RAID format are used.
5 1 end head number
6 1. The end fan area number is 6 low) and the end cylinder number is 2 high.
7 1 low 8-bit ending Cylinder Number
8 4
12 4 fan ID used by the partition
Table 1-2
Hard Disk Partitions exist in the form of a chain table. Each hard disk has a primary partition description block, which can describe four partitions. Each hard disk can have up to four primary partitions, other partitions are extended partitions. Each extended partition is joined by link fields.

The device alias used by the primary partition is from/dev/hda1 to/dev/hda4, and the subsequent partitions starting from/dev/hda5 represent extended partitions.
Read Partition Table #-the following code reads the entire hard disk partition table to form a partition description structure.
Sub read ($; $ ){
My ($ hd, $ clearall) = @_;
If ($ clearall ){
Partition_table_raw: zero_MBR_and_dirty ($ hd );
Return 1;
}
#-Read to the primary Partition Table and check the number of extended partitions in the partition table. If the value is greater than 1, an error occurs.
My $ pt = read_one ($ hd, 0) or return 0;
#-The primary partition description array is saved to the primary variable.
$ Hd-> {primary} = $ pt;
Undef $ hd-> {extended };
#-Check the primary partition, including whether the partitions overlap and whether unknown holes exist.
VerifyPrimary ($ pt );
#-If an extended partition exists, read it into the extended partition. In this case, check whether the extended partitions overlap and whether there is a circular join.
Eval {
$ Pt-> {extended} and read_extended ($ hd, $ pt-> {extended}) | return 0;
};
#-Specify the device number for the read partition, from/dev/hda1/dev/sda1) to/dev/hda4/dev/sda4 ).
Assign_device_numbers ($ hd );
#-Remove NULL connections in the extended partition.
Remove_empty_extended ($ hd );
1;
}
Write the partition operation to the partition table #-This operation only writes the partition table based on the user's operation, including the partition size, partition type, and other information.
Sub write ($ ){
My ($ hd) = @_;
$ Hd-> {isDirty} or return;
#-Set pilot flag
For ($ hd-> {primary} {raw }){
(Grep {$ _-> {local_start }=$ _-> {start };_ _-> {active} ||= 0 }@$ _) or $ _-> [0] {active} = 0x80;
}
#-Check partitions. Check whether partitions overlap and whether unknown holes exist.
VerifyParts ($ hd );
#-Write a partition table
$ Hd-> write (0, $ hd-> {primary} {raw}, $ hd-> {primary} {info}) or die "writing of partition table failed ";
$ Hd-> {isDirty} = 0;
$ Hd-> {hasBeenDirty} = 1;
If ($ hd-> {needKernelReread }){
Sync ();
$ Hd-> kernel_read;
$ Hd-> {needKernelReread} = 0;
}
}
3. Create a File System
After the partition operation is complete, you also need to create a file system on the partition to install it. The installer uses system commands such as mkdosfs, mke2fs, and mkreiserfs to create a file system.
Sub format_ext2 ($ @){
My ($ dev, @ options) = @_;
$ Dev = ~ M, (rd | ida | cciss)/, and push @ options, qw (-B 4096-R stride = 16 );
Push @ options, qw (-B 1024-O none) if arch () = ~ /Alpha /;
#-Call mke2fs to create an ext2 File System
Run_program: run ("mke2fs", @ options, devices: make ($ dev) or die _ ("% s formatting of % s failed", "ext2 ", $ dev );
}
Sub format_dos ($ @){
My ($ dev, @ options) = @_;
#-Call mkdosfs to create a fat file system
Run_program: run ("mkdosfs", @ options, devices: make ($ dev) or die _ ("% s formatting of % s failed", "dos ", $ dev );
}
Create a Reiserfs File System
Sub format_reiserfs ($ @){
My ($ dev, @ options) = @_;
#-Call mkreiserfs to create a Reiserfs File System
Run_program: run ("mkreiserfs", "-f", @ options, devices: make ($ dev) or die _ ("% s formatting of % s failed ", "reiserfs", $ dev );
}
When using the Reiserfs file system as the root partition of the system, the system needs to load the module reiserfs. o during startup. Reiserfs file systems are not supported in versions earlier than RedHat 7.0. Therefore, many RedHat-based release versions do not support Reiserfs file systems. If you want to add reiserfs support to such a system, in addition to loading the rpm package corresponding to Reiserfs, you also need to load the Reiserfs module in linuxrc when generating the Initial Startup image initrd.
Add the following statement to the/sbin/mkinitrd file:
# For the file system attached to/etc/fstab, if its type is not ext2, the device module must be loaded.
Fs = $ (awk '$2 = "/" {print $3}'/etc/fstab)
[-N "$ fs"-a "$ fs "! = "Ext2"] & findmodule $ fs
4. Supports LVM-type partitions.
Generally, the disk partition size is fixed, which requires you to have a rough understanding of the partition space usage when installing the system. When the user has exhausted all the space on the partition, it is required to re-partition or remove some files.
LVMLogical Volume Management) is the abbreviation of logical Volume Management. It splits a physical disk into several logical units, and partitions from different disks can form a logical volume. In addition, it can be dynamically added or deleted in the required time zone. For example, if you have an 8 GB disk with a 2 GB partition/usr on it, the space is exhausted. If you want to expand the/usr partition, you must first create a larger partition, and then copy all the content of/usr to this partition to change the/etc/fstab file, restart. However, if LVM is used in the system, you only need to use the commands in LVM to simply increase/usr.
LVM partitions manage partitions that require large data storage, which can greatly reduce the burden on administrators. Moreover, a flexible and efficient storage solution can be constructed after the combination of LVM and RAID devices.
4.1 basic concepts of LVM
Some terms will be used when creating LVM-type partitions. The following describes these terms.
Physical Volume PV)
The physical volume is only a physical partition initialized by LVM, so that the LVM administrator can recognize this partition.
Physical range PE)
The physical range is some big data blocks, usually several megabytes.
Volume group VG)
A vg can be composed of multiple PES. A vg can be composed of several partitions, And the PE contained in it is provided by these partitions. In a sense, we can think that VG is a hard disk device.
Logical Volume LV)
A logical volume is the part accessed by an end user and is used to store data. In a sense, we can think that LV is a logical partition. You can create any type of partition, including EXT2, ReiserFS, NTFS, and so on. It is the same as a normal disk partition during access.
Logical range LE)
Each logical volume is divided into data blocks.
To enable the LVM to work normally in the system, you must use the LVM support as a module during kernel customization. Then, to create and manage logical partitions of LVM, you also need to install the LVM package.
The procedure is as follows:
Run the fdisk command to set the partition/dev/hda3 and/dev/hdc3 to 0x8e.
Create a physical volume.
Pvcreate/dev/hda3
Pvcreate/dev/hdc3
Create a new volume group newvg, which contains/dev/hda3 and/dev/hdc3 partitions. The volume group size is the total capacity of the two partitions. After the volume group is successfully created, the/dev/newvg directory is formed under the/dev directory, and the logical volume device files generated after the group are saved in this directory.
Vgcreate newvg/dev/hda3
Vgcreate newvg/dev/hdc3
Create a new logical volume
Lvcreate-L1500-nnewlv newvg
Create a MB linear logical volume, and this command also creates the block Device File/dev/newvg/newlv corresponding to the logical volume.
Lvcreate-i2-I4-l1500-notherlv newvg
Use the staggered block stripe) as 2, and the block size is 4 KB to create another logical volume. The corresponding device alias is/dev/newvg/otherlv.
Create a File System
Mke2fs/dev/newvg/newlv
Create an ext2 File System on the device/dev/newvg/newlv. In addition to the ext2 file system, you can also create multiple file systems, such as Reiserfs.
Activate lvm logical volume
Vgchange-a y
Control the visibility of the volume group to the kernel before using the volume group. This command activates all known volume groups in the system. Before deleting a volume group, you are advised to disable the volume group by using vgchange-a n.
Read LVM partition information sub get_lvs {
My ($ lvm) = @_;
My @ fstabs;
My $ start = 0;
@ Fstabs = ();
$ Lvm-> {primary} {normal} = ();
Undef $ lvm-> {primary} {normal };
#-Use vgdisplay-v-D lvmname to obtain all volume groups.
Foreach (map {/^ LV Name \ s + (\ S + )/? $1: ()} 'vgdisplay-v-D $ lvm-> {LVMname }'){
#-Use lvdisplay-D-c lv to obtain logical volume information.
My @ lvinfo = split (':', 'lvdisplay-D-c $ _');
My $ size = $ lvinfo [6];
My $ type =-e $ _ & fsedit: typeOfPart ($ _);
My % fstab;
$ Fstab {device }=$ _;
$ Fstab {type }=$ type | 0x83;
$ Fstab {size }=$ size;
$ Fstab {isFormatted} = $ type;
$ Fstab {number }=$ lvinfo [4];
$ Fstab {start} = $ start;
$ Fstab {lvm }=$ _;
$ Start + = $ size;
Push @ fstabs, \ % fstab;
}
$ Lvm-> {primary} {normal} =\@ fstabs;
}


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.