Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am abit new to Linux.

  • I was wondering whether linux has a swap file similar to windows ?(If yes, how can I change its location ?)

  • My main hard-disk of linux is low in space, I put a new hard-disk and mounted it. Is there a common way to redirect all future app installations(And other installations) to the new hard-disk ?

share|improve this question

migrated from stackoverflow.com Mar 10 '13 at 18:12

This question came from our site for professional and enthusiast programmers.

    
It usually is not a swap file (but it could be), but a swap partition. And you could have more than one of them. But since your question is not related to source code, you are asking it on the wrong place. –  Basile Starynkevitch Mar 10 '13 at 15:46
1  
2) The easiest way to share the load between your disks is to put /home/* on the new disk (either make it a mountpoint, or make home a symlink to the new mountpoint) 1) for swap: man mkswap and man swap and man swapon –  wildplasser Mar 10 '13 at 15:55
    
What distribution? –  ott-- Mar 10 '13 at 18:42
add comment

2 Answers

If you are willing to make disruptive changes, I would recommend using Linux's Logical Volume Manager (LVM2).

It allows you to combine multiple disks of different sizes together, and you can add disks to increase the apparent size of the logical "disk" without rebooting. If you are using the default file-system (ext4 these days) it can also perform an online resize of the file-system, so again no need to reboot.

Let me demonstrate this using RAM disks (/dev/ram0 through /dev/ram15). First, let's view the partitions in my system:

# lvmdiskscan
  /dev/ram0  [      64.00 MiB]
  /dev/ram1  [      64.00 MiB]
  /dev/sda1  [      87.16 GiB]
  /dev/ram2  [      64.00 MiB]
  /dev/ram3  [      64.00 MiB]
  /dev/ram4  [      64.00 MiB]
  /dev/ram5  [      64.00 MiB]
  /dev/sda5  [       6.00 GiB]
  /dev/ram6  [      64.00 MiB]
  /dev/ram7  [      64.00 MiB]
  /dev/ram8  [      64.00 MiB]
  /dev/ram9  [      64.00 MiB]
  /dev/ram10 [      64.00 MiB]
  /dev/ram11 [      64.00 MiB]
  /dev/ram12 [      64.00 MiB]
  /dev/ram13 [      64.00 MiB]
  /dev/ram14 [      64.00 MiB]
  /dev/ram15 [      64.00 MiB]
  /dev/sdb1  [     294.09 GiB]
  /dev/sdb5  [       4.00 GiB]
  0 disks
  20 partitions
  0 LVM physical volume whole disks
  0 LVM physical volumes

So let's create a 2-disk setup, and later I'll expand it to 3-disks. First, format my "disks" for use by LVM. In this step pv stands for Physical Volume, i.e. a real device that stores data:

# pvcreate /dev/ram0
  Physical volume "/dev/ram0" successfully created

# pvcreate /dev/ram1
  Physical volume "/dev/ram1" successfully created

# pvs
  PV         VG      Fmt  Attr PSize  PFree
  /dev/ram0  vg_main lvm2 a-   60.00m 60.00m
  /dev/ram1  vg_main lvm2 a-   60.00m 60.00m

then create a Volume Group, which essentially joins them into a single unpartitioned "disk":

# vgcreate vg_main /dev/ram0 /dev/ram1
  Volume group "vg_main" successfully created

# vgs
  VG      #PV #LV #SN Attr   VSize   VFree
  vg_main   2   0   0 wz--n- 120.00m 120.00m

and now to "partition" this joined disk by creating a Logical Volume. I'm going to use all the space available:

# lvcreate --extents 100%VG --name lv_main  vg_main
  Logical volume "lv_main" created

# lvs
  LV      VG      Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  lv_main vg_main -wi-a- 120.00m

So now I have a virtual 120MB partition I can store my stuff on. The device node can be found using the more verbose lvdisplay command:

# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg_main/lv_main
  VG Name                vg_main
  LV UUID                tsKWeD-tjpB-yh32-Ong3-Jp7C-erT8-LRVlTg
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                120.00 MiB
  Current LE             30
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

So I should put my file-system on /dev/vg_main/lv_main. This might mean copying an existing one using a tool like dd, or in this demonstration creating a new one:

# mkfs.ext4 -L fs_main /dev/vg_main/lv_main
mke2fs 1.42 (29-Nov-2011)
Discarding device blocks: done
Filesystem label=fs_main
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
30720 inodes, 122880 blocks
6144 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
15 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

So now I can mount this (empty) file-system and fill it with some test-data:

# mkdir /tmp/demo

# mount /dev/vg_main/lv_main /tmp/demo

# df -h /tmp/demo
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vg_main-lv_main  117M  5.6M  105M   6% /tmp/demo

# ls /tmp/demo
lost+found

# dd if=/dev/zero of=/tmp/demo/bigfile
dd: writing to `/tmp/demo/bigfile': No space left on device
226701+0 records in
226700+0 records out
116070400 bytes (116 MB) copied, 0.982532 s, 118 MB/s

# ls -lh /tmp/demo
total 111M
-rw-r--r-- 1 root root 111M Mar 10 18:50 bigfile
drwx------ 2 root root  12K Mar 10 18:47 lost+found

My file-system is now completely full! This can be verified at the file-system level using df and the joined-disk level using vgdisplay and checking the Free PE (Physical Extents) / Size (bytes):

# df -h /tmp/demo
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vg_main-lv_main  117M  117M     0 100% /tmp/demo

# vgdisplay
  --- Volume group ---
  VG Name               vg_main
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               120.00 MiB
  PE Size               4.00 MiB
  Total PE              30
  Alloc PE / Size       30 / 120.00 MiB
  Free  PE / Size       0 / 0
  VG UUID               zWxBml-xdZU-8wgh-IK1w-ij1N-Z3eo-sRtri3

So now let's make it bigger! Let's pretend that /dev/ram2 is a lovely new SATA disk that I've just hot-plugged into my running system, and I want to use it to increase the size of my full file-system.

As before, use pvcreate to prepare the "disk" for use with LVM:

# pvcreate /dev/ram2
  Physical volume "/dev/ram2" successfully created

Now we extend our joined-disk (Volume Group) and verify that vg_main has some free space:

# vgextend vg_main /dev/ram2
  Volume group "vg_main" successfully extended

# vgdisplay
  --- Volume group ---
  VG Name               vg_main
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               180.00 MiB
  PE Size               4.00 MiB
  Total PE              45
  Alloc PE / Size       30 / 120.00 MiB
  Free  PE / Size       15 / 60.00 MiB
  VG UUID               zWxBml-xdZU-8wgh-IK1w-ij1N-Z3eo-sRtri3

This doesn't yet make any space free in our file-system though, since the "partition" (Logical Volume) is the same size as before we added our new disk:

# df -h /tmp/demo
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vg_main-lv_main  117M  117M     0 100% /tmp/demo

# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg_main/lv_main
  VG Name                vg_main
  LV UUID                tsKWeD-tjpB-yh32-Ong3-Jp7C-erT8-LRVlTg
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                120.00 MiB
  Current LE             30
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

Here comes the magic - we resize the Logical Volume, and tell LVM2 to resize the file-system within:

# lvresize --extents 100%VG --resizefs /dev/vg_main/lv_main
fsck from util-linux 2.20.1
e2fsck 1.42 (29-Nov-2011)
/dev/mapper/vg_main-lv_main is mounted.


WARNING!!!  The filesystem is mounted.   If you continue you ***WILL***
cause ***SEVERE*** filesystem damage.


Do you really want to continue<n>?

This scary warning is not actually related to resizing the file-system, but to the pre-flight fsck that LVM2 wants to do to the mounted file-system. Obviously we decline this scary prompt and answer "no":

Do you really want to continue<n>? no

check aborted.
  Extending logical volume lv_main to 180.00 MiB
  Logical volume lv_main successfully resized
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/dm-0 is mounted on /tmp/demo; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/dm-0 to 184320 (1k) blocks.
The filesystem on /dev/dm-0 is now 184320 blocks long.

LVM2 then proceeds to do the online resize of the file-system that we requested. Voila!

# df -h /tmp/demo
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vg_main-lv_main  175M  117M   49M  71% /tmp/demo

I can now create more files again:

# dd if=/dev/zero of=/tmp/demo/another_file
dd: writing to `/tmp/demo/another_file': No space left on device
118751+0 records in
118750+0 records out
60800000 bytes (61 MB) copied, 0.556078 s, 109 MB/s

# ls -lh /tmp/demo
total 169M
-rw-r--r-- 1 root root  58M Mar 10 19:04 another_file
-rw-r--r-- 1 root root 111M Mar 10 18:52 bigfile
drwx------ 2 root root  12K Mar 10 18:47 lost+found
share|improve this answer
add comment

1) Linux hasn't a swap file, Linux has a swap partition. You can change its location after the initial setup editing your /etc/fstab file as root to change the swap partition, but you need to create another one.

2) There isn't an easy way I know to achieve this, because Linux stores executables all scattered over system directories.

share|improve this answer
3  
Linux could have a swap file without a swap partition. It is just unusual, and slightly less efficient. –  Basile Starynkevitch Mar 10 '13 at 15:52
    
But you need to manually setup it, and I think the asker never did. Furthermore I don't think you can move a partition to a file and, if you can, it's simpler to create a new swap partition :) –  TM3P Mar 10 '13 at 15:58
    
1) seems speculative. just because it doesn't come out of the box with a swap file made, doesn't mean future readers won't have swap set as file. –  Lorenzo Von Matterhorn Mar 10 '13 at 18:40
    
It depends on the distribution. I've seen use whole disk as standard, so you have to add a swapfile later, or use manual partition to create a swap partition. If the OP has 2 or more GB RAM, he can run the system without any swap, unless he wants to do video editing of big movies or similar. –  ott-- Mar 10 '13 at 18:41
    
@ott-- indeed true, but you seem to have missed my point. one cannot assume someone will be restricted to somekind of configuration. –  Lorenzo Von Matterhorn Mar 11 '13 at 15:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.