您的位置:首页 > 运维架构 > Linux

linux 管理磁盘

2016-02-22 00:21 645 查看
环境:

ubuntu 15.04

学习有关磁盘的一些操作及概念,挂载,文件系统,创建ios文件,移动硬盘分区及格式化。

edemon@linux:~$ sudo mkdir /mnt/cdrom     #创建挂载目录
[sudo] password for edemon:
edemon@linux:~$ cd /mnt
edemon@linux:/mnt$ sudo mount /dev/cdrom /mnt/cdrom   # mount挂载
mount: /dev/sr0 is write-protected, mounting read-only   #只读方式挂载
edemon@linux:~$ cd /mnt/cdrom
edemon@linux:/mnt/cdrom$ ls
autorun.inf  dists     md5sum.txt  preseed             wubi.exe
boot         install   pics        README.diskdefines
casper       isolinux  pool        ubuntu
edemon@linux:/mnt/cdrom$ cd
edemon@linux:~$ sudo umount /dev/cdrom   #卸载挂载的光盘
edemon@linux:~$ cd /mnt/cdrom
edemon@linux:/mnt/cdrom$ ls
edemon@linux:/mnt/cdrom$


通过文件操作硬件,每个文件都有不同的含义,如 /dev/disk/sda1 代表第一块硬盘上的第一个主分区
存储设备不能直接访问(打开),必须先挂载到一个目录中。

查看linux下挂载分区情况:

edemon@linux:~$ sudo fdisk -l
[sudo] password for edemon:

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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: 0x2ac21aa0

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39845887 39843840   19G 83 Linux
/dev/sda2       39847934 41940991  2093058 1022M  5 Extended
/dev/sda5       39847936 41940991  2093056 1022M 82 Linux swap / Solaris


插上移动硬盘后,查看会发现多了一个设备文件:
edemon@linux:~$ sudo fdisk -l

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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: 0x2ac21aa0

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39845887 39843840   19G 83 Linux
/dev/sda2       39847934 41940991  2093058 1022M  5 Extended
/dev/sda5       39847936 41940991  2093056 1022M 82 Linux swap / Solaris

Disk /dev/sdb: 465.7 GiB, 500074283008 bytes, 976707584 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: 0x6f32fa54

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdb1        2048 976707583 976705536 465.7G  7


挂载查看移动硬盘的内容:
edemon@linux:~$ sudo mount /dev/sdb1 /mnt/usb1
Mount is denied because the NTFS volume is already exclusively opened.
The volume may be already mounted, or another software may use it which
could be identified for example by the help of the 'fuser' command.
edemon@linux:~$ sudo umount /dev/sdb1
edemon@linux:~$ sudo mount /dev/sdb1 /mnt/usb1
edemon@linux:~$ cd /mnt/usb1
edemon@linux:/mnt/usb1$ ls
autorun      ReadMe.pdf    System Volume Information    安装包
autorun.inf  $RECYCLE.BIN  WD Smartware Pro Free Trial


打开窗口:



dd制作的iso文件(同时也明白了如果目标就在当前目录,使用locate和whereis是不返回值的):

创建ios文件:
edemon@linux:~/test$ dd if=/dev/cdrom of=ubuntu1504.iso
2321984+0 records in
2321984+0 records out
1188855808 bytes (1.2 GB) copied, 102.33 s, 11.6 MB/s


解释:if=input file of=output file



查看磁盘的挂载情况:
edemon@linux:~$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev              501860        0    501860   0% /dev
tmpfs             102456     9432     93024  10% /run
/dev/sda1       19478204  6091728  12373996  33% /
tmpfs             512268      156    512112   1% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
tmpfs             512268        0    512268   0% /sys/fs/cgroup
cgmfs                100        0       100   0% /run/cgmanager/fs
tmpfs             102456        0    102456   0% /run/user/120
tmpfs             102456       60    102396   1% /run/user/1000
/dev/sr0         1160992  1160992         0 100% /media/edemon/Ubuntu 15.04 i386
/dev/sdb1      488352764 35273248 453079516   8% /mnt/usb1


