涉及到磁盘分区表删除,最好先备份整个虚拟机!
原文链接:http://t.cn/RzCP9E2 作者输入命令时都用“sudo bash”,用sudo或者su就可以了。 涉及到磁盘分区表删除,最好先备份整个虚拟机!
原文链接:http://t.cn/RzCP9E2 作者输入命令时都用“sudo bash”,用sudo或者su就可以了。
检查文件系统: 🔗
1cruz@ubuntu:~$ sudo bash
2[sudo] password for cruz:
3root@ubuntu:~# df -h
4Filesystem Size Used Avail Use% Mounted on
5/dev/sda1 9.0G 2.7G 5.9G 32% /
6udev 488M 4.0K 488M 1% /dev
7tmpfs 199M 800K 198M 1% /run
8none 5.0M 0 5.0M 0% /run/lock
9none 497M 76K 496M 1% /run/shm
10root@ubuntu:~#
检查磁盘分区表: 🔗
1root@ubuntu:~# fdisk -l /dev/sda
2Disk /dev/sda: 10.7 GB, 10737418240 bytes
3255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
4Units = sectors of 1 * 512 = 512 bytes
5Sector size (logical/physical): 512 bytes / 512 bytes
6I/O size (minimum/optimal): 512 bytes / 512 bytes
7Disk identifier: 0x00001dec
8
9Device Boot Start End Blocks Id System
10/dev/sda1 * 2048 18874367 9436160 83 Linux
11/dev/sda2 18876414 20969471 1046529 5 Extended
12/dev/sda5 18876416 20969471 1046528 82 Linux swap / Solaris
13root@ubuntu:~#
记住上面显示的交换分区大小(Blocks的数目),这里就是1046528。如果交换分区和根目录不在一个磁盘(比如/dev/sdb),就不要记了。在本文,交换分区在/dev/sda,需要重新分区。
先关闭linux: 🔗
1root@ubuntu:~# shutdown -h now
在虚拟机设置,硬盘,实用工具下选择扩展。扩展虚拟机的最大磁盘大小,这里将磁盘设置为15G,然后重启。
重新设置分区表要删除所有的旧分区、关闭系统的swap:
1cruz@ubuntu:~$ sudo bash
2[sudo] password for cruz:
3root@ubuntu:~# free -m
4 total used free shared buffers cached
5Mem: 992 924 67 0 43 426
6 -/+ buffers/cache: 454 537
7Swap: 1021 0 1021
8root@ubuntu:~# swapoff -a
9root@ubuntu:~# free -m
10 total used free shared buffers cached
11Mem: 992 924 67 0 43 426
12 -/+ buffers/cache: 454 537
13Swap: 0 0 0
14root@ubuntu:~#
接下来的步骤会删除/dev/sda1和/dev/sda2,一定要记住分区表的起始位置,这里是2048!
1root@ubuntu:~# fdisk /dev/sda
2
3Command (m for help): p
4
5Disk /dev/sda: 16.1 GB, 16106127360 bytes
6255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors
7Units = sectors of 1 * 512 = 512 bytes
8Sector size (logical/physical): 512 bytes / 512 bytes
9I/O size (minimum/optimal): 512 bytes / 512 bytes
10Disk identifier: 0x00001dec
11
12 Device Boot Start End Blocks Id System
13/dev/sda1 * 2048 18874367 9436160 83 Linux
14/dev/sda2 18876414 20969471 1046529 5 Extended
15/dev/sda5 18876416 20969471 1046528 82 Linux swap / Solaris
16
17Command (m for help): d
18Partition number (1-5): 1
19
20Command (m for help): d
21Partition number (1-5): 2
22
23Command (m for help): p
24
25Disk /dev/sda: 16.1 GB, 16106127360 bytes
26255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors
27Units = sectors of 1 * 512 = 512 bytes
28Sector size (logical/physical): 512 bytes / 512 bytes
29I/O size (minimum/optimal): 512 bytes / 512 bytes
30Disk identifier: 0x00001dec
31
32 Device Boot Start End Blocks Id System
33
34Command (m for help):
不要退出fdisk,接着创建新分区
1Command (m for help): n
2Partition type:
3 p primary (0 primary, 0 extended, 4 free)
4 e extended
5Select (default p): p
6Partition number (1-4, default 1): 1
7First sector (2048-31457279, default 2048):
8Using default value 2048
9Last sector, +sectors or +size{K,M,G} (2048-31457279, default 31457279): 30410751
10
11Command (m for help): n
12Partition type:
13 p primary (1 primary, 0 extended, 3 free)
14 e extended
15Select (default p): p
16Partition number (1-4, default 2):
17Using default value 2
18First sector (30410752-31457279, default 30410752):
19Using default value 30410752
20Last sector, +sectors or +size{K,M,G} (30410752-31457279, default 31457279):
21Using default value 31457279
注意记得创建交换分区,大小别搞错,这里是1046528(31457279-30410571=1046528),即要留一部分block用于创建swap。修改sda2的分区类型为82,即交换分区。
1Command (m for help): p
2
3Disk /dev/sda: 16.1 GB, 16106127360 bytes
4255 heads, 63 sectors/track, 1958 cylinders, total 31457280 sectors
5Units = sectors of 1 * 512 = 512 bytes
6Sector size (logical/physical): 512 bytes / 512 bytes
7I/O size (minimum/optimal): 512 bytes / 512 bytes
8Disk identifier: 0x00001dec
9
10 Device Boot Start End Blocks Id System
11/dev/sda1 2048 30410751 15204352 83 Linux
12/dev/sda2 30410752 31457279 523264 83 Linux
13
14Command (m for help): t
15Partition number (1-4): 2
16Hex code (type L to list codes): 82
17Changed system type of partition 2 to 82 (Linux swap / Solaris)
18
19Command (m for help): w
20The partition table has been altered!
21
22Calling ioctl() to re-read partition table.
23
24WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
25The kernel still uses the old table. The new table will be used at
26the next reboot or after you run partprobe(8) or kpartx(8)
27Syncing disks.
28root@ubuntu:~#
重启虚拟机: 🔗
1root@ubuntu:~# shutdown -r now
交换分区挂载需要UUID标识符。创建新的交换分区不会匹配旧的UUID,导致重启无法挂载swap分区。这里主要有两种解决方法:在/etc/fstab里写入新的UUID,或者直接将旧的UUID覆盖在新分区上,这里选择后者。 awk命令用来显示旧的UUID,dd命令确保分区没数据。
1cruz@ubuntu:~$ sudo bash
2[sudo] password for cruz:
3root@ubuntu:~# awk '/swap/ { print $1 }' /etc/fstab
4#
5UUID=8bb62351-4436-47df-92fe-af2865f03461
6root@ubuntu:~# swapoff -a
7root@ubuntu:~# free -m
8 total used free shared buffers cached
9Mem: 992 695 296 0 23 325
10-/+ buffers/cache: 346 645
11Swap: 0 0 0
12root@ubuntu:~# dd if=/dev/zero of=/dev/sda2
13dd: writing to '/dev/sda2': No space left on device
141046529+0 records in
151046528+0 records out
16535822336 bytes (536 MB) copied, 11.9388 s, 44.9 MB/s
17root@ubuntu:~# mkswap -U 8bb62351-4436-47df-92fe-af2865f03461 /dev/sda2
18Setting up swapspace version 1, size = 523260 KiB
19no label, UUID=8bb62351-4436-47df-92fe-af2865f03461
20root@ubuntu:~# swapon -a
21root@ubuntu:~# free -m
22 total used free shared buffers cached
23Mem: 992 693 298 0 23 325
24-/+ buffers/cache: 345 646
25Swap: 510 7 503
26root@ubuntu:~#
最后,调整分区大小: 🔗
1root@ubuntu:~# df -h
2Filesystem Size Used Avail Use% Mounted on
3/dev/sda1 9.0G 2.8G 5.8G 33% /
4udev 488M 4.0K 488M 1% /dev
5tmpfs 199M 788K 198M 1% /run
6none 5.0M 0 5.0M 0% /run/lock
7none 497M 200K 496M 1% /run/shm
8root@ubuntu:~# resize2fs /dev/sda1
9resize2fs 1.42 (29-Nov-2011)
10Filesystem at /dev/sda1 is mounted on /; on-line resizing required
11old_desc_blocks = 1, new_desc_blocks = 1
12Performing an on-line resize of /dev/sda1 to 3801088 (4k) blocks.
13The filesystem on /dev/sda1 is now 3801088 blocks long.
14
15root@ubuntu:~# df -h
16Filesystem Size Used Avail Use% Mounted on
17/dev/sda1 15G 2.8G 11G 21% /
18udev 488M 4.0K 488M 1% /dev
19tmpfs 199M 788K 198M 1% /run
20none 5.0M 0 5.0M 0% /run/lock
21none 497M 200K 496M 1% /run/shm
22root@ubuntu:~#