Remove all traces of GPT disk label
I’ve got an USB pen drive and I’d like to turn it into a bootable MBR device. However, at some point in its history, that device had a GPT on it, and I can’t seem to get rid of that. Even after I ran mklabel dos in parted , grub-install still complains about
Attempting to install GRUB to a disk with multiple partition labels. This is not supported yet..
I don’t want to preserve any data. I only want to clear all traces of the previous GTP, preferably using some mechanism which works faster than a dd if=/dev/zero of=… to zero out the whole drive. I’d prefer a termina-based (command line or curses) approach, but some common and free graphical tool would be fine as well.
3 Answers 3
If you don’t want to fiddle with dd , gdisk can do:
$ sudo gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.8 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): ? b back up GPT data to a file w write table to disk and exit x extra functionality (experts only) ? print this menu Command (? for help): x Expert command (? for help): ? a set attributes w write table to disk and exit z zap (destroy) GPT data structures and exit ? print this menu Expert command (? for help): z About to wipe out GPT on /dev/sdb. Proceed? (Y/N): Y GPT data structures destroyed! You may now partition the disk using fdisk or other utilities. Blank out MBR? (Y/N): Y
$ sudo gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.8 Partition table scan: MBR: not present BSD: not present APM: not present GPT: not present Creating new GPT entries. Command (? for help):
удаление gpt командой dd
А как корректно удалить (не захватив лишнее) GPT средствами dd?
Почитать в википедии или ещё где-нибудь про структуру GPT и выяснить её полный размер, затем удалять из начала и конца.
Гвозди можно забивать молотком.
А как корректно забить гвоздь гаечным ключом?
Ты хочешь так черезпопчно сделать из gpt-разметки mbr-разметку что ли?
Посмотреть исходный код wipefs.
Разметку GPT конечно смотрел: имеет два «заголовка» — один вначале, другой в конце (дублирует первый, но в обратной последовательности).
512(MBR) + 512(GPT Header) + 128/4*512(Partition Table) итого получается 17408
Первый «заголовок» стало быть удалить легко:
sudo dd if=/dev/zero of=/dev/sdX bs=512 count=34
А для удаления второго заголовка, так понимаю требуется определить размер всего диска, и вот здесь уже начинается кризис идей.
Ограничен в выборе программных средств (делаю это все в образе initramfs, лучше не спрашивайте зачем, это будет уже не по теме).
echo 'x z y w ' | gdisk /dev/sda
. либо sgdisk -Z /dev/sda (или -z, или -o).
делаю это все в образе initramfs
Решил вопрос связкой «fdisk & dd». Отпишу решение с полным тестом удаление/восстановление — возможно кому нибудь пригодится.
Тест провел на следующей флешке:
sudo fdisk -l /dev/sdd Disk /dev/sdd: 7,5 GiB, 8022654976 bytes, 15669248 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: gpt Disk identifier: 15A4B379-6E0D-4222-B39A-A60A61EB48C5 Device Start End Sectors Size Type /dev/sdd1 2048 15669214 15667167 7,5G Linux filesystem sudo gdisk -l /dev/sdd GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present
Резервное копирование Primary GPT:
sudo dd if=/dev/sdd of=~/sdd.primary_gpt bs=512 count=34 34+0 записей получено 34+0 записей отправлено скопировано 17408 байт (17 kB), 0,00642818 c, 2,7 MB/c
Резервное копирование Secondary GPT:
_blocksize=$(fdisk -l /dev/sdd | grep Units | awk '') sudo dd if=/dev/sdd of=~/sdd.secondary_gpt skip=$((($(fdisk -l /dev/sdd | grep Disk | awk '')-17408)/$_blocksize)) ibs=$_blocksize 34+0 записей получено 34+0 записей отправлено скопировано 17408 байт (17 kB), 0,00833474 c, 2,1 MB/c
Удаляю Primary GPT, проверяю что получается:
sudo dd if=/dev/zero of=/dev/sdd bs=512 count=34 34+0 записей получено 34+0 записей отправлено скопировано 17408 байт (17 kB), 0,00765282 c, 2,3 MB/c sudo fdisk -l /dev/sdd Disk /dev/sdd: 7,5 GiB, 8022654976 bytes, 15669248 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 sudo gdisk -l /dev/sdd GPT fdisk (gdisk) version 0.8.10 Caution: invalid main GPT header, but valid backup; regenerating main header from backup! Caution! After loading partitions, the CRC doesn't check out! Warning! Main partition table CRC mismatch! Loaded backup partition table instead of main partition table! Warning! One or more CRCs don't match. You should repair the disk! Partition table scan: MBR: not present BSD: not present APM: not present GPT: damaged Found invalid MBR and corrupt GPT. What do you want to do? (Using the GPT MAY permit recovery of GPT data.) 1 - Use current GPT 2 - Create blank GPT
GPT повредили, но пока не уничтожили.
sudo dd if=/dev/zero of=/dev/sdd seek=$((($(fdisk -l /dev/sdd | grep Disk | awk '')-17408)/$_blocksize)) obs=$_blocksize ibs=17408 count=1 1+0 записей получено 34+0 записей отправлено скопировано 17408 байт (17 kB), 0,0217623 c, 800 kB/c
Проверяю диск на наличие GPT:
sudo fdisk -l /dev/sdd Disk /dev/sdd: 7,5 GiB, 8022654976 bytes, 15669248 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 sudo gdisk -l /dev/sdd 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. Disk /dev/sdd: 15669248 sectors, 7.5 GiB Logical sector size: 512 bytes Disk identifier (GUID): 595409AB-6F78-4DD7-A1E9-84993BE23171 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 15669214 Partitions will be aligned on 2048-sector boundaries Total free space is 15669181 sectors (7.5 GiB) Number Start (sector) End (sector) Size Code Name
Пробую восстановить GPT из образов:
sudo dd if=~/sdd.primary_gpt of=/dev/sdd bs=512 count=34 34+0 записей получено 34+0 записей отправлено скопировано 17408 байт (17 kB), 0,00764047 c, 2,3 MB/c sudo dd if=~/sdd.secondary_gpt of=/dev/sdd seek=$((($(fdisk -l /dev/sdd | grep Disk | awk '')-17408)/$_blocksize)) obs=$_blocksize The backup GPT table is corrupt, but the primary appears OK, so that will be used. 34+0 записей получено 34+0 записей отправлено скопировано 17408 байт (17 kB), 0,022258 c, 782 kB/c
Проверяем GPT на наличие и здравие:
sudo fdisk -l /dev/sdd Disk /dev/sdd: 7,5 GiB, 8022654976 bytes, 15669248 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: gpt Disk identifier: 15A4B379-6E0D-4222-B39A-A60A61EB48C5 Device Start End Sectors Size Type /dev/sdd1 2048 15669214 15667167 7,5G Linux filesystem gdisk -l /dev/sdd GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present
Все ок! В итоге все благополучно смонтировалось, файлы не пострадали.
How to remove GPT from HDD?
I have a Asus K55VM-Sx027V laptop and I formated it with Ubuntu because whenever I tried to install Windows 7 64bit, it wouldn’t let me because my HDD had GPT protection. Now that I have Ubuntu 12.10, how do I remove GPT protection from my laptop?
6 Answers 6
Use gdisk instead of fdisk. It isn’t installed by default, so get it with:
sudo apt-get install gdisk
Then umount the drive and call gdisk for the device:
It will prompt you to select the partition:
Found valid MBR and GPT. Which do you want to use? 1 - MBR 2 - GPT 3 - Create blank GPT
Select the GPT one. In my case, 2 . Use the ? command to print the command list. Enter x for expert options, then z to zap the GPT table and all the data on the disc:
Command (? for help): x Expert command (? for help): z About to wipe out GPT on /dev/sdx. Proceed? (Y/N): y GPT data structures destroyed! You may now partition the disk using fdisk or other utilities. Blank out MBR? (Y/N): y
You can use wipefs to remove all common id blocks from a device before changing the partition format.
easier on the eye than on the computer, three years later there is no more packages.debian.org/wipefs if there ever was, click the locator to check
With a simple Python function!
def clear_gpt(target): ''' According to http://en.wikipedia.org/wiki/GUID_Partition_Table - GPT stores partition data in the first and last 34 LBA blocks. A LBA sector is normally 512 bytes. ''' fd = open(target, "w+") fd.seek(0) fd.write('\0' * 34 * 512) print "done nuking data at the beginning of disk", target fd.seek(0, 2) # SEEK_END is 2 disk_size = fd.tell() fd.seek(disk_size - 34*512) fd.write('\0' * 34 * 512) print "done nuking data at the end of disk", target
GPT is the GUID Partition Table, which is a method of defining partitions (not protection) on your computer. If Windows is complaining about your use of GPT, that means that the Windows installer has booted in BIOS mode rather than in (U)EFI mode. There are two ways to work around this problem:
- Boot the Windows installer in EFI mode. This might or might not be possible, depending on your computer. If it’s new (sold in the last 6-12 months), it probably supports EFI-mode booting. To boot in EFI mode, you may need to fiddle with your firmware settings to enable EFI-mode booting. Sometimes hitting the button to get to the boot options when you boot will produce two options for booting your CD: one in BIOS (aka legacy) mode and the other in EFI mode.
- Convert the hard disk to use MBR partitioning rather than GPT. You can do this with GPT fdisk (gdisk) by using its «g» option on the «recovery & transformation» menu. There are, however, a lot of caveats and details to such a conversion; see the GPT fdisk documentation for details. When you’re done, Ubuntu will no longer boot; you’ll need to re-install the boot loader. (You’ll need to do this after installing Windows in BIOS mode anyhow, so you might as well put this off until after you install Windows.)
EFI-mode booting with GPT is still very new and can be trouble-prone, but converting your partition table from GPT to MBR is also a rather risky endeavor. Thus, it’s hard for me to say which approach is best. Of course, you might not even be able to boot your computer in EFI mode, so you might have to do it by converting your partition table.
An alternative to either approach is to run Windows from within VirtualBox or some other virtualization environment under Linux. This is simpler and safer, and it may be adequate for many purposes. It’s most likely to be a problem if you want to run video-intensive games, if you need low-level access to the hardware, or if you have inadequate RAM to support a virtualized environment sufficient for your needs.
Whichever approach you take (aside from a virtualized Windows), I strongly recommend you back up any important data first; mucking with partition tables is always risky.
Edit: Zolar1’s comments suggest the possibility that Ubuntu is installed in BIOS mode but using GPT. Installing Windows in EFI mode in this case will require one of two things:
- Converting Ubuntu to boot in EFI mode rather than in BIOS mode
- Switching boot modes (EFI for Windows, BIOS for Linux)
Either is possible. You can convert Ubuntu to boot in EFI mode by adding an EFI boot loader. There are several options, as described here. Ubuntu uses GRUB 2 as its EFI-mode boot loader by default, but IMHO this is a poor choice.
Switching between EFI-mode and BIOS-mode boots of the computer is usually awkward, but sometimes a boot options switch (accessible by pressing F8, F12, or some other key at boot time) can make this tolerable. Another options may be to install rEFInd, which is an EFI-mode boot manager that can (as of version 0.4.6) hand off the boot process to a BIOS-mode boot loader (or to an EFI boot loader).