Statistics
| Branch: | Revision:

root / common_linux.sh @ 528c0efc

History | View | Annotate | Download (6.3 kB)

1

    
2
get_os_type() {
3
    target=$1
4
    if [ -z "${target}" ] ; then
5
        log_error "target is not set in get_os_type"
6
        exit 1
7
    fi
8
    if [ -e ${target}/etc/redhat-release ] ; then
9
        OS_TYPE="redhat"
10
    elif [ -e ${target}/etc/debian_version ] ; then
11
        OS_TYPE="debian"
12
    elif [ -e ${target}/etc/gentoo-release ] ; then
13
        OS_TYPE="gentoo"
14
    elif [ -e ${target}/etc/SuSE-release ] ; then
15
        OS_TYPE="suse"
16
    fi
17
}
18

    
19
get_os() {
20
    target=$1
21
    if [ -z "${target}" ] ; then
22
        log_error "target is not set in get_os"
23
        exit 1
24
    fi
25
#    lsb="/usr/bin/lsb_release"
26
#    if [ -e ${target}/$lsb ] ; then
27
#        OPERATING_SYSTEM="$(chroot ${target} ${lsb} -i -s | tr "[:upper:]" "[:lower:]")"
28
    if [ -e ${target}/etc/debian_version ] ; then
29
        if [ -e ${target}/etc/lsb-release ] ; then
30
            ID=$(grep ^DISTRIB_ID= ${target}/etc/lsb-release | cut -d= -f2)
31
        fi
32
        if [ "a$ID" = "aUbuntu" ] ; then
33
            OPERATING_SYSTEM="ubuntu"
34
        else
35
            OPERATING_SYSTEM="debian"
36
        fi
37
    elif [ -e ${target}/etc/gentoo-release ] ; then
38
        OPERATING_SYSTEM="gentoo"
39
    elif [ -e ${target}/etc/fedora-release ] ; then
40
        OPERATING_SYSTEM="fedora"
41
    elif [ -e ${target}/etc/redhat-release ] ; then
42
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
43
            OPERATING_SYSTEM="centos"
44
        else
45
            OPERATING_SYSTEM="redhat"
46
        fi
47
    fi
48
}
49

    
50
get_os_release() {
51
    target=$1
52
    if [ -z "${target}" ] ; then
53
        log_error "target is not set in get_os_release"
54
        exit 1
55
    fi
56
    lsb="/usr/bin/lsb_release"
57
    if [ -e ${target}/$lsb ] ; then
58
        OS_RELEASE="$(chroot ${target} ${lsb} -r -s | tr "[:upper:]" "[:lower:]")"
59
    elif [ -e ${target}/etc/debian_version ] ; then
60
        OS_RELEASE="$(cat ${target}/etc/debian_version)"
61
    elif [ -e ${target}/etc/fedora-release ] ; then
62
        OS_RELEASE="$(cat ${target}/etc/fedora-release | awk '{print $3}')"
63
    elif [ -e ${$target}/etc/redhat-release ] ; then
64
        OS_RELEASE="$(cat ${target}/etc/redhat-release | awk '{print $3}')"
65
    fi
66
}
67

    
68
linux_format_disk0() {
69
    local sfdisk_cmd="$SFDISK -uM -H 255 -S 63 --quiet --Linux --DOS $1"
70
    if [  "${SWAP}" = "yes" -a "${BOOT}" = "yes" ] ; then
71
        # Create three partitions:
72
        # 1 - 100MB /boot, bootable
73
        # 2 - Size of Memory, swap
74
        # 3 - Rest
75
        $sfdisk_cmd > /dev/null <<EOF
76
,100,L,*
77
,${SWAP_SIZE},S
78
,,L
79
EOF
80
    elif [  "${SWAP}" = "no" -a "${BOOT}" = "yes" ] ; then
81
        # Create two partitions:
82
        # 1 - 100MB /boot, bootable
83
        # 2 - Rest
84
        $sfdisk_cmd > /dev/null <<EOF
85
,100,L,*
86
,,L
87
EOF
88
    elif [  "${SWAP}" = "yes" -a "${BOOT}" = "no" ] ; then
89
        # Create two partitions:
90
        # 1 - Size of Memory, swap
91
        # 2 - Rest
92
        $sfdisk_cmd > /dev/null <<EOF
93
,$SWAP_SIZE,S
94
,,L
95
EOF
96
    elif [  "${SWAP}" = "no" -a "${BOOT}" = "no" ] ; then
97
        # Create two partitions:
98
        # 1 - Whole
99
        $sfdisk_cmd > /dev/null <<EOF
100
,,L
101
EOF
102
    fi
103
}
104

    
105
linux_mkfs_disk0() {
106
    local mkfs="mkfs.${FILESYSTEM}"
107
    # Format /
108
    $mkfs -Fq -L / $root_dev > /dev/null
109
    # Format /boot
110
    if [ -n "${boot_dev}" ] ; then
111
        $mkfs -Fq -L /boot $boot_dev > /dev/null
112
    fi
113
    # Format swap
114
    if [ -n "${swap_dev}" ] ; then
115
        # Format swap
116
        mkswap -f $swap_dev > /dev/null
117
    fi
118
    # During reinstalls, ext4 needs a little time after a mkfs so add it here
119
    # and also run a sync to be sure.
120
    sync
121
    sleep 2
122
}
123

    
124
linux_setup_fstab() {
125
    local target=$1 fs=${FILESYSTEM}
126
    get_os_type $target
127
    cat > $target/etc/fstab <<EOF
128
# /etc/fstab: static file system information.
129
#
130
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
131
UUID=$root_uuid   /               $fs     defaults        0       1
132
proc              /proc           proc    defaults        0       0
133
EOF
134

    
135
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
136
    cat >> $target/etc/fstab <<EOF
137
UUID=$boot_uuid   /boot           $fs     defaults        1       2
138
EOF
139
fi
140

    
141
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
142
    cat >> $target/etc/fstab <<EOF
143
UUID=$swap_uuid   swap            swap    defaults        0       0
144
EOF
145
fi
146

    
147
# OS Specific fstabs
148
if [ "$OS_TYPE" = "redhat" ] ; then
149
    cat >> $target/etc/fstab <<EOF
150
tmpfs             /dev/shm        tmpfs   defaults        0       0
151
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
152
sysfs             /sys            sysfs   defaults        0       0
153
EOF
154
fi
155

    
156
if [ "$OS_TYPE" = "gentoo" ] ; then
157
    cat >> $target/etc/fstab <<EOF
158
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
159
EOF
160
fi
161
}
162

    
163
linux_setup_console() {
164
    local target=$1
165
    if [ -z "$target" ] ; then
166
        log_error "target not set for setup_console"
167
        exit 1
168
    fi
169
    # Upstart is on this system, so do this instead
170
    if [ -e ${target}/etc/event.d/tty1 ] ; then
171
        cat ${target}/etc/event.d/tty1 | sed -re 's/tty1/ttyS0/' \
172
            > ${target}/etc/event.d/ttyS0
173
        return
174
    fi
175
    # upstart in karmic and newer
176
    if [ -e ${target}/etc/init/tty1.conf ] ; then
177
        cat ${target}/etc/init/tty1.conf | \
178
        sed -re 's/^exec.*/exec \/sbin\/getty -L 115200 ttyS0 vt102/' \
179
            > ${target}/etc/init/ttyS0.conf
180
        sed -ie 's/tty1/ttyS0/g' ${target}/etc/init/ttyS0.conf
181
        return
182
    fi
183
    get_os $target
184
    case $OPERATING_SYSTEM in
185
        gentoo)
186
            sed -i -e 's/.*ttyS0.*/s0:12345:respawn:\/sbin\/agetty 115200 ttyS0 vt100/' \
187
                ${target}/etc/inittab
188
            ;;
189
        centos)
190
            echo "s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100" >> \
191
                ${target}/etc/inittab
192
            ;;
193
        debian|ubuntu)
194
            sed -i -e 's/.*T0.*/T0:23:respawn:\/sbin\/getty -L ttyS0 115200 vt100/' \
195
                ${target}/etc/inittab
196
            ;;
197
        *)
198
            echo "No support for your OS in instance-image, skipping..."
199
            ;;
200
    esac
201
}
202

    
203
linux_epilogue() {
204
    local target=$1
205
    if [ -z "$target" ] ; then
206
        log_error "target not set"
207
        exit 1
208
    fi
209

    
210
    get_os $target
211

    
212
    if [ "${OPERATING_SYSTEM}" = "fedora" ]; then
213
        # we have to force a filesystem relabeling for SELinux after messing
214
        # around with the filesystem in fedora
215
        echo "Enforce an automatic relabeling in the initial boot process..."
216
        touch $target/.autorelabel
217
    fi
218
}
219