Statistics
| Branch: | Revision:

root / common_linux.sh @ 6738cd80

History | View | Annotate | Download (6.1 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
    elif [ -e ${target}/etc/debian_version ] ; then
29
        OPERATING_SYSTEM="debian"
30
    elif [ -e ${target}/etc/gentoo-release ] ; then
31
        OPERATING_SYSTEM="gentoo"
32
    elif [ -e ${target}/etc/fedora-release ] ; then
33
        OPERATING_SYSTEM="fedora"
34
    elif [ -e ${target}/etc/redhat-release ] ; then
35
        if [ -n "$(grep -i centos ${target}/etc/redhat-release)" ] ; then
36
            OPERATING_SYSTEM="centos"
37
        else
38
            OPERATING_SYSTEM="redhat"
39
        fi
40
    fi
41
}
42

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

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

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

    
117
linux_setup_fstab() {
118
    local target=$1 fs=${FILESYSTEM}
119
    get_os_type $target
120
    cat > $target/etc/fstab <<EOF
121
# /etc/fstab: static file system information.
122
#
123
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
124
UUID=$root_uuid   /               $fs     defaults        0       1
125
proc              /proc           proc    defaults        0       0
126
EOF
127

    
128
if [ -n "$boot_dev" -a -n "$boot_uuid" ] ; then
129
    cat >> $target/etc/fstab <<EOF
130
UUID=$boot_uuid   /boot           $fs     defaults        1       2
131
EOF
132
fi
133

    
134
if [ -n "$swap_dev" -a -n "$swap_uuid" ] ; then
135
    cat >> $target/etc/fstab <<EOF
136
UUID=$swap_uuid   swap            swap    defaults        0       0
137
EOF
138
fi
139

    
140
# OS Specific fstabs
141
if [ "$OS_TYPE" = "redhat" ] ; then
142
    cat >> $target/etc/fstab <<EOF
143
tmpfs             /dev/shm        tmpfs   defaults        0       0
144
devpts            /dev/pts        devpts  gid=5,mode=620  0       0
145
sysfs             /sys            sysfs   defaults        0       0
146
EOF
147
fi
148

    
149
if [ "$OS_TYPE" = "gentoo" ] ; then
150
    cat >> $target/etc/fstab <<EOF
151
shm               /dev/shm        tmpfs   nodev,nosuid,noexec 0   0
152
EOF
153
fi
154
}
155

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

    
196
linux_filesystem_check() {
197
    local target=$1
198
    if [ -z "$target" ] ; then
199
        log_error "target not set for filesystem_check"
200
        exit 1
201
    fi
202

    
203
    get_os $target
204

    
205
    if [ "${OPERATING_SYSTEM}" = "fedora" ]; then
206
        # we have to force a filesystem relabeling for SELinux after messing
207
        # around with the filesystem in fedora
208
        echo "Enforce an automatic relabeling in the initial boot process..."
209
        touch $target/.autorelabel
210
    fi
211
}
212