Hobo Chen

Add a Disk under Linux and Auto-mounting

This post will cover how to add a new disk, format it and then mount it under Linux.

Cause

I met the problem “no enough disk space” when compiling the Linux kernel at the first time, as the VMware says the recmmended size of Linux is 20GiB, and the real size which compiling needs is about 15GiB (Linux 4.4.0).

Add a Disk

This step is quite easy. Just change the settings of the VMware machine.

Check

1
sudo fdisk -l

You may see like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Many things, like /dev/ram*
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: dos
Disk identifier: 0xb0ac163a

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 37750783 37748736 18G 83 Linux
/dev/sda2 37752830 41940991 4188162 2G 5 Extended
/dev/sda3 37750784 37752829 2046 1023K 83 Linux
/dev/sda4 41940992 83886079 41945088 20G 83 Linux
/dev/sda5 37752832 41940991 4188160 2G 82 Linux swap / Solaris

Partition table entries are not in disk order.


Disk /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 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

That shows the /dev/sdb is the disk i added before.

Partition

1
sudo fdisk /dev/sdb

Then type:

1
2
3
4
g # create a new GPT disk lable
n # new partition, i only add one partition is this disk,
# so i press enter for two times then
w # write the changes

Format

1
sudo mkfs -t ext4 /dev/sdb1 # use ext4 file system

Mount

1
2
mkdir ~/flush
sudo mount /dev/sdb1 ~/flush

Then check the new disk.

1
df -h

You may see:

1
2
3
4
5
6
7
8
9
10
Filesystem      Size  Used Avail Use% Mounted on
udev 2.0G 0 2.0G 0% /dev
tmpfs 394M 5.7M 388M 2% /run
/dev/sda1 18G 7.0G 9.8G 42% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda4 20G 14G 5.2G 73% /home/hobochen/os
tmpfs 394M 0 394M 0% /run/user/1000
/dev/sdb1 493G 70M 467G 1% /home/hobochen/flush

Auto Mount when Boot

1
sudo vim /etc/fstab

Add this line in the end of file.

1
/dev/sdb1	/home/hobochen/flush ext4 defaults 0 0

All is done!