Linux使用fdisk建立swap分区

发布时间:2019-09-08编辑:脚本学堂
Linux使用fdisk建立swap分区

针对没有建立swap或者swap空间比较小时创建swap空间。

1、对硬盘进行分区。
#fdisk  /dev/sda
Command (m for help): p

Disk /dev/sda: 69.7 GB, 69793218560 bytes
255 heads, 63 sectors/track, 8485 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1305    10482381   83  linux
/dev/sda2            1829        5221    27254272+  83  Linux
/dev/sda3            1306        1827     4192965   83  Linux

Partition table entries are not in disk order

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (1828-8485, default 1828): 5222
Last cylinder or +size or +sizeM or +sizeK (5222-8485, default 8485): 7309

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 82
Changed system type of partition 4 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sda: 69.7 GB, 69793218560 bytes
255 heads, 63 sectors/track, 8485 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1305    10482381   83  Linux
/dev/sda2            1829        5221    27254272+  83  Linux
/dev/sda3            1306        1827     4192965   83  Linux
/dev/sda4            5222        7309    16771860   82  Linux swap / Solaris

Partition table entries are not in disk order

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   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
   x   extra functionality (experts only)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@ENGtest sbin]# df -k
Filesystem           1K-blocks      Used Available Use% mounted on
/dev/sda1             10153988   3347580   6282292  35% /
/dev/sda3              4061572   3432952    418972  90% /worktmp
/dev/sda2             26400508   7456328  17581468  30% /oracle
tmpfs                  3936180         0   3936180   0% /dev/shm
/dev/sdb             205392632    191892 194767412   1% /oracledata1
/dev/sdc             205392632    191892 194767412   1% /oracledata2
/dev/sdd             137272424    192132 130107264   1% /oracledata3

[root@ENGtest sbin]# fdisk -l /dev/sda

Disk /dev/sda: 69.7 GB, 69793218560 bytes
255 heads, 63 sectors/track, 8485 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1305    10482381   83  Linux
/dev/sda2            1829        5221    27254272+  83  Linux
/dev/sda3            1306        1827     4192965   83  Linux
/dev/sda4            5222        7309    16771860   82  Linux swap / Solaris

Partition table entries are not in disk order

2、建立swap分区,并且进行加载。
[root@ENGtest /]#
[root@ENGtest /]# fdisk -l /dev/sda

Disk /dev/sda: 69.7 GB, 69793218560 bytes
255 heads, 63 sectors/track, 8485 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1305    10482381   83  Linux
/dev/sda2            1829        5221    27254272+  83  Linux
/dev/sda3            1306        1827     4192965   83  Linux
/dev/sda4            5222        7309    16771860   82  Linux swap / Solaris

