59

In order to save disk space, I want to have two OS installations share a single swap partition (a dual-boot). Is this a good idea?

tshepang
  • 65,642
  • Get more ram, it's cheap, don't use swap, it'll help you with that IO issue. I realize this might not be feasible, but gone are the days where you must have swap. – xenoterracide Jan 11 '11 at 10:10
  • @xeno that perf issue has nothing to do with swap; look at botton of ur answer for an example; oh, and I'm not running out of RAM either – tshepang Jan 11 '11 at 11:01
  • 3
    @xeno if u dont have swap, u lose Suspend ability – tshepang Jan 11 '11 at 11:04
  • 3
    I don't use that, so... yeah I don't use that. – xenoterracide Jan 11 '11 at 11:11
  • if you're swapping it means (well can mean) heavy IO, heavy IO kills performance. – xenoterracide Jan 11 '11 at 11:13
  • 1
    @xenoterracide disks are also cheap, and oomkiller behave strangely. – shellholic Jan 11 '11 at 11:16
  • IO is not cheap. I read there are improvements too OOM killer in 2.6.37 – xenoterracide Jan 11 '11 at 11:17
  • anyways, this is a comment, because it's not really intended to be an answer to the question, simply my own opinion to an alternative way of doing things. I have 6G of ram an no need for swap. – xenoterracide Jan 11 '11 at 11:18
  • 2
    @xenoterracide I like your optimistic point of view, but I will continue to have swap partitions because I like safety nets. Just change your swapiness to 0. – shellholic Jan 11 '11 at 11:20
  • @shellholic, didn't work as well for me. I decided I'd rather have the space than have swap at all. If it doesn't work for you that's fine. – xenoterracide Jan 11 '11 at 11:24
  • For future readers: I just want to note the importance of having swap, as I tend to use 24GB or more of RAM regularly and am limited to 2GB or 4GB on most of my systems. It's known that yes, swap kills performance for both the disk and memory bandwidth (unless you use RAID-swap), but if you exceed RAM use without swap, your system hangs for an indefinite amount of time (minutes to years depending on process demand). Having swap prevents all of this and also opens your system to extended functionality (as mentioned). And not to forget, if you're not using swap, there's no disk performance loss. – Tcll May 04 '19 at 13:41

3 Answers3

46

It's possible. In fact, you can share the swap space between completely different operating systems, as long as you initialize the swap space when you boot. It used to be relatively common to share swap space between Linux and Windows, back when it represented a significant portion of your hard disk.

Two restrictions come to mind:

  • The OSes cannot be running concurrently (which you might want to do with virtual machines).
  • You can't hibernate one of the OSes while you run another.
  • If you try to boot into an OS when another is hibernated, will it be not possible or will it boot but the hibernation data get corrupted? I thinking that it may refuse to boot due to some logic in grub or something like that. – paradroid Dec 18 '20 at 16:54
  • 1
    @paradroid I don't think grub knows anything about hibernation. If you boot an OS while another OS is hibernated, it's safe provided that no filesystem is mounted by both OSes. If a filesystem is mounted by the hibernating OS and you mount it in the other OS, expect massive corruption. – Gilles 'SO- stop being evil' Dec 19 '20 at 20:02
  • Thanks. Well I'm wondering what would happen if an ext4 disk is shared with a Linux OS and Windows WSL, as Windows actually partially hibernates when it is 'Shut Down', so I am not sure if the second disk will still be mounted in that case, but this is a Windows issue... – paradroid Dec 21 '20 at 08:53
  • @Gilles'SO-stopbeingevil' From your above comment and answer, if I only share a Data partition between the 2 operating systems. Do you think that it can lead to corruption of data when hibernated on one OS and booted to another? – Porcupine Apr 12 '23 at 03:02
  • @Porcupine Yes. Never use a partition that's mounted by a hibernating system. It's only safe if you unmount that partition before hibernating. – Gilles 'SO- stop being evil' Apr 12 '23 at 08:37
  • @Gilles'SO-stopbeingevil' So basically we should not share any partition or logical volume between the operating systems if we do hibernation? Not even a partition that just contains data like videos and music and pictures? – Porcupine Apr 12 '23 at 23:11
25

One side effect I can think of is:

  1. Hibernate system1 (using the swap partition for hibernation).
  2. Boot system2.

You could lose data.

shellholic
  • 6,255
  • 11
    Murphys law suggests that if you do this, you will one day forget, you will hibernate an OS, or boot it into a VM, and you will hose your system, and possibly data. Simple truth - disk space is dirt cheap, your data and work is probably not. Unless you are needing terabyte's of swap space for each OS, just make separate ones. – Danny Staple Jan 11 '11 at 09:57
9

One of my friends tried this. He has installed five or six distributions in a single hard drive.

The first primary partition is for GRUB, and he is able to boot to all the distributions. The second partition is swap. The third partition is an extended partition and each of the distributions are installed into their own logical partitions.

All of the distributions are able to boot and can hibernate. I think you just need to make sure and select the correct distribution after resuming from hibernation.

So, on the basis of his experiment I should say YES this is possible, but I think it can break things. What if distribution 2 wakes up and distribution 1's resume file is using up the swap partition; what's the next thing that's going to happen?
So I, too, agree with all the above posts. Why don't you try to split the swap partitions, rather than taking this huge risk?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255