Linux Format

Terminal basics Partitions...........................

Nick Peers reveals everything you need to know about setting up a hard disk from partitioni­ng, formatting and setting permission­s.

-

Nick Peers explains how you can use text input to partition your drives, one day you just might have to do it.

Acore hard drive skill is partitioni­ng. You’ll have encountere­d this during the Ubuntu setup, but there may be times when you need to repartitio­n a drive— or set one up for the first time. In this month’s Terminal tutorial, we’ll examine how this is done from the command line. There are two tools you can use: fdisk and parted. The former, fdisk, is better known, and one of its strengths is that any changes you make aren’t immediatel­y written to disk; instead you set things up and use the w command to exit and write your changes to disk. If you change your mind or make a mistake, simply type q and press Enter instead and your drive is left untouched.

Traditiona­lly, fdisk has only been known to support the older MBR partition scheme, which limited its use to drives that are under 2TB in size. From Ubuntu 16.04, however, fdisk directly supports larger drives and the GPT partition scheme. If you’re running an older version of Ubuntu, substitute fdisk with gdisk instead for a version of fdisk with GPT support.

Another limitation of fdisk is that it’s purely a destructiv­e tool. That’s fine if you’re partitioni­ng a disk for the first time or are happy deleting individual partitions (or indeed wiping the entire disk and starting from scratch). If you want to be able to resize partitions without deleting the data on them, however, then you’ll need parted instead ( see the box Resize partitions ford et ails, p 69).

Partition with fdisk

Let’s begin by using fdisk to list the drives on your system: sudo fdisk -l

Each physical drive – sda, sdb and so on – will be displayed one after the other. To restrict its output to a specific disk, use sudo fdisk -l <device> , replacing <device> with /dev/sda or whichever disk you wish to poll. You’ll see the disk’s total size in GiB or TiB, with the total number of bytes and sectors also listed. You’ll see the sector size (typically 512 bytes) and the disk type: DOS (traditiona­l MBR) or GPT. There’s also the disk identifier, which you can ignore for the purposes of this tutorial.

Beneath this you’ll see a list of all existing partitions on the drive, complete with start and end points (in bytes), size and type, such as ‘Linux filesystem’, ‘Linux swap’ or ‘Windows recovery environmen­t’. Armed with this informatio­n you should be able to identify each drive, helping you target the one you wish to partition. This is done as follows:

sudo fdisk <device>

This will have fdisk switch to command mode. Type m and hit Enter to see a list of all supported commands. Let’s start by checking the existing partition table for the drive: type p and hit Enter. This displays the same output as the fdisk -l command. If the disk isn’t currently empty, you’ll see a list of existing partitions appear. From here you have two options: wipe the disk completely and start from scratch or remove individual partitions and replace them.

Before going any further, remember the fail-safe: until you exit with the w command, no changes are made. So if you make a mistake and want to start again, use q instead, then start from scratch.

To mark an existing partition for deletion—thereby wiping all its data, but leaving the rest of the disk intact – type d and hit Enter. You’ll be prompted to enter the partition number, which you can identify from the device list (eg, ‘1’ refers to

sdb1 and ‘2’ to sdb2 etc). Press the number and the partition is marked for deletion, which you can verify by typing p again —it should no longer be listed.

Alternativ­ely, wipe the entire disk – including all existing partitions on it – and start from scratch. To do this, you need to create a new partition table (or label). There are four options, but for most people you’ll either want a DOS/MBR partition table (type o ) or GPT (type g ) one.

Once the disk is empty or you’ve removed specific partitions, the next step is to create a new partition (or more). Type n and hit Enter. You’ll be prompted to select a partition number up to 4 (MBR) or 128 (GPT) and in most cases, just pick the next available number. You’ll then be asked to select the first sector from the available range—if in doubt, leave the default selected. Finally, you’ll be prompted to set the drive’s size, either by setting its last sector, choosing the number of sectors to add or – the easiest choice – by entering a physical size for the partition, typically in G (gigabytes) or T (terabytes). To create a partition 100GB in size, type +100G and hit Enter. At this point, fdisk will tell you it’s created a new partition of type ‘Linux filesystem’. If you’d rather the partition used a different file system, type t followed by the partition number. You’ll be prompted to enter a Hex code—pressing l lists a large range of alternativ­es, and the simplest thing from here is to select the hex code you want with the mouse, rightclick and choose ‘Copy’ and right-click at the fdisk prompt and choose ‘Paste’. If you want to create a FAT, exFAT/NTFS or FAT32 file system, eg, paste the ‘Basic Microsoft data’ code.

Happy with the way you’ve set up your partitions? Type p one more time and verify everything’s the way you want to set it, then press w and hit Enter to write your changes to the disk. Although the disk has been partitione­d, you now need to format it. This is done using the mkfs command: sudo mkfs -t <fs-type> <device> Replace <fs-type> with the relevant filesystem (ext3 or fat32, eg) and <device> with your device (such as /dev/ sdb1). Depending on the size of the partition this can take a while to complete—a couple of hours in extreme cases. Once done, you’ll need to mount the drive to a specific folder: sudo mount <device> <mountpoint> In most cases, you’ll want to set <mountpoint> to / media/<username>/<folder> , replacing <username> with your username, and creating a folder there for the partition to reside in, eg: sudo mount /dev/sdb1 /media/nick/3TBdrive .

Fix drive permission­s

If you format the drive using the default ‘Linux filesystem’ option then as things stand you have no write permission­s on the drive—to fix this for an external drive, type the following: sudo chown -R <username> <mountpoint>

If you’d like to widen access to the drive without giving up ownership, try the following three commands: sudo chgrp plugdev <mountpoint> sudo chmod g+w <mountpoint> && sudo chmod +t <mountpoint>

This will allow members of the plugdev group to create files and sub-directorie­s on the disk—the +t flag ensures they can only delete their own files and sub-folders.

Finally, note that drives aren’t automatica­lly mounted at each startup – you’ll need to manually add them to the / etc/ fstab file (see LXF111 for advice on editing configurat­ion files) – here’s the line you should add for ext3 file systems: <UUID> <mountpoint> ext3 defaults 0 2

You will need to replace <UUID> with the partition’s Disk Identifier, which you can get by using the sudo blkid <device> command. You want to use this identifier instead of the device itself, because it’s the only consistent way of identifyin­g the partition. Once saved, test that this works with the sudo mount -a command—if there are no errors, congratula­tions: your hard disk has been partitione­d and set up correctly!

 ??  ?? You must set up appropriat­e permission­s on the new partition after formatting it in order to access it.
You must set up appropriat­e permission­s on the new partition after formatting it in order to access it.

Newspapers in English

Newspapers from Australia