rsync - Do Linux swap partitions need to be cloned?

06
2014-04
  • Nate

    I'm in the process of cloning my hard disk in order to test some unstable software.

    One of the partitions is Linux-swap. From my understanding, it can be used by the os as additional ram. So this does not need to be cloned right?

    Additionally: The disk I have has a capacity ~120 GB, while my original drive is ~250GB (though only about 70 GB are used). Since I can not dd, I created the partitions manually and I'm currently using rsync to "clone" the partitions. Is this the best way to do it? How do I make it bootable?

  • Answers
  • Area 51

    It doesn't need to be cloned. It simply contains whatever was at certain memory locations when you unmounted that swap partition (or it may have been wiped for security). It doesn't contain any sensitive information.

    As for the migration question, you can resize the partitions on the original disk first (you can do that with gparted) so that it fits in the new disk, making sure it still works (boot the system), and then dd'ing that to the new disk. (I highly recommend you make a per-bit backup of the 250 GB disk before you resize the partitions on it.. Better yet, resize the partition on the backup.)

    Otherwise, rsync may work (and be a little safer in that it doesn't require you to fiddle with the existing system) but indeed your new drive won't be bootable. You'll then need to reinstall the bootloader (I'm assuming grub); this can simply be done by booting on your favorite LiveCD and running grub-install on the correct device (grub-install /dev/sdX)


  • Related Question

    Can I hibernate Linux without a swap partition?
  • Hanno Fietz

    I hadn't set up a swap partition on my PC, because a) I have plenty of RAM (8 GB) and b) I have large harddrives that I didn't want to chop into tiny pieces, so my smallest partitions are 50 GiB, and I'm already using those for the OS's.

    Now, to hibernate, my Linux (Ubuntu Jaunty) required swap. I had a spare 100 GB partition so I set that up as swap, but I'm not satisfied with that. It's only ever used for hibernation (there's always plenty of RAM free and the system never goes to swap) and it's also a little large for swap. Also, I'm eventually going to need it for something else.

    How can I hibernate (suspend to disk) without a swap partition in Ubuntu Jaunty?


  • Related Answers
  • Robert Munteanu

    Untested idea: why don't you create a wrapper for s2disk or whichever utility handles suspend to disk which manages a swap file ( as opposed to a swap partition ) and deletes it on resume?

    • Create the swap file : dd if=/dev/zero of=/swapfile bs=1024 count=8388608 ( 8GB )
    • Setup the swap file: mkswap /swapfile
    • Only when you need to set suspend you can activate it: swapon /swapfile
    • When you resume you can deactivate it: swapoff /swapfile

    Resuming from swap files is possible, and is documented on kernel.org

  • nik

    You probably have two ways here,

    1. Consider a swap-file instead of a swap-partition
    2. Using a small (compared to your partitions sizes, but large enough for memory) USB stick for swap-partition

    Whatever you do, I think a swapoff and swapon after the resume would be useful.
    And, since you don't really require the swap, you could leave it swapoff after resume.

    Update: The comment makes a good point about slow USB hibernation.
    So, check in the numbered order -- the first scheme has notes for hibernation using swap-files.


    Out of curiosity,
    I'd like to know why you want to hibernate when linuxes like Ubuntu can shutdown and start so fast.
    I am guessing,

    • you load up your 8GB ram with some applications and leave them there
    • or, you Wake-up-on-LAN

    But, is that so really? or, do you have some other reason to hibernate?

    I use a USB booting Ubuntu and always shutdown.

  • nagul

    Yes, but not without some effort. There are 2 different ways to hibernate (suspend-to-disk) on linux:

    1. swswap, which is included in the kernel
    2. tuxonice (formerly suspend2), which is not.

    Tuxonice is available as a patch to the kernel, and will let you write the suspend image to an ordinary file.

    From Wikipedia:

    TuxOnIce (formerly known as Suspend2) is an implementation of the suspend-to-disk (or hibernate) feature which is available as patches for the 2.6 Linux kernel. It was formerly known as 'swsusp'. During the 2.5 kernel era, Pavel Machek forked the original out-of-tree version of swsusp (then at approximately beta 10) and got it merged into the vanilla kernel, while development continued in the swsusp/Suspend2/TuxOnIce line. TuxOnIce includes support for SMP, highmem and preemption. Its major advantages over swsusp are:
    
        * It has an extensible architecture that allows for arbitrary transformations on the image and arbitrary backends for writing the image;
        * It prepares the image and allocates storage prior to doing any storage and accounts for memory and storage usage very carefully, thereby becoming more reliable;
        * Its current modules for writing the image have been designed for speed, combining asynchronous I/O, multithreading and readahead with LZF compression in its default configuration to read and write the image as fast as hardware is able;
        * It has an active community supporting it via a wiki, mailing lists and irc channel (see the TuxOnIce website);
        * It is more flexible and configurable (via a /sys/power/tuxonice interface);
        * Whereas the current swsusp (and uswsusp) implementations support writing the image to one swap device only, TuxOnIce supports multiple devices in any combination of swap files and swap partitions. It can also write the image to an ordinary file, thereby avoiding potential race issues in freeing memory when preparing to suspend.
        * It supports encryption by various methods;
        * It can store a full image of memory (resulting in a more responsive system post-resume), while uswsusp and swsusp write at most half the amount of RAM.
    

    Since it's not included in the default kernel, you'd unfortunately have to pick up the kernel patches available for Jaunty and compile the kernel yourself.

    There are some extended instructions here, but you might want to try out Robert's suggestion before wandering down this road, unless you are an old hand at rolling your own kernel images.

  • 0x89

    As Joel and Jeff have discussed on the podcast, turning off swap is generally not a good idea - even if it would be worth it (which it isn't) - disk space is so incredibly cheap nowadays that leaving it on does not cost you anything. You can get a gig for less than ten cent (Euro cent, that is)!

    to quote:

    Atwood: I think that every geek at some point goes through this thought process: I got tons and tons of memory, maybe I can turn of my page file. I actually have a blog entry about this. ... The punch line is: it is never worth it.

    Listen to the podcast for more of the conversation (it starts roughly at minute 59), or read the question at serverfault linked to on the podcast page (question 23621). They talk about the windows page file, but the argument is equally valid for UNIX (although they may manage memory in a different way).