Partition table entries are not in disk order
[root@ENGtest /]# ls -l
total 392
drwxr-xr-x   2 root   root       4096 Dec 30 13:32 bin
drwxr-xr-x   3 root   root       4096 Dec 30 19:59 boot
drwxr-xr-x  11 root   root       3800 Feb  2 16:23 dev
drwxr-xr-x 100 root   root      12288 Feb  2 16:30 etc
drwxr-xr-x   5 root   root       4096 Jan  9 21:14 home
drwxr-xr-x   2 root   root       4096 Jan 21 10:59 install_doc
drwxr-xr-x  12 root   root       4096 Jan 17 04:02 lib
drwxr-xr-x   8 root   root       4096 Dec 30 13:31 lib64
drwx------   2 root   root      16384 Dec 30 19:53 lost+found
drwxr-xr-x   2 root   root       4096 Feb  2 16:23 media
drwxr-xr-x   2 root   root          0 Feb  2 16:23 misc
drwxr-xr-x   4 root   root       4096 Jan 15 16:42 mnt
drwxr-xr-x   2 root   root          0 Feb  2 16:23 net
drwxr-xr-x   2 root   root       4096 Oct 11  2006 opt
drwxr-xr-x   6 oracle oinstall   4096 Jan 22 10:54 oracle
drwxr-xr-x   3 root   root       4096 Jan  9 16:22 oracledata1
drwxr-xr-x   3 root   root       4096 Jan  9 16:35 oracledata2
drwxr-xr-x   3 root   root       4096 Jan  9 16:41 oracledata3
dr-xr-xr-x 150 root   root          0 Feb  2 16:22 proc
drwxr-x---  23 root   root       4096 Feb  2 16:23 root
drwxr-xr-x   2 root   root      12288 Feb  2 16:23 sbin
-rw-r--r--   1 root   root     199516 Jan 21 10:58 Screenshot-Oracle Database 10g Installation - Installation Method.png
drwxr-xr-x   4 root   root          0 Feb  2 16:22 selinux
drwxr-xr-x   2 root   root       4096 Oct 11  2006 srv
drwxrwxrwx   2 root   root       4096 Feb  2 16:26 swap
drwxr-xr-x  12 root   root          0 Feb  2 16:22 sys
drwxrwxrwt  17 root   root       4096 Feb  2 16:23 tmp
drwxr-xr-x  16 root   root       4096 Jan 16 16:28 usr
drwxr-xr-x  24 root   root       4096 Dec 30 20:20 var
drwxr-xr-x   6 root   root       4096 Jan 19 10:46 worktmp
[root@ENGtest /]# rmdir swap
[root@ENGtest /]# ls -l
total 384
drwxr-xr-x   2 root   root       4096 Dec 30 13:32 bin
drwxr-xr-x   3 root   root       4096 Dec 30 19:59 boot
drwxr-xr-x  11 root   root       3800 Feb  2 16:23 dev
drwxr-xr-x 100 root   root      12288 Feb  2 16:30 etc
drwxr-xr-x   5 root   root       4096 Jan  9 21:14 home
drwxr-xr-x   2 root   root       4096 Jan 21 10:59 install_doc
drwxr-xr-x  12 root   root       4096 Jan 17 04:02 lib
drwxr-xr-x   8 root   root       4096 Dec 30 13:31 lib64
drwx------   2 root   root      16384 Dec 30 19:53 lost+found
drwxr-xr-x   2 root   root       4096 Feb  2 16:23 media
drwxr-xr-x   2 root   root          0 Feb  2 16:23 misc
drwxr-xr-x   4 root   root       4096 Jan 15 16:42 mnt
drwxr-xr-x   2 root   root          0 Feb  2 16:23 net
drwxr-xr-x   2 root   root       4096 Oct 11  2006 opt
drwxr-xr-x   6 oracle oinstall   4096 Jan 22 10:54 oracle
drwxr-xr-x   3 root   root       4096 Jan  9 16:22 oracledata1
drwxr-xr-x   3 root   root       4096 Jan  9 16:35 oracledata2
drwxr-xr-x   3 root   root       4096 Jan  9 16:41 oracledata3
dr-xr-xr-x 150 root   root          0 Feb  2 16:22 proc
drwxr-x---  23 root   root       4096 Feb  2 16:23 root
drwxr-xr-x   2 root   root      12288 Feb  2 16:23 sbin
-rw-r--r--   1 root   root     199516 Jan 21 10:58 Screenshot-Oracle Database 10g Installation - Installation Method.png
drwxr-xr-x   4 root   root          0 Feb  2 16:22 selinux
drwxr-xr-x   2 root   root       4096 Oct 11  2006 srv
drwxr-xr-x  12 root   root          0 Feb  2 16:22 sys
drwxrwxrwt  17 root   root       4096 Feb  2 16:23 tmp
drwxr-xr-x  16 root   root       4096 Jan 16 16:28 usr
drwxr-xr-x  24 root   root       4096 Dec 30 20:20 var
drwxr-xr-x   6 root   root       4096 Jan 19 10:46 worktmp

[root@ENGtest /]# mkswap /dev/sda4
Setting up swapspace version 1, size = 17174380 kB
[root@ENGtest /]# swapon /dev/swap
swapon: cannot stat /dev/swap: No such file or directory
[root@ENGtest /]# swapon /dev/sda4

[root@ENGtest /]# top

