Having a hard time deleting partitions

So I have finished the course content and I am now training for the exam.

One of the issues that I see often (and have tried Googling) relates to partitions. When I create a partition and delete it using gdisk, I re-create it and mkfs will complain the partition already exists?! 

A quick rundown of what I mean: 

[root@nickfm1c ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 20G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 20G 0 part /
xvdb 202:16 0 2G 0 disk
├─xvdb1 202:17 0 500M 0 part
└─xvdb2 202:18 0 500M 0 part
[root@nickfm1c ~]# gdisk /dev/xvdb
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): d
Partition number (1-2): 1

Command (? for help): d
Using 2

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-4194270, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-4194270, default = 4194270) or {+-}size{KMGTP}: +500M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/xvdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
[root@nickfm1c ~]# mkfs -t xfs ^Cev/xvdb1
[root@nickfm1c ~]# partprobe
Error: Partition(s) 2 on /dev/xvdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
[root@nickfm1c ~]# umount -a
umount: /run/user/1004: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /sys/fs/cgroup/systemd: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /sys/fs/cgroup: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /run: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount: /dev: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@nickfm1c ~]# partprobe
[root@nickfm1c ~]# mkfs -t xfs /dev/xvdb1
mkfs.xfs: /dev/xvdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@nickfm1c ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 20G 0 disk
├─xvda1 202:1 0 1M 0 part
└─xvda2 202:2 0 20G 0 part /
xvdb 202:16 0 2G 0 disk
└─xvdb1 202:17 0 500M 0 part
[root@nickfm1c ~]#