linux常常使用的文件格式,ext2,ext3,查看自己文件系统的格式:
edemon@linux:~$ cat -n /etc/fstab
1    # /etc/fstab: static file system information.
2    #
3    # Use 'blkid' to print the universally unique identifier for a
4    # device; this may be used with UUID= as a more robust way to name devices
5    # that works even if disks are added and removed. See fstab(5).
6    #
7    # <file system> <mount point>   <type>  <options>       <dump>  <pass>
8    # / was on /dev/sda1 during installation
9    UUID=0ed3d27d-eca2-4548-86a3-8454da1bc10a /               ext4    errors=remount-ro 0       1
10    # swap was on /dev/sda5 during installation
11    UUID=a80ee812-2cbb-45df-822c-98080e663e51 none            swap    sw              0       0
12    /dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0


查看各种文件系统的格式并列出和移动硬盘文件系统相同的文件系统:
edemon@linux:/mnt/usb1$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,relatime,size=501860k,nr_inodes=125465,mode=755)
#......
#......
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=102456k,mode=700,uid=1000,gid=1000)
gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
/dev/sr0 on /media/edemon/Ubuntu 15.04 i386 type iso9660 (ro,nosuid,nodev,relatime,uid=1000,gid=1000,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)
/dev/sdb1 on /mnt/usb1 type fuseblk (rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096)
edemon@linux:/mnt/usb1$ df -t fuseblk   #查看特定文件格式的文件系统
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sdb1      488352764 35273248 453079516   8% /mnt/usb1


可以用fsck命令修复文件系统。
检测文件系统:
edemon@linux:~$ sudo mkfs -c /dev/sdb1
mke2fs 1.42.12 (29-Aug-2014)
Found a gpt partition table in /dev/sdb1
Proceed anyway? (y,n) y
Creating filesystem with 122088192 4k blocks and 30523392 inodes
Filesystem UUID: 8029ae8d-de1a-4e6d-a48b-9b68d4eca0b9
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000

Checking for bad blocks (read-only test):   0.00% done, 0:00 elapsed. (0/0/0 err


压缩文件:
gzip filename
解压:
gunzip filename
查看压缩文件属性:
edemon@linux:~/test$ gzip -l t.tar.gz
compressed uncompressed ratio uncompressed_name
136 10240 98.8% t.tar

----------------------------------------------------------------------------------------------------------------------------------------------------------------
移动硬盘分区:
之前由于技能掌握的不好,加上太胆大,直接把500G的移动硬盘弄坏了,于是一晚上就用PE的diskGenius不断恢复数据和分区。。。
现在分了一个10G的空闲空间,随便玩。。。

拿最小的sdb6分区实验
edemon@linux:~$ sudo fdisk /dev/sdb6

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


查看分区情况:

Command (m for help): p
Disk /dev/sdb6: 9.7 GiB, 10421397504 bytes, 20354292 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: 0x6e697373

Device      Boot      Start        End    Sectors   Size Id Type
/dev/sdb6p1 ?    1936269394 3772285809 1836016416 875.5G 4f QNX4.x 3rd part
/dev/sdb6p2 ?    1917848077 2462285169  544437093 259.6G 73 unknown
/dev/sdb6p3 ?    1818575915 2362751050  544175136 259.5G 2b unknown
/dev/sdb6p4 ?    2844524554 2844579527      54974  26.9M 61 SpeedStor

Partition table entries are not in disk order.


因为已经有四个分区了,所以删除一些再操作:
Command (m for help): d
Partition number (1-4, default 4): 1

Partition 1 has been deleted.

Command (m for help): d
Partition number (2-4, default 4): 2

Partition 2 has been deleted.

Command (m for help): d
Partition number (3,4, default 4): 3

Partition 3 has been deleted.

Command (m for help): d
Selected partition 4
Partition 4 has been deleted.


新增主分区1
Command (m for help): n
Partition type
p   primary (0 primary, 0 extended, 4 free)
e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20354291, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20354291, default 20354291): +5G