top - 16:33:00 up 10 min,  1 user,  load average: 0.10, 0.28, 0.29
Tasks: 135 total,   4 running, 129 sleeping,   2 stopped,   0 zombie
Cpu(s):  0.5%us,  0.0%sy,  0.0%ni, 99.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   7872512k total,   814760k used,  7057752k free,    25516k buffers
Swap: 16771852k total,        0k used, 16771852k free,   330096k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND          
 2961 root      15   0 83872  10m 5316 S    1  0.1   0:02.59 Xorg             
 3240 root      16   0  260m  13m 8476 S    1  0.2   0:00.44 gnome-terminal   
 3565 root      15   0 12716 1068  792 R    1  0.0   0:00.09 top              
    1 root      15   0 10312  664  560 S    0  0.0   0:00.78 init             
    2 root      RT   0     0    0    0 S    0  0.0   0:00.08 migration/0      
    3 root      34  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/0      
    4 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/0       
    5 root      RT   0     0    0    0 S    0  0.0   0:00.06 migration/1      
    6 root      34  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/1      
    7 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/1       
    8 root      10  -5     0    0    0 S    0  0.0   0:00.05 events/0         
    9 root      10  -5     0    0    0 S    0  0.0   0:00.02 events/1         
   10 root      10  -5     0    0    0 S    0  0.0   0:00.00 khelper          
   11 root      11  -5     0    0    0 S    0  0.0   0:00.00 kthread          
   13 root      10  -5     0    0    0 S    0  0.0   0:00.00 xenwatch         
   14 root      10  -5     0    0    0 S    0  0.0   0:00.00 xenbus           
   17 root      10  -5     0    0    0 S    0  0.0   0:00.00 kblockd/0        

[3]+  Stopped                 top
[root@ENGtest /]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             10153988   3347708   6282164  35% /
/dev/sda3              4061572   3432952    418972  90% /worktmp
/dev/sda2             26400508   7456328  17581468  30% /oracle
tmpfs                  3936180         0   3936180   0% /dev/shm
/dev/sdb             205392632    191892 194767412   1% /oracledata1
/dev/sdc             205392632    191892 194767412   1% /oracledata2
/dev/sdd             137272424    192132 130107264   1% /oracledata3

3、修改文件 /etc/fstab或者/etc/rc.d/rc.local,是启动的时候自动加载swap
如果让swap开机就加载,应该改 /etc/fstab文件,加类似如下一行;
 

复制代码 代码如下:
/dev/sda6         swap        swap    defaults        0 0 

注:把此行中的/dev/hda7 改为您的交换分区就行;
或者把命令行直接写入 /etc/rc.d/rc.local中也行;

复制代码 代码如下:
swapon  /dev/sda6

[root@ENGtest /]# cd  etc
[root@ENGtest etc]# cd  rc.d
[root@ENGtest rc.d]# ls
init.d  rc0.d  rc2.d  rc4.d  rc6.d     rc.sysinit
rc      rc1.d  rc3.d  rc5.d  rc.local
[root@ENGtest rc.d]# ls -l
total 112
drwxr-xr-x 2 root root  4096 Jan 15 16:41 init.d
-rwxr-xr-x 1 root root  2255 Sep 22  2006 rc
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc0.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc1.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc2.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc3.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc4.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc5.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc6.d
-rwxr-xr-x 1 root root   220 Jun 24  2003 rc.local
-rwxr-xr-x 1 root root 26376 Jan 19  2007 rc.sysinit
[root@ENGtest rc.d]# cp rc
rc          rc1.d/      rc3.d/      rc5.d/      rc.local  
rc0.d/      rc2.d/      rc4.d/      rc6.d/      rc.sysinit
[root@ENGtest rc.d]# cp rc.local  rc.local20090202
[root@ENGtest rc.d]# ls-l
-bash: ls-l: command not found
[root@ENGtest rc.d]# ls -l
total 120
drwxr-xr-x 2 root root  4096 Jan 15 16:41 init.d
-rwxr-xr-x 1 root root  2255 Sep 22  2006 rc
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc0.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc1.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc2.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc3.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc4.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc5.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41 rc6.d
-rwxr-xr-x 1 root root   220 Jun 24  2003 rc.local
-rwxr-xr-x 1 root root   220 Feb  2 16:40 rc.local20090202
-rwxr-xr-x 1 root root 26376 Jan 19  2007 rc.sysinit

[root@ENGtest rc.d]# vi rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
swapon /dev/sda4
~
"rc.local" 8L, 237C written
[root@ENGtest rc.d]# ls
init.d  rc0.d  rc2.d  rc4.d  rc6.d     rc.local20090202
rc      rc1.d  rc3.d  rc5.d  rc.local  rc.sysinit
[root@ENGtest rc.d]# reboot

Broadcast message from root (pts/1) (Mon Feb  2 16:41:28 2009):
The system is going down for reboot NOW!
[root@ENGtest rc.d]#
[root@ENGtest rc.d]#
[root@ENGtest rc.d]# reboot