Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unrecognized disk label for disk that exists and is discoverable #83

Closed
goigle opened this issue Sep 21, 2021 · 2 comments
Closed

Unrecognized disk label for disk that exists and is discoverable #83

goigle opened this issue Sep 21, 2021 · 2 comments
Assignees
Labels

Comments

@goigle
Copy link

goigle commented Sep 21, 2021

I am trying to create a partition on /dev/sda, but for some reason pyparted is having issues. At first I was directly creating the device based on the path, but that gave the 'unrecognized disk label' error so I tried enumerating the disks and checking the path but I had the same results. The disk is a 120GB SATA SSD on /dev/sda. It works fine and GParted isn't having issues.

libparted-dev is on version 3.3-4ubuntu0.20.04.1. I'm on Ubuntu 20.04.3 LTS with Python 3.8.10.

Offending code (args.device is '/dev/sda'):

    devs = parted.getAllDevices()
    dev = None
    for d  in devs:
        print(f'Discovered {d.path}')
        if d.path == args.device:
            dev = d

    if dev is None:
        print(f'Could not find device {args.device}')
    
    capacity = dev.getSize("GB")
    if capacity > 200:
        ans = input(f'{dev.path} is a {capacity}GB drive, are you sure you have the right device? (y/n): ')
        if ans.lower() != 'y':
            return
    print(f'Clearing partitions on {dev.path}')
    dev.clobber()
    # error is on this line
    disk = parted.newDisk(dev)

Console Output:

Discovered /dev/sda
Discovered /dev/nvme0n1
Discovered /dev/nvme1n1
Clearing partitions on /dev/sda
Traceback (most recent call last):
  File "provision.py", line 107, in <module>
    main()
  File "provision.py", line 58, in main
    disk = parted.newDisk(dev)
  File "/home/j/projects/analysis_python/hw-provision/env/lib/python3.8/site-packages/parted/decorators.py", line 42, in new
    ret = fn(*args, **kwds)
  File "/home/j/projects/analysis_python/hw-provision/env/lib/python3.8/site-packages/parted/__init__.py", line 485, in newDisk
    peddisk = disk_new(device.getPedDevice())
_ped.DiskException: /dev/sda: unrecognised disk label
@dcantrell dcantrell self-assigned this Feb 28, 2022
@dcantrell
Copy link
Owner

Since you are using clobber() on the device, it is destroying the disk label. newDisk() can't work without a disk label. If you want to create a new Disk object with a specific lable, you need to use freshDisk:

dev.clobber()
disk = parted.freshDisk(dev, TYPE)

Where TYPE is a value from the parted.diskType dict. Such as parted.diskType["gpt"]:

disk = parted.freshDisk(dev, parted.diskType["gpt"])

Which will create a new Disk object for dev that has a GPT disk label.

@dcantrell
Copy link
Owner

Note that you will need to use disk.commitToDevice() to write the label to the disk, otherwise it's just all in memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants