Wednesday, July 29, 2020

Online disk resize and MBR to GPT upgrade

I'm upgrading from a 2TB drive to a 6TB drive in my home server, Gus (named for the character in Psych), which has been running since just after Christmas 2010.

In 2016 I had to rebuild the OS onto a new HDD due to disk corruption.

First, I checked the new 6TB Western Digital Blue HDD for bad blocks by plugging it into my WavLink dual-bay docking station/drive cloner (aka "the toaster") and running commands like this:

jonathan@hastings:~$ # Determine the device file for the disk I just connected.
jonathan@hastings:~$ dmesg | tail
[1429251.453182] scsi host7: uas
[1429251.453762] scsi 7:0:0:0: Direct-Access USB3.0 0101 PQ: 0 ANSI: 6
[1429251.454622] sd 7:0:0:0: Attached scsi generic sg1 type 0
[1429251.455556] sd 7:0:0:0: [sdc] 11721045168 512-byte logical blocks: (6.00 TB/5.46 TiB)
[1429251.455757] sd 7:0:0:0: [sdc] Write Protect is off
[1429251.455759] sd 7:0:0:0: [sdc] Mode Sense: 67 00 10 08
[1429251.456138] sd 7:0:0:0: [sdc] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[1429251.478343] sdc: sdc1
[1429251.481041] sd 7:0:0:0: [sdc] Attached SCSI disk

jonathan@hastings:~$ # Note the 'sdc' above, and replace 'sdx' below with that.
jonathan@hastings:~$ sudo badblocks -sw -b 4096 -c 1024 -o ~/tmp/badblocks.out /dev/sdx

This took 3 days to run a single write/read pass, but that's my fault; I plugged it into a USB 2.0 port by accident, and then, when I realized, I figured, "How long can it take, really?" But it was definitely worth it, because this was actually the fourth drive I received; the first three were all bad, likely due to rough shipping.

Once that was complete, it was time for the copy. I unplugged the toaster from my laptop, pulled the old 2TB HDD out of Gus, and put it into the 'Source' bay, with the new 6TB HDD in the 'Target' bay. Then I turned it on, held the red button until it started flashing, and then tapped the red button again to start the copy. It took about three hours.

Next, I put the new HDD back into Gus and powered it up. This is where things got interesting, because I was unaware of MBR vs. GPT partition table formats, and the fact that an MBR table only supports disks up to 2TB. Luckily, because I was only using 2TB, Gus booted up fine.

How to upgrade from MBR to GPT

I've filtered the commands below to just the useful stuff, without including all of the wrong paths I took trying to figure out what I needed to do:

jonathan@gus:~$ # Convert from MBR to GPT:
jonathan@gus:~$ # Once you've started, DO NOT REBOOT until you have finished!
jonathan@gus:~$ sudo gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.3

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: MBR only
BSD: not present
APM: not present
GPT: damaged

Found valid MBR and corrupt GPT. Which do you want to use? (Using the
GPT MAY permit recovery of GPT data.)
1 - MBR
2 - GPT
3 - Create blank GPT

Your answer: 1

Command (? for help): p
Disk /dev/sda: 11721045168 sectors, 5.5 TiB
Model: WDC WD60EZAZ-00Z
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 28E343DC-CD48-47A5-BE29-82C07F3BC14E
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 11721045134
Partitions will be aligned on 2048-sector boundaries
Total free space is 7814017981 sectors (3.6 TiB)

Number Start (sector) End (sector) Size Code Name
1 2048 3907029167 1.8 TiB 8E00 Linux LVM

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/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

jonathan@gus:~$ # Resize from 2TB to 6TB, leaving a small partition at the end for Grub's second stage:
jonathan@gus:~$ sudo parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA WDC WD60EZAZ-00Z (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 2000GB 2000GB Linux LVM lvm

(parted) help resizepart
resizepart NUMBER END resize partition NUMBER

NUMBER is the partition number used by Linux. On MS-DOS disk labels, the primary partitions number from 1 to 4, logical partitions from 5 onwards.
END is disk location, such as 4GB or 10%. Negative value counts from the end of the disk. For example, -1s specifies exactly the last sector.
(parted) resizepart 1 6000GB
(parted) print
Model: ATA WDC WD60EZAZ-00Z (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 6000GB 6000GB Linux LVM lvm

(parted) help mkpart
mkpart PART-TYPE [FS-TYPE] START END make a partition

PART-TYPE is one of: primary, logical, extended
FS-TYPE is one of: zfs, btrfs, nilfs2, ext4, ext3, ext2, fat32, fat16, hfsx, hfs+, hfs, jfs, swsusp, linux-swap(v1), linux-swap(v0), ntfs, reiserfs, freebsd-ufs,
hp-ufs, sun-ufs, xfs, apfs2, apfs1, asfs, amufs5, amufs4, amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6, affs5, affs4, affs3, affs2, affs1, affs0, linux-swap,
linux-swap(new), linux-swap(old)
START and END are disk locations, such as 4GB or 10%. Negative values count from the end of the disk. For example, -1s specifies exactly the last sector.

'mkpart' makes a partition without creating a new file system on the partition. FS-TYPE may be specified to set an appropriate partition ID.
(parted) mkpart primary 6000GB 100%
(parted) print
Model: ATA WDC WD60EZAZ-00Z (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 6000GB 6000GB Linux LVM lvm
2 6000GB 6001GB 1174MB primary

(parted) help toggle
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER

NUMBER is the partition number used by Linux. On MS-DOS disk labels, the primary partitions number from 1 to 4, logical partitions from 5 onwards.
FLAG is one of: boot, root, swap, hidden, raid, lvm, lba, hp-service, palo, prep, msftres, bios_grub, atvrecv, diag, legacy_boot, msftdata, irst, esp
(parted) toggle 2 bios_grub
(parted) print
Model: ATA WDC WD60EZAZ-00Z (scsi)
Disk /dev/sda: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
1 1049kB 6000GB 6000GB Linux LVM lvm
2 6000GB 6001GB 1174MB primary bios_grub

(parted) quit
Information: You may need to update /etc/fstab.

jonathan@gus:~$ # Install Grub into the new GPT and trailing drive partition:
jonathan@gus:~$ sudo grub-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.

jonathan@gus:~$ # It is now safe to reboot.

jonathan@gus:~$ # The remaining steps add the new bytes in the expanded partition to my LVM volume group.
jonathan@gus:~$ # Resize the physical LVM volume to take up the full partition:
jonathan@gus:~$ sudo pvresize /dev/sda1
Physical volume "/dev/sda1" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
jonathan@gus:~$ sudo vgdisplay gus_vg
--- Volume group ---
VG Name gus_vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 19
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 9
Open LV 9
Max PV 0
Cur PV 1
Act PV 1
VG Size <5.46 TiB
PE Size 4.00 MiB
Total PE 1430510
Alloc PE / Size 451584 / 1.72 TiB
Free PE / Size 978926 / 3.73 TiB
VG UUID XHTcXt-iK4c-yFt6-CGt8-rdyA-o2Jl-NW2X7r

jonathan@gus:~$ # And finally, my wife has been asking for more space in her home directory, so:
jonathan@gus:~$ sudo lvextend -L +100G /dev/gus_vg/home_sara_lv
Size of logical volume gus_vg/home_sara_lv changed from 570.00 GiB (145920 extents) to 670.00 GiB (171520 extents).
Logical volume gus_vg/home_sara_lv successfully resized.

jonathan@gus:~$ sudo resize2fs /dev/gus_vg/home_sara_lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/gus_vg/home_sara_lv is mounted on /home/sara; on-line resizing required
old_desc_blocks = 36, new_desc_blocks = 42
The filesystem on /dev/gus_vg/home_sara_lv is now 175636480 (4k) blocks long.

Note: As an Amazon associate, I may earn a commission from purchases made through links followed from this page.

No comments: