Linux_利用fdisk建立swap分区

来源:互联网 发布:尚观云计算 编辑:程序博客网 时间:2024/06/11 14:26

这种情况可以针对没有建立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

   DeviceBoot     Start        End     Blocks   Id System
/dev/sda1                 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
    extended
    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, default8485): 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

   DeviceBoot     Start        End     Blocks   Id System
/dev/sda1                 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
    toggle a bootable flag
    edit bsd disklabel
    toggle the dos compatibilityflag
    delete a partition
    list known partitiontypes
    print this menu
    add a new partition
    create a new empty DOSpartition table
    print the partitiontable
    quit without savingchanges
    create a new empty Sundisklabel
    change a partition's systemid
    change display/entryunits
    verify the partitiontable
    write table to disk andexit
    extra functionality (expertsonly)

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         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

   DeviceBoot     Start        End     Blocks   Id System
/dev/sda1                 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

   DeviceBoot     Start        End     Blocks   Id System
/dev/sda1                 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   2root  root      4096 Dec 30 13:32 bin
drwxr-xr-x   3root  root      4096 Dec 30 19:59 boot
drwxr-xr-x  11root  root      3800 Feb  2 16:23 dev
drwxr-xr-x 100 root  root     12288 Feb  2 16:30 etc
drwxr-xr-x   5root  root      4096 Jan  9 21:14 home
drwxr-xr-x   2root  root      4096 Jan 21 10:59 install_doc
drwxr-xr-x  12root  root      4096 Jan 17 04:02 lib
drwxr-xr-x   8root  root      4096 Dec 30 13:31 lib64
drwx------   2root  root     16384 Dec 30 19:53 lost+found
drwxr-xr-x   2root  root      4096 Feb  2 16:23 media
drwxr-xr-x   2root  root         0 Feb  2 16:23 misc
drwxr-xr-x   4root  root      4096 Jan 15 16:42 mnt
drwxr-xr-x   2root  root         0 Feb  2 16:23 net
drwxr-xr-x   2root  root      4096 Oct 11  2006 opt
drwxr-xr-x   6 oracleoinstall   4096 Jan 22 10:54oracle
drwxr-xr-x   3root  root      4096 Jan  9 16:22 oracledata1
drwxr-xr-x   3root  root      4096 Jan  9 16:35 oracledata2
drwxr-xr-x   3root  root      4096 Jan  9 16:41 oracledata3
dr-xr-xr-x 150 root  root         0 Feb  2 16:22 proc
drwxr-x---  23root  root      4096 Feb  2 16:23 root
drwxr-xr-x   2root  root     12288 Feb  2 16:23 sbin
-rw-r--r--   1root  root    199516 Jan 21 10:58 Screenshot-Oracle Database 10g Installation -Installation Method.png
drwxr-xr-x   4root  root         0 Feb  2 16:22 selinux
drwxr-xr-x   2root  root      4096 Oct 11  2006 srv
drwxrwxrwx   2root  root      4096 Feb  2 16:26 swap
drwxr-xr-x  12root  root         0 Feb  2 16:22 sys
drwxrwxrwt  17root  root      4096 Feb  2 16:23 tmp
drwxr-xr-x  16root  root      4096 Jan 16 16:28 usr
drwxr-xr-x  24root  root      4096 Dec 30 20:20 var
drwxr-xr-x   6root  root      4096 Jan 19 10:46 worktmp
[root@ENGtest /]# rmdir swap
[root@ENGtest /]# ls -l
total 384
drwxr-xr-x   2root  root      4096 Dec 30 13:32 bin
drwxr-xr-x   3root  root      4096 Dec 30 19:59 boot
drwxr-xr-x  11root  root      3800 Feb  2 16:23 dev
drwxr-xr-x 100 root  root     12288 Feb  2 16:30 etc
drwxr-xr-x   5root  root      4096 Jan  9 21:14 home
drwxr-xr-x   2root  root      4096 Jan 21 10:59 install_doc
drwxr-xr-x  12root  root      4096 Jan 17 04:02 lib
drwxr-xr-x   8root  root      4096 Dec 30 13:31 lib64
drwx------   2root  root     16384 Dec 30 19:53 lost+found
drwxr-xr-x   2root  root      4096 Feb  2 16:23 media
drwxr-xr-x   2root  root         0 Feb  2 16:23 misc
drwxr-xr-x   4root  root      4096 Jan 15 16:42 mnt
drwxr-xr-x   2root  root         0 Feb  2 16:23 net
drwxr-xr-x   2root  root      4096 Oct 11  2006 opt
drwxr-xr-x   6 oracleoinstall   4096 Jan 22 10:54oracle
drwxr-xr-x   3root  root      4096 Jan  9 16:22 oracledata1
drwxr-xr-x   3root  root      4096 Jan  9 16:35 oracledata2
drwxr-xr-x   3root  root      4096 Jan  9 16:41 oracledata3
dr-xr-xr-x 150 root  root         0 Feb  2 16:22 proc
drwxr-x---  23root  root      4096 Feb  2 16:23 root
drwxr-xr-x   2root  root     12288 Feb  2 16:23 sbin
-rw-r--r--   1root  root    199516 Jan 21 10:58 Screenshot-Oracle Database 10g Installation -Installation Method.png
drwxr-xr-x   4root  root         0 Feb  2 16:22 selinux
drwxr-xr-x   2root  root      4096 Oct 11  2006 srv
drwxr-xr-x  12root  root         0 Feb  2 16:22 sys
drwxrwxrwt  17root  root      4096 Feb  2 16:23 tmp
drwxr-xr-x  16root  root      4096 Jan 16 16:28 usr
drwxr-xr-x  24root  root      4096 Dec 30 20:20 var
drwxr-xr-x   6root  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,  1user,  load average: 0.10, 0.28, 0.29
Tasks: 135 total,   4 running,129 sleeping,   2stopped,   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:   7872512ktotal,   814760kused,  7057752kfree,    25516kbuffers
Swap: 16771852ktotal,       0k used, 16771852k free,  330096k cached

  PIDUSER     PR  NI  VIRT RES  SHR S %CPU%MEM   TIME+ COMMAND           
 2961root     15   0 83872 10m 5316 S    0.1   0:02.59Xorg              
 3240root     16  260m  13m 8476S    0.2   0:00.44gnome-terminal    
 3565root     15   0 127161068  792R    0.0   0:00.09top               
    1root     15   0 10312 664  560S    0.0   0:00.78init              
    2root     RT          0S    0.0   0:00.08migration/0       
    3root     34 19         0S    0.0   0:00.00ksoftirqd/0       
    4root     RT          0S    0.0   0:00.00watchdog/0        
    5root     RT          0S    0.0   0:00.06migration/1       
    6root     34 19         0S    0.0   0:00.00ksoftirqd/1       
    7root     RT          0S    0.0   0:00.00watchdog/1        
    8root     10 -5         0S    0.0   0:00.05events/0          
    9root     10 -5         0S    0.0   0:00.02events/1          
   10root     10 -5         0S    0.0   0:00.00khelper           
   11root     11 -5         0S    0.0   0:00.00kthread           
   13root     10 -5         0S    0.0   0:00.00xenwatch          
   14root     10 -5         0S    0.0   0:00.00xenbus            
   17root     10 -5         0S    0.0   0:00.00kblockd/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         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文件,加类似如下一行;

<code>
/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:41init.d
-rwxr-xr-x 1 root root  2255 Sep22  2006 rc
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc0.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc1.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc2.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc3.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc4.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc5.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc6.d
-rwxr-xr-x 1 root root   220 Jun24  2003 rc.local
-rwxr-xr-x 1 root root 26376 Jan 19  2007rc.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:41init.d
-rwxr-xr-x 1 root root  2255 Sep22  2006 rc
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc0.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc1.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc2.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc3.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc4.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc5.d
drwxr-xr-x 2 root root  4096 Jan 15 16:41rc6.d
-rwxr-xr-x 1 root root   220 Jun24  2003 rc.local
-rwxr-xr-x 1 root root   220Feb  2 16:40 rc.local20090202
-rwxr-xr-x 1 root root 26376 Jan 19  2007rc.sysinit
[root@ENGtest rc.d]# vi rc.local

#!/bin/sh
#
# This script will be executed *after* all the other initscripts.
# You can put your own initialization stuff in here if youdon'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  216:41:28 2009):

The system is going down for reboot NOW!
[root@ENGtest rc.d]#
[root@ENGtest rc.d]#
[root@ENGtest rc.d]# reboot


0 0