2.4 使用磁盘添加swap
先划分一个1G的磁盘分区出来:
[root@rhce /]# fdisk /dev/sdb
Device contains neither a valid DOSpartition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with diskidentifier 0x65edb587.
Changes will remain in memory only, untilyou decide to write them.
After that, of course, the previous contentwon't be recoverable.
Warning: invalid flag 0x0000 of partitiontable 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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G}(1-652, default 652): +1G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@rhce /]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x000bcd24
Device Boot Start End Blocks Id System
/dev/sda1 * 1 128 1024000 83 Linux
Partition 1 does not end on cylinderboundary.
/dev/sda2 128 511 3072000 82 Linux swap / Solaris
Partition 2 does not end on cylinderboundary.
/dev/sda3 511 2611 16874496 83 Linux
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x65edb587
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
…
注意这里fdisk ID的编号。 这里显示的83,它代表这个分区是ext的分区,这个是不能用来做swap的。
在fdisk 命令可以看到具体数据的含义:
Command (m for help): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIXroot 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIXusr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16<32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS /.
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIXbootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD orSys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS/ HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 NovellNetware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
ext的分区ID号:0x83
swap的分区ID号:0x82
--将/dev/sdb1分区改成swap 分区:
[root@rhce /]# mkswap /dev/sdb1
Setting up swapspace version 1, size =1060252 KiB
no label, UUID= bc913e6b-209a-49a7-b561-9b8c57294681
[root@rhce /]# mkswap –L swap-disk /dev/sdc1
一个设置了Lable,一个没设置。我们在添加/etc/fstab时,可是用LABLE名称,或者使用UUID来配置。 具体如上。
这里很奇怪,我们设置完之后,查看fdisk, 显示的ID还是83. 即是ext分区:
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x65edb587
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
Disk /dev/sdc: 5368 MB, 5368709120 bytes
181 heads, 40 sectors/track, 1448 cylinders
Units = cylinders of 7240 * 512 = 3706880bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdc1 1 291 1053400 83 Linux
注意: 但我们通过看,其类型已经变成了swap:
[root@rhce ~]# blkid
/dev/sda1:UUID="935f7fb2-2ef4-486f-ae4e-265eaf9f580f" TYPE="ext4"
/dev/sda2:UUID="43343402-9188-4cdc-9c79-0e36ab737517" TYPE="swap"
/dev/sda3:UUID="7e49fb2c-6fd8-461a-95d9-65d1f3994160" TYPE="ext4"
/dev/sdb1:UUID="bc913e6b-209a-49a7-b561-9b8c57294681" TYPE="swap"
/dev/sdc1:LABEL="swap-disk" UUID="799b4379-ca59-4508-ad04-2d18847b3fd9"TYPE="swap"
[root@rhce ~]#
因为这里的类型已经改变,我们可以尝试手工的启动swap:
[root@rhce ~]# swapon /dev/sdc1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
/dev/sdc1 partition 1053392 0 -3
这里可以直接启用swap。
我们这里把/dev/dbb1和/dev/sdc1都添加/etc/fstab 文件,在重启一下看看。 添加内容如下:
UUID=bc913e6b-209a-49a7-b561-9b8c57294681swap swap defaults 0 0
LABEL=swap-disk swap swap defaults 0 0
重启系统后查看:
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
/dev/sdc1 partition 1053392 0 -3
--这里已经添加成功了。 不过我们的fdisk 还是没有改变过来,显示的还是83:
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x65edb587
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
[root@rhce ~]# blkid
/dev/sda1:UUID="935f7fb2-2ef4-486f-ae4e-265eaf9f580f" TYPE="ext4"
/dev/sda2:UUID="43343402-9188-4cdc-9c79-0e36ab737517" TYPE="swap"
/dev/sda3:UUID="7e49fb2c-6fd8-461a-95d9-65d1f3994160" TYPE="ext4"
/dev/sdb1:UUID="bc913e6b-209a-49a7-b561-9b8c57294681" TYPE="swap"
/dev/sdc1: LABEL="swap-disk"UUID="799b4379-ca59-4508-ad04-2d18847b3fd9" TYPE="swap"
注:我这里的测试系统是:RedHat6.3。
--对/dev/sdb1 进行测试:
[root@rhce ~]# swapoff /dev/sdb1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
[root@rhce ~]# swapon /dev/sdb1
[root@rhce ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071992 0 -1
/dev/sdb1 partition 1060248 0 -2
2.5 SWAP 分区的删除Swapoff文件之后,删除掉对应的空间,在从/etc/fstab删除对应的记录即可。