-
Notifications
You must be signed in to change notification settings - Fork 18
/
rename
executable file
·114 lines (100 loc) · 3.1 KB
/
rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# Copyright (C) 2010, 2011, 2012 Oregon State University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. common.sh
debug set -x
if [ "${NOMOUNT}" = "yes" ] ; then
# do nothing if NOMOUNT is enabled
exit 0
fi
TARGET=`mktemp -d` || exit 1
CLEANUP+=("rmdir $TARGET")
# If the target device is not a real block device we'll first losetup it.
# This is needed for file disks.
if [ ! -b $blockdev ]; then
ORIGINAL_BLOCKDEV=$blockdev
blockdev=$($LOSETUP -sf $blockdev)
CLEANUP+=("$LOSETUP -d $blockdev")
fi
filesystem_dev=$(map_disk0 $blockdev)
CLEANUP+=("unmap_disk0 $blockdev")
root_dev=$(map_partition $filesystem_dev root)
boot_dev=$(map_partition $filesystem_dev boot)
mount_disk0 $TARGET
get_os_type $TARGET
case "${OS_TYPE}" in
debian)
HNAME="${TARGET}/etc/hostname"
OLD_HNAME="$(cat $HNAME)"
;;
redhat)
HNAME="${TARGET}/etc/sysconfig/network"
OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
sed -e 's/HOSTNAME=//' | \
sed -e s/\"//g)"
;;
gentoo)
HNAME="${TARGET}/etc/conf.d/hostname"
# baselayout-2.x support
if [ -d ${TARGET}/usr/share/openrc ] ; then
OLD_HNAME="$(grep hostname ${HNAME} | \
sed -e 's/hostname=//' | \
sed -e s/\"//g)"
else
OLD_HNAME="$(grep HOSTNAME ${HNAME} | \
sed -e 's/HOSTNAME=//' | \
sed -e s/\"//g)"
fi
;;
suse)
HNAME="${TARGET}/etc/HOSTNAME"
OLD_HNAME="$(cat $HNAME)"
;;
esac
if [ "$OLD_HNAME" = "$old_name" \
-o "${OLD_HNAME}.$(echo $old_name | cut -d . -f 2,3)" \
= "$old_name" ] ; then
case "${OS_TYPE}" in
debian|suse)
echo $instance > $HNAME
;;
redhat)
sed -ie "s/HOSTNAME=${OLD_HNAME}/HOSTNAME=${instance}/" $HNAME
;;
gentoo)
if [ -d ${TARGET}/usr/share/openrc ] ; then
sed -ie "s/hostname.*/hostname=\"${instance}\"/" $HNAME
else
sed -ie "s/HOSTNAME.*/HOSTNAME=\"${instance}\"/" $HNAME
fi
;;
esac
if [ -x ${CUSTOMIZE_DIR}/zz_ddns ] ; then
TARGET=$TARGET
export TARGET
${CUSTOMIZE_DIR}/zz_ddns -r
fi
else
log_error "Cannot rename from $old_name to $instance:"
log_error "Instance has a different hostname ($OLD_HNAME)"
exit 1
fi
# execute cleanups
cleanup
trap - EXIT
exit 0