What is the correct way, to make sure I get entirely rid of the partitions, so mkfs doesn't complain? 
  • post-author-pic
    Terry P
    01-20-2019

    You need to execute the write command to gdisk.
    As an example.

    [root@1nuxg33k1 user]# lsblk
    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0   20G  0 disk
    └─xvda1 202:1    0   20G  0 part /
    xvdf    202:80   0    1G  0 disk
    └─xvdf1 202:81   0  500M  0 part
    [root@1nuxg33k1 user]# gdisk /dev/xvdf
    GPT fdisk (gdisk) version 0.8.10

    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present

    Found valid GPT with protective MBR; using GPT.

    Command (? for help): d
    Using 1

    Command (? for help): d
    No partitions

    Command (? for help): w

    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!

    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/xvdf.
    The operation has completed successfully.
    [root@1nuxg33k1 user]# lsblk
    NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0  20G  0 disk
    └─xvda1 202:1    0  20G  0 part /
    xvdf    202:80   0   1G  0 disk

  • post-author-pic
    Ermin K
    01-20-2019

    Did you manage to solve the problem?

  • post-author-pic
    nickfm
    01-23-2019

    Sadly not Ermin. I tried to Terry's solution and it seems to work (lsblk just displays the drive, not any partitions), but when I create the partition using gdisk, I just get the following message: 

    [root@nickfm1c ~]# lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvda 202:0 0 20G 0 disk
    ├─xvda1 202:1 0 1M 0 part
    └─xvda2 202:2 0 20G 0 part /
    xvdb 202:16 0 2G 0 disk
    └─xvdb1 202:17 0 500M 0 part
    [root@nickfm1c ~]# gdisk /dev/xvdb
    GPT fdisk (gdisk) version 0.8.10

    Partition table scan:
    MBR: protective
    BSD: not present
    APM: not present
    GPT: present

    Found valid GPT with protective MBR; using GPT.

    Command (? for help): d
    Using 1

    Command (? for help): w

    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!

    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/xvdb.
    The operation has completed successfully.
    [root@nickfm1c ~]# wipefs -a /dev/xvdb
    /dev/xvdb: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
    /dev/xvdb: 8 bytes were erased at offset 0x7ffffe00 (gpt): 45 46 49 20 50 41 52 54
    /dev/xvdb: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
    /dev/xvdb: calling ioclt to re-read partition table: Success
    [root@nickfm1c ~]# gdisk /dev/xvdb
    GPT fdisk (gdisk) version 0.8.10

    Partition table scan:
    MBR: not present
    BSD: not present
    APM: not present
    GPT: not present

    Creating new GPT entries.

    Command (? for help): n
    Partition number (1-128, default 1):
    First sector (34-4194270, default = 2048) or {+-}size{KMGTP}:
    Last sector (2048-4194270, default = 4194270) or {+-}size{KMGTP}: +500M
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'

    Command (? for help): w

    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!

    Do you want to proceed? (Y/N): y
    OK; writing new GUID partition table (GPT) to /dev/xvdb.
    The operation has completed successfully.
    [root@nickfm1c ~]# mkfs -t xfs /dev/xvdb1
    mkfs.xfs: /dev/xvdb1 appears to contain an existing filesystem (xfs).
    mkfs.xfs: Use the -f option to force overwrite.


  • post-author-pic
    Michael C
    01-23-2019

    Hi nickfm,


    I'm able to reproduce this consistently, and it looks like the solution is to just use `-f` to force the overwrite.  

    [root@mchristian1 ~]# gdisk /dev/xvdf
    GPT fdisk (gdisk) version 0.8.10

    Partition table scan:
    MBR: protective
    BSD: not present
    APM: not present
    GPT: present

    Found valid GPT with protective MBR; using GPT.

    Command (? for help): p
    Disk /dev/xvdf: 2097152 sectors, 1024.0 MiB
    Logical sector size: 512 bytes
    Disk identifier (GUID): 117732DA-D485-474F-9A8F-2FA8813EFEAF
    Partition table holds up to 128 entries
    First usable sector is 34, last usable sector is 2097118
    Partitions will be aligned on 2048-sector boundaries
    Total free space is 1073085 sectors (524.0 MiB)

    Number Start (sector) End (sector) Size Code Name
    1 2048 1026047 500.0 MiB 8300 Linux filesystem

    Command (? for help): d
    Using 1

    Command (? for help): w

    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!

    Do you want to proceed? (Y/N): Y
    OK; writing new GUID partition table (GPT) to /dev/xvdf.
    The operation has completed successfully.
    [root@mchristian1 ~]# lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvda 202:0 0 20G 0 disk
    ├─xvda1 202:1 0 1M 0 part
    └─xvda2 202:2 0 20G 0 part /
    xvdf 202:80 0 1G 0 disk
    [root@mchristian1 ~]# blkid
    /dev/xvda2: UUID="668dbd02-c201-44bc-be76-f606fc9ab8db" TYPE="xfs" PARTUUID="9146b810-9a31-4c10-a206-01b0bbaca807"
    /dev/xvda1: PARTUUID="c32f79de-9ad9-4d19-a8f2-f3a61628211b"
    /dev/xvdf: PTTYPE="gpt"
    [root@mchristian1 ~]# !g
    gdisk /dev/xvdf
    GPT fdisk (gdisk) version 0.8.10

    Partition table scan:
    MBR: protective
    BSD: not present
    APM: not present
    GPT: present

    Found valid GPT with protective MBR; using GPT.

    Command (? for help): n
    Partition number (1-128, default 1):
    First sector (34-2097118, default = 2048) or {+-}size{KMGTP}:
    Last sector (2048-2097118, default = 2097118) or {+-}size{KMGTP}: +500M
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'

    Command (? for help): w

    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!

    Do you want to proceed? (Y/N): Y
    OK; writing new GUID partition table (GPT) to /dev/xvdf.
    The operation has completed successfully.
    [root@mchristian1 ~]# lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    xvda 202:0 0 20G 0 disk
    ├─xvda1 202:1 0 1M 0 part
    └─xvda2 202:2 0 20G 0 part /
    xvdf 202:80 0 1G 0 disk
    └─xvdf1 202:81 0 500M 0 part
    [root@mchristian1 ~]# blkid
    /dev/xvda2: UUID="668dbd02-c201-44bc-be76-f606fc9ab8db" TYPE="xfs" PARTUUID="9146b810-9a31-4c10-a206-01b0bbaca807"
    /dev/xvda1: PARTUUID="c32f79de-9ad9-4d19-a8f2-f3a61628211b"
    /dev/xvdf1: UUID="c72818f0-5f7b-4790-aae7-da987f1266c9" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="a928cd24-1fbf-4f8a-87cc-464ca8fe8fe7"
    [root@mchristian1 ~]# mkfs.xfs /dev/xvdf1
    mkfs.xfs: /dev/xvdf1 appears to contain an existing filesystem (xfs).
    mkfs.xfs: Use the -f option to force overwrite.
    [root@mchristian1 ~]# mkfs.xfs -f /dev/xvdf1
    meta-data=/dev/xvdf1 isize=512 agcount=4, agsize=32000 blks
    = sectsz=512 attr=2, projid32bit=1
    = crc=1 finobt=0, sparse=0
    data = bsize=4096 blocks=128000, imaxpct=25
    = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0 ftype=1
    log =internal log bsize=4096 blocks=855, version=2
    = sectsz=512 sunit=0 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0
    [root@mchristian1 ~]#


    If you notice when I rank `blkid` after deleting and reformatting the partition, it kept the xfs designation on the partition. 

    /dev/xvdf1: UUID="c72818f0-5f7b-4790-aae7-da987f1266c9" TYPE="xfs"...

     It did this for both `gdisk` and `fdisk`, and I was able to see where others have noticed the same issue.

    In the end, forcing the new filesystem seems to be the simplest solution.  

    Additionally, I only noticed this when trying to put an xfs filesystem on it, using a different type seemed to work fine.


  • post-author-pic
    nickfm
    01-23-2019

    It seems like we came to the same conslusion. 

    I will just force the mkfs in the exam, if it comes to that :) 

Looking For Team Training?

Learn More