Create swap partition after install
Solution 1
To create a swap you need to do several things:
have a space available where the swap will reside - either a spare block device (usually a partition) or a regular file. To create a 1GB file use e.g.:
dd if=/dev/zero of=/path/to/swap/file bs=1M count=1k
prepare the swap with the
mkswap
command - this erases the data on the device/file (it creates some data structures there).activate the swap with
swapon /path/to/swap/device_or_file
. In case of a swap file the underlying filesystem obviously has to be mounted first.put it into fstab so that it can be mounted easily:
/path/to/swap/device_or_file swap swap defaults 0 0
swapoff
disables swapping to a device. Both swapon
and swapoff
have the -a
option that enables/disables swapping to all swaps: swapon -a
enables all swaps mentioned in /etc/fstab
that don't have the noauto
option set, swapoff -a
disables all swaps.
Swapping to a file has the advantage of not needing a separate partition/device reserved just for swapping, but incurs varying overhead: reads/writes go through the filesystem layer and the file contents may be scattered across the device (fragmented) which on hard drives with spinning plates can cause slower response.
From my understanding it's not possible to hibernate (suspend to disk) with only file-swap, since on waking up, the kernel needs to read the stored image from swap and would need to mount the filesystem first, which on a hibernated system could have grave consequences.
Solution 2
To create a swap partition in your lvm (supposed your volume group is called vgroup000 and you want to create a 4GB swap partition called lv_swap):
lvm lvcreate vgroup000 -n lv_swap -L 4GB
mkswap /dev/vgroup000/lv_swap
To mount it on every boot append the following line to /etc/fstab:
/dev/vgroup000/lv_swap swap swap defaults 0 0
To mount it instantly:
swapon -a
Solution 3
You can create swap space using the following steps (here we are creating swap at /home/
)
1) dd if=/dev/zero of=/home/swapfile1 bs=1024 count=8388608
(the count is a kilobyte count of swap space)
2) mkswap /home/swapfile1
3) vi /etc/fstab
make entry:
/home/swapfile1 swap swap defaults 0 0
4) swapon -a
iLinux85
Updated on September 18, 2022Comments
-
iLinux85 over 1 year
I already have parititon contain data under lvm enviroment with centos 5.8
output of fdisk -l
root@server [~]# fdisk -l
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes 255 heads, 63 sectors/track, 121601 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 121601 976559220 8e Linux LVM
output of lvdisplay
# lvdisplay --- Logical volume --- LV Name /dev/sysvg/ROOT VG Name sysvg LV UUID 6oy3Rj-ka3K-mL9s-vjjG-1Iqw-dniq-UbWzvJ LV Write Access read/write LV Status available # open 1 LV Size 919.44 GB Current LE 29422 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Name /dev/sysvg/TMP VG Name sysvg LV UUID jTKLBt-eNz0-KxmV-E5Nk-jjC0-FlRb-qny62p LV Write Access read/write LV Status available # open 1 LV Size 9.88 GB Current LE 316 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Name /dev/sysvg/SHM VG Name sysvg LV UUID NpKjhl-tzzn-Dk3G-A6dl-4QJB-QCc2-IkbDH5 LV Write Access read/write LV Status available # open 1 LV Size 2.00 GB Current LE 64 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
output of df -h
root@server [~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/sysvg-ROOT 891G 125G 721G 15% / /dev/mapper/sysvg-TMP 9.6G 153M 9.0G 2% /tmp /dev/mapper/sysvg-SHM 8.0G 8.0K 8.0G 1% /dev/shm /dev/sda1 190M 19M 162M 11% /boot tmpfs 8.0G 8.0K 8.0G 1% /dev/shm
output for /etc/fstab
root@server [~]# cat /etc/fstab /dev/sysvg/ROOT / ext3 usrjquota=quota.user,jqfmt=vfsv0 1 1 /dev/sysvg/TMP /tmp ext3 defaults 1 2 /dev/sysvg/SHM /dev/shm ext3 defaults,usrquota 1 2 LABEL=/boot /boot ext3 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /tmp /var/tmp ext3 defaults,bind,noauto 0 0
I don't have any idea how to create swap partition , and i worried about creating cause any DATA LOSS
-
Mat over 11 years
/dev/shm
isn't supposed to have backing store, it usually is an in-memory file-system. You could just re-assign that volume for swap. -
taffer over 11 yearsDo you want to create the swap inside the LVM?
-
iLinux85 over 11 years@taffer no matter but i want to be swap partition instead of swap file
-
-
iLinux85 over 11 yearsthanks for answer but i want to create a swap partition not swap file because swap file slow in performance
-
taffer over 11 years@iLinux85 "swap file slow in performance" that is not true. As long as the swap file is not fragmented there shouldn't be any noticeable performance loss when using a swap file. A swap partition however makes sense in case you have a dedicated hard drive for swap.
-
mattdm over 11 yearsI think the key point is that all swap is horribly slow, and if you're worried about swap performance, you're tackling the problem from the wrong angle.
-
peterph over 11 years@iLinux85 whatever you want - my answer does mention both :)
-
peterph over 11 years@taffer well, it is slower than a separate partition (which is of course slower than separate hw device) - I'm not claiming how much though.
-
taffer over 11 years@mattdm Indeed and that is why in my opinion it doesn't matter if it is a file or not
-
markusN almost 8 yearsWorks fine, thanks! Just one security addition: I needed to do: "chmod 0600 /home/swapfile1"