Linux Disk Management Tools
Background info
The old standard for managing disk partitions was via the Master Boot Record (MBR). This was replaced by GUID Partition Table (GPT). This is more flexible, more resilient, allows larger partition sizes, and is used with UEFI systems.
Tools like fdisk
used to only support MBR, which made parted more useful for GPT support, but fdisk now also supports GPT.
fdisk
fdisk is an interactive partition manager
> fdisk -l [optional_disk_id] # view all available partitions <on selected disk>
> fdisk -s /dev/sda1 # view size (in blocks) of specified partition
> fdisk /dev/sda # start interactive management with selected disk
Command (m for help): m
Help:
GPT
M enter protective/hybrid MBR
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
In this context label
is the partition table, and a DOS
partition table refers to MBR
. You’ll probably want GPT
.
So, to delete a partition:
Command (m for help): d
Partition number (1-4): 2
Command (m for help): w
The partition table has been altered!
To create a new partition:
Command (m for help): n
First cylinder (2824-8269, default 2662):
Using default value 2662
Last cylinder, +cylinders or +size{K,M,G} (2824-8269, default 8269):
Using default value 3264
Command (m for help): w
The partition table has been altered!
Note that you can specify the last cylinder, an offset number of cylinders or an offset size in K, M or G.
Once a partition is created, you need to format it. You can use mkfs:
mkfs.ext4 /dev/sda1
gdisk
gdisk
is the GPT version of fdisk
However, fdisk
now has baked in support for GPT, so you can just use that.
cfdisk
cfdisk
is an ncurses-based version of fdisk, which makes navigating the interface a bit nicer.
fixparts
This is part of the fdisk family of commands, and lets you manipulate MBR partitions, and even fix brokem MBRs.
parted
# parted
GNU Parted 3.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
To list all available partitions on a disk:
parted /dev/sda
print # print partitions on selected disk, if none was specified, defaults to first disk
print free # print the free space on the disk
print all # print all partitions on all disks
select /dev/sdb # select a different device
To create a partition:
parted /dev/sda
mklabel msdos # make a MBR partition table
mklabel gpt # make a GUID partition table
unit % % change unit to % (can be s, kB,MB,GB,TB, kiB,MiB,GiB,TiB...)
mkpart
Partition type? primary/extended? primary # create primary partition
File system type? [ext4]?
Start? 0 # start of partition
End? 100 # end of partition in units
(parted) print
### gparted
```gparted``` is a gui based patition manager. Despite the name, it is not feature equivalent to ```parted```., although ```parted``` is a dependency for ```gparted```.
You can use the mkfs
command within the parted shell.
You can also rm, resize and rescue partitions and set flag states.
You can also pass commands as arguments to skip the interactive shell.
parted /dev/sda mklabel gpt unit % mkpart P1 ext3 0 100
mount
In order to access data in partitions, they need to be mounted into the file system directory tree.
mount # show current mount status
mount -t <type> <device> <dir> -o <options> # usual command format
mount -t ext4 /dev/sda1 /mnt -o ro # mount a read only partition at /mnt
mount /dev/sda2 /mnt -o defaults # mount with explicit defaults
You can umount
a mounted disk.
You can also mount SMB and NFS network shares.
Samba
You’ll need to ensure you have the samba-client samba-common cifs-utils
packages.
Find the share:
smbclient -L 192.168.0.94
Store your credentials in a safe place:
mkdir /media/samba
echo “username=DONT_WRITE_YOUR_CREDENTIALS_IN_FSTAB” > /media/samba/.samba_credentials
echo “password=DONT_WRITE_YOUR_CREDENTIALS_IN_FSTAB” >> /media/samba/.samba_credentials
chmod 600 /media/samba/.samba_credentials
Add the share to the fstab file
//192.168.0.94/shared_folder /media/samba cifs credentials=/media/samba/.samba_credentials,defaults 0 0
Reboot, or mount //192.168.0.10/shared_folder
.
NFS Shares
You’ll need to ensure you have the nfs-utils nfs-utils-lib / nfs-common
packages.
mkdir /media/nfs
Appendt to etc/fstab
file.
192.168.0.94:/NFS-SHARED /media/nfs nfs defaults 0 0
And, again, reboot or mount manually.
See LFCS: How to Mount/Unmount Local and Network (Samba & NFS) Filesystems in Linux - Part 5 for full details.
fstab
The /etc/fstab
file is consulted at boot time to mount the listed partitions and removable media. You can specify partitions by UUID or label
# <file system> <mount point> <type> <options> <dump> <pass>
LABEL=TECMINT /mnt ext4 rw,noexec 0 0
//192.168.0.94/shared_folder /media/samba cifs credentials=/media/samba/.samba_credentials,defaults 0 0
UUID=a833982-9db8b23ca-cb8a8739eab /mnt/backup ext4 defaults 0 0
mdadm
This tool lets you easily create and manage software raid arrays.
mdadm --create --verbose <target> --level=1 <source_partition_1> <source_partition_2>
mdadm --create --verbose /dev/md0 --level=1 /dev/sda1 /dev/sdb2 # RAID 1
mdadm --create --verbose /dev/md0 --level=10 /dev/sda1 /dev/sdb2 /dev/sda3 /dev/sdb4 # RAID 10
Then add store the configuration
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
To remove a disk from an array,:
mdadm --remove /dev/md0 /dev/sda1
The disk needs to have failed first, so you can manually fail a good disk with
mdadm --fail /dev/md0 /dev/sda1
To add a new replacement drive:
mdadm --add /dev/md0 /dev/sdb1
To check the status of a raid array
mdadm --detail /dev/md0
zpool
Find the by-id paths for the drives you want to use
ls -l /dev/disk/by-id/ | grep "ata" | grep "sd[a|b]$" --color
lrwxrwxrwx 1 root root 9 May 15 18:55 ata-ST4000DM000-1F2168_W300F2F0 -> ../../sdb
lrwxrwxrwx 1 root root 9 May 15 18:55 ata-ST4000VN000-1H4168_Z30150DN -> ../../sda
Then create zpools:
# worse way, but easier to read (see below)
zpool create -f my_pool_raid0 /dev/sdb /dev/sdc # create a raid 0 zpool called my_pool_raid0
zpool create -f my_pool_raid1 mirror /dev/sdb /dev/sdc # create a raid 0 zpool called my_pool_raid0
# better way
zpool create -f my_pool_raid0 /dev/disk/by-id/ata-ST4000VN000-1H4168_Z30150DN /dev/disk/by-id/ata-ST4000DM000-1F2168_W300F2F0 # create a raid 0 zpool called my_pool_raid0
zpool create -f my_pool_raid1 mirror /dev/disk/by-id/ata-ST4000VN000-1H4168_Z30150DN /dev/disk/by-id/ata-ST4000DM000-1F2168_W300F2F0 # create a raid 0 zpool called my_pool_raid0
The mirror keyword indicates that you wish to mirror the listed drives. Otherwise, the drives are striped. You can thus combine these, and create a raid 10 pool:
zpool create my_pool_raid10 mirror disk1 disk2 mirror disk3 disk4
Create a raidz
zpool create -f my_pool_raidz raidz disk1 disk2 disk3
Create a raidz2 (my preference)
zpool create -f my_pool_raidz2 raidz2 disk1 disk2 disk3
You can add a drive to an existing pool:
zpool add my_pool_raidz2 disk4 # add drive to existing zpool
Note on disk IDs
You should make sure that you use the by-id disk paths, rather than the OS drive path (/dev/sda
) because the OS paths can change if you ever move the disks physically in your chassis. This can cause your zpool to become degraded.
You can fix this (or stop it happening) by exporting the zpool config, and re-importing it, since zpool will import the pool with the correct IDs.
zpool export [my pool name]
zpool import -d [my pool name]
See Also
See ZFS Tutorials : Creating ZFS pools and file systems – The Geek Diary for further information about creating a zfs file system and assigning reservation and quota space.
lsblk
lsblk
lists the block devices on your machine. cf. lsusb
df
df
shows you the disk capacity of disks on your system
df -ah # show all the mounted drives, with memory information in human-readabli format
du
du
shows the disk usage, recursively for all (sub)directorys you have permisssion to access.
du -sh .
The ncurses version, ncdu
is a bit nicer to use interactively.