Example of how to mount disks under Linux

  • 2021-06-28 14:37:19
  • OfStack

When you use the virtual machine, you find that there is not enough disk space and you need to mount a disk for continued use, but the disk is not added for use and you need to mount it.

1. Add disks

Add a new hard drive to restart the server

After adding, you can restart the machine. If your machine is on, you can't see the disk you just added when entering the system. The installed disk will not be displayed until the system is restarted.

2. Enter the system

Use root User Entry System

3. View hard disk information


[root@localhost ~]# fdisk -l  // Disk Command 

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c4cb5

  Device Boot   Start     End   Blocks  Id System
/dev/sda1  *      1     64   512000  83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2       64    2611  20458496  8e Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd0f5c869

  Device Boot   Start     End   Blocks  Id System
/dev/sdb1        1    2610  20964793+ 83 Linux

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

You can see that this machine has three disks loaded: sda, sdb, sdc

sda is the initial disk, sdb has been initialized and used, sdc is the newly loaded, unformatted new disk

4. Create a new hard disk partition


[root@localhost ~]# fdisk /dev/sdc # Enter Disk 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x45a3cadb.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
     switch off the mode (command 'c') and change display units to
     sectors (command 'u').

Command (m for help): m
Command action
  a  toggle a bootable flag   # Set Startable Tag 
  b  edit bsd disklabel
  c  toggle the dos compatibility flag
  d  delete a partition     # delete 1 Partitions 
  l  list known partition types # Corresponding to each partition type ID
  m  print this menu       # menu 
  n  add a new partition     # Add to 1 Partitions 
  o  create a new empty DOS partition table
  p  print the partition table  # Display current partition information under this disk 
  q  quit without saving changes # Exit without saving 
  s  create a new empty Sun disklabel
  t  change a partition's system id
  u  change display/entry units
  v  verify the partition table
  w  write table to disk and exit # Save Exit 
  x  extra functionality (experts only)

Once you know the command, you can partition


Command (m for help): p // Print partition information , You can see that there are currently no partitions 

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x45a3cadb

  Device Boot   Start     End   Blocks  Id System

Command (m for help): n // Establish 1 New partition 
Command action
  e  extended// input e To create an extended partition  
  p  primary partition (1-4) // input p To create logical partitions  
p
Partition number (1-4): 1// Partition Logical Partition 
First cylinder (1-2610, default 1): // I'll return directly here , Is not to divide the disk into partitions , Use the entire disk as a 1 Partitions 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 
Using default value 2610

Command (m for help): p // Check again to see that the disk already exists 1 Partitions 

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x45a3cadb

  Device Boot   Start     End   Blocks  Id System
/dev/sdc1        1    2610  20964793+ 83 Linux
Command (m for help): w // Save partition 
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Use the "fdisk-l" command again to view disk information


Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x406a4c58

  Device Boot   Start     End   Blocks  Id System
/dev/sdc1        1    2610  20964793+ 83 Linux

Disk 3 / dev / sdc is partitioned

5. Format partitions


[root@localhost ~]# mkfs.ext3 /dev/sdc1
// take /dev/sdc1 Format as ext3 type , It looks like most disks are formatted ext3 type , Why is there no in-depth study? , Not clear yet , Friends you want to know can check for themselves 1 lower 

[root@localhost ~]# mkfs.ext3 /dev/sdc1
mke2fs 1.41.12 (17-May-2010)
 File System Label =
 operating system :Linux
 Block size =4096 (log=2)
 Block size =4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241198 blocks
262059 blocks (5.00%) reserved for the super user
 No. 1 Data blocks =0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
  4096000

 Writing inode surface :  complete               
Creating journal (32768 blocks):  complete 
Writing superblocks and filesystem accounting information:  complete 

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Now that the formatting is complete, you can mount the partition using the mount command and use this disk space

6. Mount partition and boot-up automount


[root@localhost ~]# df -h // At this point only sda1 and sdb1 Two Disk Mounts 
Filesystem          Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  18G  15G 1.5G 92% /
tmpfs             932M  76K 932M  1% /dev/shm
/dev/sda1           485M  40M 421M  9% /boot
/dev/sdb1           20G 1.2G  18G  7% /disk/diskone
/dev/sr0           4.2G 4.2G   0 100% /media/CentOS_6.5_Final
[root@localhost /]# cd /disk/
[root@localhost disk]# ll
 Total dosage  4
drwxr-xr-x. 4 root root 4096 7 month  28 17:04 diskone
[root@localhost disk]# cd diskone/
[root@localhost diskone]# ll
 Total dosage  20
drwx------. 2 root root 16384 7 month  28 16:12 lost+found
drwxr-xr-x. 2 root root 4096 7 month  28 17:09 software
[root@localhost diskone]# cd ../
[root@localhost disk]# mkdir disktwo // Create mounted path 
[root@localhost disk]# ll
 Total dosage  8
drwxr-xr-x. 4 root root 4096 7 month  28 17:04 diskone
drwxr-xr-x. 2 root root 4096 8 month   1 16:48 disktwo
[root@localhost disk]# mount /dev/sdc1 /disk/disktwo/ // Mount 
[root@localhost disk]# df -h // View again , find sdc1 Already mounted on /disk/disktwo Under Path 
Filesystem          Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  18G  15G 1.5G 92% /
tmpfs             932M  76K 932M  1% /dev/shm
/dev/sda1           485M  40M 421M  9% /boot
/dev/sdb1           20G 1.2G  18G  7% /disk/diskone
/dev/sr0           4.2G 4.2G   0 100% /media/CentOS_6.5_Final
/dev/sdc1           20G 173M  19G  1% /disk/disktwo

Disk is mounted

Set up boot-up automount below

Edit/etc/fstab file and add it at the end
/dev/sdc1 (Disk Partition)/disk/disktwo (Mount Directory) ext3 (File Format) defaults 0


vim /etc/fstab
/dev/sdc1( Disk Partition ) /disk/disktwo (mount directory)  ext3 (File format) defaults 0 0

Then restart OK!!!


Related articles: