Statistics
| Branch: | Revision:

root / rename @ e34560c8

History | View | Annotate | Download (3 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
. common.sh
23

    
24
debug set -x
25

    
26
if [ "${NOMOUNT}" = "yes" ] ; then
27
    # do nothing if NOMOUNT is enabled
28
    exit 0
29
fi
30

    
31
TARGET=`mktemp -d` || exit 1
32
CLEANUP+=("rmdir $TARGET")
33

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

    
42
filesystem_dev=$(map_disk0 $blockdev)
43
CLEANUP+=("unmap_disk0 $blockdev")
44
root_dev=$(map_partition $filesystem_dev root)
45
boot_dev=$(map_partition $filesystem_dev boot)
46

    
47
mount_disk0 $TARGET
48
get_os_type $TARGET
49

    
50
case "${OS_TYPE}" in
51
    debian)
52
        HNAME="${TARGET}/etc/hostname"
53
        OLD_HNAME="$(cat $HNAME)"
54
        ;;
55
    redhat)
56
        HNAME="${TARGET}/etc/sysconfig/network"
57
        OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
58
            sed -e 's/HOSTNAME=//' | \
59
            sed -e s/\"//g)"
60
        ;;
61
    gentoo)
62
        HNAME="${TARGET}/etc/conf.d/hostname"
63
        # baselayout-2.x support
64
        if [ -d ${TARGET}/usr/share/openrc ] ; then
65
            OLD_HNAME="$(grep hostname ${HNAME} | \
66
                sed -e 's/hostname=//' | \
67
                sed -e s/\"//g)"
68
        else
69
            OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
70
                sed -e 's/HOSTNAME=//' | \
71
                sed -e s/\"//g)"
72
        fi
73
        ;;
74
    suse)
75
        HNAME="${TARGET}/etc/HOSTNAME"
76
        OLD_HNAME="$(cat $HNAME)"
77
        ;;
78
esac
79

    
80
if [ "$OLD_HNAME" = "$old_name" \
81
        -o "${OLD_HNAME}.$(echo $old_name | cut -d . -f 2,3)" \
82
        = "$old_name" ] ; then
83
    case "${OS_TYPE}" in
84
        debian|suse)
85
            echo $instance > $HNAME
86
            ;;
87
        redhat)
88
            sed -ie "s/HOSTNAME=${OLD_HNAME}/HOSTNAME=${instance}/" $HNAME
89
            ;;
90
        gentoo)
91
            if [ -d ${TARGET}/usr/share/openrc ] ; then
92
                sed -ie "s/hostname.*/hostname=\"${instance}\"/" $HNAME
93
            else
94
                sed -ie "s/HOSTNAME.*/HOSTNAME=\"${instance}\"/" $HNAME
95
            fi
96
            ;;
97
    esac
98
else
99
    log_error "Cannot rename from $old_name to $instance:"
100
    log_error "Instance has a different hostname ($OLD_HNAME)"
101
    exit 1
102
fi
103

    
104
# execute cleanups
105
cleanup
106
trap - EXIT
107

    
108
exit 0