Statistics
| Branch: | Revision:

root / rename @ 6161ab06

History | View | Annotate | Download (2.5 kB)

1
#!/bin/bash
2

    
3
# Copyright (C) 2010 Oregon State University
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
# General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
# 02110-1301, USA.
19

    
20
set -e
21

    
22
if [ "${IMAGE_DEBUG}" = "1" ] ; then
23
    set -x
24
fi
25

    
26
. common.sh
27

    
28
TARGET=`mktemp -d` || exit 1
29
CLEANUP+=("rmdir $TARGET")
30

    
31
# If the target device is not a real block device we'll first losetup it.
32
# This is needed for file disks.
33
if [ ! -b $blockdev ]; then
34
  ORIGINAL_BLOCKDEV=$blockdev
35
  blockdev=$($LOSETUP -sf $blockdev)
36
  CLEANUP+=("$LOSETUP -d $blockdev")
37
fi
38

    
39
filesystem_base_dev=$(map_disk0 $blockdev)
40
if [ "${SWAP}" = "yes" ] ; then
41
    boot_dev="${filesystem_base_dev}-1"
42
    swap_dev="${filesystem_base_dev}-2"
43
    root_dev="${filesystem_base_dev}-3"
44
else
45
    boot_dev="${filesystem_base_dev}-1"
46
    root_dev="${filesystem_base_dev}-2"
47
fi
48
CLEANUP+=("unmap_disk0 $blockdev")
49

    
50
mount_disk0 $TARGET $root_dev $boot_dev
51

    
52
get_os_type
53

    
54
case "${OS_TYPE}" in
55
    debian)
56
        HNAME="${TARGET}/etc/hostname"
57
        OLD_HNAME="$(cat $HNAME)"
58
        ;;
59
    redhat)
60
        HNAME="${TARGET}/etc/sysconfig/network"
61
        OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
62
            sed -e 's/HOSTNAME=//' | cut -d \".\" -f 1)"
63
        ;;
64
    gentoo)
65
        HNAME="${TARGET}/etc/conf.d/hostname"
66
        OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
67
            sed -e 's/HOSTNAME=//' | cut -d \".\" -f 1)"
68
        ;;
69
    suse)
70
        HNAME="${TARGET}/etc/HOSTNAME"
71
        OLD_HNAME="$(cat $HNAME)"
72
        ;;
73
esac
74

    
75
if [ "$OLD_HNAME" = "$old_name" ]; then
76
    case "${OS_TYPE}" in
77
        debian|suse)
78
            echo $instance > $HNAME
79
            ;;
80
        redhat|gentoo)
81
            sed -ei "s/HOSTNAME=${OLD_HNAME}/HOSTNAME=${instance}/" $HNAME
82
            ;;
83
    esac
84
else
85
    log_error "Cannot rename from $old_name to $instance:"
86
    log_error "Instance has a different hostname ($OLD_HNAME)"
87
    exit 1
88
fi
89

    
90
# execute cleanups
91
cleanup
92
trap - EXIT
93

    
94
exit 0