- Change the number of the partition from sda1 to sda2
- 7 Answers 7
- 8 Parted Commands to Manage Disk Partitions in Linux
- How to Install Parted on Linux
- 1. Check Parted Version
- 2. List Disk Partitions in Linux
- 3. Change or Switch Partition in Linux
- 4. Create Partition in Linux
- 5. Resize Linux Disk Partition
- 6. Delete Linux Partition
- 7. Rescue Linux Disk Partition
- 8 Change Linux Partition Flag
Change the number of the partition from sda1 to sda2
I have a disk with two partitions: sda1 and sda2. I would like change the number of sda1 to sda2 and sda2 to sda1. It’s possible but I don’t remember the procedure. i.e. My first partition will be sda2 and the second sda1, so I need to specify a manual order, not an automatic ordering like in fdisk -> x -> f. How can I change the order? Links to manuals or tutorials are also welcome. Thanks. The reason: I have an application that needs to read data from sda1 but the data is in sda2. Changing the partition table is the fastest fix for this issue. The system isn’t critical but I don’t want to keep the system halted for too much time. Update: the fdisk version of OpenBSD includes this functionality.
7 Answers 7
I just did this in an easier way:
# sfdisk --dump /dev/sdb > sdb.bkp
now edit sdb.new changing ONLY the lines order and partition numbers, as in my case:
# partition table of /dev/sdb unit: sectors /dev/sdb1 : start= 1026048, size=975747120, : start= 2048, size= 204800, : start= 206848, size= 819200, : start= 0, size= 0, >to
# partition table of /dev/sdb unit: sectors /dev/sdb1 : start= 2048, size= 204800, : start= 206848, size= 819200, : start= 1026048, size=975747120, : start= 0, size= 0, >then throw it back to the disk partition table?
My numbering sequence was mangled after I shrank & shifted right the only partition (sdb1) to add two smaller partitions at the start of the disk using gparted .
If the last command does not work, as in my case, change it for:
# sfdisk --no-reread --force /dev/sdb < sdb.new
This worked perfectly for my USB. I use Rufus to boot FAT32 partition to NTFS partition with EFI so I can reinstall Windows 10 if there are issues. However, I have an extra partition for getting files from my PS4. This allowed me to set that partition as sdb1 so PS4 sees it as the first option. The only thing I can nitpick about is replacing -d with --dump argument just to make it clear what the command is doing. Apart from that, I believe this should be the best answer for it's simplicity.
Hi, I'm from the far future. Linux works more or less exactly the same way, and this worked for me even though I did it with a live, mounted drive on a headless Raspberry Pi, like an idiot would do. It was still fine.
Works like a charm! After cloning ESXi OS I got the following error: "Bank 5: not found Bank 6: invalid configuration". After doing some research I found out that ESXi expects the two 250.0 MB partitions to be numbered 5 and 6. I've applied your method using a LiveCD and the problem was solved in less than 5 minutes!
FYI, it is a bad idea and you can lose everything. If you still want to do it, here are the steps:
- Don't do it. If this doesn't help, then:
- Use the sfdisk tool: First, make a backup of the partition table using
sfdisk /dev/sda -O sda-partition-sectors.save
Checking that no-one is using this disk right now . OK Disk /dev/sda: 1018 cylinders, 124 heads, 62 sectors/track Old situation: Units = cylinders of 3936256 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sda1 0+ 5 6- 23063+ 83 Linux /dev/sda2 6 1017 1012 3890128 83 Linux /dev/sda3 0 - 0 0 0 Empty /dev/sda4 0 - 0 0 0 Empty Input in the following format; absent fields get a default value. Usually you only need to specify and (and perhaps ). /dev/sda1 :
Now it is asking you to give the new details for the 'sda1' partition. So you have to give the numbers of sda2 here. So, I put '6 1012' here and press Enter:
/dev/sda1 :6 1012 /dev/sda1 6 1017 1012 3890128 83 Linux /dev/sda2 :
Now check if the numbers printed after you pressed Enter are exactly the same as those printed earlier for sda2. If it is okay, continue with giving the new numbers for sda2:
/dev/sda2 :0 /dev/sda2 0+ 5 6- 23063+ 83 Linux /dev/sda3 :
This time it was enough to enter "0" in my case - but you have to make sure the numbers aren't messed up in yours.
Next, continue with the other partitions in the same manner. If you already reached the end of the disk, pressing Enter is enough. Finally, check again that all the numbers are okay and save the partition table (or not). If you messed something up, have a look at man sfdisk and the descriptions of '-d', '-O' and '-I' options.
Notice also, that once you've made the crazy changes, you might need to run 'sync' so that the partitions are re-read before you try to mount them.
8 Parted Commands to Manage Disk Partitions in Linux
Parted is a renowned command-line utility designed to facilitate the management of hard disk partitions in a user-friendly manner.
With Parted, you can effortlessly perform tasks such as adding, deleting, shrinking, and extending disk partitions, while also managing the file systems associated with them.
Over time, Parted has undergone significant development and evolution, introducing various enhancements and changes to its functionality. Certain features have been retired, while new capabilities have been introduced, rendering it a versatile tool for partition management.
This tutorial aims to provide a comprehensive introduction to Parted, covering its fundamental concepts and demonstrating practical examples. If you are new to Parted, it is essential to note that any modifications made using Parted are immediately written to the disk.
Therefore, it is crucial to exercise caution while attempting to modify your disk partitions to avoid unintended consequences or data loss. Throughout this tutorial, we will guide you step-by-step, ensuring that you grasp the basics of Parted and understand the potential implications of your actions when working with disk partitions.
If you intend to experiment with Parted, it is recommended to utilize a virtual machine or an old computer/laptop that does not contain any critical data. When making modifications to a disk partition, it is crucial that the partition is not actively in use.
In the case of primary partitions, you may consider booting into rescue mode, which provides a safe environment for performing partition-related tasks without interference from the running operating system. This precautionary approach ensures the integrity of your valuable data and minimizes the risk of unintended consequences during partition manipulation.
Note: You will need to have root access to the machine you will be working on in order to use parted.
How to Install Parted on Linux
On many Linux distributions, parted comes pre-installed. If it is not included in your distro, you can install it with:
$ sudo apt install parted [On Debian, Ubuntu and Mint] $ sudo yum install parted [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] $ sudo emerge -a sys-apps/parted [On Gentoo Linux] $ sudo apk add parted [On Alpine Linux] $ sudo pacman -S parted [On Arch Linux] $ sudo zypper install parted [On OpenSUSE]
Once you have made sure that parted is installed, you can proceed further to check out some real-world examples of parted command in the rest of this article.
1. Check Parted Version
To observe a similar message as depicted in the image below, execute the following command. Please note that the output may differ slightly depending on your specific version of Parted. By default, Parted will operate on your primary drive, typically identified as /dev/sda , unless otherwise specified.
If you want to exit parted, simply type:
2. List Disk Partitions in Linux
Now that parted is started, let’s list the partitions of the selected hard disk. As mentioned earlier, parted chooses your first drive by default. To see the disk partitions run print .
When running print , it will also display the hard disk information and model. Here is an example from a real hard disk (not virtual as shown in the image above) :
(parted) print Model: ATA TOSHIBA MQ01ACF0 (scsi) Disk /dev/sda: 320GB Sector size (logical/physical): 512B/4096B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 256MB 255MB primary ext2 boot 2 257MB 320GB 320GB extended 5 257MB 320GB 320GB logical lvm
In the example above, you can see the disk model, capacity sector size, and partition table.
3. Change or Switch Partition in Linux
If you have more than one hard disk, you can easily switch between disks, by using the “ select ” command. In the example below, I will switch from /dev/sda to /dev/sdb which is a secondary drive on my system.
To easily switch between disks you can use:
Change "X" with the letter of the disk to which you wish to switch.
4. Create Partition in Linux
The parted can be used to create primary and logical disk partitions. In this example, I will show you how to create a primary partition, but the steps are the same for logical partitions.
To create a new partition, parted uses “ mkpart “. You can give it additional parameters like "primary" or "logical" depending on the partition type that you wish to create.
Before you start creating partitions, it’s important to make sure that you are using (you have selected) the right disk.
As shown in the above image, we are using a virtual drive of 34 GB. First, we will give the new disk a label and then create a partition and set a file system on it.
Now the first step is to give the new disk a label name with:
Now create the new partition with mkpart . The listed units are in megabytes (MB). We will create a 10 GB partition starting from 1 to 10000:
(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? Start? 1 End? 10000 (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 34.4GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 10.0GB 9999MB primary ext2 lba
Next, exit parted with "quit" command. We will format our new partition in the ext4 file system using mkfs . To make this happen run the following command:
Note: It’s important to select the right disk and partition when executing the above command!
Now let’s verify our results, by printing the partition table on our secondary disk. Under file system column, you should see ext4 or the file system type that you have decided to use for your partition:
5. Resize Linux Disk Partition
Parted includes multiple useful functions and one of them is "resizepart" . As you have probably figured this out by now, "resizepart" helps you resize a partition.
In the example below, you will see how to resize an existing partition. For the purpose of this example, we will be using the earlier created partition.
First, you will need to know the number of the partition that you will be resizing. This can be easily found by using "print" :
In our example, the partition number is "1" . Now run the resizepart command:
You will be asked for the number of the partition that you will resize. Enter its number. After that, you will be asked to set the new ending point for this partition. Remember that by default the units are in MB. In our example, we have set the new partition size to 15 GB:
(parted) resizepart Partition number? 1 End? [10.0GB]? 15000
Now verify the results with "print" :
6. Delete Linux Partition
The next thing you will learn is how to delete a partition from your hard drive. To do this, you will need to use the rm command within parted. To delete a disk partition you will need to know its number.
As mentioned earlier, you can easily obtain this number by using "print" . In our example, we will delete the partition with a number 1 from our secondary drive /dev/sdb1 :
Verify the results by printing the partitions table:
7. Rescue Linux Disk Partition
Parted supports a “ rescue" utility that helps you recover a lost partition between a starting and ending point. If a partition is found within that range, it will attempt to restore it.
(parted) rescue Start? 1 End? 15000 (parted) print Model: Unknown (unknown) Disk /dev/sdb1: 15.0GB Sector size (logical/physical): 512B/512B Partition Table: loop Disk Flags: Number Start End Size File system Flags 1 0.00B 15.0GB 15.0GB ext4
8 Change Linux Partition Flag
Using parted, you can change the state of a flag for disk partitions. The supported flags are:
The states can be either "on" or "off" . To change a flag simply run "set" command within parted:
The above command sets lba flag to on for the second partition. Verify the results with print :
Conclusion
Parted is a useful and powerful utility that can help you manage your disk partitions in Linux systems. As always, when working with disk partitions you need to be extra careful.
It is strongly recommended to go through parted man pages to learn how you can customize its output and find more information about its capabilities.
If you have any questions or comments, please do not hesitate to use the comment section below.