题目:创建一个10G的文件系统,类型为ext4,要求开机可自动挂载至单独数据/data目录;
利用脚本实现:
[root@localhost tmp]# cat partition.sh #!/bin/bash # #Created by Jimlv on Tue Jan 19 15:12:53 2016 #This script will guide you to create an 10GB ext4 filesystem and mount to boot up dd if=/dev/zero of=/dev/sdb bs=512 count=1 &> /dev/null sync echo ' n p 1 #一个空格,相当于使用磁盘的默认柱面 +10GB n p 2 #一个空格,相当于使用磁盘的默认柱面 +10GB n p 3 #一个空格,相当于使用磁盘的默认柱面 +10GB #此次为两个空格 t 2 82 w' | fdisk /dev/sdb fdisk -l mkfs.ext4 /dev/sdb3 #格式化分区 blkid /dev/sdb3 mkdir -p /data e2label /dev/sdb3 /data #设置分区的标签 echo "/data /dev/sdb3 ext4 defaults 0 0 " >> /etc/fstab #实现开机自动挂载 cat /etc/fstab
脚本执行结果:
[root@localhost tmp]# ./partition.sh Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x5782bdb5. Command (m for help): Command (m for help): Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Partition number (1-4, default 1): First sector (2048-83886079, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): Partition 1 of type Linux and of size 9.3 GiB is set Command (m for help): Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): Partition number (2-4, default 2): First sector (19533824-83886079, default 19533824): Using default value 19533824 Last sector, +sectors or +size{K,M,G} (19533824-83886079, default 83886079): Partition 2 of type Linux and of size 9.3 GiB is set Command (m for help): Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): Partition number (3,4, default 3): First sector (39065600-83886079, default 39065600): Using default value 39065600 Last sector, +sectors or +size{K,M,G} (39065600-83886079, default 83886079): Partition 3 of type Linux and of size 9.3 GiB is set Command (m for help): Command (m for help): Partition number (1-3, default 3): Hex code (type L to list all codes): Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. Disk /dev/sda: 21.5 GB, 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 Disk label type: dos Disk identifier: 0x00011c46 Device Boot Start End Blocks Id System /dev/sda1 * 2048 616447 307200 83 Linux /dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris /dev/sda3 4810752 41943039 18566144 83 Linux Disk /dev/sdb: 42.9 GB, 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 Disk label type: dos Disk identifier: 0x5782bdb5 Device Boot Start End Blocks Id System /dev/sdb1 2048 19533823 9765888 83 Linux /dev/sdb2 19533824 39065599 9765888 82 Linux swap / Solaris /dev/sdb3 39065600 58597375 9765888 83 Linux mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 610800 inodes, 2441472 blocks 122073 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2151677952 75 block groups 32768 blocks per group, 32768 fragments per group 8144 inodes per group 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 /dev/sdb3: UUID="635fb22f-100f-4dd0-ae8c-08ba0617d0b5" TYPE="ext4" # # /etc/fstab # Created by anaconda on Fri Dec 18 15:12:53 2015 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=d038aa4c-3935-435e-a9e6-e8671791f871 / xfs defaults 0 0 UUID=4d0f6b5b-65d5-4b6a-8fef-47060c483ab9 /boot xfs defaults 0 0 UUID=70f8fe06-94a1-44cc-9368-f28eb298d755 swap swap defaults 0 0 /data /dev/sdb3 ext4 defaults 0 0