handle swap partitions better during import/export
[ext/instance-debootstrap.git] / import
1 #!/bin/bash
2
3 # Copyright (C) 2007, 2008, 2009 Google Inc.
4 # Portions Copyright 2013 Equinox Software, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 set -e
22
23 . common.sh
24
25 # If the target device is not a real block device we'll first losetup it.
26 # This is needed for file disks.
27 if [ ! -b $blockdev ]; then
28   ORIGINAL_BLOCKDEV=$blockdev
29   blockdev=$(losetup -sf $blockdev)
30   CLEANUP+=("losetup -d $blockdev")
31 fi
32
33 # First line in the dump contains the partion and/or 
34 # filesystem type.
35 read -r export_type
36 if [ "$export_type" = "swap" ]; then
37   mkswap -f $blockdev
38   exit 0
39 fi
40
41 if [ "$PARTITION_STYLE" = "none" ]; then
42   filesystem_dev=$blockdev
43 elif [ "$PARTITION_STYLE" = "msdos" ]; then
44   # Create one big partition, and make it bootable
45   format_disk0 $blockdev
46   filesystem_dev=$(map_disk0 $blockdev)
47   CLEANUP+=("unmap_disk0 $blockdev")
48 else
49   echo "Unknown partition style $PARTITION_STYLE"
50   exit 1
51 fi
52
53 mke2fs -Fjq $filesystem_dev
54 root_uuid=$($VOL_ID $filesystem_dev )
55
56 if [ -n "$swapdev" ]; then
57   mkswap $swapdev
58   swap_uuid=$($VOL_ID $swapdev || true )
59 fi
60
61 TMPDIR=`mktemp -d` || exit 1
62 CLEANUP+=("rmdir $TMPDIR")
63
64 mount $filesystem_dev $TMPDIR
65 CLEANUP+=("umount $TMPDIR")
66
67 ( cd $TMPDIR; restore -r -y -f - )
68 rm -f $TMPDIR/etc/udev/rules.d/z*_persistent-net.rules
69
70 # Fix /etc/fstab with the new volumes' UUIDs
71 if [ -e $TMPDIR/etc/fstab ]; then
72   ROOT_LINE="UUID=$root_uuid  /     ext3  defaults  0  1"
73   if [ -n "$swapdev" -a -n "$swap_uuid" ]; then
74     SWAP_LINE="UUID=$swap_uuid  swap  swap  defaults  0  0"
75     cat $TMPDIR/etc/fstab | \
76       sed -re "s#^(/dev/sda|UUID=[a-f0-9-]+)\s+/\s+.*\$#$ROOT_LINE#" \
77           -e "s#^(/dev/sdb|UUID=[a-f0-9-]+)\s+swap\s+.*\$#$SWAP_LINE#" \
78       > $TMPDIR/etc/fstab.new
79   else
80     cat $TMPDIR/etc/fstab | \
81       sed -re "s#^(/dev/sda|UUID=[a-f0-9-]+)\s+/\s+.*\$#$ROOT_LINE#" \
82       > $TMPDIR/etc/fstab.new
83   fi
84   mv $TMPDIR/etc/fstab.new  $TMPDIR/etc/fstab
85 fi
86
87 # execute cleanups
88 cleanup
89 trap - EXIT
90
91 exit 0