0

I wanted to add a partition to the /dev/sda mistakenly, I started fdisk with /dev/sda2 and treated It like a disk. How do i revert it?

ghostrider@dev1:~$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   800G  0 disk 
├─sda1   8:1    0   953M  0 part /boot
└─sda2   8:2    0 399.1G  0 part /

ghostrider@dev1:~$ sudo fdisk /dev/sda2

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

The old ext4 signature will be removed by a write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x1d9559e3.

Command (m for help): p
Disk /dev/sda2: 399.7 GiB, 428495339520 bytes, 836904960 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: 0x1d9559e3

Command (m for help): F
Unpartitioned space /dev/sda2: 399.7 GiB, 428494290944 bytes, 836902912 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

Start       End   Sectors   Size
 2048 836904959 836902912 399.1G

Command (m for help): n  
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-836904959, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-836904959, default 836904959): 

Created a new partition 1 of type 'Linux' and of size 399.1 GiB.

Command (m for help): w
The partition table has been altered.
Failed to add partition 1 to system: Invalid argument

The kernel still uses the old partitions. The new table will be used at the next reboot. 
Syncing disks.

ghostrider@dev1:~$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   800G  0 disk 
├─sda1   8:1    0   953M  0 part /boot
└─sda2   8:2    0 399.1G  0 part /

Edit: After the server restarted later, server got stuck in initramfs with error message

Gave up waiting for root 
ALERT! UUID=xx-xx-xx-xx-xx doesn't exist dropping to shell
1

It looks like nothing happened because fdisk failed to overwrite your filesystem. When it tried, it showed the error:

Failed to add partition 1 to system: Invalid argument

This is, however, a good time to ensure that your backups are current and restorable. It's possible that something did go wrong anyway.


Note that fdisk did try to warn you about the problem before you did anything. It warned:

The old ext4 signature will be removed by a write command.
 
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x1d9559e3.

It's a good idea to read through the messages that your tools print, even if you are very familiar with them, as something unexpected may appear one day. If you had noticed this, you may have realized something was wrong before any damage may have been done.

2
  • oh! my bad. The kernel still uses the old partitions. The new table will be used at the next reboot. This made me believe that something terrible has happened with the root partition. – Sudip Bhattarai Feb 19 at 4:17
  • I've added an edit – Sudip Bhattarai Mar 19 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.