Created a new partition 1 of type 'Linux' and of size 5 GiB.

Command (m for help): p
Disk /dev/sdb6: 9.7 GiB, 10421397504 bytes, 20354292 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: 0x6e697373

Device      Boot Start      End  Sectors Size Id Type
/dev/sdb6p1       2048 10487807 10485760   5G 83 Linux


新增拓展分区
Command (m for help): n
Partition type
p   primary (1 primary, 0 extended, 3 free)
e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): 2
First sector (10487808-20354291, default 10487808):
Last sector, +sectors or +size{K,M,G,T,P} (10487808-20354291, default 20354291): +3G

Created a new partition 2 of type 'Extended' and of size 3 GiB.

Command (m for help): p
Disk /dev/sdb6: 9.7 GiB, 10421397504 bytes, 20354292 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: 0x6e697373

Device      Boot    Start      End  Sectors Size Id Type
/dev/sdb6p1          2048 10487807 10485760   5G 83 Linux
/dev/sdb6p2      10487808 16779263  6291456   3G  5 Extended


新增逻辑分区
Command (m for help): n
Partition type
p   primary (1 primary, 1 extended, 2 free)
l   logical (numbered from 5)
Select (default p): l

Adding logical partition 5
First sector (10489856-16779263, default 10489856):
Last sector, +sectors or +size{K,M,G,T,P} (10489856-16779263, default 16779263):

Created a new partition 5 of type 'Linux' and of size 3 GiB.


查看效果:
Command (m for help): p
Disk /dev/sdb6: 9.7 GiB, 10421397504 bytes, 20354292 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: 0x6e697373

Device      Boot    Start      End  Sectors Size Id Type
/dev/sdb6p1          2048 10487807 10485760   5G 83 Linux
/dev/sdb6p2      10487808 16779263  6291456   3G  5 Extended
/dev/sdb6p5      10489856 16779263  6289408   3G 83 Linux


写入磁盘(w命令慎重使用,反悔的话用q退出即可):
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

edemon@linux:~$ partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: Can't have a partition outside the disk!

edemon@linux:~$ sudo fdisk /dev/sdb6 Welcome to fdisk (util-linux 2.25.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.Command (m for help): p
Disk /dev/sdb6: 9.7 GiB, 10421397504 bytes, 20354292 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: 0x6e697373

Device Boot Start End Sectors Size Id Type
/dev/sdb6p1 2048 10487807 10485760 5G 83 Linux
/dev/sdb6p2 10487808 16779263 6291456 3G 5 Extended
/dev/sdb6p5 10489856 16779263 6289408 3G 83 Linux


格式化分区
edemon@linux:~$ sudo mkfs -t ext3 /dev/sdb6
mke2fs 1.42.12 (29-Aug-2014)
/dev/sdb6 contains a ntfs file system labelled '文档'
Proceed anyway? (y,n) y
/dev/sdb6 is mounted; will not make a filesystem here!
edemon@linux:~$ sudo umount /dev/sdb6

edemon@linux:~$ sudo mkfs -t ext3 /dev/sdb6
mke2fs 1.42.12 (29-Aug-2014)
/dev/sdb6 contains a ntfs file system labelled '文档'
Proceed anyway? (y,n) y
Creating filesystem with 2544286 4k blocks and 636480 inodes
Filesystem UUID: 65688033-5e57-4640-8296-ffc53e17f605
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done


格式化成vfat便于windowsh和linux系统共享
edemon@linux:~$ sudo mkfs -t vfat /dev/sdb6
mkfs.fat 3.0.27 (2014-11-12)


#检查磁盘命令:
edemon@linux:~$ sudo fsck -f /dev/sdb6
[sudo] password for edemon:
fsck from util-linux 2.25.2
fsck.fat 3.0.27 (2014-11-12)
0x41: Dirty bit is set. Fs was not properly unmounted and some data may be corrupt.
1) Remove dirty bit
2) No action
?


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: