Disk Management With Parted Command In Linux | Manage Partitions on MBR & GPT Disks Using Parted

Disk Management With Parted Command In Linux | Manage Partitions on MBR & GPT Disks Using Parted

Nehra Classes

2 года назад

11,542 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Vikas Nehra
Vikas Nehra - 12.09.2021 11:09

Disk Management With Parted Command In Linux: ============================================= MBR partition scheme and GPT partition scheme both use different method to store partition information in disk. We cannot use both scheme in a single disk. Since in this tutorial we will create both types of partitions, we need two separate disks. We have already added two virtual disks in our system. # lsblk OR # fdisk –l Creating MBR Partition with parted command: The parted command needs the name of disk where we want to create the partition as its argument. # parted Always use parted command with argument. Without argument it will start with first available disk. # parted /dev/sdb print The print command provides important information about the hard disk. This information includes manufacturer information, disk model, disk size, Partition Table and disk flag. If this is a new disk, we will get unrecognised disk label error. Disk label is the information which is used to determine the type of disk. The parted command needs a valid disk label before it can do anything with disk. To see it practically run mkpart command. The mkpart command is used to create new partition. mkpart As above output confirmations that we are not allowed to create new partition unless it has a valid disk label. The msdos and gpt are two valid disk labels which we can use here. The msdos label is used to define the disk as MBR disk and the gpt label is used to define the disk as GPT disk. To assign the label mklabel command is used. Let’s assign the msdos label to this disk. Type mklabel and press Enter key. Type msdos and press Enter key. Verify the action with print command. # parted /dev/sdb mklabel msdos print Do not use this command if disk contains data. This command is very risky. This command will convert disk in un-partitioned blank disk silently. Now disk has label. We can create partition in it. To create partition following steps are used. Type mkpart and press Enter key. As we know in MBR partition scheme we are allowed to create maximum four primary partitions. If more partitions are required, we have to use one primary partition as extended partition. Further Extended partition can be used to create 15 logical partitions. Type the partition type and press Enter key. Type the starting point for partition and press Enter key. The parted utility supports very limited file system types. To add worse, most of them are out dated nowadays. We will make file system individually later. By now just keep the default file system type. Type the ending point for partition. We can use KB, MB or GB units to define the size of partition. We can verify the creation of partition with print command Following figure illustrates above steps mkpart primary ext2 1M 600M print Always start first partition with 1MB even only first 512 bytes are used to store the MBR data. This way we can avoid several entry point errors. Following same process create two more partitions mkpart primary ext2 601 800 mkpart primary 801 1000 print We have created three primary partitions from maximum (four) allowed. Now we can create one more primary partition or can create one extended partition. Let’s create one allowed extended partition. Since extended partition is used to create all allowed logical partitions, we should allocate the maximum disk space in this partition. We will assign all remaining disk space in this partition. mkpart extended 1001 2100 print All logical partitions will be created in extended partition. The sum (of size) of all logical partitions cannot go beyond the extended partition. In this example we created an extended partition of 1145MB, so we can create logical partitions from 1145MB. We are allowed to create up to 15 logical partitions in extended partition. We can create any number of partitions from range of one to fifteen. The only limit is that the sum of all partitions must be below the size of extended partition. Let’s create a single logical partition of all available space. mkpart logical ext2 1002 2045 print We have used all space of extended partition in one logical partition. So even we are allowed to create up to 15 logical partitions and free space is available outside the extended partition, we cannot create the another logical partition. To create another logical partition we need free space inside the extended partition not outside the partition. So far we have created all possible partitions including primary, extended and logical partitions. To exit from parted use quit command. quit The parted cannot update the in-memory kernel partition table. Kernel reads partition table when system boots. So the kernel will be updated about this change at next reboot automatically. Meanwhile if we want to update the kernel immediately, we can use partprobe command which force kernel to reread the partition table. # partprobe /dev/sdb # lsblk # fdisk -l /dev/sdb Creating GPT partition with parted command: GPT is the new partition scheme. It allows us to create up to 128 partitions. It does not divide partitions in primary, extended and logical. First part of this article explains basic concepts of GPT in detail with examples. We will create GPT partition in /dev/sdb disk. Let’s initiate parted again for /dev/sdb disk # parted /dev/sdc print mkpart mklabel gpt Now disk has a valid label. We can create partition in it. Creating partition is a six steps process. Type mkpart and press Enter key Set desired descriptive name for partition Accept default file system type Enter starting point of partition Enter ending point of partition Verify the partition with print command mkpart linux ext2 1M 600M print mkpart documents 601 1000 print quit # partprobe /dev/sdc lsblk So far we have created MBR and GPT partitions in /dev/sdb and /dev/sdc respectively. These partitions are useless unless we put a file system in them. File system is a logical container which allows us to store files and directories. How to create file system To create a file system in partition we format it with supported file system. To format a partition following command is used. # mkfs.ext3 /dev/sdb1 # mkfs.ext4 /dev/sdb2 # mkdir /srv/test1 ; mkdir /srv/test2 # mount /dev/sdb1 /srv/test1 # mount /dev/sdc1 /srv/test2 # df -hT Make entries in /etc/fstab for permanent mounting. # cd /srv/test1 # touch {1..10} # ls -l For LVM: # pvcreate /dev/sdc1 # pvs # vgcreate LinuxVG /dev/sdc1 # vgs # lvcreate -L 500M -n LinuxLV LinuxVG # lvs # mkfs.xfs /dev/LinuxLV/LinuxVG # mkdir /srv/lvm # mount /dev/LinuxLV/LinuxVG /srv/lvm # df -hT # cd /srv/lvm # touch {a..m} # ls -l How to delete MBR partitions To delete a partition rm sub-command is used. Before we delete a partition it must be un-mounted. We cannot delete a mounted partition. Let’s see it practically. # umount /srv/test* # df -hT # parted /dev/sdb print rm 1 rm 2 rm 3 rm 5 print rm 4 print quit # partprobe Delete GPT partitions Deleting a GPT partition is as same as deleting a MBR partition. Initiate parted command with GPT disk (/dev/sdb) and use print command to view the number of partition and use rm sub command to delete that partition.

Ответить
Mohd Suhail
Mohd Suhail - 11.07.2023 21:06

Waah sir , straight forward padhaya and in simple way in simple language.salute to you from depth of my heart

Ответить
Ajeet Kumar
Ajeet Kumar - 04.02.2023 08:47

How to convert gpt to mbr in Ubuntu without data loss

Ответить
Sawan Tyagi
Sawan Tyagi - 11.06.2022 17:42

Sir using parted command you did not select partion type as LVM while in fdisk we creats partion as LVM first . Then we creates file system .

Ответить
Samir Maniyar
Samir Maniyar - 02.10.2021 17:16

Thanks Vikas

Ответить
Abbas Hussain
Abbas Hussain - 16.09.2021 07:19

Dear Sir, Here have Questions ,
Creating partition its requesting for system and by default its assigning ext2 then why again we need to write file system ? can you please help me in this regard.
2nd Question === Why are giving space as sector , can we left 1st part of size like 1M and parted should auto understand to left 1M as fdisk never ask for that. and can we give only size and system will auto calculate starting and ending sector information as per our size mentioned. ?

Ответить
Tamesh Chakraborty
Tamesh Chakraborty - 12.09.2021 16:12

Sir correct me if m wrong..
we use parted if we need to create partition above 2TB right ? ?
Anything less than 2 TB , we can use fdisk utility right ? or do you have more clarity to this point. please correct me if m wrong

Ответить