I used Gparted to delete EFI (/dev/sda1) and Ubuntu (/dev/sda2) partitions on my secondary drive. The third partition (/dev/sda3) is for data, so I resized it in order to use all unallocated space I've got by deleting the first two partitions. Now, having only a unique partion on the disk, I would like to fix it's number, in order to get /dev/sda1. What's the easiest way to do it that works with a disk having GPT partition table? Thank you!
-
1You do know that renumbering is just beautification for your own eyes, right? Anyway... you should have resized /dev/sda1 to be bigger than /dev/sda3. Then copy sda3 over sda1. File check sda1 and if no problems, remove sda3, resize sda1 accordingly.– user680858May 26, 2017 at 10:03
-
By the way... you said sda is your secondary drive ? Then what it the primary known as?– user680858May 26, 2017 at 10:07
-
What does primary drive has to do with it? My question is about /dev/sda drive. I said "secondary" because there is not Ubuntu installed anymore and so dev/sdb has become the primary one where I have Windows 10 installed.– GenerosoMay 26, 2017 at 10:22
-
Nothing, I was just curious. It is usual for sda to be primary drive.– user680858May 26, 2017 at 10:24
-
You are right. I have to reverse the SATA connectors on my motherboard. :) Anyway, any suggestions to fix the number? The situation is that, I didn't mind to do as you explained! :D– GenerosoMay 26, 2017 at 10:29
3 Answers
You may use gdisk
to rewrite the partition-table:
sudo gdisk /dev/sda p (the p-command prints the recent partition-table on-screen) s (the s-command sorts the partition-table entries) p (use the p-command again to see the result on your screen) w (write the changed partition-table to the disk) q (quit gdisk)
You will find most useful information about gdisk
at http://www.rodsbooks.com/gdisk/
-
Before I found your answer, I used fdisk to rewrite the patition-table: sudo fdisk /dev/sdb and then, in this order, x-f-r-w. It worked like a charm, getting /dev/sdb1 as I expected. I can deduce that using either fdisk or gdisk command is exactly the same and that fdisk command is proper for GPT patition-table too, is it?– GenerosoMay 27, 2017 at 10:12
-
@Generoso Older versions of
fdisk
didn't support GPT, recent version does, probablyfdisk
usesgdisk
in a way, difficult to find deeper insight... I wouldn't have tried it withfdisk
, but good to know that it works too. +1 for your effort...– mook765May 27, 2017 at 10:26 -
Understood! :) Anyway, if I try to use gdisk, after I press s, it says: "You may need to edit /etc/fstab and/or your boot loader configuration". What about this message?– GenerosoMay 27, 2017 at 20:20
-
@Generoso If you mount your partition via fstab you should check if the related fstab-entry is still valid. Your boot-loader configuration shouldn't be affected as the partition doesn't contain the OS but only data.– mook765May 27, 2017 at 20:25
-
1Yes, it is just a warning,
gdisk
can't know what the purpose of the partition is, and gently gives this message. If you auto-mount your partition in your installed OS you should care about and check fstab-entry for validity after the changes. Imagine you auto-mount/dev/sda3
instead of using UUID, fstab-entry would fail becausesda3
doesn't exist anymore. If you don't auto-mount the partition in any OS then you don't need to care.– mook765May 28, 2017 at 16:19
Intrigued by finding that last link and keen learning something new, I replayed Generoso's partition situation on my system using a 32GB USB drive. Below are my findings, but first my:
Conclusion: As my example is aimed at 1 (final) partition, near the end I merely changed its name. I did not touch the actual sectors. In the link in my comment above, an example is shown where sector number are moved around. I followed that and miserably goofed and destroyed the USB disk, proving how dangerous playing with partitions really is !!
Maybe my scenario also works with multiple partitions, feel free to try it yourself. I dare you to use your primary partition for it.. haha.
Ok, here are my:
Findings: Using Gparted I created 3 partitions, each 1 GB.
# fdisk -l /dev/sdc
Disk /dev/sdc: 30 GiB, 32176472064 bytes, 62844672 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf33657fc
Device Boot Start End Sectors Size Id Type
/dev/sdc1 2048 2099199 2097152 1G 83 Linux
/dev/sdc2 2099200 4196351 2097152 1G 83 Linux
/dev/sdc3 4196352 6293503 2097152 1G 83 Linux
Using Disks to mount partition 3.
Manually placed a file on it.
# ls -l /media/willem/cd4746ac-6d9f-4057-9396-a593ce2f301d/
total 21436
drwx------ 2 root root 16384 May 27 00:12 lost+found
-rw-r--r-- 1 root root 21930117 May 27 00:14 SomeFile.txt
Using Disks to unmount partition 3.
Using Gparted to remove partitions 1 & 2 and grow partition 3 to use the whole disk.
# fdisk -l /dev/sdc
Disk /dev/sdc: 30 GiB, 32176472064 bytes, 62844672 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf33657fc
Device Boot Start End Sectors Size Id Type
/dev/sdc3 8192 62844671 62836480 30G 83 Linux
Using Disks to mount partition 3.
Check the file on partition 3.
# ls -l /media/willem/cd4746ac-6d9f-4057-9396-a593ce2f301d/
total 21436
drwx------ 2 root root 16384 May 27 00:12 lost+found
-rw-r--r-- 1 root root 21930117 May 27 00:14 SomeFile.txt
Using Disks to unmount partition 3.
Copy the partition table from the drive.
Show the partition table.
# sfdisk -d /dev/sdc > sdc.tab
# cat sdc.tab
label: dos
label-id: 0xf33657fc
device: /dev/sdc
unit: sectors
/dev/sdc3 : start= 8192, size= 62836480, type=83
Using vi sdc.tab
to change sdc3 into sdc1.
Apply the changed partition table.
# sfdisk --no-reread -f /dev/sdc < sdc.tab
Disk /dev/sdc: 30 GiB, 32176472064 bytes, 62844672 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf33657fc
Old situation:
Device Boot Start End Sectors Size Id Type
/dev/sdc3 8192 62844671 62836480 30G 83 Linux
>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0xf33657fc.
Created a new partition 1 of type 'Linux' and of size 30 GiB.
/dev/sdc2:
New situation:
Device Boot Start End Sectors Size Id Type
/dev/sdc1 8192 62844671 62836480 30G 83 Linux
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
As soon as the command finishes, partition 1 auto-mounts.
Check the file.
# ls -l /media/willem/cd4746ac-6d9f-4057-9396-a593ce2f301d/
total 21436
drwx------ 2 root root 16384 May 27 00:12 lost+found
-rw-r--r-- 1 root root 21930117 May 27 00:14 SomeFile.txt
Using Disks to unmount partition 1.
Done.
PS. I did cat SomeFile.txt
every time but I felt it unnecessary to include that output.
-
Man, thank you for your detailed answer! Anyway, I solved my problem by using fdisk command. It's very very simple. Take a look to keep in mind if you will need it in future.– GenerosoMay 27, 2017 at 10:06
You should have resized /dev/sda1 to be bigger than /dev/sda3. Then copy sda3 over sda1. File check sda1 and if no problems, remove sda3, resize sda1 accordingly.
-
As
/dev/sda1
was a (presumably FAT) ESP and/dev/sda3
already contained user data (perhaps FAT, but I'd guess not), doing exactly as you suggest might mean changing the filesystem type. That said, depending on the partitions' sizes, creating a new/dev/sda1
with the right filesystem type, copying the files over, deleting/dev/sda3
, and then resizing/dev/sda1
might have been slightly safer than moving the start point of/dev/sda3
. Of course, this is all moot now to the OP. May 29, 2017 at 1:30